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.

EasyTechnical
41 practiced
Table `events` has columns: event_id INT, user_id INT, event_type TEXT, occurred_at TIMESTAMP.
Write a SQL query that returns one row per user who had an 'purchase' event in the last 30 days: columns user_id and last_purchase_at. Order users by last_purchase_at descending. Use ANSI SQL and assume no DB-specific DISTINCT ON.
MediumTechnical
44 practiced
Compare a correlated subquery and a join+aggregate for computing order counts per customer. Given `customers` and `orders`, write both approaches to get customer_id and order_count. Discuss performance implications and when the DB optimizer may transform one into the other.
HardTechnical
38 practiced
You need to join a very large table to a dimension where one key (a 'heavy-hitter') appears in billions of rows, causing severe skew and long-running distributed joins. Propose at least four strategies (SQL-level and architecture-level) to mitigate skew and explain trade-offs.
EasyTechnical
44 practiced
Table `profiles(profile_id, user_id, phone_number TEXT, updated_at TIMESTAMP)` may contain NULL or empty-string phone numbers.
Write a SQL query to find profiles that are missing a usable phone number (treat empty string and NULL as missing). Return profile_id and user_id.
MediumTechnical
56 practiced
You are asked to compute average purchase amount per user, but users with no purchases should be treated as having zero for business reporting. Given `users(user_id)` and `purchases(user_id, amount)`, write a query that returns user_id and avg_amount_treated_zero. Explain the difference between treating NULL as zero and excluding NULLs in averages.

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.