SQL interview questions and answers
SQL questions appear in interviews for developers, analysts, testers and support roles. Practise writing the queries, not only reading them.
13 questions. Read each one, start the timer and answer out loud as if you were in the room, then open the tip. Nothing is recorded.
What is the difference between WHERE and HAVING?
What a good answer covers
WHERE filters rows before grouping. HAVING filters groups after GROUP BY, so it can use aggregates like COUNT.
What types of JOIN are there?
What a good answer covers
INNER returns matches only. LEFT keeps all rows from the left table, RIGHT from the right, FULL from both. CROSS gives every combination.
How do you find the second highest salary?
What a good answer covers
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees); or use DENSE_RANK().
DELETE, TRUNCATE and DROP: what is the difference?
What a good answer covers
DELETE removes chosen rows and can be rolled back. TRUNCATE removes all rows fast and resets auto-increment. DROP removes the table itself.
What is the difference between a primary key and a unique key?
What a good answer covers
A table has one primary key and it can’t be NULL. It can have many unique keys, which allow NULL values.
What is an index, and when can it slow things down?
What a good answer covers
It speeds up reads on the indexed columns but slows inserts and updates, and takes space.
What is normalisation?
What a good answer covers
Organising tables to remove duplicate data: 1NF has atomic values, 2NF removes partial dependencies, 3NF removes transitive ones.
How do you find duplicate rows?
What a good answer covers
SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1;
What are window functions?
What a good answer covers
Functions over a set of rows without grouping them away: ROW_NUMBER, RANK, DENSE_RANK, and running totals with SUM() OVER().
What is the difference between UNION and UNION ALL?
What a good answer covers
UNION removes duplicates; UNION ALL keeps them and is faster.
What is a CTE?
What a good answer covers
A named temporary result defined with WITH, which makes long queries easier to read and can be recursive.
What are transactions and ACID?
What a good answer covers
A group of statements that succeed or fail together. ACID: atomicity, consistency, isolation, durability.
Count employees in every department, including empty ones.
What a good answer covers
SELECT d.name, COUNT(e.id) FROM departments d LEFT JOIN employees e ON e.dept_id = d.id GROUP BY d.name;
All 13 questions with answers
1. What is the difference between WHERE and HAVING?
WHERE filters rows before grouping. HAVING filters groups after GROUP BY, so it can use aggregates like COUNT.
2. What types of JOIN are there?
INNER returns matches only. LEFT keeps all rows from the left table, RIGHT from the right, FULL from both. CROSS gives every combination.
3. How do you find the second highest salary?
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees); or use DENSE_RANK().
4. DELETE, TRUNCATE and DROP: what is the difference?
DELETE removes chosen rows and can be rolled back. TRUNCATE removes all rows fast and resets auto-increment. DROP removes the table itself.
5. What is the difference between a primary key and a unique key?
A table has one primary key and it can’t be NULL. It can have many unique keys, which allow NULL values.
6. What is an index, and when can it slow things down?
It speeds up reads on the indexed columns but slows inserts and updates, and takes space.
7. What is normalisation?
Organising tables to remove duplicate data: 1NF has atomic values, 2NF removes partial dependencies, 3NF removes transitive ones.
8. How do you find duplicate rows?
SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1;
9. What are window functions?
Functions over a set of rows without grouping them away: ROW_NUMBER, RANK, DENSE_RANK, and running totals with SUM() OVER().
10. What is the difference between UNION and UNION ALL?
UNION removes duplicates; UNION ALL keeps them and is faster.
11. What is a CTE?
A named temporary result defined with WITH, which makes long queries easier to read and can be recursive.
12. What are transactions and ACID?
A group of statements that succeed or fail together. ACID: atomicity, consistency, isolation, durability.
13. Count employees in every department, including empty ones.
SELECT d.name, COUNT(e.id) FROM departments d LEFT JOIN employees e ON e.dept_id = d.id GROUP BY d.name;
Before the interview
- Read the job description twice and match two of your examples to what it asks for.
- Keep printed copies of your resume, ID proof and certificates ready, even for online rounds.
- Know your current CTC, expected salary and notice period. Check typical pay in our salary guide, and compare offers with the in-hand salary calculator.
- Genuine employers never ask you to pay for an interview, training kit or joining. Report anyone who does.