Lesson Description
Build confidence in Filtering & Logic by practicing this lesson with structured prompts and real query execution.
Lesson 4 of 4
Progress: 0/4 exercises solved (0%)
Solve all exercises below to unlock the next lesson.
Lesson Description
Build confidence in Filtering & Logic by practicing this lesson with structured prompts and real query execution.
Easy Project
Mini project: Turn this lesson into a real-world query artifact by writing one clean business report query and validating output quality.
EXISTS returns TRUE if its subquery returns at least one row. It stops as soon as the first match is found, which makes it efficient.
SELECT col1, col2
FROM table_name t
WHERE EXISTS (
SELECT 1
FROM other_table ot
WHERE ot.fk = t.id
);
The inner SELECT 1 is conventional — EXISTS only cares whether any row is returned, not what the columns contain.
SELECT first_name, last_name
FROM customers c
WHERE EXISTS (
SELECT 1 FROM orders o
WHERE o.customer_id = c.id
);
SELECT first_name, last_name
FROM customers c
WHERE NOT EXISTS (
SELECT 1 FROM orders o
WHERE o.customer_id = c.id
);
This returns customers who have never placed an order.
| Approach | Notes |
|---|---|
EXISTS | Short-circuits at first match; safe with NULLs |
IN (subquery) | Simple; beware NOT IN with NULLs in list |
LEFT JOIN ... WHERE rhs IS NULL | Same as NOT EXISTS; sometimes faster |
Critical rule: Prefer NOT EXISTS over NOT IN when the subquery could return NULLs — NOT IN silently returns zero rows if the list contains a NULL.
EXISTS is a semi-join — it does not pull data from the subquery, only checks for existence.This lesson is part of Filtering & Logic. Focus on the core idea in EXISTS — test for related rows, then validate with deliberate practice.
What to master
Common mistakes
High-level strategy
Task ladder
Transparent data checks
Retention loop
Logical reasoning for commands
WHERE
Why: Limits rows to only the business-relevant subset.
Memory cue: Filter early to reduce noise.
ORDER BY
Why: Makes output deterministic and reviewable.
Memory cue: No ORDER BY means no guaranteed row order.
AS alias
Why: Makes output columns readable for teams and reports.
Memory cue: If the name is clear, the query is easier to trust.
Concept check
3 quick questions. One at a time. Instant score at the end.
From the store sandbox, return first_name and last_name of customers who have placed at least one order. Use EXISTS. Order by last_name.
From the store sandbox, return first_name and last_name of customers who have never placed an order. Use NOT EXISTS. Order by last_name.
From the hospital sandbox, return first_name and last_name of patients who appear in the appointments table at least once. Use EXISTS. Order by last_name.
Return first_name and last_name of HR employees who are the manager_id for at least one other employee. Use EXISTS. Order by last_name.
Final checkpoint
Pass mark: 80%. Review each explanation after you submit.
1. Which clause should you use to filter grouped results?
2. What is the safest way to avoid accidental row multiplication in joins?
3. Which feature is best for ranking rows within partitions?
4. Why should final analytical queries usually include ORDER BY?
5. When query logic gets complex, what improves readability most?
Lesson 4 of 4
0/4 solved (0%)
Finish this lesson to unlock next.