CodeRang
Where to start

SQL Exercises and Practice Problems

SQL exercises turn syntax knowledge into the ability to solve a problem without being told which clause to use. In SQL Arena, queries run in the browser and are checked automatically against hidden data.

Updated · SQL Arena team

What you will practice

  • Filtering and expressions
  • Joining related tables
  • Aggregation
  • Window functions
  • Subqueries and multi-step logic

A practice problem in the trainer

This is how a problem looks in SQL Arena: the task, the editor, the result and the database schema.

PrevNextRandom
TaskSubmissionsAI Mentor
Count attempts and successful solutions

Use the challenges and attempts tables. For each challenge, count all attempts as attempts and successful attempts as passed. Keep challenges with at least two attempts. Return title, attempts, and passed, sorted by attempt count descending and then title.

Query 1PostgreSQLv16
SELECT c.title,
       COUNT(a.id) AS attempts,
       COUNT(*) FILTER (WHERE a.passed) AS passed
FROM challenges c
JOIN attempts a ON a.challenge_id = c.id
GROUP BY c.id, c.title
HAVING COUNT(a.id) >= 2
ORDER BY attempts DESC, c.title;
Submit
Resultchallengesattempts
Answer accepted

Step-by-step solution

attempts.challenge_id points to challenges.id, so the JOIN creates one row for every recorded attempt together with its challenge information. If a challenge has three attempts, it appears three times before aggregation.

COUNT(a.id) counts all attempt rows. PostgreSQL's FILTER syntax lets the second aggregate count only rows where a.passed is true. GROUP BY then creates one result row per challenge, and HAVING removes groups with fewer than two attempts.

The final ORDER BY uses two keys. attempt count is sorted descending, and the title resolves ties. An explicit ordering matters in SQL practice because without ORDER BY the database does not guarantee that repeated executions will return rows in the same sequence.

SQLPostgreSQL v16
SELECT c.title,
       COUNT(a.id) AS attempts,
       COUNT(*) FILTER (WHERE a.passed) AS passed
FROM challenges c
JOIN attempts a ON a.challenge_id = c.id
GROUP BY c.id, c.title
HAVING COUNT(a.id) >= 2
ORDER BY attempts DESC, c.title;
Query result
titleattemptspassed
Active Accounts32
Daily Revenue32
Customer Rank21

Common SQL exercise mistakes

01

Overfitting to visible data

An incorrect query can accidentally match a small sample. Hidden-data checks expose assumptions about duplicates, extra rows, missing values, and other cases that were not visible while you wrote the solution.

02

Reading the answer too early

SQL practice problems are most useful when you first choose an approach yourself. If you are stuck, a targeted hint or explanation preserves more of the learning process than copying the complete solution.

03

Judging difficulty by query length

A difficult SQL problem can have a short final query. The hard part may be choosing the correct grain, understanding the relationship between tables, or handling an edge case.

Practice problems on this topic

All practice problems

SQL Arena contains 1,192 auto-checked SQL exercises, each with its own tables. Solutions are evaluated on hidden data rather than by comparing your query text with a template. You can practice PostgreSQL, MySQL, ClickHouse, and BigQuery in separate browser sandboxes. The free tier includes 292 problems, mostly Easy, across all four dialects. The available exercise set covers basic SQL exercises as well as Medium, Hard, and Expert problems. Examples include event distribution by hour of day in ClickHouse, row counts by status code, average latency per service, minimum and maximum latency, paid-event filtering, platform geography, user activity timelines, and session endings. When you need help, the AI mentor can provide hints and explain errors; 10 uses are available free in total. Interview mode removes hints, uses a server-side timer, and produces a report after the session, with one free run per day.

Frequently asked questions

Where can I practice SQL exercises online?

Use an environment where you can run real queries and receive automatic feedback. SQL Arena provides browser-based PostgreSQL, MySQL, ClickHouse, and BigQuery sandboxes with task-specific tables.

What SQL practice problems should beginners solve?

Start with SELECT, WHERE, sorting, expressions, and simple aggregation. Once those feel routine, add joins and problems that require several operations in the same query.

How are SQL exercises checked?

The query result is compared with the expected result on hidden data. This checks whether your logic generalizes beyond the rows you could see while solving the exercise.

Are MySQL practice questions available?

Yes. SQL Arena has a separate MySQL sandbox, along with PostgreSQL, ClickHouse, and BigQuery. The exercise catalog can therefore be used to practice different SQL dialects in the browser.

Solve a SQL problem yourself

Open a task, inspect its tables, write the query, and let the checker test your logic on hidden data. You can begin with the free exercise set without adding a card.

Start SQL Practice