SQL Practice Questions
152 SQL problems for data engineering and analytics interviews: 74 easy, 63 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
These three beginner exercises are free to run and submit, with no sign-in needed to start. Filter rows, count records, then join related tables. Each exercise supplies sample tables and an expected result.
- 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.
- 139 of the 152 problems are free to run and submit. The 13 marked Premium need a paid unlock.
SQL Window Functions practice (63)
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 · interview practice: Flipkart, Amazon
- 30-Day Rolling Sales Amount — Easy · interview practice: Amazon
- 7-Day Rolling Average Sales — Easy · interview practice: Amazon
- ROW_NUMBER: Assign Unique Rank to Each Employee — Easy · interview practice: Amazon, Flipkart
- ROW_NUMBER: Find the Highest-Paid Employee in Each Department — Easy · interview practice: Amazon, Microsoft
- RANK: Rank Employees by Salary (With Gaps) — Easy · interview practice: Amazon, Google
- RANK vs DENSE_RANK: See the Difference Side by Side — Easy · interview practice: Databricks, Microsoft
- SUM OVER: Calculate Running Total of Daily Sales — Easy · interview practice: Amazon, Flipkart
- SUM OVER: Show Grand Total on Every Row — Easy · interview practice: Amazon, Google
- SUM OVER PARTITION BY: Running Total Per Department — Easy · interview practice: Databricks, Amazon
- LAG: Show Previous Day's Sales Amount — Easy · interview practice: Amazon, Google
- LAG: Calculate Day-Over-Day Sales Change — Easy · interview practice: Amazon, Databricks
- LEAD: Show Next Day's Sales Amount — Easy · interview practice: Amazon, Flipkart
- LEAD: Detect Days Where Sales Will Increase Next Day — Easy · interview practice: Amazon, Google
- FIRST_VALUE: Show Highest Salary in Each Department for Every Employee — Easy · interview practice: Microsoft, Amazon
- LAST_VALUE: Show Last Day's Sales on Every Row — Easy · interview practice: Amazon, Databricks
- ROW_NUMBER: Remove Duplicate Rows and Keep One Per Order — Easy · interview practice: Amazon, Flipkart
- Running Total of Daily Sales — Easy · interview practice: TCS, Accenture
- Day-over-Day Change in Sales — Easy · interview practice: Cognizant, Infosys
- Maximum Difference Between Consecutive Values — Easy · interview practice: Cognizant, Infosys
- Top 3 Products per Category — Medium · interview practice: Amazon, Flipkart
- Running Total Revenue — Medium · interview practice: Swiggy, Zomato
- 3-Day Rolling Sum of Sales — Medium · interview practice: Amazon
- 7-Day Rolling Purchase Amount by Customer — Medium · interview practice: Amazon
- 3-Day Rolling Sum Over Sparse Sales Data — Medium · interview practice: Amazon
- Rolling 3-Month Revenue — Medium · interview practice: Amazon
- 7-Day Rolling Sales by Product Category — Medium · interview practice: Amazon
- 7-Day Rolling Sales by Store — Medium · interview practice: Amazon
- Rolling 12-Month Revenue per Month — Medium · interview practice: Amazon
- Find Peak Rolling Sales Period — Medium · interview practice: Amazon
- 7-Day Rolling Sales by Region — Medium · interview practice: Amazon
- Compare Current vs Previous 7-Day Rolling Sum — Medium · interview practice: Amazon
- 7-Day Rolling Sales by Product — Medium · interview practice: Amazon
- Month-to-Date (MTD) Rolling Sum — Medium · interview practice: Amazon
- 7-Day Rolling Sum with Increase Flag — Medium · interview practice: Amazon
- 7-Day Rolling Sum by Customer (Partitioned) — Medium · interview practice: Amazon
- 7-Day Rolling Sum of Positive Sales Only — Medium · interview practice: Amazon
- Rolling Sum vs Daily Target (Show Difference) — Medium · interview practice: Amazon
- DENSE_RANK: Rank Employees Without Gaps — Medium · interview practice: Amazon, Google
- Top 3 Highest-Paid Employees in Each Department — Medium · interview practice: Amazon, Google
- Rolling 7-Day Average Daily Sales — Medium · interview practice: IBM, TCS
- 3-Day Moving Average of Sales — Medium · interview practice: IBM, TCS
- Cumulative Sales per Product — Medium · interview practice: Infosys, IBM
- Month-over-Month Growth Percentage — Medium · interview practice: Infosys, IBM
- Year-over-Year Growth Percentage — Medium · interview practice: Infosys, IBM
- Week-over-Week Growth Percentage — Medium · interview practice: Cognizant, Infosys
- 7-Day Moving Average of Sales — Medium · interview practice: TCS, Accenture
- 12-Month Rolling Revenue — Medium · interview practice: Accenture, Cognizant
- Best Selling Month of Each Year — Medium · interview practice: Accenture, Cognizant
- Top 3 Customers by Region — Medium
- Detect Repeat Toll Crossings — Medium · interview practice: Uber · Premium
- Reduced Toll for Repeat Crossings — Medium · interview practice: Uber · Premium
- Detect Rapid Repeat Events — Medium · interview practice: Uber · Premium
- Detect Significant Salary Declines — Medium · interview practice: RBS
- Generate Missing IDs for Fruits — Medium · interview practice: Adobe
- Zomato: Top 5 Most-Ordered Dishes at a Restaurant — Medium · interview practice: Zomato · Premium
- Cumulative Store Sales on Days Above Average — Medium · interview practice: Amazon · Premium
- Median Salary per Department — Hard · interview practice: Microsoft, Oracle · Premium
- Identify 4-Day Consecutive Rolling Sum Increase — Hard · interview practice: Amazon
- Days Where Rolling Sum Increased by 20% vs Previous Day — Hard · interview practice: Amazon
- Rolling Sum Reset on Date Gap — Hard · interview practice: Amazon
- Minimum Days to Reach Rolling Sum of 1000 — Hard · interview practice: Amazon
- 7-Day Rolling Sum by Store and Product — Hard · interview practice: Amazon
SQL Joins practice (34)
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 · interview practice: Accenture
- Customers With at Least One Order — Easy · interview practice: Flipkart, Amazon
- Customers Who Have Never Placed an Order — Easy · interview practice: Flipkart, Infosys
- Count Orders Per Customer Including Zero — Easy · interview practice: Databricks, Amazon
- Total Amount Spent Per Customer (With Zero) — Easy · interview practice: Flipkart, Amazon
- Find Employees in the HR Department — Easy · interview practice: Infosys, TCS
- Count Employees Per Department — Easy · interview practice: Microsoft, TCS
- List All Employees With Department Names — Easy · interview practice: Infosys, Wipro
- Find Employees NOT in the HR Department — Easy · interview practice: Infosys, Accenture
- E-commerce: Customers Who Placed an Order (IN Subquery) — Easy · interview practice: Flipkart, Amazon
- E-commerce: Count Orders Per Customer — Easy · interview practice: Flipkart, Amazon
- E-commerce: Customers With No Orders (NOT IN) — Easy · interview practice: Flipkart, Infosys
- Banking: Customer Account Type and Balance — Easy · interview practice: Paytm, PhonePe
- Healthcare: Appointments Per Patient — Easy · interview practice: Oracle, Infosys
- Healthcare: Total Fees Collected Per Doctor — Easy · interview practice: Oracle, Infosys
- Retail: Total Amount Spent Per Customer — Easy · interview practice: Flipkart, Myntra
- Logistics: Total Freight Earned Per Driver — Easy · interview practice: Swiggy, Zomato
- Advanced Joins: Customers With No Orders — Easy · interview practice: Databricks, Infosys
- Orders: List Orders With Customer Names (INNER JOIN) — Easy · interview practice: Amazon, Flipkart
- Customers Who Never Placed an Order — Easy · interview practice: Flipkart, Google
- Count Employees Per Department Including Empty Departments — Easy · interview practice: Microsoft, TCS
- List Departments That Have No Employees — Easy · interview practice: Infosys, TCS
- List Every Employee With Their Manager's Name (Self JOIN) — Easy · interview practice: Amazon, Infosys
- Find Products That Have Never Been Ordered — Easy · interview practice: Flipkart, Amazon
- E-commerce: Total Amount Spent Per Customer — Medium · interview practice: Flipkart, Myntra
- Banking: Total Amount Deposited Per Customer — Medium · interview practice: Paytm, PhonePe
- Retail: Most Expensive Product in Each Category — Medium · interview practice: Flipkart, Amazon
- Advanced Joins: All Customers With Orders (Including No Orders) — Medium · interview practice: Databricks, Cloudflare
- Advanced Joins: Payments With Order and Customer Info — Medium · interview practice: Databricks, Netflix
- Bonus: Total Payment Per Customer (3-Table JOIN) — Medium · interview practice: Databricks, Netflix
- Bonus: Each Order With Customer Name and Payment Amount — Medium · interview practice: Databricks, Netflix
- Total Sales Amount by Product Category (JOIN) — Medium · interview practice: Amazon, Flipkart
- Amazon: Non-Managers Across Multiple Departments — Medium · interview practice: Amazon · Premium
- HR: Employees With Rating >= 4 in Latest Review — Hard · interview practice: Infosys, Microsoft
SQL Aggregation practice (29)
GROUP BY with COUNT, SUM and AVG, HAVING versus WHERE, and conditional aggregation with CASE WHEN. All aggregation problems →
- Find Duplicate Emails — Easy · interview practice: Amazon, Google
- HR: Average Salary by Department — Easy · interview practice: Infosys, TCS
- Logistics: Count Shipments by Status — Easy · interview practice: Swiggy, Cloudflare
- Count Total Orders Placed by Each Customer — Easy · interview practice: Amazon, Microsoft
- Find Average Order Amount for Each Customer — Easy · interview practice: Amazon, Infosys
- Find Customers Who Placed More Than 5 Orders (HAVING) — Easy · interview practice: Flipkart, Amazon
- Find the Most Recent Order Date for Each Customer — Easy · interview practice: Flipkart, Google
- Find Customers Who Spent More Than ₹1000 in Total (HAVING SUM) — Easy · interview practice: Flipkart, Amazon
- Find the Customer Who Placed the Most Orders — Easy · interview practice: Amazon, Flipkart
- Top 5 Customers by Total Amount Spent — Easy · interview practice: Flipkart, Amazon
- Departments With More Than 2 Employees (HAVING) — Easy · interview practice: Microsoft, Google
- Top 5 Best-Selling Products by Quantity Sold — Easy · interview practice: Flipkart, Amazon
- Find the Average Rating of Each Product — Easy · interview practice: Flipkart, Amazon
- Total Sales Amount by Each Customer — Easy · interview practice: Amazon, Flipkart
- Peak Sales Day and Its Share of Total — Easy · interview practice: Infosys, Wipro
- First Order Date per Customer — Easy · interview practice: Capgemini, TCS
- Analyze Product Sales by Region — Easy · interview practice: Amazon, Flipkart · Premium
- Zomato: Customers Who Ordered from More Than 3 Restaurants — Easy · interview practice: Zomato · Premium
- Departments With Many Employees AND High Average Salary — Medium · interview practice: Amazon, Microsoft
- Number of Orders by Each Customer per Month — Medium · interview practice: Amazon, Netflix
- Month-over-Month Average Order Value — Medium · interview practice: Wipro, Cognizant
- Cumulative Active Users Over Time — Medium · interview practice: Infosys, Wipro
- Daily Toll Revenue by Vehicle Type — Medium · interview practice: Uber · Premium
- Team Match Statistics — Medium · interview practice: Deloitte
- Sales Data Analysis — Medium · interview practice: Deloitte
- Customers Who Purchased From Every Category — Medium · interview practice: Wipro, Cognizant · Premium
- Rolling 30-Day Active Customers — Hard · interview practice: Cognizant, Infosys
- D1, D7 and D30 Retention per User — Hard · interview practice: IBM, TCS
- Monthly Customer Churn Count — Hard · interview practice: IBM, TCS
More SQL patterns (26)
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 · interview practice: Wipro, Cognizant
- Monthly Sales Pivot by Product Type — Hard · interview practice: TCS, Accenture
- Total Sales per Region using CTEs — Easy
- Monthly Active Users Count — Easy · interview practice: Cognizant, Capgemini
- Calculate DAU, WAU, MAU for an E-commerce Platform — Hard · interview practice: Cognizant, Infosys
- Monthly Revenue Report Using DATE_FORMAT — Easy · interview practice: Amazon, DataDog
- Count Employees Hired in Each Year — Easy · interview practice: Infosys, Oracle
- Find the Month With the Highest Number of New Hires — Easy · interview practice: Infosys, TCS
- Zomato: A User's Orders from the Last 30 Days — Easy · interview practice: Zomato
- Amazon: Customers Active on Exactly 3 Days Last Month — Easy · interview practice: Amazon · Premium
- IBM: Average VM Uptime Hours per Region — Medium · interview practice: IBM · Premium
- Deduplicate User Login Sessions — Medium · interview practice: Cognizant, Capgemini
- Remove Duplicate Customer Records — Medium
- Zomato: Top 10 Restaurants by Latest Customer Ratings — Medium · interview practice: Zomato
- Find All Products That Are Out of Stock — Easy · interview practice: Flipkart, Amazon
- Filter Numeric Values Stored as Strings (SQL) — Easy · interview practice: Capgemini, TCS
- E-commerce Funnel Conversion Rates — Hard · interview practice: Cognizant, Infosys
- Consecutive Low-Sales Islands per Product — Hard · interview practice: Cognizant, Infosys
- Current and Previous Address Tracking with SCD Type 2 — Medium · interview practice: Snowflake, Databricks
- Longest Session Duration per User — Easy · interview practice: Cognizant, Capgemini
- Zomato: Sessionize App Events and Flag Sessions That Led to an Order — Medium · interview practice: Zomato
- Count Individual Tags — Medium · interview practice: Adobe
- Second Highest Salary — Easy · interview practice: Google, Amazon
- Find Employees Who Earn More Than Their Department Average — Medium · interview practice: Amazon, Google
- Orders Above Average Amount — Easy · interview practice: TCS, Infosys
- Identifying Users with Purchase Frequency Spikes — Hard · interview practice: 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