SQL for Data Analysis Questions
Using SQL as a tool for data analysis and reporting. Focuses on writing queries to extract metrics, perform aggregations, join disparate data sources, use subqueries and window functions for trends and rankings, and prepare data for dashboards and reports. Includes best practices for reproducible analytical queries, handling time series and date arithmetic, basic query optimization considerations for analytic workloads, and when to use SQL versus built in reporting tools in analytics platforms.
MediumTechnical
61 practiced
Create an SQL test case (sample rows and query) that demonstrates an off-by-one error when computing monthly cohorts using date_trunc; explain the bug and provide the corrected SQL. Use sample data and show expected vs incorrect outputs conceptually.
HardTechnical
64 practiced
Given this EXPLAIN ANALYZE snippet (simplified):Explain why this plan might be slow and list concrete SQL or schema changes you would try to improve runtime.
Hash Join (cost=1000..2000 rows=10000) (actual time=2000..2500 rows=10000)
Hash Cond: (a.user_id = b.user_id)
-> Seq Scan on large_table a (cost=0..800 rows=1000000) (actual time=0..800 rows=1000000)
-> Hash (cost=900..900 rows=10000) (actual time=100..100 rows=10000)HardSystem Design
63 practiced
You must design a time-series metrics schema for storing per-minute metrics for millions of users, with retention rules (store raw per-minute for 30 days, hourly rollups for 1 year). Sketch the table schemas, partitioning/clustering strategy, and SQL/automation approach to create hourly rollups from raw data. Focus on SQL patterns and trade-offs.
MediumTechnical
61 practiced
Given two tables `page_views(session_id, user_id, view_ts)` and `purchases(session_id, user_id, purchase_ts, amount)`, write ANSI SQL to compute average time (in seconds) from first page view in a session to purchase for sessions that converted within 24 hours. Return `avg_seconds_to_purchase`. Explain how you handle sessions with multiple views and multiple purchases.
HardTechnical
66 practiced
Given a denormalized fact table with repeated product attributes that change over time, explain strategies to model product attributes so analytics queries show the attribute value at time of purchase. Describe SQL approaches to join with slowly changing dimensions (SCD Type 2), and show example SQL to join orders to the correct product attribute snapshot.
Unlock Full Question Bank
Get access to hundreds of SQL for Data Analysis interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.