SQL Interview Questions & Answers
These notes cover SQL from the first SELECT to the ten queries that come up most in placement interviews. Every answer uses the same small
employeestable, so you can follow each result by hand.
About these notes
Each question has a short answer, a query you can run, and the result. The content comes from the “SQL for Placements: Beginner Notes” pages by @codingdidi.
Table of Contents
Core SQL
-
What is SQL?
SQL means Structured Query Language. You use it to read and change data in a database. A table has rows (records) and columns (fields).
-
What does the sample table look like?
Every answer in these notes uses this
employeestable:id name dept salary manager_id 1 Riya IT 90000 NULL 2 Aman HR 45000 1 3 Sagar IT 60000 1 4 Neha Sales 50000 1 5 Kabir IT 60000 3 6 Pooja HR 40000 2 NULLmeans “no value”. Riya has no manager.
-
How do you read data with SELECT?
Query What it does SELECT * FROM employees;all columns, all rows SELECT name, salary FROM employees;chosen columns SELECT name AS emp FROM employees;rename a column SELECT DISTINCT dept FROM employees;IT, HR, Sales ... ORDER BY salary DESC;highest first ... LIMIT 3;only 3 rows Rule of thumb: SELECT what, FROM where, ORDER BY how.
-
How do you get the top 3 earners?
Sort by salary from high to low, then keep 3 rows.
SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 3;Result: Riya 90000, Sagar 60000, Kabir 60000.
-
How do you filter rows with WHERE?
WHERE keeps only the rows that match a condition. It filters rows before anything else happens.
WHERE … Result salary > 55000Riya, Sagar, Kabir salary BETWEEN 45000 AND 60000Aman, Sagar, Neha, Kabir dept IN ('HR', 'Sales')Aman, Neha, Pooja name LIKE 'A%'Aman manager_id IS NULLRiya dept = 'IT' AND salary < 70000Sagar, Kabir SELECT name, salary FROM employees WHERE dept = 'IT' AND salary < 70000;Result: Sagar 60000, Kabir 60000.
-
How do LIKE patterns work?
%matches any number of characters._matches exactly one character.'A%'starts with A'%a'ends with a'_a%'has a as the second letter
-
Why can’t you write = NULL?
NULL means “unknown”, so
manager_id = NULLis never true. Always useIS NULLorIS NOT NULL.SELECT name FROM employees WHERE manager_id IS NULL; -- Riya
-
What are aggregate functions?
They turn many rows into one value.
Function Query Result COUNT SELECT COUNT(*) FROM employees;6 SUM SELECT SUM(salary) ...345000 AVG SELECT AVG(salary) ...57500 MIN SELECT MIN(salary) ...40000 MAX SELECT MAX(salary) ...90000 Aggregates ignore NULL, except
COUNT(*).
-
What is the difference between COUNT(*) and COUNT(column)?
COUNT(*)counts all rows.COUNT(column)skips rows where that column is NULL.SELECT COUNT(*) FROM employees; -- 6 SELECT COUNT(manager_id) FROM employees; -- 5
-
What does GROUP BY do?
GROUP BY gives one result per group.
SELECT dept, COUNT(*) AS emp_count, AVG(salary) AS avg_salary FROM employees GROUP BY dept;dept emp_count avg_salary HR 2 42500 IT 3 70000 Sales 1 50000
-
What is HAVING and how is it different from WHERE?
WHERE filters rows. HAVING filters groups. Use HAVING for conditions on COUNT, SUM or AVG.
SELECT dept, COUNT(*) FROM employees GROUP BY dept HAVING COUNT(*) >= 2;Result: HR 2, IT 3.
-
In what order does SQL run a query?
SQL is not run in the order you write it.
- You write: SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT
- SQL runs: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT
FROM first, LIMIT last. This is a classic interview question.
-
Why can’t WHERE use a SELECT alias or COUNT()?
WHERE runs before SELECT, so an alias made in SELECT does not exist yet. WHERE also runs before grouping, so it can’t use
COUNT(). Use HAVING for that.
Joins & Subqueries
-
What is a JOIN and what types are there?
A JOIN combines rows from two tables. The examples use a second table,
departments:dept location IT Pune HR Mumbai Sales Delhi Finance Noida JOIN Returns INNER JOIN only matching rows in both LEFT JOIN all of left + matches (else NULL) RIGHT JOIN all of right + matches FULL OUTER JOIN all rows from both SELF JOIN a table joined with itself CROSS JOIN every row × every row Finance has no employees. Watch for it in JOINs.
-
How does an INNER JOIN work?
SELECT e.name, d.location FROM employees e INNER JOIN departments d ON e.dept = d.dept;Result: Riya Pune, Aman Mumbai, Sagar Pune … (6 rows). Finance is missing because no employee matches it.
-
How do you find rows that have no match?
Use LEFT JOIN, then keep the rows where the right side is NULL.
SELECT d.dept FROM departments d LEFT JOIN employees e ON e.dept = d.dept WHERE e.id IS NULL;Result: Finance.
-
What is a subquery?
A query inside another query, in
( ). The inner query runs first and the outer query uses its result.SELECT name, salary FROM employees WHERE salary > ( SELECT AVG(salary) FROM employees );The inner query gives 57500. Result: Riya 90000, Sagar 60000, Kabir 60000.
-
How do you use a subquery with IN?
SELECT name FROM employees WHERE dept IN ( SELECT dept FROM departments WHERE location = 'Pune' );Result: Riya, Sagar, Kabir.
Window Functions
-
What are window functions?
They work like GROUP BY, but every row stays visible.
SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) rn, RANK() OVER (ORDER BY salary DESC) rnk, DENSE_RANK() OVER (ORDER BY salary DESC) drnk FROM employees;name salary rn rnk drnk Riya 90000 1 1 1 Sagar 60000 2 2 2 Kabir 60000 3 2 2 Neha 50000 4 4 3 Aman 45000 5 5 4 Pooja 40000 6 6 5
-
What is the difference between ROW_NUMBER, RANK and DENSE_RANK?
They only differ when there is a tie.
- ROW_NUMBER: always unique (1, 2, 3 …).
- RANK: ties share a rank, then it skips (2, 2, 4).
- DENSE_RANK: ties share a rank, no gap (2, 2, 3).
-
What does PARTITION BY do?
PARTITION BY restarts the window for each group. PARTITION BY splits the rows; ORDER BY sorts inside each part.
SELECT name, dept, salary, RANK() OVER (PARTITION BY dept ORDER BY salary DESC) AS dept_rank FROM employees;Result: HR: Aman 1, Pooja 2. IT: Riya 1, Sagar 2, Kabir 2.
-
What are LAG and LEAD?
LAG gets a value from the previous row. LEAD gets a value from the next row.
SELECT name, salary, LAG(salary) OVER (ORDER BY id) AS prev FROM employees;Result: Riya 90000 NULL, Aman 45000 90000 …
Top 10 Interview Queries
-
How do you find the second-highest salary?
Way 1, with a subquery:
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);Way 2, with LIMIT and OFFSET:
SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1;Result: 60000 both ways. DISTINCT matters here, because 60000 appears twice.
-
How do you find the Nth highest salary?
Use the same query as Way 2 above, with
OFFSET N-1. For N = 3:SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 2; -- OFFSET N-1Result: 50000.
-
How do you find duplicate emails?
Table
usershas: a@x.com, b@x.com, a@x.com, c@x.com, b@x.com.SELECT email, COUNT(*) AS cnt FROM users GROUP BY email HAVING COUNT(*) > 1;Result: a@x.com 2, b@x.com 2. Duplicates = GROUP BY +
HAVING COUNT(*) > 1.
-
How do you find the highest salary in each department?
SELECT dept, MAX(salary) AS top_salary FROM employees GROUP BY dept;dept top_salary HR 45000 IT 90000 Sales 50000
-
Who earns more than their department average?
This is a correlated subquery: the inner query runs once per row, using that row’s department.
SELECT e.name, e.dept, e.salary FROM employees e WHERE e.salary > ( SELECT AVG(salary) FROM employees WHERE dept = e.dept );Result: Riya IT 90000, Aman HR 45000.
-
How do you show each employee with their manager’s name?
Join the table to itself with two aliases. That is a SELF JOIN.
SELECT e.name AS employee, m.name AS manager FROM employees e LEFT JOIN employees m ON e.manager_id = m.id;Result: Aman: Riya, Kabir: Sagar, Riya: NULL …
-
How do you find the top earner in each department, with names?
SELECT name, dept, salary FROM ( SELECT name, dept, salary, DENSE_RANK() OVER (PARTITION BY dept ORDER BY salary DESC) AS r FROM employees ) t WHERE r = 1;Result: Aman HR, Riya IT, Neha Sales. Use
r <= 3for “top 3 per department”.
-
How do you count employees per department, including zero?
Start from
departmentsand LEFT JOIN, then counte.id, not*.SELECT d.dept, COUNT(e.id) AS emp_count FROM departments d LEFT JOIN employees e ON e.dept = d.dept GROUP BY d.dept;Result: Finance 0, HR 2, IT 3, Sales 1.
COUNT(*)would give Finance 1, because the NULL row still counts.
-
How do you delete duplicates and keep one?
Keep the smallest
idfor each email and delete the rest.DELETE FROM users WHERE id NOT IN ( SELECT MIN(id) FROM users GROUP BY email );Rows left: 1 a@x.com, 2 b@x.com, 4 c@x.com.
Note: MySQL does not let you read from the same table you are deleting from. There, wrap the subquery once more:
SELECT id FROM (SELECT MIN(id) AS id FROM users GROUP BY email) t.
-
How do you calculate a running total?
SUM() OVER (ORDER BY ...)gives a running total.SELECT name, salary, SUM(salary) OVER (ORDER BY id) AS running_total FROM employees;name salary running_total Riya 90000 90000 Aman 45000 135000 Sagar 60000 195000 Neha 50000 245000 Kabir 60000 305000 Pooja 40000 345000
Recap
-
What should you be able to answer before an interview?
What these notes covered: SELECT, WHERE, ORDER BY, LIMIT, DISTINCT; COUNT, SUM, AVG with GROUP BY and HAVING; INNER, LEFT and SELF JOIN; subqueries; ROW_NUMBER, RANK, DENSE_RANK and PARTITION BY; and the 10 most-asked interview queries.
Quick test, try without looking back:
- What is the difference between WHERE and HAVING?
- Write the second-highest salary query.
- Which runs first: SELECT or WHERE?
- INNER JOIN vs LEFT JOIN?
- RANK vs DENSE_RANK with a tie?
- How do you find duplicate rows?
Practice on real data: every query, every day.