SQL Practice

SQL Practice Questions and Exercises

One candidate we saw lost an onsite at a payments company because their JOIN fan-out turned a 40-row expected result into 12,000 duplicated rows and they couldn't explain why in under two minutes. That's what these exercises train for. Real schemas. Real tables. Real failure modes. You write the query, the grader runs it against Postgres, and you see exactly what your logic produced, not a green check on pattern matching.

150+ exercises. Eight categories. Every query hits a live database. Break things fast so you don't break them in the interview.

854

SQL challenges shipped

275

Companies in dataset

29%

INNER JOIN frequency

21%

PARTITION BY frequency

Source: DataDriven analysis of 1,042 verified data engineering interview rounds.

SQL Exercise Categories

Each category targets a specific SQL skill that appears in real interviews. They are ordered roughly by frequency: the topics you will see most often are at the top.

Aggregation and GROUP BY

25+ exercises

COUNT, SUM, AVG, MIN, MAX with GROUP BY, HAVING, and conditional aggregation. This is the single most-tested SQL category in data engineering interviews. Nearly one in four SQL questions involves grouping rows and computing summary statistics.

Skills covered:

  • Basic GROUP BY with single and multiple columns
  • HAVING vs WHERE filtering
  • Conditional aggregation with CASE WHEN inside SUM and COUNT
  • ROLLUP and CUBE for subtotals
  • Counting distinct values and handling NULLs in aggregates

Sample question:

Given an orders table, find each customer's total spend, number of orders, and average order value. Only include customers with more than 5 orders.

Practice Aggregation and GROUP BY

JOINs

30+ exercises

INNER, LEFT, RIGHT, FULL OUTER, CROSS, and self-joins. JOIN questions are the second most common topic in SQL interviews. The tricky part is not the syntax. It is predicting the output row count and handling NULLs from non-matching rows.

Skills covered:

  • INNER JOIN for matching rows only
  • LEFT JOIN with COALESCE for zero-fill
  • Anti-join pattern (LEFT JOIN + WHERE IS NULL)
  • Self-join for hierarchies and row comparisons
  • CROSS JOIN for scaffolding complete grids
  • Multi-table joins with 3+ tables

Sample question:

Find all customers who signed up in 2024 but never placed an order. Return their name and signup date.

Practice JOINs

Window Functions

20+ exercises

ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD, SUM OVER, AVG OVER, and frame clauses. Window functions separate senior-level candidates from juniors. If you can write a correct window function under time pressure, you are in the top quartile of SQL interviewees.

Skills covered:

  • ROW_NUMBER for deduplication and pagination
  • RANK and DENSE_RANK for ranking within groups
  • LAG and LEAD for comparing consecutive rows
  • Running totals with SUM() OVER(ORDER BY ...)
  • Moving averages with frame clauses (ROWS BETWEEN)
  • PARTITION BY for computing values within subsets

Sample question:

For each department, rank employees by salary (highest first). Return the top 3 earners per department, handling ties.

Practice Window Functions

Subqueries and CTEs

20+ exercises

Scalar subqueries, correlated subqueries, EXISTS, IN, and Common Table Expressions. CTEs make complex queries readable. Correlated subqueries test whether you understand how the inner query references the outer query. Both appear frequently in interviews.

Skills covered:

  • Scalar subqueries in SELECT and WHERE
  • Correlated subqueries that reference the outer table
  • EXISTS vs IN and their performance implications
  • CTEs for breaking multi-step logic into named blocks
  • Recursive CTEs for hierarchical data
  • Replacing nested subqueries with CTEs for clarity

Sample question:

Using a CTE, find all products whose price is above the average price in their category. Return the product name, price, and category average.

Practice Subqueries and CTEs

String and Date Functions

15+ exercises

SUBSTRING, CONCAT, REPLACE, TRIM, UPPER, LOWER, DATE_TRUNC, DATE_DIFF, EXTRACT, and interval arithmetic. These questions are less glamorous than window functions but they appear consistently. Interviewers use them to test attention to detail and edge case handling.

Skills covered:

  • String manipulation: SPLIT_PART, SUBSTRING, REGEXP_REPLACE
  • Date truncation to month, quarter, year
  • Date arithmetic: adding intervals, computing differences
  • EXTRACT for pulling year, month, day, hour components
  • Combining string and date functions in real scenarios

Sample question:

Given a user events table with a timestamp column, compute the number of active users per week for the last 12 weeks. A user is active if they had at least one event that week.

Practice String and Date Functions

CASE WHEN and Conditional Logic

15+ exercises

CASE WHEN is the if/else of SQL. It shows up in almost every non-trivial query: conditional aggregation, pivot-style transformations, bucketing values into categories, and computing derived columns. Mastering CASE WHEN means you can express any business rule in SQL.

Skills covered:

  • Simple CASE WHEN for value mapping
  • Searched CASE WHEN for range-based bucketing
  • CASE WHEN inside aggregate functions
  • Nested CASE WHEN for multi-condition logic
  • NULL handling inside CASE expressions

Sample question:

Classify each order as 'small' (under $50), 'medium' ($50-200), or 'large' (over $200). Then count the number of orders in each category per month.

Practice CASE WHEN and Conditional Logic

NULL Handling

10+ exercises

Three-valued logic, COALESCE, NULLIF, IS NULL, IS NOT NULL, and how NULLs interact with JOINs, aggregations, and comparisons. NULL behavior is one of the most common sources of bugs in production SQL. Interviewers love it because it reveals whether you actually understand the language or just memorize patterns.

Skills covered:

  • Three-valued logic: TRUE, FALSE, UNKNOWN
  • COALESCE for default values
  • NULLIF for conditional NULL creation
  • NULL behavior in COUNT, SUM, AVG
  • NULL propagation through JOINs
  • NOT IN with NULLs (the classic trap)

Sample question:

A LEFT JOIN produces NULLs in the right table columns. Write a query that counts both matched and unmatched rows, replacing NULLs with meaningful defaults.

Practice NULL Handling

Advanced Patterns

15+ exercises

Gap-and-island problems, running totals, sessionization, deduplication, pivot/unpivot, and recursive queries. These are the problems that show up in final rounds at top companies. They combine multiple SQL concepts into a single question.

Skills covered:

  • Gap-and-island detection for consecutive sequences
  • Sessionization: grouping events by time gaps
  • Deduplication with ROW_NUMBER
  • Pivot and unpivot transformations
  • Recursive CTEs for tree traversal
  • MERGE / UPSERT for incremental loads

Sample question:

Given a table of user logins with timestamps, identify 'sessions' where consecutive logins are within 30 minutes of each other. Assign a session_id to each group.

Practice Advanced Patterns

How to Progress Through SQL Exercises

Skipping ahead is how people waste six weeks. We watched a bootcamp grad grind window function problems for a month before realizing she couldn't reliably write a three-table JOIN. Walk the progression in order. Eight weeks. No shortcuts.

Week 1-2: Foundations

Focus: SELECT, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT

You can write basic queries without looking up syntax. Aggregations with GROUP BY feel natural.

Start with Aggregation exercises (beginner level) and basic JOINs.

Week 3-4: Core Patterns

Focus: Multi-table JOINs, subqueries, CTEs, CASE WHEN

You can combine 2-3 tables, use CTEs to break down complex logic, and write conditional aggregations.

Move to intermediate JOIN exercises, CTE practice, and CASE WHEN drills.

Week 5-6: Intermediate Skills

Focus: Window functions, date/string manipulation, NULL edge cases

ROW_NUMBER, RANK, LAG, and LEAD feel comfortable. You spot NULL traps before they bite.

Window function exercises, date function drills, NULL handling challenges.

Week 7-8: Advanced Patterns

Focus: Gap-and-island, sessionization, recursive CTEs, optimization

You can solve multi-step problems under time pressure. Your queries are correct on the first try most of the time.

Advanced pattern exercises. Timed practice: 5 questions in 60 minutes.

How to Practice SQL Effectively

Volume matters, but it is not the only factor. How you practice determines how fast you improve. These five strategies separate people who plateau at 50 exercises from people who are interview-ready at 100.

Write before you look+
Try every problem before reading hints or solutions. The struggle of writing incorrect SQL and debugging it builds real skill. Looking at the answer first teaches you to recognize solutions, not produce them. Recognition does not transfer to interviews.
Time yourself+
Real interviews give you 20-30 minutes per SQL question. Practice under the same constraint. If you can solve a medium-difficulty problem in 15 minutes untimed, it will take you 25 minutes under pressure. Build the time buffer now.
Explain your approach out loud+
Interviewers evaluate your thought process, not just your final query. Practice verbalizing: 'First I need to join these two tables, then aggregate by customer, then filter with HAVING.' This habit prevents blank-screen panic.
Review your mistakes+
Keep a running list of the errors you make: missing GROUP BY columns, forgetting NULL handling, wrong join type. After 20 exercises, you will see patterns. Those patterns are your weak spots. Drill them.
Practice on real data shapes+
Toy datasets with 5 rows do not reveal edge cases. Practice on tables with NULLs, duplicates, missing dates, and messy strings. Real interview data has these properties. If your practice data is clean, you are not preparing for reality.

SQL Practice FAQ

How many SQL practice questions should I do before interviewing?+
Aim for 80-120 exercises across all categories. That is not a random number. After about 80 problems, most people have seen every common pattern at least twice. You want pattern recognition, not memorization. If you can solve a medium-difficulty problem in under 15 minutes without hints, you are ready for most interviews.
Should I practice SQL in a specific dialect?+
PostgreSQL is the safest choice for interview prep. Most interview platforms run PostgreSQL or something close to it. The syntax differences between PostgreSQL, MySQL, and BigQuery are small for interview-level questions. Window function syntax, CTE syntax, and JOIN syntax are identical across all three. The differences (STRING_AGG vs GROUP_CONCAT, date function names) rarely matter in interviews.
What is the most important SQL topic for data engineering interviews?+
Aggregation with GROUP BY. It appears in nearly one in four SQL interview questions. After that: JOINs (especially LEFT JOIN and anti-join patterns), window functions (ROW_NUMBER, RANK, LAG/LEAD), and CTEs. If you can handle these four categories fluently, you can solve the vast majority of interview problems.
How do I go from solving SQL problems slowly to solving them quickly?+
Speed comes from two things: pattern recognition and muscle memory. Pattern recognition means you see a problem and immediately know which SQL constructs to reach for. Muscle memory means the syntax flows without thinking. Both require volume. There is no shortcut. Do 10-15 exercises per week, timed, and review your mistakes after each session. Speed will follow within 3-4 weeks.

Break Queries. Fix Queries. Ship.

The fastest path to interview-ready SQL is writing a wrong query and debugging it. Start now.