InterviewStack.io LogoInterviewStack.io

Structured Query Language Fundamentals and Aggregation Questions

This topic covers core Structured Query Language fundamentals for analytical querying and reporting. Candidates should be able to write correct, readable, and maintainable SELECT queries with filtering using WHERE, sorting with ORDER BY, grouping with GROUP BY, and group filtering with HAVING. They should apply aggregate functions such as COUNT, COUNT DISTINCT, SUM, AVG, MIN, and MAX and understand how NULL values affect results, how empty result sets behave, and when to use different counting approaches. The scope includes date and time filtering, basic cohort segmentation, and common time based comparisons used to compute metrics such as daily active users, average revenue per user, and period over period comparisons. Candidates are expected to use basic joins and join predicates including inner joins and left joins, write simple subqueries and conditional expressions, and perform common data transformation and cleansing patterns to prepare data for analysis. Finally, this topic assesses query readability and maintainability practices such as aliasing and formatting, plus awareness of elementary performance considerations including index usage and avoiding unnecessary full table scans for entry to mid level analytical tasks.

MediumTechnical
62 practiced
Using transactions(transaction_id INT, user_id INT, amount DECIMAL, occurred_at TIMESTAMP) and users(user_id INT, signup_date DATE), write SQL to compute ARPU (Average Revenue Per User) for the last 30 days defined as: total_amount_in_period / count_distinct_active_users_in_period. Return period_start, total_revenue, active_users, arpu. Ensure your query avoids division by zero and clarifies how to treat users with 0 revenue.
EasyTechnical
54 practiced
Explain the differences between COUNT(*), COUNT(column) and COUNT(DISTINCT column) in SQL. For each variant describe how NULL values are treated, what exactly is being counted, and provide a short example (3-5 rows) showing differing outputs. Also discuss performance and when you'd prefer one form over another in reporting queries.
MediumTechnical
50 practiced
Write a single SQL query to compute month, revenue, and month_over_month_pct_change for the last 12 full months using transactions(created_at TIMESTAMP, amount DECIMAL). Use window functions or self-join to compute the previous month's revenue and calculate percent change. Make sure your query handles months with zero revenue and avoids division by zero.
MediumTechnical
58 practiced
Write a SQL query to flag transactions where amount > 3 * average_transaction_amount_for_that_user_over_the_previous_30_days (excluding the current transaction). Use transactions(transaction_id, user_id, amount, occurred_at). Return transaction_id, user_id, amount, avg_30d, is_anomaly boolean. Provide a solution using window functions or correlated subqueries and explain performance trade-offs.
HardTechnical
46 practiced
Write a SQL query to compute daily conversion_rate = conversions / active_users for each day using two aggregated tables or subqueries (daily_conversions and daily_active_users). Safeguard against division by zero and NULLs so the rate is 0 when active_users = 0 and NULL only when both numerator and denominator are NULL. Explain your use of COALESCE and NULLIF.

Unlock Full Question Bank

Get access to hundreds of Structured Query Language Fundamentals and Aggregation interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.