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
50 practiced
A report requires products whose monthly revenue has increased for each of the last three months. Write a SQL approach to identify such products using grouped monthly revenue and window functions to compare consecutive months. Explain edge cases when data is missing for months.
MediumTechnical
50 practiced
Write a SQL query that returns the top 3 products by monthly revenue for each month. Use tables: products(product_id, name), orders(order_id, order_date), order_items(order_item_id, order_id, product_id, price). The output should be: month (YYYY-MM), product_id, product_name, revenue, and only top 3 per month.
MediumTechnical
42 practiced
Show how to group by an expression: from users(email TEXT), produce a count of users per email domain by extracting substring after '@' (case-insensitive). Also explain how to speed up this grouping with a functional index in Postgres.
HardTechnical
44 practiced
Compare materialized views, summary tables, and OLAP cubes for serving aggregated reports. Discuss maintenance cost, freshness, storage overhead, query latency, and best application scenarios for each approach in an enterprise analytics stack.
HardTechnical
44 practiced
Design a streaming aggregation strategy for 5-minute tumbling windows that allows up to 2 hours of late events using Apache Flink or Kafka Streams. Explain watermarking, state storage, updates/emitted results, and how you would persist incremental aggregates to a downstream OLAP store.

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.