InterviewStack.io LogoInterviewStack.io

Relational Databases and SQL Questions

Focuses on relational database fundamentals and practical SQL skills. Candidates should be able to write and reason about SELECT queries, JOINs, aggregations, grouping, filtering, common table expressions, and window functions. They should understand schema design trade offs including normalization and denormalization, indexing strategies and index types, query performance considerations and basic optimization techniques, how to read an execution plan, and transaction semantics including isolation levels and ACID guarantees. Interviewers may test writing efficient queries, designing normalized schemas for given requirements, suggesting appropriate indexes, and explaining how to diagnose and improve slow queries.

MediumTechnical
92 practiced
Explain what a covering index is and how it can produce index-only scans. Given a reporting query that selects columns (customer_id, order_date, total_amount) with WHERE order_date BETWEEN X AND Y, recommend an index to make the query an index-only scan and justify column order in the index.
MediumTechnical
76 practiced
You need to return all customers who are in the customers table but have no orders in the last 12 months. Given:
customers(customer_id, name)
orders(order_id, customer_id, order_date)
Write two SQL solutions: one using LEFT JOIN/IS NULL (anti-join) and one using NOT EXISTS. Explain performance trade-offs and which you prefer for large datasets.
EasyTechnical
66 practiced
You need to break a complex query into readable steps. Given:
orders(order_id, customer_id, order_date, amount)
Write a Common Table Expression (CTE) that first computes monthly_revenue (month, customer_id, revenue) and then selects the top 5 customers by revenue for the latest month. Use PostgreSQL-compatible SQL and explain when CTEs can harm performance.
MediumTechnical
92 practiced
You are shown this query executed frequently:
SELECT o.id, o.customer_id, o.order_date
FROM orders o JOIN customers c ON o.customer_id = c.id
WHERE o.order_date BETWEEN '2024-01-01' AND '2024-01-31' AND c.country = 'US';
Given that orders has millions of rows and current indexes are on orders.id and customers.id, recommend indexes to improve performance, and explain trade-offs for write-heavy workloads.
EasyTechnical
64 practiced
Schema:
products(product_id INT, name VARCHAR, price NUMERIC)
orders(order_id INT, product_id INT, qty INT, order_date DATE)
Write a correlated subquery that returns product_id, name, price, and last_order_date (the most recent order_date for that product). Use ANSI SQL and explain when a correlated subquery is less efficient than a JOIN.

Unlock Full Question Bank

Get access to hundreds of Relational Databases and SQL interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.