SQL Interview Questions for 9+ Years Java Developers
This guide consolidates common SQL interview questions for experienced Java developers (9+ years), with solutions and explanations.
Topic Overview
Section titled “Topic Overview”flowchart TD
A[Advanced SQL Interviews]
A --> B[Query Writing]
A --> C[Window Functions]
A --> D[Joins]
A --> E[Aggregations]
A --> F[Performance]
F --> G[Indexing]
F --> H[Execution Plans]
1. Find the 2nd Highest Salary
Section titled “1. Find the 2nd Highest Salary”Using a subquery:
SELECT MAX(salary)FROM EmployeeWHERE salary < (SELECT MAX(salary) FROM Employee);Using DENSE_RANK:
SELECT salaryFROM ( SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) rnk FROM Employee) tWHERE rnk = 2;2. Find the Nth Highest Salary
Section titled “2. Find the Nth Highest Salary”SELECT salaryFROM ( SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) rnk FROM Employee) tWHERE rnk = N;3. Find Duplicate Records
Section titled “3. Find Duplicate Records”SELECT email, COUNT(*)FROM EmployeeGROUP BY emailHAVING COUNT(*) > 1;4. Delete Duplicate Records
Section titled “4. Delete Duplicate Records”Keep the smallest ID.
DELETE e1FROM Employee e1JOIN Employee e2ON e1.email = e2.emailAND e1.id > e2.id;5. Find Employees Without a Department
Section titled “5. Find Employees Without a Department”SELECT e.*FROM Employee eLEFT JOIN Department dON e.dept_id = d.idWHERE d.id IS NULL;6. INNER JOIN vs LEFT JOIN
Section titled “6. INNER JOIN vs LEFT JOIN”INNER JOIN
Section titled “INNER JOIN”Returns only matching rows.
SELECT *FROM Employee eINNER JOIN Department dON e.dept_id = d.id;LEFT JOIN
Section titled “LEFT JOIN”Returns all employees, even when no department exists.
SELECT *FROM Employee eLEFT JOIN Department dON e.dept_id = d.id;| JOIN Type | Returns |
|---|---|
| INNER JOIN | Matching rows only |
| LEFT JOIN | All left-table rows plus matches |
7. Department-wise Highest Salary
Section titled “7. Department-wise Highest Salary”SELECT dept_id, MAX(salary)FROM EmployeeGROUP BY dept_id;8. Highest Paid Employee in Each Department
Section titled “8. Highest Paid Employee in Each Department”SELECT *FROM ( SELECT e.*, ROW_NUMBER() OVER ( PARTITION BY dept_id ORDER BY salary DESC ) rn FROM Employee e) xWHERE rn = 1;9. Running Total
Section titled “9. Running Total”SELECT id, salary, SUM(salary) OVER (ORDER BY id) AS running_totalFROM Employee;10. Employees Earning More Than Department Average
Section titled “10. Employees Earning More Than Department Average”SELECT *FROM Employee eWHERE salary >( SELECT AVG(salary) FROM Employee WHERE dept_id = e.dept_id);11. Employees Joined in the Last 30 Days
Section titled “11. Employees Joined in the Last 30 Days”Standard SQL:
SELECT *FROM EmployeeWHERE joining_date >= CURRENT_DATE - INTERVAL 30 DAY;MySQL:
SELECT *FROM EmployeeWHERE joining_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY);12. Find Consecutive Records
Section titled “12. Find Consecutive Records”Note: num appearing 3+ times anywhere in the table is a different, easier question than 3 consecutive rows with the same num — the two queries below solve the actual “consecutive” version. The correct self-join solution:
SELECT DISTINCT l1.numFROM Logs l1JOIN Logs l2ON l1.id = l2.id - 1AND l1.num = l2.numJOIN Logs l3ON l2.id = l3.id - 1AND l2.num = l3.num;If you only needed “appears at least 3 times anywhere” (not necessarily consecutively), a simple GROUP BY/HAVING would do instead:
SELECT numFROM LogsGROUP BY numHAVING COUNT(*) >= 3;13. Top 3 Salaries in Each Department
Section titled “13. Top 3 Salaries in Each Department”SELECT *FROM ( SELECT e.*, DENSE_RANK() OVER ( PARTITION BY dept_id ORDER BY salary DESC ) rnk FROM Employee e) tWHERE rnk <= 3;14. Find Missing IDs
Section titled “14. Find Missing IDs”Example IDs:
123568SELECT t1.id + 1FROM Employee t1LEFT JOIN Employee t2ON t1.id + 1 = t2.idWHERE t2.id IS NULL;15. Customers Who Never Ordered
Section titled “15. Customers Who Never Ordered”SELECT c.*FROM Customers cLEFT JOIN Orders oON c.id = o.customer_idWHERE o.customer_id IS NULL;16. Monthly Sales
Section titled “16. Monthly Sales”SELECT YEAR(order_date), MONTH(order_date), SUM(amount)FROM OrdersGROUP BY YEAR(order_date), MONTH(order_date);17. Pivot Query
Section titled “17. Pivot Query”SELECTSUM(CASE WHEN dept='IT' THEN salary ELSE 0 END) AS IT,SUM(CASE WHEN dept='HR' THEN salary ELSE 0 END) AS HR,SUM(CASE WHEN dept='Finance' THEN salary ELSE 0 END) AS FinanceFROM Employee;18. Employees Sharing the Same Salary
Section titled “18. Employees Sharing the Same Salary”SELECT salaryFROM EmployeeGROUP BY salaryHAVING COUNT(*) > 1;19. ROW_NUMBER vs RANK vs DENSE_RANK
Section titled “19. ROW_NUMBER vs RANK vs DENSE_RANK”SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC), RANK() OVER (ORDER BY salary DESC), DENSE_RANK() OVER (ORDER BY salary DESC)FROM Employee;| Function | Behavior with Duplicate Values |
|---|---|
| ROW_NUMBER | Always unique sequence |
| RANK | Skips ranking numbers after ties |
| DENSE_RANK | No gaps in ranking |
20. SQL Performance Questions
Section titled “20. SQL Performance Questions”Improving Slow Queries
Section titled “Improving Slow Queries”- Create indexes.
- Avoid
SELECT *. - Use pagination.
- Analyze execution plans.
- Avoid unnecessary joins.
- Partition large tables.
- Use appropriate data types.
Identify Slow Queries
Section titled “Identify Slow Queries”EXPLAINSELECT *FROM EmployeeWHERE email = 'abc@test.com';Clustered vs Non-Clustered Index
Section titled “Clustered vs Non-Clustered Index”| Clustered Index | Non-Clustered Index |
|---|---|
| Data stored physically in index order | Separate index structure |
| One per table | Multiple per table |
Topics to Practice
Section titled “Topics to Practice”- Top-N per group
- Window functions
- Common Table Expressions (CTEs)
- Recursive queries
- Self joins
- Pivot and unpivot
- Ranking functions
- Query optimization
- Indexing
- Execution plans
- Partitioning
- Transactions and isolation levels
- Deadlocks
- Pagination
- Stored procedures
- WHERE vs HAVING
Summary
Section titled “Summary”- Senior SQL interviews focus on solving business problems efficiently rather than memorizing syntax.
- Window functions (
ROW_NUMBER,RANK,DENSE_RANK) are frequently tested. - Understanding joins, aggregation, ranking, and query optimization is essential.
- Interviewers also evaluate knowledge of indexing, execution plans, and performance tuning.
- Practicing real-world query patterns is key for Java developers with 9+ years of experience.