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
58 practiced
Explain and demonstrate with SQL how to safely roll up hourly event counts into daily aggregates using a materialized view that refreshes incrementally (assume PostgreSQL or a data warehouse that supports incremental refresh). Discuss trade-offs between full and incremental refresh and how to handle late-arriving events.
MediumTechnical
54 practiced
You ingest transaction amounts as formatted strings like '$1,234.56' in a raw staging table. Write SQL to clean and convert these to numeric values and compute total revenue per day. Use standard ANSI SQL or PostgreSQL functions; include handling for NULLs and malformed values. Schema:
staging(tx_id BIGINT, tx_ts TIMESTAMP, raw_amount TEXT)
MediumTechnical
60 practiced
From the orders table below, write a SQL query to find countries where average order value (AOV) is > 100 AND there are at least 100 unique customers. Use GROUP BY and HAVING. Explain why HAVING is necessary here and any caveats with NULLs in order_amount.
orders(order_id BIGINT, user_id BIGINT, country TEXT, order_amount NUMERIC)
HardTechnical
50 practiced
Explain how NULL values interact with aggregate functions and GROUP BY semantics. Provide short SQL examples that show: COUNT(col) vs COUNT(*), SUM on an empty group, and how GROUP BY treats NULL values. Then explain when to use COALESCE and any implications for analytics correctness.
MediumTechnical
61 practiced
Write SQL to return, for each user, the total number of sessions they had in a date range, where sessions are identified by session_id in events. Ensure your query avoids counting duplicate session rows due to session updates, and explain how you handle NULL session_ids. Schema:
events(event_id, user_id, session_id TEXT NULL, event_ts TIMESTAMP)

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.