TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2458 8.58K
โœ… SQL Subqueries with Interview Q&A ๐Ÿ”๐Ÿง 

Subqueries and CTEs help you write cleaner, modular, and more powerful SQL queries. They're often asked in interviews!

1๏ธโƒฃ Subqueries (Nested Queries)
A query inside another query.

Example:
SELECT name  
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

๐Ÿ“Œ Use case: Find employees earning above average.

Types:
โฆ In SELECT
โฆ In WHERE
โฆ In FROM (Inline Views)

2๏ธโƒฃ Correlated Subqueries
Inner query depends on outer query.

Example:
SELECT name  
FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept_id = e.dept_id);

๐Ÿ“Œ Use case: Find employees earning above average in their department.

3๏ธโƒฃ Common Table Expressions (CTE)
Temporary result set using WITH. Improves readability.

Example:
WITH high_paid AS (
SELECT name, salary FROM employees WHERE salary > 100000
)
SELECT * FROM high_paid;

๐Ÿ“Œ Use case: Simplify complex queries, recursive queries.

4๏ธโƒฃ Recursive CTE
Used for hierarchical data (e.g. org charts, folders).

Example:
WITH RECURSIVE emp_tree AS (
SELECT id, name, manager_id FROM employees WHERE manager_id IS NULL
UNION ALL
SELECT e.id, e.name, e.manager_id
FROM employees e
JOIN emp_tree et ON e.manager_id = et.id
)
SELECT * FROM emp_tree;


๐Ÿง  Interview Questions

Q1: When should you use a subquery vs JOIN?
A: Use subquery when working with aggregates or filtering logic. JOINs are better for combining related data.

Q2: What's the difference between subquery and CTE?
A: Subquery is inline; CTE improves readability and can be reused in the query.

Q3: What is a correlated subquery?
A: It depends on data from the outer query. Runs row by row.

Q4: When do you use recursive CTEs?
A: For hierarchical/parent-child relationships like org charts, file systems.

Q5: Can subqueries be used in the FROM clause?
A: Yes, they're called derived tables or inline views.

๐Ÿ’ฌ Double Tap โค๏ธ for more!
  • โค 17
More from @sqlspecialist
  1. Oct 12, 20268๏ธโƒฃ You receive a dataset with inconsistent date formats. How would you clean it? Sample Aโ€ฆ
  2. Oct 12, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 7 Guys, let's continue our Data Analyst Interviewโ€ฆ
  3. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  4. Oct 9, 2026โ€œHere, the data is sorted by the second column in descending order and the first five rowsโ€ฆ
  5. Oct 9, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 6 Guys, let's continue our Data Analyst Interviewโ€ฆ
  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 โ†’