TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2297 3.09K
๐Ÿ“Œ 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
  • โค 5
More from @sqlanalyst
  1. Oct 12, 2026SQL Interview Series โ€” Part 6 ๐Ÿ“Œ Question 6: Find the Top 2 Highest-Paid Employees in Eachโ€ฆ
  2. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  3. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  4. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  5. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  6. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’