LearnFrenzy
Sign in Take a free test

DBMS & SQL · Topic 10 of 11

Advanced SQL Queries: questions with answers

Subqueries, CTEs and recursive CTEs, UNION/INTERSECT/EXCEPT, pivots, Nth-highest and duplicate puzzles, multi-table DML, views and triggers.

Questions
45
Timed sets
3
Queries executed
39
Your accuracy
–

What this topic covers

30 subtopics
  • Subquery4
  • CTE3
  • Dates3
  • Nth highest3
  • Recursive CTE3
  • UNION3
  • Duplicates2
  • Triggers2
  • CASE1
  • COALESCE1
  • Conditional aggregation1
  • Correlated subquery1
Show all 30 subtopics
  • Derived table1
  • EXCEPT1
  • EXISTS1
  • Gaps1
  • Greatest per group1
  • GROUP_CONCAT1
  • HAVING1
  • INTERSECT1
  • Multi-table UPDATE1
  • NULL1
  • NULL in subqueries1
  • NULLIF1
  • Pivot1
  • Self join1
  • String functions1
  • Top-N per group1
  • UPDATE with CASE1
  • Views1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

The questions interviewers love: Nth-highest salary, deleting duplicates, finding gaps, top-N per group, organisation charts with recursive CTEs, and turning rows into columns. Every answer here was produced by actually running the query, so watch the details – ties, NULLs inside subqueries and the difference between UNION and UNION ALL decide most of them.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 UNION Beginner Practice Query executed

What does this return?

staff
idnamedeptsalarymanager_idjoinedemail
1AshaEng90000NULL2019-04-01asha@example.com
2RaviEng7000012020-07-15ravi@example.com
3MeeraEng7000012021-01-10meera@example.com
4JohnSales6000012020-02-01john@example.com
5PriyaSales5500042022-09-01priya@example.com
6KaranSales6200042023-03-15NULL
7NehaHR5000012021-06-20neha@example.com
8ArjunEng6500022024-01-05ravi@example.com
sales
idstaff_idquarteramount
14Q1100
24Q2150
35Q180
45Q140
56Q290
65Q260
SELECT
  (SELECT COUNT(*) FROM (SELECT dept FROM staff UNION SELECT dept FROM staff) a) AS with_union,
  (SELECT COUNT(*) FROM (SELECT dept FROM staff UNION ALL SELECT dept FROM staff) b) AS with_union_all;
with_unionwith_union_all
1616
with_unionwith_union_all
33
with_unionwith_union_all
816
with_unionwith_union_all
316
Show answer and explanation
Correct answer D

Explanation

UNION removes duplicate rows from the combined result, leaving the three departments. UNION ALL keeps everything: 8 + 8 = 16 rows. UNION ALL is also faster because it skips the de-duplication step.

Why the other options are wrong

  • AUNION removes duplicates.
  • BUNION ALL keeps duplicates.
  • CUNION de-duplicates across both inputs and within each.
Question 2 Subquery Beginner Practice Query executed

Who earns more than the company average?

staff
idnamedeptsalarymanager_idjoinedemail
1AshaEng90000NULL2019-04-01asha@example.com
2RaviEng7000012020-07-15ravi@example.com
3MeeraEng7000012021-01-10meera@example.com
4JohnSales6000012020-02-01john@example.com
5PriyaSales5500042022-09-01priya@example.com
6KaranSales6200042023-03-15NULL
7NehaHR5000012021-06-20neha@example.com
8ArjunEng6500022024-01-05ravi@example.com
sales
idstaff_idquarteramount
14Q1100
24Q2150
35Q180
45Q140
56Q290
65Q260
SELECT name FROM staff
WHERE salary > (SELECT AVG(salary) FROM staff)
ORDER BY id;
name
Asha
Ravi
Meera
name
Asha
Ravi
Meera
Karan
Arjun
name
Asha
name
Asha
Ravi
Meera
Arjun
Show answer and explanation
Correct answer A

Explanation

The scalar subquery computes the average once: 522000 / 8 = 65250. Arjun's 65000 is just below it.

Why the other options are wrong

  • BKaran and Arjun are below the average.
  • CRavi and Meera also exceed the average.
  • DArjun earns 65000, below 65250.
Question 3 Nth highest Intermediate Practice Query executed

Who has the third-highest salary?

staff
idnamedeptsalarymanager_idjoinedemail
1AshaEng90000NULL2019-04-01asha@example.com
2RaviEng7000012020-07-15ravi@example.com
3MeeraEng7000012021-01-10meera@example.com
4JohnSales6000012020-02-01john@example.com
5PriyaSales5500042022-09-01priya@example.com
6KaranSales6200042023-03-15NULL
7NehaHR5000012021-06-20neha@example.com
8ArjunEng6500022024-01-05ravi@example.com
sales
idstaff_idquarteramount
14Q1100
24Q2150
35Q180
45Q140
56Q290
65Q260
WITH r AS (
  SELECT name, salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rk
  FROM staff
)
SELECT name FROM r WHERE rk = 3;
name
Ravi
Meera
name
Arjun
name
Meera
name
Karan
Show answer and explanation
Correct answer B

Explanation

DENSE_RANK gives tied salaries the same rank with no gaps: 90000 → 1, 70000 → 2 (Ravi and Meera), 65000 → 3. With ROW_NUMBER the answer would depend on how ties are broken.

Why the other options are wrong

  • AThey share rank 2.
  • CThat is what ROW_NUMBER might give, ignoring the tie.
  • DKaran's 62000 has rank 4.
Question 4 NULL in subqueries Advanced Practice Query executed

The intent is "people who manage nobody". What does the query return?

staff
idnamedeptsalarymanager_idjoinedemail
1AshaEng90000NULL2019-04-01asha@example.com
2RaviEng7000012020-07-15ravi@example.com
3MeeraEng7000012021-01-10meera@example.com
4JohnSales6000012020-02-01john@example.com
5PriyaSales5500042022-09-01priya@example.com
6KaranSales6200042023-03-15NULL
7NehaHR5000012021-06-20neha@example.com
8ArjunEng6500022024-01-05ravi@example.com
sales
idstaff_idquarteramount
14Q1100
24Q2150
35Q180
45Q140
56Q290
65Q260
SELECT COUNT(*) AS n FROM staff
WHERE id NOT IN (SELECT manager_id FROM staff);
n
0
n
3
n
5
n
8
Show answer and explanation
Correct answer A

Explanation

Asha's manager_id is NULL, so the subquery's list contains a NULL. id NOT IN (…, NULL) can never be TRUE, so no row qualifies. NOT EXISTS (SELECT 1 FROM staff r WHERE r.manager_id = s.id) returns the intended 5.

Why the other options are wrong

  • BThree people are managers, but that is not what is counted.
  • CThat is the intended answer, which NOT EXISTS would give.
  • DNOT IN filters every row here.
41 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

DBMS & SQL

All 11 topics

Topics, levels and full mocks
  1. 01 DBMS and SQL Fundamentals45 questions · 3 levels
  2. 02 Joins and Subqueries45 questions · 3 levels
  3. 03 Aggregates and GROUP BY45 questions · 3 levels
  4. 04 Window Functions45 questions · 3 levels
  5. 05 Keys and Constraints45 questions · 3 levels
  6. 06 ER Model and Relational Mapping45 questions · 3 levels
  7. 07 Normalization45 questions · 3 levels
  8. 08 Transactions and Concurrency45 questions · 3 levels
  9. 09 Indexing and Query Optimisation45 questions · 3 levels
  10. 10 Advanced SQL Queries45 questions · 3 levels
  11. 11 GATE-Style DBMS Questions (MSQ & NAT)45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links