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, order_date, channel, total_amount), write a query producing monthly revenue with separate online_revenue and in_store_revenue columns, using conditional aggregation (SUM with CASE WHEN).
Given orders(order_id, vendor_id, amount, status) and vendors(vendor_id, name), compute average order value per vendor, but only for completed orders and only for vendors with at least 100 such orders. Write the query, applying the status filter with WHERE (before aggregation) and the count threshold with HAVING (after aggregation), and explain why each belongs where it does.
Given page_views(user_id, page_id, viewed_at), compare using DISTINCT versus GROUP BY to compute the number of unique users per page. Which is more appropriate here, and why?
Given orders(order_id, coupon_code VARCHAR, amount) where many rows have coupon_code NULL, write a query showing discount usage counts grouped by coupon_code, labeling NULLs as 'NO_COUPON' via COALESCE. Explain how GROUP BY treats NULL values by default.
Can a SELECT alias be used in GROUP BY or ORDER BY? Explain how this differs across PostgreSQL, MySQL, and SQL Server, and give a portable recommendation for writing SQL that needs to run on more than one engine.
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.