Programming
SQL coding practice
Asked in nearly every data, analyst and backend interview — and where most candidates lose marks.
6 exercises
Try each one before reading the hint. If you finish without using the hint, move on; if the hint is not enough, work the neighbouring DSA pattern.
01Second highest salary
TaskWrite a query to find the second highest salary from an Employee table, returning NULL if it does not exist.
HintUse a subquery with LIMIT 1 OFFSET 1, or MAX(salary) WHERE salary < (SELECT MAX(salary) ...).
02Employees earning more than their manager
TaskGiven Employee(id, name, salary, managerId), list employees who earn more than their manager.
HintSelf-join the table to itself on managerId = manager.id and compare salaries.
03Duplicate emails
TaskFind all email addresses that appear more than once in a Person table.
HintGROUP BY email HAVING COUNT(*) > 1.
04Department-wise highest salary
TaskReturn each department along with the name of its highest-paid employee.
HintUse ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) and filter to rank 1.
05Consecutive login days
TaskFind users who logged in on at least three consecutive days.
HintSubtract a ROW_NUMBER() from the date; consecutive days share the same difference.
06Running total of orders
TaskFor each order date, show the cumulative number of orders up to that date.
HintSUM(...) OVER (ORDER BY order_date).
Practise here. Get placed there.
Prep+ is unlimited practice. When you are ready to turn it into an offer, TCC Placement Partner handles portfolio, resume and interviews.