PostgreSQL Practice Online with Queries and Exercises
PostgreSQL practice is most effective when your queries run on PostgreSQL rather than a generic SQL environment. SQL Arena provides a dedicated browser sandbox for PostgreSQL queries, exercises, and dialect-specific syntax.
Updated · SQL Arena team
What you will practice
- Write SELECT, JOIN, GROUP BY, and window queries in PostgreSQL 16
- Use FILTER for conditional aggregates
- Control row selection with DISTINCT ON and ORDER BY
- Work with ::cast, dates, intervals, and PostgreSQL syntax
- Test solutions against task-specific tables and hidden data
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.
You have a table of fitness clubs and a table of class sessions. For every club that has at least one session, return the club name as club_name, the number of premium sessions as premium_sessions, and the number of regular sessions as regular_sessions. Sort the result by club id.
SELECT c.name AS club_name, COUNT(*) FILTER (WHERE s.is_premium) AS premium_sessions, COUNT(*) FILTER (WHERE NOT s.is_premium) AS regular_sessions FROM fitness_clubs c JOIN class_sessions s ON s.club_id = c.id GROUP BY c.id, c.name ORDER BY c.id;
| # | club_name | premium_sessions | regular_sessions |
|---|---|---|---|
| 1 | North Studio | 2 | 1 |
| 2 | River Gym | 1 | 1 |
| 3 | Atlas Club | 2 | 1 |
| 4 | Pine Fitness | 0 | 1 |
You have a table of fitness clubs and a table of class sessions. For every club that has at least one session, return the club name as club_name, the number of premium sessions as premium_sessions, and the number of regular sessions as regular_sessions. Sort the result by club id.
SELECT c.name AS club_name, COUNT(*) FILTER (WHERE s.is_premium) AS premium_sessions, COUNT(*) FILTER (WHERE NOT s.is_premium) AS regular_sessions FROM fitness_clubs c JOIN class_sessions s ON s.club_id = c.id GROUP BY c.id, c.name ORDER BY c.id;
PostgreSQL Practice Example with FILTER and GROUP BY
The first step is joining fitness_clubs to class_sessions through club_id. An inner JOIN fits the requirement because the result should contain clubs with sessions. Lake Center has no matching rows in class_sessions, so it is intentionally excluded.
Next, group the joined rows by club. PostgreSQL lets us calculate separate conditional counts with FILTER. COUNT(*) FILTER (WHERE s.is_premium) counts only premium sessions, while a second aggregate with WHERE NOT s.is_premium counts the regular ones.
FILTER is particularly convenient when one group needs several metrics. Instead of filtering the entire query, each aggregate gets its own condition. The same pattern works for paid versus unpaid orders, completed versus cancelled jobs, or several categories of events.
The query groups by both c.id and c.name and ends with ORDER BY c.id. That last step is important in an exercise because SQL does not promise an output order unless you request one. Ordering by the identifier gives the result a deterministic sequence.
For the sample data, North Studio has two premium sessions and one regular session. River Gym has one of each, Atlas Club has two premium and one regular, and Pine Fitness has one regular session. The example is small, but the technique scales naturally to larger grouped reports.
PostgreSQL supports standard SQL alongside database-specific features that are worth practicing directly. FILTER can apply a condition to an individual aggregate, allowing several conditional counts or sums within one grouped query. PostgreSQL also provides DISTINCT ON, the :: cast syntax, arrays, JSONB, and many other dialect-specific features. Practicing against the actual PostgreSQL engine helps you learn which syntax is portable SQL and which behavior belongs specifically to PostgreSQL.
SELECT c.name AS club_name, COUNT(*) FILTER (WHERE s.is_premium) AS premium_sessions, COUNT(*) FILTER (WHERE NOT s.is_premium) AS regular_sessions FROM fitness_clubs c JOIN class_sessions s ON s.club_id = c.id GROUP BY c.id, c.name ORDER BY c.id;
| club_name | premium_sessions | regular_sessions |
|---|---|---|
| North Studio | 2 | 1 |
| River Gym | 1 | 1 |
| Atlas Club | 2 | 1 |
| Pine Fitness | 0 | 1 |
Common PostgreSQL Query Mistakes
Filtering rows before the aggregation
Putting WHERE s.is_premium before GROUP BY removes every regular session from the input. That makes it impossible for another aggregate to count those rows. FILTER narrows one aggregate without changing the rows available to the others.
Treating DISTINCT ON like ordinary DISTINCT
DISTINCT removes duplicate selected rows. PostgreSQL DISTINCT ON chooses one row for each specified key, and ORDER BY usually decides which row survives. This matters in exercises that ask for the latest event, highest price, or newest record per entity.
Assuming SQL dialects are interchangeable
Core SQL travels well, but date functions, casts, row-selection techniques, and helper functions vary. PostgreSQL exercises are a good place to practice ::cast, INTERVAL, generate_series, and other syntax in the engine where it actually runs.
Practice problems on this topic
SQL from scratch courseFrom this example, you can move into PostgreSQL exercises with different schemas instead of repeating the same demo database. Beginner tasks include selecting rock artists from a festival database, counting festivals held in 2024, and filtering Berlin fitness clubs. Medium tasks add richer aggregation, such as finding a trainer with the most sessions or calculating average values by group. SQL Arena contains 1,192 exercises, with separate sandboxes for PostgreSQL, MySQL, ClickHouse, and BigQuery. Solutions are checked on hidden data as well as the visible example, so hard-coded output or a query that only happens to fit the sample is less likely to pass. The free tier includes 292 exercises across all four dialects and does not require a card. If you want a structured path rather than isolated PostgreSQL practice problems, the SQL from Scratch course uses PostgreSQL and combines explanation with an exercise in every lesson. ER diagrams and EXPLAIN are available when you need to inspect the schema or understand a query plan, while the AI mentor can provide hints and explain errors.
- EasyfreeRock Artistspostgresqlmysqlclickhouse
- EasyfreeFestivals in 2024postgresqlmysqlclickhouse
- EasyfreeBerlin Clubspostgresqlmysqlclickhouse
- EasyfreePremium Subscriptionspostgresqlmysqlclickhouse
- MediumPremiumTop Platinum Sponsorspostgresqlmysqlclickhouse
- MediumPremiumTrainer with Most Sessionspostgresqlmysqlclickhouse
- MediumPremiumMost Attended Classpostgresqlmysqlclickhouse
- MediumPremiumAverage Room Price per Hotelpostgresqlmysqlclickhouse
Reference on this topic
In-depth articles with syntax and examples in the SQL Arena reference.
Frequently asked questions
Where can I practice PostgreSQL?
Use an environment that actually runs PostgreSQL so you can practice both standard SQL and PostgreSQL-specific behavior. SQL Arena provides a dedicated PostgreSQL sandbox in the browser.
How can I practice PostgreSQL online?
Write queries against prepared tables and check the results directly in a browser-based PostgreSQL environment. Practice should progress from SELECT and JOIN to grouping, window functions, subqueries, and PostgreSQL-specific syntax.
What should I practice in PostgreSQL?
Start with filtering, sorting, joins, grouping, and subqueries. Then add window functions, date operations, FILTER, casts, JSONB, and other PostgreSQL-specific features.
Is PostgreSQL practice different from general SQL practice?
Yes. Much of the core language is shared, but PostgreSQL also has its own functions, operators, data types, and syntax, so practicing on the actual PostgreSQL engine matters.
Practice PostgreSQL Queries Online
Open the PostgreSQL sandbox and start with free Easy exercises. No local setup or card is required.
Practice PostgreSQLMore practice from CodeRang
- Python ArenaPython Arena has 1300+ Python 3.12 problems covering functions, classes, async code and technical interview tasks.1300+ tasks · Python 3.12
- Playwright ArenaPlaywright Arena has 30 JavaScript and E2E problems where you write a test and immediately see what it did in the browser.30 tasks · JavaScript / E2E