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.

HardTechnical
53 practiced
You need to one-hot encode a categorical column 'category' with moderate cardinality (under 20) into columns for model training using SQL. Write a SQL query that pivots categories into separate boolean columns per user, explain the approach (CASE aggregates or crosstab), and discuss performance and maintainability trade-offs when the cardinality increases.
MediumTechnical
60 practiced
You maintain a long, nested SQL that calculates monthly metrics with several subqueries. Rewrite the structure into readable CTEs (WITH ...), explain the pros/cons of CTEs for readability and performance, and show a short example converting two nested subqueries into two named CTEs.
EasyTechnical
56 practiced
You are handed a long, single-line SQL used in a report with no aliases or formatting. Describe 5 concrete readability and maintainability changes you would apply (naming, aliasing, comments, CTEs, ordering), then show a short example refactor of a messy JOIN into a readable CTE-based query.
MediumTechnical
53 practiced
Tables:orders(order_id, user_id, placed_at)order_items(item_id, order_id, product_id, price)
Write a SQL query to compute the number of unique users who purchased in the last 30 days. Explain why joining orders to order_items naively can inflate counts and demonstrate the safe SQL pattern to avoid inflated unique-user counts.
HardTechnical
45 practiced
Tables:transactions(user_id, occurred_at, revenue)
Write SQL to compute cohort LTV (lifetime value): for each signup cohort (by month), compute the cumulative revenue per user at 30, 60, and 90 days after signup, then compute the cohort median LTV at each horizon. Use window functions and explain steps to avoid double-counting revenue across horizons.

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.