Day 5 is where SQL thinking becomes nested and analytical. This is the stage where many interview questions come from because it tests logical depth, not just syntax.
You will learn subqueries, also called nested queries.
A subquery is a query inside another query.
Structure:
SELECT ...
FROM ...
WHERE column OPERATOR (
SELECT ...
);The inner query runs first and its result is used by the outer query.
Tables:
- users
- orders
- products
- order_items
First understand the average:
SELECT AVG(amount) FROM orders;Now combine:
SELECT *
FROM orders
WHERE amount > (
SELECT AVG(amount)
FROM orders
);Explanation:
- Inner query calculates average.
- Outer query compares each row to that value.
SELECT *
FROM users
WHERE id IN (
SELECT user_id
FROM orders
);The inner query returns a list of IDs.
SELECT *
FROM users
WHERE id NOT IN (
SELECT user_id
FROM orders
);This is logically similar to a LEFT JOIN + NULL filter.
Subqueries can return:
1️⃣ Single value (scalar subquery) 2️⃣ Single column list 3️⃣ Full table result
SQL uses them depending on operator.
This creates temporary tables.
SELECT AVG(total_spent)
FROM (
SELECT user_id, SUM(amount) AS total_spent
FROM orders
GROUP BY user_id
) AS user_spending;Explanation:
Inner query:
Creates temporary table:
| user_id | total_spent |
Outer query:
Calculates average.
This technique powers:
- dashboards
- analytics
- reports
- data pipelines
These appear constantly in SQL interviews.
Example table:
CREATE TABLE employees (
id INT,
name VARCHAR(100),
salary INT
);Insert:
INSERT INTO employees VALUES
(1,'Alice',5000),
(2,'Bob',7000),
(3,'Carol',6000),
(4,'David',7000);Query:
SELECT MAX(salary)
FROM employees
WHERE salary < (
SELECT MAX(salary)
FROM employees
);Logic:
1️⃣ Find highest salary 2️⃣ Find highest salary less than that
Example:
CREATE TABLE accounts (
id INT,
email VARCHAR(100)
);Solution:
SELECT email
FROM accounts
GROUP BY email
HAVING COUNT(*) > 1;SELECT user_id, SUM(amount)
FROM orders
GROUP BY user_id
HAVING SUM(amount) > (
SELECT AVG(amount)
FROM orders
);SELECT *
FROM products
WHERE price > (
SELECT AVG(price)
FROM products
);A correlated subquery runs once for each row.
Example:
Find users with more orders than average.
SELECT u.name
FROM users u
WHERE (
SELECT COUNT(*)
FROM orders o
WHERE o.user_id = u.id
) > 2;The inner query references outer query variable.
This is called correlation.
Database executes inner query repeatedly.
Modern planners often optimize them into joins.
Find orders with amount greater than average order amount.
Find products with price below average price.
Find users who have placed at least one order.
Find users who have never placed an order.
Find users whose total spending is greater than the average user spending.
Find products that were ordered more than once.
Find employees with the third highest salary.
(Hint: nested subqueries)
Focus on subquery problems.
- https://leetcode.com/problemset/database/
- https://pgexercises.com/
- https://sqlzoo.net/
- https://www.hackerrank.com/domains/sql
You should now:
✔ Understand nested queries ✔ Solve interview-style SQL problems ✔ Write queries with subqueries ✔ Understand correlated queries ✔ Think analytically about data
Using the store dataset, answer:
1️⃣ Which user spent the most? 2️⃣ Which product generated the most revenue? 3️⃣ Which users spent above average? 4️⃣ Which products were never ordered? 5️⃣ Which order had the highest total quantity?
Tomorrow (Day 6):
We go deeper into database design and performance, including:
- Normalization (1NF, 2NF, 3NF)
- Indexes (how queries become fast)
- Constraints
- Designing real database schemas
This is where SQL knowledge becomes professional database engineering.