SQL interview questions for beginners
Eight SQL interview questions beginners actually get asked, with short answers and runnable examples on the SQLazo record store.
SQL interview questions for beginners
Interview SQL is rarely about memorizing syntax. It is about showing you can reason from a question to a query. These eight questions cover the ground most beginner interviews actually test.
1. What is the difference between WHERE and HAVING?
WHERE filters rows before grouping; HAVING filters groups after aggregation. If the condition mentions COUNT, SUM or AVG, it belongs in HAVING.
2. INNER JOIN vs LEFT JOIN?
INNER JOIN keeps only rows that match on both sides. LEFT JOIN keeps every row from the left table and fills the right side with NULL when there is no match. Use LEFT JOIN when the missing rows matter.
3. What does NULL do in comparisons?
NULL means unknown, so any comparison with it returns unknown — even NULL = NULL. This returns nothing:
SELECT name FROM artists WHERE formed_year = NULL;
This returns the two artists whose year is missing:
SELECT name, formed_year FROM artists WHERE formed_year IS NULL;
4. How do you find duplicates?
Group by the column that should be unique and keep groups with more than one row:
SELECT country, COUNT(*) AS customers
FROM customers
GROUP BY country
HAVING COUNT(*) > 1
ORDER BY customers DESC, country;
Three countries have more than one customer: Spain, USA and Brazil.
5. What is a primary key? A foreign key?
A primary key uniquely identifies a row — artists.id, for example. A foreign key is a column that points at another table's primary key; tracks.album_id points at albums.id. The database uses it to enforce that a track cannot belong to an album that doesn't exist.
6. How do you get the top row per group?
Rank inside each group with ROW_NUMBER() and filter the first row in an outer query:
WITH ranked AS (
SELECT g.name AS genre, t.title, t.duration_sec,
ROW_NUMBER() OVER (PARTITION BY g.id ORDER BY t.duration_sec DESC) AS rn
FROM tracks t
JOIN genres g ON g.id = t.genre_id
)
SELECT genre, title, duration_sec
FROM ranked
WHERE rn = 1
ORDER BY genre;
PARTITION BY g.id restarts the numbering for every genre. The result has one row per genre: its longest track.
7. UNION vs UNION ALL?
UNION combines two result sets and removes duplicates. UNION ALL keeps everything and is faster because it skips the duplicate check.
SELECT country FROM artists
UNION
SELECT country FROM customers
ORDER BY country;
8. How do you count NULLs?
COUNT(*) counts rows; COUNT(column) counts only non-NULL values:
SELECT COUNT(*) AS artists, COUNT(formed_year) AS with_year
FROM artists;
Twelve artists, ten known formation years. The difference is the two NULLs.
Practice under pressure
Interviews reward clear thinking, not memorized trivia. Practice explaining each query out loud, then try the exercise hub and the practice page.
Keep exploring: the first-queries chapter covers the fundamentals these questions assume.
Questions and answers
What is the difference between WHERE and HAVING?
WHERE filters rows before grouping; HAVING filters groups after aggregation. WHERE cannot see COUNT or SUM because those values do not exist yet.
When should I use a LEFT JOIN?
When unmatched rows on the left still matter — for example, listing every customer including those with no invoices.
How do I return the top row per group?
Use ROW_NUMBER() OVER (PARTITION BY group ORDER BY …) in a CTE, then filter WHERE rn = 1 in the outer query.
What does NULL do in comparisons?
Any comparison with NULL yields unknown, not true — even NULL = NULL. Use IS NULL / IS NOT NULL instead.
What is the difference between UNION and UNION ALL?
UNION removes duplicate rows; UNION ALL keeps everything and is faster because it does no duplicate check.