# SQL for Interviews and OAs

> Write the queries interviewers ask: joins, GROUP BY with HAVING, subqueries, nth highest salary and window functions. Free notes, key concepts, resources and a...

Canonical: https://www.internhack.in/roadmaps/tracks/dsa-placement/sql-interviews
Last updated: 2026-11-28

Write the queries interviewers ask: joins, GROUP BY with HAVING, subqueries, nth highest salary and window functions.

Beginner · about 6 hours · difficulty 2/5

Learn first: [DBMS Concepts](https://www.internhack.in/roadmaps/tracks/dsa-placement/dbms)

Many OAs include 1 or 2 SQL questions, and technical interviewers often ask you to write a query on paper. Practice by writing, not by reading.

### Must-know syntax

- SELECT, WHERE, ORDER BY, LIMIT, DISTINCT.
- **Joins**: INNER, LEFT, RIGHT, FULL, SELF and CROSS joins; know which rows each keeps.
- **Aggregation**: COUNT, SUM, AVG, MIN, MAX with GROUP BY; filter groups with HAVING.
- **Subqueries**: in WHERE, in FROM, and correlated subqueries.
- **Window functions**: ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD, running totals with SUM() OVER.
- DDL vs DML vs DCL; DELETE vs TRUNCATE vs DROP; UNION vs UNION ALL.

### Classic interview queries

Second or Nth highest salary (DENSE_RANK or LIMIT OFFSET), employees earning more than their managers (self join), department-wise highest salary, duplicate emails, customers who never ordered (LEFT JOIN with IS NULL), consecutive numbers, rank scores.

### Pitfalls

- Using WHERE to filter aggregates instead of HAVING.
- NULL handling: NULL = NULL is not true; use IS NULL, and remember COUNT(column) skips NULLs.
- Selecting non-aggregated columns that are not in GROUP BY.

You are ready to move on when you can write the Nth highest salary query three different ways and explain the difference between RANK and DENSE_RANK.

## Key concepts

- **SELECT, WHERE, ORDER BY**
- **Joins**
- **GROUP BY and HAVING**
- **Subqueries and correlated subqueries**
- **Window functions**
- **NULL handling**
- **DELETE vs TRUNCATE vs DROP**

## Mini project

Solve 25 SQL problems in 7 days on HackerRank SQL or LeetCode's database problems, including Second Highest Salary, Nth Highest Salary, Employees Earning More Than Their Managers, Department Highest Salary, Duplicate Emails, Customers Who Never Order, Rank Scores and Consecutive Numbers. Log the concept each one tested.

## Check yourself

Why does SELECT dept, MAX(salary) FROM emp WHERE MAX(salary) > 50000 GROUP BY dept fail, and how do you fix it?

## Free resources

- [SQLBolt interactive lessons](https://sqlbolt.com/) (SQLBolt)
- [HackerRank SQL practice](https://www.hackerrank.com/domains/sql) (HackerRank)
- [W3Schools SQL tutorial](https://www.w3schools.com/sql/) (W3Schools)

Previous: [DBMS Concepts](https://www.internhack.in/roadmaps/tracks/dsa-placement/dbms) · Next: [Operating Systems](https://www.internhack.in/roadmaps/tracks/dsa-placement/operating-systems)

## More in CS Core Subjects for Interviews

- [Object-Oriented Programming](https://www.internhack.in/roadmaps/tracks/dsa-placement/oop)
- [DBMS Concepts](https://www.internhack.in/roadmaps/tracks/dsa-placement/dbms)
- [SQL for Interviews and OAs](https://www.internhack.in/roadmaps/tracks/dsa-placement/sql-interviews)
- [Operating Systems](https://www.internhack.in/roadmaps/tracks/dsa-placement/operating-systems)
- [Computer Networks](https://www.internhack.in/roadmaps/tracks/dsa-placement/computer-networks)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DSA and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/dsa-placement)
