InterviewStack.io LogoInterviewStack.io

Aggregation Functions and Group By Questions

Fundamentals of aggregation in Structured Query Language covering aggregate functions such as COUNT, SUM, AVG, MIN, and MAX and how to use them to calculate totals, averages, minima, maxima, and row counts. Includes mastery of the GROUP BY clause to group rows by one or more dimensions such as customer, product, region, or time period, and producing metrics like total revenue by month, average order value by product, or count of transactions by date. Covers the HAVING clause for filtering aggregated groups and explains how it differs from WHERE, which filters rows before aggregation. Also addresses related topics commonly tested in interviews and practical problems: grouping by multiple columns, grouping on expressions and date truncation, using DISTINCT inside aggregates, handling NULL values, ordering and limiting grouped results, using aggregates in subqueries or derived tables, and basic performance considerations when aggregating large datasets. Practice examples include calculating monthly revenue, finding customers with more than a threshold number of orders, and identifying top products by sales.

HardTechnical
41 practiced
Write SQL or pseudocode to compute distinct users per (country, device) pair using scalable, memory-efficient techniques. Discuss trade-offs of concatenating keys and using exact COUNT(DISTINCT) vs HyperLogLog (HLL) sketches and provide an example using BigQuery APPROX_COUNT_DISTINCT or an HLL UDF.
HardTechnical
49 practiced
You see a GROUP BY query running very slowly on Redshift that aggregates on product_id and date. Describe a step-by-step plan to profile and optimize it: explain explain/analyze output interpretation, distribution and sort key choices, vacuum/analyze, use of DISTKEY/SORTKEY, and possible rewrite using materialized views or pre-aggregations.
EasyTechnical
86 practiced
Write an ANSI SQL query to produce total revenue per customer per month using the orders table (order_id, customer_id, total_amount, created_at). Use a portable method to group by month (e.g., date_trunc or EXTRACT depending on dialect). Show expected output for January and February 2025 for a few sample rows.
MediumTechnical
47 practiced
Using PySpark DataFrame API, write code to compute weekly revenue per product from a large orders DataFrame (order_id, product_id, price, quantity, created_at). Include optimizations: reduce shuffle, partitioning, and use of map-side aggregations (if applicable).
MediumTechnical
61 practiced
You maintain a dbt model that creates daily aggregated metrics (daily_revenue by product). Describe how to implement this model as incremental in dbt, including SQL structure, unique keys, handling late-arriving data/backfills, and tests to ensure correctness.

Unlock Full Question Bank

Get access to hundreds of Aggregation Functions and Group By interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.