SQL Query Fundamentals Questions
Core SQL for reading and shaping data: SELECT, filtering with WHERE, sorting, DISTINCT, and single-table aggregation with GROUP BY, HAVING, and aggregate functions. Covers reasoning about NULL handling, grouping semantics, and writing correct queries against a given schema. The baseline query-writing surface most data and engineering interviews open with.
Using ROLLUP(category, subcategory) on sales(category, subcategory, amount), write a query returning category, subcategory, total_amount, and a label distinguishing detail rows from subtotal and grand-total rows. Explain how GROUPING() or GROUPING_ID helps identify which rows are totals.
Discuss the trade-offs of referring to GROUP BY columns by ordinal position (GROUP BY 1, 2) versus repeating the full expression versus using a CTE/alias. When is each acceptable in production SQL?
When grouping by a dimension such as customer_segment that may contain NULLs, SQL groups all the NULLs together. Show how to COALESCE NULLs to 'Unknown' for a dashboard, how to still analyze the NULL group separately when needed, and what it means for joining the result back to a segment dimension table.
SELECT category, product_name FROM sales GROUP BY category; raises an error about a non-aggregated column. Explain why, and give two different corrections depending on business intent: picking one representative product per category, versus listing every category/product pair.
Given orders(order_id, status, amount), write a single statement producing counts of orders per status and the percentage of the grand total for each status, rounded to two decimals, ensuring percentages sum to 100 and handling the case where total orders is zero.
Unlock Full Question Bank
Get access to all SQL Query Fundamentals interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.