SQL Interview Questions and Answers for Beginners
These are questions that come up again and again in entry-level SQL interviews. Learn the idea behind each answer instead of memorizing words.
1. What is the difference between WHERE and HAVING?
WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY, usually on an aggregate.
SELECT city, SUM(amount) AS revenue
FROM orders
WHERE status = 'paid'
GROUP BY city
HAVING SUM(amount) > 50000;2. INNER JOIN vs LEFT JOIN
INNER JOIN returns only rows that match in both tables. LEFT JOIN returns every row from the left table and fills the right side with NULL when there is no match. See our JOIN guide.
3. PRIMARY KEY, UNIQUE and FOREIGN KEY
A primary key uniquely identifies each row and cannot be NULL. A unique constraint also prevents duplicates but a table can have several. A foreign key links a column to the primary key of another table to keep the data consistent.
4. DELETE vs TRUNCATE vs DROP
DELETE removes chosen rows and can be rolled back inside a transaction. TRUNCATE quickly removes all rows and keeps the table structure. DROP removes the table itself. Exact rollback behavior depends on the database.
5. What is normalization?
Organizing data into separate related tables to reduce repeated information and update errors, for example keeping customers in one table and orders in another.
6. UNION vs UNION ALL
UNION combines results and removes duplicates. UNION ALL keeps every row and is faster because it skips the duplicate check.
7. How do you handle NULL?
NULL means unknown, so = NULL never works. Use IS NULL and IS NOT NULL, and COALESCE to supply a default.
8. What is an index?
A structure that helps the database find rows faster, like an index in a book. It speeds up reads on the indexed columns but adds some cost to inserts and updates.
9. What are ACID properties?
Atomicity (all or nothing), Consistency (rules stay valid), Isolation (transactions do not interfere) and Durability (committed data survives a crash).
10. Find the second highest salary
A clear answer shows you can use a subquery:
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);11. Find duplicate emails
SELECT email, COUNT(*) AS times
FROM users
GROUP BY email
HAVING COUNT(*) > 1;12. Subquery or join?
Both can often solve the same problem. Joins are usually easier to read and optimize for combining columns; subqueries are handy for filtering with a computed value.
Brush up on the basics with SELECT and WHERE and SQL functions.
Frequently asked questions
How should I prepare for an SQL interview?
Practice writing queries by hand on small tables, and be ready to explain why your query works.
Do I need to know every database?
No. Know standard SQL well and be honest about which database you have used.
What topics come up most?
Joins, GROUP BY with aggregates, subqueries, keys, indexes, NULL handling and basic design.