InterviewStack.io LogoInterviewStack.io

SQL Fundamentals and Query Writing Questions

Comprehensive query writing skills from basic to intermediate level. Topics include SELECT and WHERE, joining tables with inner and outer joins, grouping with GROUP BY and filtering groups with HAVING, common aggregation functions such as COUNT SUM AVG MIN and MAX, ORDER BY and DISTINCT, subqueries and common table expressions, basic window functions such as ROW_NUMBER and RANK, union operations, and principles of readable and maintainable query composition. Also covers basic query execution awareness and common performance pitfalls and how to write correct, efficient queries for combining and summarizing relational data.

HardTechnical
0 practiced
You're creating parameter-driven SQL reports exposed to business users. Describe safe methods to accept and use user-supplied filters (e.g., product category, date range) to avoid SQL injection and ensure predictable plans: prepared statements, bind parameters, whitelist column names, and escaping. Provide concrete recommendations for BI tools that interpolate SQL.
MediumTechnical
0 practiced
You join sales_fact to promotions dimension and find revenue doubles due to many-to-many relationships between products and promotions. As a BI analyst, describe SQL patterns to avoid double-counting (pre-aggregate fact rows before joining, use distinct promotion mapping, or apply proportional allocation). Provide a corrected SQL example that aggregates at the fact grain first.
HardTechnical
0 practiced
Explain why applying functions to a partition key (e.g., DATE(order_date) = '2024-09-01') prevents partition pruning. Rewrite such a predicate to allow pruning and discuss performance differences. Provide a concrete before/after SQL example.
MediumTechnical
0 practiced
You are combining three quarterly partitioned tables using UNION ALL and need a global top 100 by revenue across all quarters. Where should you apply ORDER BY and LIMIT for best efficiency? Provide the SQL pattern and explain why sorting per partition is suboptimal.
MediumTechnical
0 practiced
Produce revenue rollups by region, by product, and a grand total in a single query using GROUPING SETS or ROLLUP. Tables: sales(sale_id, region VARCHAR, product_id INT, amount DECIMAL). Write the SQL and explain how to detect which rows are subtotals vs detail rows in the result.

Unlock Full Question Bank

Get access to hundreds of SQL Fundamentals and Query Writing interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.