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 campaigns(id, name, daily_budget) and spends(id, campaign_id, amount, occurred_at), write a query finding campaigns whose total spend on a given date exceeded their daily budget, returning campaign id, name, daily_spend, and daily_budget.
What is the difference between GROUP BY and DISTINCT? Give one example where either works, and one example where GROUP BY with an aggregate is necessary because DISTINCT alone is insufficient.
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.
Given transactions(transaction_id, user_id, amount, occurred_at), write a query returning, per user, the total, average, minimum, maximum, and count of transaction amounts over the last 90 days, including only users with at least 3 transactions in the period, ordered by total descending.
Given contacts(contact_id, email, phone), write a query finding rows where phone or email IS NULL, and a separate query for rows where they are empty strings (''). Explain why NULL and '' are different values in SQL and why IS NULL (not = NULL) is required to test for the former.
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.