TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2228 3.36K
โœ… SQL Subqueries & Nested Queries ๐Ÿง ๐Ÿ”

Subqueries help you write powerful queries inside other queries. They're useful when you need intermediate results.

1๏ธโƒฃ What is a Subquery?
A subquery is a query inside () that runs first and passes its result to the outer query.

Example: Get employees who earn above average salary.
SELECT name, salary  
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

โœ… Subquery calculates average salary โ†’ main query finds those above it.

2๏ธโƒฃ Subquery in SELECT Clause
You can use subqueries to return values in each row.

Example: Show employee names with department name.
SELECT name,  
(SELECT dept_name FROM departments d WHERE d.id = e.dept_id) AS department
FROM employees e;

3๏ธโƒฃ Subquery in FROM Clause
Use when you want to filter or group temporary results.

Example: Get department-wise highest salary.
SELECT dept_id, MAX(salary)  
FROM (SELECT * FROM employees WHERE active = 1) AS active_emps
GROUP BY dept_id;

4๏ธโƒฃ Correlated Subquery
A subquery that uses a value from the outer query row.

Example: Get employees with highest salary in their department.
SELECT name, salary  
FROM employees e
WHERE salary = (SELECT MAX(salary) FROM employees WHERE dept_id = e.dept_id);

โœ… Subquery runs for each row using outer query value.

๐Ÿ’ก Real Use Cases:
โ€ข Filter rows based on dynamic conditions
โ€ข Compare values across groups
โ€ข Fetch related info in SELECT

๐ŸŽฏ Practice Tasks:
โ€ข Write a query to find 2nd highest salary
โ€ข Use subquery to get customers who placed more than 3 orders
โ€ข Create a nested query to show top-selling product per category


โœ… Solution for Practice Tasks ๐Ÿ‘‡

1๏ธโƒฃ Find 2nd Highest Salary
SELECT MAX(salary) AS second_highest_salary  
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

โ–ถ๏ธ Finds the highest salary less than the max salary โ†’ gives the 2nd highest.


2๏ธโƒฃ Customers Who Placed More Than 3 Orders
SELECT customer_id  
FROM orders
GROUP BY customer_id
HAVING COUNT(order_id) > 3;

โ–ถ๏ธ Groups orders by customer and filters those with more than 3.

You can join to get customer names:
SELECT name  
FROM customers
WHERE id IN (
SELECT customer_id
FROM orders
GROUP BY customer_id
HAVING COUNT(order_id) > 3
);

3๏ธโƒฃ Top-Selling Product Per Category
SELECT p.name, p.category_id, p.sales  
FROM products p
WHERE p.sales = (
SELECT MAX(sales)
FROM products
WHERE category_id = p.category_id
);

โ–ถ๏ธ Correlated subquery finds the highest sales within each category.

๐Ÿ’ฌ Tap โค๏ธ for more!
  • โค 6
  • ๐ŸŽ‰ 1
More from @sqlanalyst
  1. Oct 13, 2026SQL Interview Series โ€” Part 8 ๐Ÿ“Œ Question 8: Find Customers Who Have Never Placed an Orderโ€ฆ
  2. Oct 13, 2026๐—ง๐—ข๐—ฃ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐—ง๐—ข ๐—•๐—ข๐—ข๐—ฆ๐—ง ๐—ฌ๐—ข๐—จ๐—ฅ ๐—ฆ๐—ž๐—œ๐—Ÿ๐—Ÿ๐—ฆ ๐—œ๐—ก ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ! Explorโ€ฆ
  3. Oct 12, 2026SQL Interview Series โ€” Part 6 ๐Ÿ“Œ Question 6: Find the Top 2 Highest-Paid Employees in Eachโ€ฆ
  4. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  5. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  6. Oct 9, 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 โ†’