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
72 practiced
Design an efficient SQL strategy to compute a 7-day moving average of daily revenue for three years of data to support interactive dashboards. Discuss options such as window functions with appropriate frames, pre-computed daily sums used as base for rolling calculations, incremental rolling updates when new days arrive, and trade-offs between compute time and freshness.
MediumTechnical
57 practiced
Explain how COALESCE and NULLIF assist when aggregating to handle missing or zero values. Given invoice(invoice_id, amount, tax_amount, invoice_date), write SQL to compute monthly average tax_rate defined as tax_amount / NULLIF(amount, 0) aggregated by month, demonstrating protection against division by zero and handling of NULLs for averaging.
MediumTechnical
60 practiced
Describe how parameterized filters in BI tools such as Tableau, Looker and Power BI affect aggregated SQL queries. Explain the difference between applying filters at the data source versus client-side, how passing a product category parameter into aggregated queries changes load, and best practices to keep dashboards responsive while supporting dynamic user filters.
MediumTechnical
58 practiced
A query using HAVING COUNT(*) > 5 runs slowly. Explain why HAVING can be expensive and show how to rewrite or restructure the query so that filtering happens earlier. Provide an example rewrite that moves selective predicates into WHERE or uses a subquery to reduce work before GROUP BY.
HardTechnical
55 practiced
A global dashboard aggregates orders by day; describe strategies to ensure correct daily aggregation across time zones. Discuss storing timestamps in UTC, converting to users' time zones during aggregation, date_trunc with timezone functions, handling daylight savings time, and provide sample SQL to compute daily revenue in America/Los_Angeles timezone.

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.