Let’s be trustworthy — interviews could be brutal, particularly when the questions appear simple after you’ve left the room. Throughout a latest SQL interview, I stumbled over what appeared like “primary” queries. Trying again, these questions have been much less about syntax and extra about considering clearly underneath strain.
Should you’re making ready for an SQL interview, listed below are 10 sensible SQL questions I didn’t reply, with examples and proper options that I realized the onerous method!
Drawback:
Given a Gross sales
desk with columns id
, sale_date
, and quantity
, discover the longest consecutive sequence of gross sales (days with gross sales in a row).
SQL:
WITH ConsecutiveSales AS (
SELECT sale_date,
DATEDIFF(sale_date, ROW_NUMBER() OVER (ORDER BY sale_date)) AS group_id
FROM Gross sales
)
SELECT MIN(sale_date) AS start_date, MAX(sale_date) AS end_date, COUNT(*) AS size
FROM ConsecutiveSales
GROUP BY group_id
ORDER BY size DESC
LIMIT 1;
1. Discover the Longest Consecutive Sequence of Gross salesEnter Desk: Gross sales
id | sale_date | quantity
----|------------|-------
1 | 2025-05-01 | 100
2 | 2025-05-02 | 150
3…