๐ SQL Subqueries CTEs
1๏ธโฃ What is a Subquery?
A subquery is a query inside another query. It runs first and passes its result to the outer query.
Think like this ๐
> โFirst find something โ then use it to filter or calculate something elseโ
Why Subqueries exist (business thinking)
Real questions like:
โข Find customers who spent more than average
โข Find products with highest sales
โข Find employees earning more than their manager
These need one queryโs result inside another query.
โ
Basic Subquery Structure
SELECT column
FROM text
WHERE column OPERATOR (
SELECT column
FROM text
);
Example Tables: orders
order_id | customer_id | amount
1 | 101 | 5000
2 | 102 | 8000
3 | 103 | 3000
2๏ธโฃ Subquery in WHERE clause (Most Common)
๐น Scenario: Find orders with amount greater than average order value
SELECT *
FROM orders
WHERE amount > (
SELECT AVG(amount)
FROM orders
);
What this query does
1. Inner query calculates average order amount
2. Outer query keeps only orders above that average
โ
Very common interview question
3๏ธโฃ Subquery with IN
๐น Scenario: Find customers who have placed at least one order
Tables: customers(customer_id, name) orders(customer_id)
SELECT name
FROM customers
WHERE customer_id IN (
SELECT customer_id
FROM orders
);
What this query does
โข Inner query gets customers who ordered
โข Outer query fetches their names
4๏ธโฃ Subquery in SELECT clause
๐น Scenario: Show each order with total number of orders
SELECT order_id, amount, (
SELECT COUNT(*)
FROM orders
) AS total_orders
FROM orders;
What this query does
โข Inner query runs once
โข Adds total order count to every row
โ ๏ธ Use carefully โ can be inefficient
5๏ธโฃ Correlated Subquery (Important)
A correlated subquery depends on the outer query. It runs once per row.
๐น Scenario: Find customers who spent more than their cityโs average
Tables: customers(customer_id, city) orders(customer_id, amount)
SELECT c.customer_id
FROM customers c
WHERE (
SELECT AVG(o.amount)
FROM orders o
WHERE o.customer_id = c.customer_id
) > 5000;
What this query does
โข For each customer
โข Calculates their average spend
โข Filters based on condition
โ ๏ธ Powerful but slower on large data
6๏ธโฃ Problems with Subqueries
โ Hard to read
โ Hard to debug
โ Performance issues
โ Nested logic becomes messy
๐ Thatโs why CTEs exist
7๏ธโฃ What is a CTE (Common Table Expression)?
A CTE is a named temporary result.
It makes complex queries readable and reusable.
CTE Syntax
WITH cte_name AS (
SELECT ...
)
SELECT *
FROM cte_name;
8๏ธโฃ Same Problem Solved Using CTE (Cleaner)
๐น Find customers with total spend > 10,000
WITH customer_spend AS (
SELECT customer_id, SUM(amount) AS total_spend
FROM orders
GROUP BY customer_id
)
SELECT *
FROM customer_spend
WHERE total_spend > 10000;
What this does
โข First block calculates spend
โข Second block filters results
โข Very readable
9๏ธโฃ CTE vs Subquery
โข Readability: CTE is excellent, Subquery is poor
โข Reusability: CTE is yes, Subquery is no
โข Debugging: CTE is easy, Subquery is hard
โข Performance: Both depend on usage
๐ When to Use What?
Use Subquery when:
โ Logic is small
โ Used only once
Use CTE when:
โ Logic is complex
โ Multiple steps
โ Interview or production query
Common Beginner Mistakes
โ Writing very deep nested subqueries
โ Using correlated subqueries unnecessarily
โ Forgetting CTE scope (only valid for one query)
Interview Tip ๐ก
> Subqueries solve problems inside queries, while CTEs solve readability and maintainability.
Double Tap โฅ๏ธ For More
Post #2297
3.09K
- โค 5