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.

MediumTechnical
50 practiced
Write a SQL query to find all products with average rating > 4.5 and at least 50 reviews. Use the following schema and apply GROUP BY and HAVING correctly.
Tables:
products(product_id, name)
reviews(review_id, product_id, rating int, created_at timestamp)
Return product_id, name, avg_rating, review_count ordered by avg_rating DESC.
HardTechnical
54 practiced
Explain the difference between ROWS BETWEEN and RANGE BETWEEN in window frame clauses. Provide SQL examples where the two produce different results (e.g., duplicate timestamps) and discuss performance implications and when each is appropriate.
HardTechnical
43 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.
EasyTechnical
57 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.
HardTechnical
43 practiced
A JOIN between orders (1B rows) and products (10K rows) is slow. Describe join strategies (broadcast/hash/nested-loop/merge) and write a SQL hint or configuration example to force or suggest a broadcast join in a distributed engine such as Spark SQL. Explain risks.

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.