MySQL Query Puzzles: 10 Interview Problems with Solutions

MySQL 60 min min read Updated: Nov 27, 2026 Intermediate
MySQL Query Puzzles: 10 Interview Problems with Solutions
Intermediate Topic 3 of 3

The Data

All puzzles use one table. Try each puzzle before reading the solution.

sql SELECT id, name, dept, salary, manager_id FROM employees ORDER BY id;

Result:

idnamedeptsalarymanager_id
1PriyaMgmt9000NULL
2OmarSales60001
3LenaSales70002
4SamSales65002
5ZoeIT80001
6IvanIT85005
7KaiIT85005
8JunIT50005

Puzzle 1: Second Highest Salary

Ties must count once. Using DENSE_RANK handles ties; a plain LIMIT 1 OFFSET 1 does not:

sql SELECT DISTINCT salary AS second_highest FROM (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS r FROM employees) t WHERE r = 2;

Result:

second_highest
8500

Puzzle 2: Highest Paid in Each Department

sql SELECT dept, name, salary FROM (SELECT dept, name, salary, RANK() OVER (PARTITION BY dept ORDER BY salary DESC) AS r FROM employees) t WHERE r = 1 ORDER BY dept, name;

Result:

deptnamesalary
ITIvan8500
ITKai8500
MgmtPriya9000
SalesLena7000

RANK keeps both people when two share the top salary (Ivan and Kai in IT).

Puzzle 3: Employees Earning More Than Their Manager

sql SELECT e.name, e.salary, m.name AS manager, m.salary AS manager_salary FROM employees e JOIN employees m ON m.id = e.manager_id WHERE e.salary > m.salary ORDER BY e.id;

Result:

namesalarymanagermanager_salary
Lena7000Omar6000
Sam6500Omar6000
Ivan8500Zoe8000
Kai8500Zoe8000

Puzzle 4: Departments Whose Average Is Above the Company Average

sql SELECT dept, ROUND(AVG(salary), 0) AS dept_avg FROM employees GROUP BY dept HAVING AVG(salary) > (SELECT AVG(salary) FROM employees) ORDER BY dept;

Result:

deptdept_avg
IT7500
Mgmt9000

Puzzle 5: Running Total of Salaries

sql SELECT id, name, salary, SUM(salary) OVER (ORDER BY id) AS running_total FROM employees ORDER BY id;

Result:

idnamesalaryrunning_total
1Priya90009000
2Omar600015000
3Lena700022000
4Sam650028500
5Zoe800036500
6Ivan850045000
7Kai850053500
8Jun500058500

Puzzle 6: Duplicate Detection and Removal

sql CREATE TABLE signups (id INT PRIMARY KEY, email VARCHAR(20)); INSERT INTO signups VALUES (1, 'a@x'), (2, 'b@x'), (3, 'a@x'), (4, 'c@x'), (5, 'b@x'), (6, 'a@x'); SELECT email, COUNT(*) AS copies FROM signups GROUP BY email HAVING COUNT(*) > 1 ORDER BY email;

Result:

emailcopies
a@x3
b@x2

Delete extras, keeping the lowest id for each email, using a self join:

sql CREATE TABLE signups (id INT PRIMARY KEY, email VARCHAR(20)); INSERT INTO signups VALUES (1, 'a@x'), (2, 'b@x'), (3, 'a@x'), (4, 'c@x'), (5, 'b@x'), (6, 'a@x'); DELETE s FROM signups s JOIN signups k ON k.email = s.email AND k.id < s.id; SELECT id, email FROM signups ORDER BY id;

Result:

idemail
1a@x
2b@x
4c@x

Puzzle 7: Find Gaps in a Sequence

sql CREATE TABLE invoices (no INT PRIMARY KEY); INSERT INTO invoices VALUES (1), (2), (3), (7), (8), (12); SELECT no + 1 AS missing_from, next_no - 1 AS missing_to FROM (SELECT no, LEAD(no) OVER (ORDER BY no) AS next_no FROM invoices) t WHERE next_no - no > 1 ORDER BY no;

Result:

missing_frommissing_to
46
911

Puzzle 8: Consecutive Days

sql CREATE TABLE logins (u VARCHAR(5), d DATE); INSERT INTO logins VALUES ('ann','2025-03-01'), ('ann','2025-03-02'), ('ann','2025-03-03'), ('ann','2025-03-07'), ('bob','2025-03-01'), ('bob','2025-03-03'); SELECT u, MIN(d) AS streak_start, MAX(d) AS streak_end, COUNT(*) AS days FROM ( SELECT u, d, DATE_SUB(d, INTERVAL ROW_NUMBER() OVER (PARTITION BY u ORDER BY d) DAY) AS grp FROM logins ) t GROUP BY u, grp HAVING COUNT(*) >= 2 ORDER BY u, streak_start;

Result:

ustreak_startstreak_enddays
ann2025-03-012025-03-033

The trick: subtracting the row number from the date gives the same value for consecutive days.

Puzzle 9: Pivot Salaries by Department

sql SELECT SUM(dept = 'Sales') AS sales_people, SUM(dept = 'IT') AS it_people, SUM(CASE WHEN dept = 'IT' THEN salary ELSE 0 END) AS it_payroll FROM employees;

Result:

sales_peopleit_peopleit_payroll
3430000

Puzzle 10: The Management Chain of an Employee

sql WITH RECURSIVE chain AS ( SELECT id, name, manager_id, 0 AS level FROM employees WHERE name = 'Jun' UNION ALL SELECT e.id, e.name, e.manager_id, c.level + 1 FROM employees e JOIN chain c ON e.id = c.manager_id ) SELECT level, name FROM chain ORDER BY level;

Result:

levelname
0Jun
1Zoe
2Priya

Tips for the Interview

  • Ask about ties, NULLs and empty results before you write.
  • Say your plan in words: join, filter, group, rank.
  • Check your query on a tiny example by hand.
  • Mention performance: which index would help?

Conclusion

These ten patterns (ranking, top per group, self joins, running totals, deduplication, gaps, streaks, pivots and recursion) cover most coding questions.

Quick Summary: DENSE_RANK, top per group, self join, running total, deduplication, gaps, streaks, pivot, recursion.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators