Top 50 SQL Interview Questions
Master essential SQL interview queries from basic filtering to advanced window functions, CTEs, and self-joins tested at Amazon, Google, Meta, and Flipkart.
Find Active Subscribers with Valid Billing in SQL
Filter subscriber records based on active status flags and non-null payment verification timestamps.
Filter High-Value Transactions in Price Ranges in SQL
Select transaction records within a defined numeric threshold using the BETWEEN operator.
Conditional Categorization Using CASE WHEN in SQL
Categorize orders into priority tiers based on delivery distance and preparation duration.
IN vs EXISTS in SQL Subquery Filtering
Compare query performance and NULL handling behavior between IN and EXISTS.
Filtered Aggregations with COUNT(CASE WHEN) in SQL
Pivot counts of different status values into separate columns in a single scan.
Calculate Customer Lifetime Value with INNER JOIN in SQL
Join customer details with order history to aggregate total order count and lifetime spend.
Find Unmatched Records with LEFT JOIN Anti-Pattern in SQL
Identify records in table A that have no corresponding matching records in table B using LEFT JOIN and IS NULL.
Self-Joins for Organizational Hierarchy in SQL
Join an employee table with itself to map employees to their direct managers.
Compare Sequential Daily Records with Self-Join in SQL
Identify dates where the temperature was higher than the previous consecutive day.
Aggregate Monthly Revenue and Order Volumes in SQL
Truncate timestamps to monthly calendar periods and compute monthly GMV and volume.
Filter Aggregated Groups with HAVING Clause in SQL
Filter aggregated groups using HAVING to identify VIP customers who placed over 5 orders with total spend exceeding $10,000.
Calculate Repeat Purchase Rate in SQL
Calculate the percentage of customers who have made more than 1 purchase over total customers.
Immediate Food Delivery Rate on First Orders in SQL
Identify each customer's first order and calculate the percentage where scheduled date equals delivery date.
Day 1 User Retention Rate in SQL
Compute the fraction of players who logged back in on the exact consecutive day after their first install.
Find the Second Highest Salary in SQL (Handling Ties & NULLs)
Retrieve the second highest distinct salary from an employee table, returning NULL if no second highest salary exists.
Dynamic Nth Highest Salary Function in SQL
Write a parameterized SQL function or query to return the Nth highest salary from an employee table.
Find Employees Earning Above Department Average in SQL
Use correlated subqueries or window functions to filter employees earning above their specific department average.
Find Numbers Appearing at Least Three Times Consecutively in SQL
Identify values that appear at least three consecutive times in sequential log IDs.
Gaps and Islands: Detect 3+ Day Active Login Streaks in SQL
Master the classic Gaps and Islands problem to group consecutive calendar date streaks.
Find Product Price as of a Target Date in SQL
Resolve the latest historical price of every product on or before '2026-08-16', defaulting to 10 for products with no prior updates.
Traverse Organizational Hierarchy with Recursive CTEs in SQL
Recursively traverse an employee management tree to compute organizational hierarchy levels and reporting paths.
ROW_NUMBER vs RANK vs DENSE_RANK in SQL Explained
Understand how ROW_NUMBER, RANK, and DENSE_RANK handle duplicate values with visual comparisons.
Department Top Three Salaries in SQL
Find high earners who earn in the top three unique salaries for each department.
Top N Records Per Group Using PARTITION BY in SQL
Select the top 3 best-selling products within each product category.
Calculate Rolling 7-Day Moving Sums in SQL
Compute rolling 7-day average daily spend using window frames and date bounds.
Calculate Month-over-Month Growth with LAG() in SQL
Use the LAG() analytical window function to compare current month revenue against previous month.
Compute Cumulative Running Totals with SUM() OVER in SQL
Calculate progressive cumulative running totals across transactions ordered by date.