SQL Interview Questions With Queries and Output (2026)
Eight core SQL interview questions on one practice table, with queries tested in SQLite and the actual output shown.
Last updated: 21 September 2026 · By the Asuraa Team
What are the most common SQL interview questions?
The most common SQL interview questions cover joins, grouping, filtering with WHERE and HAVING, NULL handling, duplicates, ranking and the second-highest value. They appear in data analyst, data scientist and software roles, and interviewers usually ask you to write the query and explain the result.
This post works through eight questions on one small practice table. We ran every query in SQLite 3.45.1 and the outputs shown are the real ones. Our post on data analyst interview questions uses a different dataset for more practice.
What practice data do these examples use?
Two small tables, both invented for this post. The employees table has emp_id, name, dept, salary and manager_id, and the depts table has dept and city.
| emp_id | name | dept | salary | manager_id |
|---|---|---|---|---|
| 1 | Asha | Data | 90 | NULL |
| 2 | Ravi | Data | 70 | 1 |
| 3 | Meera | Data | 70 | 1 |
| 4 | Kabir | Sales | 60 | 1 |
| 5 | Divya | Sales | 80 | 1 |
| 6 | Farid | Ops | 50 | 5 |
The depts table holds Data (Pune), Sales (Delhi) and HR (Mumbai). Note that Ops has no row in depts, and HR has no employees.
What is the difference between INNER JOIN and LEFT JOIN?
An INNER JOIN keeps only matching rows, and a LEFT JOIN keeps every row from the left table. The SQLite documentation says a LEFT JOIN adds an extra row for each left-hand row with no match, with NULL in the right-hand columns.
SELECT d.dept, COUNT(e.emp_id) AS n
FROM depts d
LEFT JOIN employees e ON e.dept = d.dept
GROUP BY d.dept
ORDER BY d.dept;
This returns Data 3, HR 0 and Sales 2. HR appears with a count of 0 because of the LEFT JOIN, while an INNER JOIN would drop it.
What is the difference between WHERE and HAVING?
WHERE filters rows before grouping, and HAVING filters groups after aggregation. The PostgreSQL tutorial puts it this way: WHERE selects input rows before groups and aggregates are computed, while HAVING selects group rows after.
SELECT dept, COUNT(*) AS n, AVG(salary) AS avg_sal
FROM employees
GROUP BY dept
HAVING COUNT(*) >= 2
ORDER BY dept;
The result is Data with 3 employees and an average of 76.67, and Sales with 2 employees and an average of 70.0. Ops is dropped because its group has one employee.
How do you find the second highest salary?
Rank the salaries and keep rank 2, or use a subquery. Both approaches are common, so be ready to explain each.
-- Option 1: DENSE_RANK
SELECT name, salary
FROM (
SELECT name, salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS r
FROM employees
)
WHERE r = 2;
-- Option 2: subquery
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
Both give 80, which is Divya's salary. Option 1 also returns the name, and it returns everyone tied at that salary.
What is the difference between ROW_NUMBER, RANK and DENSE_RANK?
They differ in how they treat ties. The SQLite window function documentation describes rank() as the rank with gaps and dense_rank() as the rank without gaps.
SELECT name, salary,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS rn,
RANK() OVER (ORDER BY salary DESC) AS rk,
DENSE_RANK() OVER (ORDER BY salary DESC) AS dr
FROM employees;
| name | salary | ROW_NUMBER | RANK | DENSE_RANK |
|---|---|---|---|---|
| Asha | 90 | 1 | 1 | 1 |
| Divya | 80 | 2 | 2 | 2 |
| Ravi | 70 | 3 | 3 | 3 |
| Meera | 70 | 4 | 3 | 3 |
| Kabir | 60 | 5 | 5 | 4 |
| Farid | 50 | 6 | 6 | 5 |
Ravi and Meera tie at 70. ROW_NUMBER still separates them, and which one gets 3 is not guaranteed unless you add a tie-breaker to the ORDER BY. The same page says window functions were first added in SQLite 3.25.0 (2018-09-15).
How do you find duplicates, and the top row per group?
Group by the column and keep groups with a count above 1. For the top row per group, number the rows within each group and keep the first.
-- Duplicate emails (assume a table emails(id, email))
SELECT email, COUNT(*)
FROM emails
GROUP BY email
HAVING COUNT(*) > 1;
-- Highest-paid employee in each department
SELECT name, dept, salary
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) AS rn
FROM employees
)
WHERE rn = 1;
On a test table with six emails, the duplicate query returned a@x.com three times and b@x.com twice. The second query returns Asha (Data, 90), Farid (Ops, 50) and Divya (Sales, 80).
How does SQL treat NULL?
NULL is not equal to anything, including another NULL, so you test it with IS NULL. The SQLite aggregate function page says count(X) counts the times X is not NULL, while count(*) returns the total number of rows.
SELECT COUNT(*), COUNT(manager_id) FROM employees; -- 6, 5
SELECT * FROM employees WHERE manager_id = NULL; -- no rows
SELECT name FROM employees WHERE manager_id IS NULL; -- Asha
The comparison manager_id = NULL returns no rows even though Asha's manager is NULL. That is a classic trap, and IS NULL is the fix.
How do you use a self join?
Join the table to itself under two aliases. This is how you list each employee with their manager.
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON m.emp_id = e.manager_id
ORDER BY e.emp_id;
The output pairs Ravi, Meera, Kabir and Divya with Asha, and Farid with Divya. Asha appears with a NULL manager because we used a LEFT JOIN.
What do most guides on SQL interview questions get wrong?
Most guides list 50 or more questions with query text and no output. These are the gaps.
- They show no result. Without a small table and the output, you cannot tell whether you understand the query.
- They skip ties and NULLs. These two topics cause most wrong answers, so test them on purpose.
- They ignore dialects. Syntax such as LIMIT, TOP and date functions differs between databases, so ask which one the interviewer uses.
- They forget explanation. Say your logic first, then write the query, then check it on the sample rows.
For the learning path, see how to learn SQL for jobs. For the wider interview process, use our technical interview preparation guide, and pair this post with Python interview questions.
FAQ
What are the most common SQL interview questions?
The usual topics are joins, GROUP BY with HAVING, WHERE versus HAVING, NULL handling, finding duplicates, the second-highest value, subqueries and window functions such as ROW_NUMBER, RANK and DENSE_RANK. Practise writing each query from scratch and explaining the result on a small table.
What is the difference between WHERE and HAVING in SQL?
The PostgreSQL documentation says WHERE selects input rows before groups and aggregates are computed, while HAVING selects group rows after they are computed. So WHERE cannot contain aggregate functions, and HAVING normally does. Use WHERE to filter rows and HAVING to filter groups.
What is the difference between INNER JOIN and LEFT JOIN?
An INNER JOIN returns only rows with a match in both tables. A LEFT JOIN also keeps every left-table row that has no match, filling the right-hand columns with NULL, as the SQLite documentation describes. Use it to find missing matches, such as departments with no employees.
How do you find the second highest salary in SQL?
Rank the salaries with DENSE_RANK in descending order and keep rank 2, or take the maximum salary below the overall maximum using a subquery. DENSE_RANK handles ties without gaps. Say which you would use and why, then check the result on a small example.
What is the difference between ROW_NUMBER, RANK and DENSE_RANK?
ROW_NUMBER gives every row a unique number. RANK gives tied rows the same number and leaves gaps afterwards, while DENSE_RANK gives tied rows the same number with no gaps. SQLite documents all three, and window functions arrived in SQLite version 3.25.0.
How do I practise SQL for interviews?
Create a small database on your own machine, for example with SQLite, and write each query yourself before looking at an answer. Predict the output first, then run it. Repeat with joins, grouping, NULLs and window functions until you can explain each result.
Final thoughts
SQL interviews reward small, checked examples. Build a tiny table, predict each output, run the query and explain the result out loud.
If you are looking for roles that use these skills, our page on SQL jobs in India is a good next step.
Related articles
How to Learn SQL for Jobs in India: A Practical Path
A six-stage path to learn SQL for jobs, with tested example queries, official documentation and proof you can show.
Data Analyst Interview Questions With Worked Examples
Data analyst interview questions by category, with worked SQL, Excel, statistics and case examples and a simple preparation plan.