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
0 practiced
Write a SQL query that returns all customers who have total_spent greater than the average customer spend. Use a subquery to compute the average. Table schema:
customers(customer_id, name)
payments(payment_id, customer_id, amount, paid_at)
Return customer_id, name, total_spent, and ensure correct handling of customers with no payments.
EasyTechnical
0 practiced
Using the events table below, write a query to return the most recent event row per user using a window function. Return user_id, event_id, event_ts, and event_type.
Table: events
event_id bigint,
user_id bigint,
event_type text,
event_ts timestamp,
properties jsonb
Use ROW_NUMBER() to pick the latest event per user and exclude ties by timestamp appropriately.
HardTechnical
0 practiced
You have two daily snapshots of user profiles (yesterday and today). Write a SQL workflow to quickly detect rows that changed (any column) using hashing techniques. Provide SQL to compute and compare row hashes and list changed user_ids. Discuss false positives/negatives and null handling.
HardTechnical
0 practiced
Describe approximate distinct count functions available in modern warehouses (e.g., approx_count_distinct, HyperLogLog). Write a SQL example using an approximate function and discuss accuracy, memory, and when to choose approximation over exact count.
MediumTechnical
0 practiced
You're asked to compute COUNT(DISTINCT user_id) for billions of rows. Describe SQL-side options to make it faster and more resource-efficient (exact and approximate). When is it acceptable to use an approximate result in production?

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.