TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2770 1.33K
๐Ÿš€ SQL Roadmap 2026 โ€” Part 12

๐Ÿงฉ SQL Subqueries โ€” Using One Query Inside Another

So far, we've learned how to retrieve, filter, group, transform, and combine data.

But sometimes a business question requires one query to use the result of another query.

For example:

ยซFind employees whose salary is higher than the average salary.ยป

First, we need to calculate:

SELECT AVG(salary)
FROM employees;


Then compare every employee against that result.

A subquery allows us to do both inside one SQL statement.

๐Ÿง  1. What Is a Subquery?

A subquery is a SQL query written inside another SQL query.

SELECT *
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);


The inner query:

SELECT AVG(salary) FROM employees


calculates the average salary. The outer query then uses that result.

Think of it as:

Outer Query โ†’ Needs an answer โ†’ Subquery calculates the answer โ†’ Outer Query uses it

๐Ÿ”น 2. Basic Subquery Structure

SELECT column_name
FROM table_name
WHERE column_name operator (
SELECT ...
);


The inner query is enclosed in ( ... ).

๐Ÿ“Š 3. Subquery Returning One Value

A scalar subquery returns a single value.

SELECT
employee_name,
salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);


The inner query returns something like 65000. The outer query then finds employees earning more than 65000.

๐Ÿ’ฐ 4. Employees Earning Above Average

Classic interview question.

SELECT
employee_name,
salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);


Logic: Calculate average salary โ†’ Compare every employee โ†’ Keep salary > average

๐Ÿ† 5. Products More Expensive Than Average

SELECT
product_name,
price
FROM products
WHERE price > (
SELECT AVG(price)
FROM products
);


๐Ÿ”ข 6. Subquery with COUNT()

Customers who have placed more than 5 orders:

SELECT
customer_id,
customer_name
FROM customers
WHERE customer_id IN (
SELECT customer_id
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 5
);


๐Ÿ”— 7. Subquery with IN

Useful when a subquery returns multiple values.

SELECT *
FROM customers
WHERE customer_id IN (
SELECT customer_id
FROM orders
);


Returns customers who have at least one order.

โš ๏ธ 8. Single Value vs Multiple Values

One value โ†’ Use =, >, <, >=, <=

WHERE salary > (
SELECT AVG(salary)
FROM employees
)


Multiple values โ†’ Use IN

WHERE customer_id IN (
SELECT customer_id
FROM orders
)


๐Ÿšซ 9. NOT IN

SELECT
customer_id,
customer_name
FROM customers
WHERE customer_id NOT IN (
SELECT customer_id
FROM orders
);


โš ๏ธ Important NULL Warning: NOT IN can produce unexpected results if the subquery contains NULL. For anti-matching, NOT EXISTS is often safer.

๐Ÿ”Ž 10. EXISTS

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


Means: Return the customer if at least one matching order exists.

๐Ÿšซ 11. NOT EXISTS

SELECT
c.customer_id,
c.customer_name
FROM customers c
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);
  • โค 2
More from @sqlanalyst
  1. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  2. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
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 โ†’