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
50 practiced
Given a page_views table:
page_views( view_id SERIAL PRIMARY KEY, user_id INT, page_url TEXT, viewed_at TIMESTAMP)
Write a SQL query (PostgreSQL) to list the distinct pages visited by user_id = 123 in the last 7 days, sorted alphabetically. Return only the page_url column and limit results to 100. Explain why DISTINCT and ORDER BY together may be costly on large tables.
MediumTechnical
48 practiced
You have a raw_import_users table with duplicates. Schema: raw_import_users(id, user_id, email, updated_at). Write a SQL query to deduplicate by user_id, keeping the row with the latest updated_at. Use a window function to assign row numbers and return only the latest rows per user_id.
MediumTechnical
54 practiced
A report joins a large orders table to several dimension tables but runs slowly. The original query filters only on order_date. Show 2 concrete SQL refactorings to push down filters or pre-aggregate data and reduce join volume. Explain why pushing predicates or pre-aggregating before joining helps performance.
MediumTechnical
38 practiced
A CTE is used multiple times in a query and seems to be recomputed by the planner. Explain differences between inline CTEs and materialized CTEs, and describe approaches (temp tables, materialized views, explicit materialization hints) you might use when a heavy CTE is reused several times in a reporting query.
EasyTechnical
78 practiced
Explain the difference between WHERE and HAVING in SQL. Then write a PostgreSQL query using the products table below that finds product_category and average(price) per category, returning only categories whose average price is greater than 100.
products(product_id INT, product_name TEXT, product_category TEXT, price NUMERIC)
Show why HAVING is necessary in this example.

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.