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.
Given orders(order_id, customer_id, order_date, total_amount), write a query finding duplicate orders based on the combination of (customer_id, order_date, total_amount), returning one representative order_id per duplicate group and the duplicate_count. Show the GROUP BY/HAVING approach, and mention how a window function offers an alternative.
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.
Given events(event_id, user_id, event_type, occurred_at), write a query computing Daily Active Users (DAU) for the last 30 full days: count of distinct users per UTC day, treating multiple same-day events per user as one. Compare writing this with COUNT(DISTINCT ...) versus GROUP BY, and note performance considerations at scale.
Given customers(id), orders(order_id, customer_id), and order_items(order_item_id, order_id, product_id, qty, unit_price), compute total revenue per customer. Explain why joining customers directly to order_items and then grouping can silently double-count revenue, and show the safe pattern: pre-aggregate order_items to order totals first, then join up to customers.
Given products(product_id, name, category), write a query returning rows where category is one of 'electronics', 'appliances', or 'furniture'. Show it two ways: using IN and using chained OR comparisons. Which is clearer, and does it matter for performance?
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.