TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2746 8.28K
๐Ÿ”ฅ Now, letโ€™s move to the next topic of SQL Roadmap

โœ… Subqueries (Nested Queries)

๐Ÿง  1. What is a Subquery?

A subquery is a query inside another query

๐Ÿ‘‰ Think like this:
โ€œFirst get some data โ†’ then use that result in another queryโ€

๐Ÿ“Š Basic Example

๐Ÿ‘‰ Find employees earning above average salary
SELECT FROM employees
WHERE salary > (
SELECT AVG(salary) FROM employees
);

๐Ÿ‘‰ Inner query โ†’ gives average salary
๐Ÿ‘‰ Outer query โ†’ filters employees

โšก 2. Types of Subqueries

๐Ÿ”น Single Row Subquery

Returns only one value

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

๐Ÿ”น Multiple Row Subquery

Returns multiple values
SELECT FROM employees
WHERE dept_id IN (
SELECT dept_id FROM departments
);

๐ŸŽฏ 3. Subquery with IN

SELECT name
FROM employees
WHERE dept_id IN (
SELECT dept_id FROM departments WHERE dept_name = 'IT'
);

โœ” Finds employees in IT department

โšก 4. Subquery with EXISTS

SELECT name
FROM employees e
WHERE EXISTS (
SELECT 1 FROM departments d
WHERE e.dept_id = d.dept_id
);

โœ” Checks if matching record exists

๐Ÿšจ 5. Important Difference

IN ->
Compares values & Slower with large data

EXISTS -> Checks existence & Faster with large data

๐ŸŽฏ 6. Practice Tasks

1. Find employees with salary > average salary
2. Find employees in IT department using subquery
3. Get departments that have employees
4. Find employees with max salary
5. Get employees not in HR department

๐Ÿ”ฅ Here are the solutions for Subqueries practice tasks

โœ… 1. Find employees with salary > average salary

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

โœ… 2. Find employees in IT department using subquery

SELECT FROM employees
WHERE dept_id IN (
SELECT dept_id FROM departments
WHERE dept_name = 'IT'
);

โœ… 3. Get departments that have employees

SELECT
FROM departments
WHERE dept_id IN (
SELECT dept_id FROM employees
);

๐Ÿ‘‰ Alternative (using EXISTS):

SELECT FROM departments d
WHERE EXISTS (
SELECT 1 FROM employees e
WHERE d.dept_id = e.dept_id
);

โœ… 4. Find employees with max salary

SELECT
FROM employees
WHERE salary = (
SELECT MAX(salary) FROM employees
);

โœ… 5. Get employees not in HR department

SELECT FROM employees
WHERE dept_id NOT IN (
SELECT dept_id FROM departments
WHERE dept_name = 'HR'
);


โšก Mini Challenge ๐Ÿ”ฅ

๐Ÿ‘‰ Find employees earning second highest salary using subquery

โšก Mini Challenge Solution ๐Ÿ”ฅ

SELECT
FROM employees
WHERE salary = (
SELECT MAX(salary)
FROM employees
WHERE salary < (
SELECT MAX(salary) FROM employees
)
);

๐Ÿ”ฅ Pro Tip:
If question says:
๐Ÿ‘‰ โ€œabove averageโ€, โ€œmaxโ€, โ€œsecond highestโ€
โ†’ Think Subquery instantly ๐Ÿ’ฏ

Double Tap โค๏ธ For More
  • โค 16
More from @sqlspecialist
  1. Oct 9, 2026โ€œHere, the data is sorted by the second column in descending order and the first five rowsโ€ฆ
  2. Oct 9, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 6 Guys, let's continue our Data Analyst Interviewโ€ฆ
  3. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  4. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  5. Oct 7, 2026๐Ÿ“Š Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vidโ€ฆ
  6. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions suchโ€ฆ
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 โ†’