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.

HardSystem Design
57 practiced
Design a daily ETL and reporting architecture that produces aggregated KPIs (daily active users, total revenue, avg session length) for Tableau and Power BI dashboards, given an event stream of 100M events/day. Cover ingestion, staging, incremental aggregation, failure handling, and how you expose data to BI tools for low-latency dashboards.
HardSystem Design
57 practiced
A dashboard serves many concurrent users with live DB queries for aggregates. Propose an architecture that uses pre-aggregation layers, caching, and materialized views to handle concurrency while preserving acceptable freshness (e.g., 5-15 minutes). Describe cache invalidation and refresh strategies.
MediumTechnical
52 practiced
Design a SQL approach to aggregate hierarchical geography sales: country -> region -> city. Provide an example using ROLLUP or GROUPING SETS to produce subtotals at each level and explain how to display hierarchy in a BI tool that consumes this result.
HardTechnical
58 practiced
Count distinct pairs (user_id, product_id) in different SQL engines. Show approaches to compute the count of unique (user_id, product_id) pairs in PostgreSQL, BigQuery, and a database that lacks tuple DISTINCT support. Explain pitfalls like string concatenation collisions and type differences.
HardTechnical
51 practiced
Your report uses a correlated subquery that computes customer_total in the SELECT for each row and runs slowly on 100M rows. Explain why correlated subqueries can be slow and show a refactor that computes customer totals once (e.g., using a CTE or JOIN to an aggregated table). Provide before-and-after query sketches.

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.