Cyber Security
Generative AI
Data Science
Blog About
WhatsApp Free Demo
Data Science · Interview Prep

SQL Interview Questions

SQL interview questions ranging from fundamentals to the window functions and query patterns that come up in real analyst interviews.

What's the difference between WHERE and HAVING?
WHERE filters rows before aggregation happens; HAVING filters groups after aggregation. You can't use an aggregate function like COUNT() in a WHERE clause — that's exactly the case HAVING exists for.
Write a query to find the second-highest salary in a table.
One common approach: SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees) — finding the highest value that's still less than the actual maximum.
What is a window function and give an example use case.
A window function performs a calculation across a set of rows related to the current row, without collapsing them into a single output row like GROUP BY does. Example: calculating a running total of sales per day while still showing every individual transaction row.
Explain the difference between UNION and UNION ALL.
UNION combines result sets and removes duplicate rows, which requires extra processing. UNION ALL combines them without removing duplicates — faster, and the right choice whenever you know duplicates aren't a concern.
How would you find customers who made a purchase in one month but not the next?
Typically done with a LEFT JOIN from this month's purchases to next month's, filtering for NULL matches in the second table — or using NOT IN / NOT EXISTS with a subquery of next month's customer IDs.
Go deeper

These questions are just the surface

Our SQL for Data Analysis program builds the hands-on experience behind these answers — not just memorized responses.

View SQL for Data Analysis
Chat with us
WhatsApp Call Free Demo