Lesson 1 of 4

Progress: 0/4 exercises solved (0%)

Solve all exercises below to unlock the next lesson.

Subqueries (IN, EXISTS, scalar)

Lesson Description

A query inside another query.

Easy Project

Mini project: Turn this lesson into a real-world query artifact by writing one clean business report query and validating output quality.

Subqueries — queries inside queries

A subquery is a SELECT statement nested inside another query. It can appear in WHERE, FROM, or SELECT.

Scalar subquery (returns one value)

Returns a single value and can be used with =, >, <, etc.:

-- Employees earning more than the company average
SELECT first_name, last_name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

IN subquery (returns a list)

Returns a column of values; the outer query checks membership:

-- Products that have been ordered at least once
SELECT name
FROM products
WHERE id IN (SELECT DISTINCT product_id FROM order_items);

NOT IN subquery

-- Products never ordered
SELECT name
FROM products
WHERE id NOT IN (SELECT product_id FROM order_items);

Caution: NOT IN returns no rows if the subquery contains any NULL. Prefer NOT EXISTS when NULLs are possible.

EXISTS subquery

Returns TRUE if the subquery returns at least one row:

SELECT first_name, last_name
FROM customers c
WHERE EXISTS (
  SELECT 1 FROM orders o WHERE o.customer_id = c.id
);

Derived table (subquery in FROM)

A subquery in FROM acts like a temporary table:

SELECT dept, avg_sal
FROM (
  SELECT department_id AS dept, AVG(salary) AS avg_sal
  FROM employees
  GROUP BY department_id
) dept_stats
WHERE avg_sal > 8000;

Tips

  • Scalar subqueries that return more than one row will error — use LIMIT 1 or aggregate if unsure.
  • For complex derived tables, a CTE (WITH) is often more readable.

Learning context

This lesson is part of Subqueries & CTEs. Focus on the core idea in Subqueries (IN, EXISTS, scalar), then validate with deliberate practice.

What to master

  • Understand clause order and intent
  • Write deterministic result queries
  • Use clear aliases and readable structure

Common mistakes

  • Skipping ORDER BY in final outputs
  • Using joins before validating base rows
  • Not checking row counts after changes

High-level strategy

  • Start simple, then layer complexity
  • Run often and compare expected shape
  • Refactor with CTEs when logic grows

Task ladder

  1. Baseline query: get a correct first output.
  2. Add one complication: edge case, join, filter, or ranking rule.
  3. Re-run and refine until output is stable and explainable.

Transparent data checks

  • Check row count before and after each major clause.
  • Validate one manual sample row for correctness.
  • Confirm final ordering and column naming for trust.

Retention loop

  • Run the solution once.
  • Rewrite from memory with one variation.
  • Explain the logic in plain English in one sentence.

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

Quick check: Subqueries (IN, EXISTS, scalar)

3 quick questions. One at a time. Instant score at the end.

Exercise 1

Employees earning above average

Enterprise Workforce Analyticsmedium

Return first_name, last_name, and salary of employees earning strictly more than the company average salary.

Run a query to see results here.
Exercise 2

Products never ordered

Commerce Operations Intelligencemedium

Return the name of every product that does NOT appear in any order_items row.

Run a query to see results here.
Exercise 3

Customers who have placed at least one order

Commerce Operations Intelligenceeasy

Return first_name and last_name of customers who have placed at least one order. Use EXISTS. Order by last_name.

Run a query to see results here.
Exercise 4

Products priced above their category average

Commerce Operations Intelligenceeasy

From the store sandbox, return name, price, and category_id for products whose price is strictly above the average price in their own category. Order by category_id, then price descending.

Run a query to see results here.

Lesson 1 of 4

0/4 solved (0%)

Finish this lesson to unlock next.