SQL Practice Questions
145 SQL problems for data engineering and analytics interviews: 73 easy, 57 medium and 15 hard. Each one gives you the tables, sample rows and the result to produce. Write the query in the browser, run it against the sample data, then submit it to be checked against hidden test cases. Free, and no setup or sign-in needed to start.
Start here:
- Latest Order Per Customer — window functions, easy
- Find Customers Missing from New Table — joins, easy
- Find Duplicate Emails — aggregation, easy
SQL exercises for beginners
Start with filtering rows, then count records and join related tables. Each exercise supplies sample tables and an expected result, so you can focus on writing the query.
- Filter rows: Find All Products That Are Out of Stock
- Group and count: Count Total Orders Placed by Each Customer
- Join tables: Orders: List Orders With Customer Names (INNER JOIN)
Run checks your query on the sample data. Submit checks the returned rows against the exercise tests. SQL challenges use MySQL 8; keep columns in the order requested by the problem.
New to SQL? Follow the SQL beginner learning path, then return here to practise.
How SQL practice works here
- Queries run on MySQL 8, so window functions and CTEs work. SQLite-only functions such as
strftimedo not; useDATE_FORMAT. - An answer is graded on the rows it returns: column names and row order do not matter, but put the columns in the order the problem shows. Numbers compare by value, so 2500 and 2500.00 are equal, and the first test also checks you return no extra rows.
- Start with an easy problem in a topic, then move up a level once you pass.
- 140 of the 145 problems are free to run and submit. The 5 marked Premium need a paid unlock.
SQL Window Functions practice (62)
ROW_NUMBER, RANK and DENSE_RANK, LAG and LEAD, running totals and moving averages: the pattern behind "top N per group", "compare with the previous row" and rolling metrics. All window functions problems →
- Latest Order Per Customer — Easy · asked at Flipkart, Amazon
- 30-Day Rolling Sales Amount — Easy · asked at Amazon
- 7-Day Rolling Average Sales — Easy · asked at Amazon
- ROW_NUMBER: Assign Unique Rank to Each Employee — Easy · asked at Amazon, Flipkart
- ROW_NUMBER: Find the Highest-Paid Employee in Each Department — Easy · asked at Amazon, Microsoft
- RANK: Rank Employees by Salary (With Gaps) — Easy · asked at Amazon, Google
- RANK vs DENSE_RANK: See the Difference Side by Side — Easy · asked at Databricks, Microsoft
- SUM OVER: Calculate Running Total of Daily Sales — Easy · asked at Amazon, Flipkart
- SUM OVER: Show Grand Total on Every Row — Easy · asked at Amazon, Google
- SUM OVER PARTITION BY: Running Total Per Department — Easy · asked at Databricks, Amazon
- LAG: Show Previous Day's Sales Amount — Easy · asked at Amazon, Google
- LAG: Calculate Day-Over-Day Sales Change — Easy · asked at Amazon, Databricks
- LEAD: Show Next Day's Sales Amount — Easy · asked at Amazon, Flipkart
- LEAD: Detect Days Where Sales Will Increase Next Day — Easy · asked at Amazon, Google
- FIRST_VALUE: Show Highest Salary in Each Department for Every Employee — Easy · asked at Microsoft, Amazon
- LAST_VALUE: Show Last Day's Sales on Every Row — Easy · asked at Amazon, Databricks
- ROW_NUMBER: Remove Duplicate Rows and Keep One Per Order — Easy · asked at Amazon, Flipkart
- Running Total of Daily Sales — Easy
- Day-over-Day Change in Sales — Easy
- Maximum Difference Between Consecutive Values — Easy
- Top 3 Products per Category — Medium · asked at Amazon, Flipkart
- Running Total Revenue — Medium · asked at Swiggy, Zomato
- 3-Day Rolling Sum of Sales — Medium · asked at Amazon
- 7-Day Rolling Purchase Amount by Customer — Medium · asked at Amazon
- 3-Day Rolling Sum Over Sparse Sales Data — Medium · asked at Amazon
- Rolling 3-Month Revenue — Medium · asked at Amazon
- 7-Day Rolling Sales by Product Category — Medium · asked at Amazon
- 7-Day Rolling Sales by Store — Medium · asked at Amazon
- Rolling 12-Month Revenue per Month — Medium · asked at Amazon
- Find Peak Rolling Sales Period — Medium · asked at Amazon
- 7-Day Rolling Sales by Region — Medium · asked at Amazon
- Compare Current vs Previous 7-Day Rolling Sum — Medium · asked at Amazon
- 7-Day Rolling Sales by Product — Medium · asked at Amazon
- Month-to-Date (MTD) Rolling Sum — Medium · asked at Amazon
- 7-Day Rolling Sum with Increase Flag — Medium · asked at Amazon
- 7-Day Rolling Sum by Customer (Partitioned) — Medium · asked at Amazon
- 7-Day Rolling Sum of Positive Sales Only — Medium · asked at Amazon
- Rolling Sum vs Daily Target (Show Difference) — Medium · asked at Amazon
- DENSE_RANK: Rank Employees Without Gaps — Medium · asked at Amazon, Google
- Top 3 Highest-Paid Employees in Each Department — Medium · asked at Amazon, Google
- Rolling 7-Day Average Daily Sales — Medium
- 3-Day Moving Average of Sales — Medium
- Cumulative Sales per Product — Medium
- Month-over-Month Growth Percentage — Medium
- Year-over-Year Growth Percentage — Medium
- Week-over-Week Growth Percentage — Medium
- 7-Day Moving Average of Sales — Medium
- 12-Month Rolling Revenue — Medium
- Best Selling Month of Each Year — Medium
- Top 3 Customers by Region — Medium
- Detect Repeat Toll Crossings — Medium · asked at Uber · Premium
- Reduced Toll for Repeat Crossings — Medium · asked at Uber · Premium
- Detect Rapid Repeat Events — Medium · asked at Uber · Premium
- Detect Significant Salary Declines — Medium · asked at RBS
- Generate Missing IDs for Fruits — Medium · asked at Adobe
- Zomato: Top 5 Most-Ordered Dishes at a Restaurant — Medium · asked at Zomato · Premium
- Median Salary per Department — Hard · asked at Microsoft, Oracle
- Identify 4-Day Consecutive Rolling Sum Increase — Hard · asked at Amazon
- Days Where Rolling Sum Increased by 20% vs Previous Day — Hard · asked at Amazon
- Rolling Sum Reset on Date Gap — Hard · asked at Amazon
- Minimum Days to Reach Rolling Sum of 1000 — Hard · asked at Amazon
- 7-Day Rolling Sum by Store and Product — Hard · asked at Amazon
SQL Joins practice (33)
INNER, LEFT and self joins, anti-joins ("customers who never ordered") and joins across three or more tables, including the NULLs a LEFT JOIN brings back. All joins problems →
- Find Customers Missing from New Table — Easy · asked at Accenture
- Customers With at Least One Order — Easy · asked at Flipkart, Amazon
- Customers Who Have Never Placed an Order — Easy · asked at Flipkart, Infosys
- Count Orders Per Customer Including Zero — Easy · asked at Databricks, Amazon
- Total Amount Spent Per Customer (With Zero) — Easy · asked at Flipkart, Amazon
- Find Employees in the HR Department — Easy · asked at Infosys, TCS
- Count Employees Per Department — Easy · asked at Microsoft, TCS
- List All Employees With Department Names — Easy · asked at Infosys, Wipro
- Find Employees NOT in the HR Department — Easy · asked at Infosys, Accenture
- E-commerce: Customers Who Placed an Order (IN Subquery) — Easy · asked at Flipkart, Amazon
- E-commerce: Count Orders Per Customer — Easy · asked at Flipkart, Amazon
- E-commerce: Customers With No Orders (NOT IN) — Easy · asked at Flipkart, Infosys
- Banking: Customer Account Type and Balance — Easy · asked at Paytm, PhonePe
- Healthcare: Appointments Per Patient — Easy · asked at Oracle, Infosys
- Healthcare: Total Fees Collected Per Doctor — Easy · asked at Oracle, Infosys
- Retail: Total Amount Spent Per Customer — Easy · asked at Flipkart, Myntra
- Logistics: Total Freight Earned Per Driver — Easy · asked at Swiggy, Zomato
- Advanced Joins: Customers With No Orders — Easy · asked at Databricks, Infosys
- Orders: List Orders With Customer Names (INNER JOIN) — Easy · asked at Amazon, Flipkart
- Customers Who Never Placed an Order — Easy · asked at Flipkart, Google
- Count Employees Per Department Including Empty Departments — Easy · asked at Microsoft, TCS
- List Departments That Have No Employees — Easy · asked at Infosys, TCS
- List Every Employee With Their Manager's Name (Self JOIN) — Easy · asked at Amazon, Infosys
- Find Products That Have Never Been Ordered — Easy · asked at Flipkart, Amazon
- E-commerce: Total Amount Spent Per Customer — Medium · asked at Flipkart, Myntra
- Banking: Total Amount Deposited Per Customer — Medium · asked at Paytm, PhonePe
- Retail: Most Expensive Product in Each Category — Medium · asked at Flipkart, Amazon
- Advanced Joins: All Customers With Orders (Including No Orders) — Medium · asked at Databricks, Cloudflare
- Advanced Joins: Payments With Order and Customer Info — Medium · asked at Databricks, Netflix
- Bonus: Total Payment Per Customer (3-Table JOIN) — Medium · asked at Databricks, Netflix
- Bonus: Each Order With Customer Name and Payment Amount — Medium · asked at Databricks, Netflix
- Total Sales Amount by Product Category (JOIN) — Medium · asked at Amazon, Flipkart
- HR: Employees With Rating >= 4 in Latest Review — Hard · asked at Infosys, Microsoft
SQL Aggregation practice (28)
GROUP BY with COUNT, SUM and AVG, HAVING versus WHERE, and conditional aggregation with CASE WHEN. All aggregation problems →
- Find Duplicate Emails — Easy · asked at Amazon, Google
- HR: Average Salary by Department — Easy · asked at Infosys, TCS
- Logistics: Count Shipments by Status — Easy · asked at Swiggy, Cloudflare
- Count Total Orders Placed by Each Customer — Easy · asked at Amazon, Microsoft
- Find Average Order Amount for Each Customer — Easy · asked at Amazon, Infosys
- Find Customers Who Placed More Than 5 Orders (HAVING) — Easy · asked at Flipkart, Amazon
- Find the Most Recent Order Date for Each Customer — Easy · asked at Flipkart, Google
- Find Customers Who Spent More Than ₹1000 in Total (HAVING SUM) — Easy · asked at Flipkart, Amazon
- Find the Customer Who Placed the Most Orders — Easy · asked at Amazon, Flipkart
- Top 5 Customers by Total Amount Spent — Easy · asked at Flipkart, Amazon
- Departments With More Than 2 Employees (HAVING) — Easy · asked at Microsoft, Google
- Top 5 Best-Selling Products by Quantity Sold — Easy · asked at Flipkart, Amazon
- Find the Average Rating of Each Product — Easy · asked at Flipkart, Amazon
- Total Sales Amount by Each Customer — Easy · asked at Amazon, Flipkart
- Peak Sales Day and Its Share of Total — Easy
- First Order Date per Customer — Easy
- Analyze Product Sales by Region — Easy · asked at Amazon, Flipkart
- Zomato: Customers Who Ordered from More Than 3 Restaurants — Easy · asked at Zomato
- Departments With Many Employees AND High Average Salary — Medium · asked at Amazon, Microsoft
- Number of Orders by Each Customer per Month — Medium · asked at Amazon, Netflix
- Month-over-Month Average Order Value — Medium
- Cumulative Active Users Over Time — Medium
- Daily Toll Revenue by Vehicle Type — Medium · asked at Uber · Premium
- Team Match Statistics — Medium · asked at Deloitte
- Sales Data Analysis — Medium · asked at Deloitte
- Rolling 30-Day Active Customers — Hard
- D1, D7 and D30 Retention per User — Hard
- Monthly Customer Churn Count — Hard
More SQL patterns (22)
CTEs and subqueries, date functions, deduplication with ROW_NUMBER, pivoting with CASE WHEN, DAU/WAU/MAU, funnels, sessions and gaps-and-islands.
- Quarterly Sales Pivot by Product — Medium
- Monthly Sales Pivot by Product Type — Hard
- Total Sales per Region using CTEs — Easy
- Monthly Active Users Count — Easy
- Calculate DAU, WAU, MAU for an E-commerce Platform — Hard
- Monthly Revenue Report Using DATE_FORMAT — Easy · asked at Amazon, DataDog
- Count Employees Hired in Each Year — Easy · asked at Infosys, Oracle
- Find the Month With the Highest Number of New Hires — Easy · asked at Infosys, TCS
- Zomato: A User's Orders from the Last 30 Days — Easy · asked at Zomato
- Deduplicate User Login Sessions — Medium
- Remove Duplicate Customer Records — Medium
- Find All Products That Are Out of Stock — Easy · asked at Flipkart, Amazon
- Filter Numeric Values Stored as Strings (SQL) — Easy
- E-commerce Funnel Conversion Rates — Hard
- Consecutive Low-Sales Islands per Product — Hard
- Current and Previous Address Tracking with SCD Type 2 — Medium · asked at Snowflake, Databricks
- Longest Session Duration per User — Easy
- Count Individual Tags — Medium · asked at Adobe
- Second Highest Salary — Easy · asked at Google, Amazon
- Find Employees Who Earn More Than Their Department Average — Medium · asked at Amazon, Google
- Orders Above Average Amount — Easy
- Identifying Users with Purchase Frequency Spikes — Hard · asked at Amazon, Flipkart
Learn the concepts first
SQL window functions with examples · SQL CTEs: the WITH clause · SQL tutorials · SQL: beginner to advanced path · SQL interview questions