SQL interviews for analyst, developer and data engineer jobs reuse a small set of classic problems: the second highest salary, employees and their managers, the top row per group, month-over-month change. Each one tests a specific idea, such as subqueries, self joins, window functions or LEFT JOIN with IS NULL. This lesson solves eight of them on the sample tables from this course, with real output and the idea behind each answer.
In this lesson you will learn
- Second and Nth highest value, including ties
- Self joins for employee–manager questions
- Finding rows with no match
- Correlated subqueries and top row per group
- Month-over-month change with
LAG
These questions combine ideas from subqueries, joins and window functions. The employees table has a manager_id column that points to another employee.
Salaries, Managers and Missing Rows
-- Q1. Second highest salary
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
-- Q2. Nth highest salary (N = 3), safe with ties
SELECT DISTINCT salary AS third_highest
FROM (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM employees) AS ranked
WHERE rnk = 3;
-- Q3. Employees who earn more than half of their manager's salary
SELECT e.name AS employee, 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 / 2
ORDER BY e.salary DESC;
-- Q4. Departments that have no students
SELECT d.code, d.name
FROM departments d
LEFT JOIN students s ON s.dept = d.code
WHERE s.id IS NULL;
Output
+----------------+
| second_highest |
+----------------+
| 180000 |
+----------------+
(1 row)
+---------------+
| third_highest |
+---------------+
| 170000 |
+---------------+
(1 row)
+----------+--------+---------+----------------+
| employee | salary | manager | manager_salary |
+----------+--------+---------+----------------+
| Faisal | 180000 | Shahana | 250000 |
| Lipi | 170000 | Shahana | 250000 |
| Mou | 95000 | Faisal | 180000 |
+----------+--------+---------+----------------+
(3 rows)
+------+------------------------+
| code | name |
+------+------------------------+
| ME | Mechanical Engineering |
+------+------------------------+
(1 row)
- Q1: the inner query finds the top salary (250,000); the outer query finds the highest salary below it. If several people share the top salary, this still returns the true second value.
- Q2:
DENSE_RANKgives equal salaries the same rank and leaves no gaps, so rank 3 is the third distinct salary.LIMIT 1 OFFSET 2is a common wrong answer, because it counts rows, not distinct values. - Q3: a self join uses the same table twice with different aliases:
efor the employee andmfor the manager. The classic version asks for employees who earn more than their manager; on this data nobody does, so the example uses half the manager’s salary. - Q4:
LEFT JOINkeeps every department, andWHERE s.id IS NULLkeeps only those with no matching student.NOT EXISTSis an equally good answer.
Groups, Rankings and Trends
-- Q5. Students whose CGPA is above their department's average
SELECT s.name, s.dept, s.cgpa
FROM students s
WHERE s.cgpa > (SELECT AVG(cgpa) FROM students WHERE dept = s.dept)
ORDER BY s.dept, s.cgpa DESC;
-- Q6. The best student (highest marks) in each course
SELECT title, name, marks
FROM (SELECT c.title, s.name, e.marks,
RANK() OVER (PARTITION BY c.id ORDER BY e.marks DESC) AS rnk
FROM enrollments e
JOIN students s ON s.id = e.student_id
JOIN courses c ON c.id = e.course_id) AS ranked
WHERE rnk = 1
ORDER BY title;
-- Q7. Delivered revenue per month and change from the previous month
SELECT month, revenue,
revenue - LAG(revenue) OVER (ORDER BY month) AS change
FROM (SELECT substr(order_date, 1, 7) AS month, SUM(amount) AS revenue
FROM orders
WHERE status = 'delivered'
GROUP BY month) AS monthly
ORDER BY month;
-- Q8. Customers with more than one order, and their cancelled count
SELECT customer, COUNT(*) AS orders,
SUM(status = 'cancelled') AS cancelled
FROM orders
GROUP BY customer
HAVING COUNT(*) > 1
ORDER BY orders DESC, customer;
Output
+--------+------+------+
| name | dept | cgpa |
+--------+------+------+
| Sabbir | BBA | 3.55 |
| Tania | CSE | 3.95 |
| Nusrat | CSE | 3.82 |
| Arif | EEE | 3.41 |
+--------+------+------+
(4 rows)
+-----------------+--------+-------+
| title | name | marks |
+-----------------+--------+-------+
| Circuit Theory | Arif | 81 |
| Data Structures | Tania | 95 |
| Databases | Nusrat | 92 |
| Marketing | Sabbir | 85 |
+-----------------+--------+-------+
(4 rows)
+---------+---------+--------+
| month | revenue | change |
+---------+---------+--------+
| 2026-01 | 1650 | NULL |
| 2026-02 | 2300 | 650 |
| 2026-03 | 1800 | -500 |
+---------+---------+--------+
(3 rows)
+----------+--------+-----------+
| customer | orders | cancelled |
+----------+--------+-----------+
| Karim | 3 | 1 |
| Joya | 2 | 0 |
| Rina | 2 | 0 |
+----------+--------+-----------+
(3 rows)
- Q5: a correlated subquery runs once per outer row;
dept = s.deptlinks it to the current student. Nadia has no CGPA, so the comparison is unknown and she is left out. Mitu (3.67) is below the CSE average of about 3.81, which also ignores Nadia. - Q6: “top N per group” is a window-function problem.
RANK() OVER (PARTITION BY c.id ORDER BY e.marks DESC)ranks students within each course, and the outer query keeps rank 1. WithRANK, tied students would all be returned. - Q7: the inner query totals delivered orders per month using
substr(order_date, 1, 7);LAG(revenue)fetches the previous month’s total. The first month has no previous value, so its change is NULL. - Q8:
SUM(status = 'cancelled')counts matching rows because a true comparison is 1 in SQLite and MySQL. The portable version isSUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END).
Question Patterns
| If the question says… | Reach for… |
|---|---|
| “Second / Nth highest” | Subquery with MAX, or DENSE_RANK |
| “Employee and their manager” | Self join |
| “Never”, “without any”, “no orders” | LEFT JOIN … IS NULL or NOT EXISTS |
| “Above the average of their group” | Correlated subquery or AVG() OVER (PARTITION BY …) |
| “Top N in each category” | ROW_NUMBER/RANK with PARTITION BY |
| “Change from previous”, “growth” | LAG/LEAD |
| “Running total”, “cumulative” | SUM() OVER (ORDER BY …) |
| “Duplicates” | GROUP BY … HAVING COUNT(*) > 1 |
Interview Tips
- Ask about edge cases: ties, NULLs, empty groups. Interviewers often want to hear you raise them.
- Say the plan before writing: “I’ll rank salaries in a subquery, then filter rank 2.”
- Build in steps. Write the inner query, check its output, then wrap it. A CTE keeps multi-step answers readable.
- Know one alternative for each answer, such as
NOT EXISTSversusLEFT JOIN, and when each is better.
Practice Exercises
Exercise 1. Find employees who have no one reporting to them.
Show answer
SELECT e.name
FROM employees e
WHERE NOT EXISTS (SELECT 1 FROM employees r WHERE r.manager_id = e.id);
It returns Mou, Anik and Sumi, the employees at the bottom of the hierarchy.
Exercise 2. Show each delivered order with a running total of delivered revenue by date.
Show answer
SELECT order_date, amount,
SUM(amount) OVER (ORDER BY order_date) AS running_total
FROM orders
WHERE status = 'delivered'
ORDER BY order_date;
Frequently Asked Questions
What SQL topics come up most in interviews?
Joins (especially LEFT JOIN and self joins), GROUP BY with HAVING, subqueries, window functions, NULL handling and the order in which a query’s clauses run. Analyst roles add date handling and conditional aggregation.
Which database should I practise on?
Any of them. The questions rely on standard SQL, which works almost unchanged in PostgreSQL, MySQL 8, SQL Server and SQLite. Mention the dialect you are using, and note any differences such as LIMIT versus TOP.
Summary
- Most interview questions map to a handful of patterns.
- Use
DENSE_RANKfor “Nth distinct value” and window ranking for “top per group”. - Self joins handle hierarchies;
LEFT JOIN … IS NULLfinds missing matches. - Check ties and NULLs before you call an answer finished.
Next: GROUP_CONCAT and STRING_AGG.
