SQL interview questions
The SQL questions interviewers ask most, with short answers you can explain in your own words. Tap a question to see the answer.
01What is the difference between WHERE and HAVING?+
WHERE filters rows before grouping. HAVING filters groups after GROUP BY, so it can use aggregates like SUM or COUNT.
02In what order does SQL process a query?+
FROM and JOIN, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, then TOP or LIMIT. That is why a SELECT alias cannot be used in WHERE.
03What is the difference between DELETE, TRUNCATE and DROP?+
DELETE removes chosen rows and supports WHERE. TRUNCATE quickly removes all rows but keeps the table. DROP removes the table completely.
04Primary key vs unique key?+
A primary key identifies each row, cannot be NULL and there is only one per table. A table can have several unique keys, and they usually allow NULL.
05How does NULL behave in comparisons?+
NULL means unknown, so NULL = NULL is not true. Use IS NULL. COUNT(column) skips NULLs, COUNT(*) counts every row.
06UNION vs UNION ALL?+
UNION removes duplicates (extra work). UNION ALL keeps every row and is faster.
07Explain INNER, LEFT and FULL OUTER JOIN.+
INNER returns matching rows only. LEFT returns all left rows plus matches. FULL returns all rows from both sides, with NULLs where there is no match.
08How do you find customers with no orders?+
LEFT JOIN orders and keep rows where the order side is NULL, or use NOT EXISTS.
SELECT c.*
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;09Why can a LEFT JOIN act like an INNER JOIN?+
A WHERE filter on a right-table column removes the NULL rows. Move that condition into the ON clause.
10EXISTS vs IN?+
EXISTS stops at the first match and handles NULLs safely. NOT IN returns nothing if the subquery has a NULL, so prefer NOT EXISTS.
11How do you find duplicate rows?+
Group by the columns that should be unique and keep groups with COUNT(*) > 1.
SELECT email, COUNT(*)
FROM customers
GROUP BY email
HAVING COUNT(*) > 1;12Find the second highest salary.+
Use DENSE_RANK so ties are handled correctly.
SELECT DISTINCT salary FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS r
FROM employees) t
WHERE r = 2;13ROW_NUMBER vs RANK vs DENSE_RANK?+
For ties: ROW_NUMBER gives 1,2,3. RANK gives 1,1,3. DENSE_RANK gives 1,1,2.
14How do you get the latest record per customer?+
ROW_NUMBER partitioned by customer, ordered by date descending, keep row 1.
15How do you write a running total?+
SUM as a window function ordered by date.
SUM(amount) OVER (ORDER BY order_date ROWS UNBOUNDED PRECEDING)16What is a CTE?+
A named subquery written with WITH. It makes long queries readable and can be recursive.
17What is a correlated subquery?+
A subquery that uses a value from the outer query, so it runs per row. A join or window function is often faster.
18What is an index?+
A sorted structure that lets the database find rows without scanning the whole table. It speeds up reads and slightly slows writes.
19What does SARGable mean?+
A filter that can use an index. Wrapping the column in a function, like YEAR(order_date) = 2026, usually breaks it. Use a date range instead.
20What is a star schema?+
A central fact table with measures, joined to dimension tables like date, customer and product. It is simple and fast for reporting.
Want all 50 SQL questions as a PDF?
Free download with answers and examples.
β¨οΈ Practise SQL in your browser
40 interview problems with instant answer checking. Free.
Save with packages
Bundles that include SQL. See all packages β
SQL Developer Combo
SQL Developer, both levels: Intermediate and Advanced.
Data Engineer Track
Everything a data engineering interview covers.
BI Analyst Track
For data analyst and Power BI developer roles.
Complete Bundle
All 12 courses, both levels. 24 PDFs.
Questions before you buy
How do I get the PDF?+
Tap "Buy on WhatsApp". After payment I send you a private download link on WhatsApp, and you can download the file straight away.
How long does delivery take?+
Usually within a few hours of payment, and always within 24 hours.
How do I pay?+
I share the payment options on WhatsApp before you pay. Prices are in US dollars, and you can pay in your local currency at the day's rate.
Can I see a sample first?+
Yes. Every premium pack has a free sample with the first 5 questions. Use the "Free sample" button on the pack.
Can I get a refund?+
Because these are digital files, payments are not refundable once the file is sent. If a file does not open or something is missing, message me and I will fix it quickly.
What students say
All reviews βπ€ Practise with a real mock interview
60 minutes live with Hikmat Ullah, plus written feedback. Bronze 20 USD (1 session), Silver 50 USD (3 sessions), Gold 80 USD (5 sessions).
SQL Developer
From your first SELECT to stored procedures, data models and fast queries.
Learn it 1:1 β PROJECTSSQL Developer projects
A free starter project, plus Small, Large and Enterprise projects.
See projects β MOREOther subjects
Free interview questions for all 12 subjects.
All interview questions β