PostgreSQL Practice Problems

These PostgreSQL practice problems cover much more than basic SELECT queries. Work through joins, aggregations, window functions, table definitions, JSON data, and query optimization. You can also practice PostgreSQL-specific features where a problem calls for them. Every query runs in a real PostgreSQL database and is automatically checked using hidden data. Problems: 1,003, free: 240.

Problems in this set: 1,003, free: 240

Start with a free problem

PostgreSQL problems

Problems found: 463

Reset filters
  • MediumPremium
    PostgreSQL

    First charge attempt failed

    QUERY, the archive AI mentor, spotted something odd: the first charge attempt can fail while the payment's final status can still be different — for example, succeeded.

  • MediumPremium
    PostgreSQL

    Goods by app version

    Solve it in PostgreSQL (app_events). A Kotomarket analyst wants to compare which items priced above $20 users ordered from different app versions.

  • MediumPremium
    PostgreSQL

    On sale but not new

    The station Commandant is looking for discounted products that are no longer marked as new. Card tags are stored in the attrs → tags array.

  • MediumPremium
    PostgreSQL

    What each source sends

    The quality report needs to be extended with the raw feed. Break down messages from raw_inbox by source and content type so it is clear which sources send valid JSON and which send junk.

  • MediumPremium
    PostgreSQL

    Who has push notifications on

    You need to find the customers who actually turned push notifications on. Channels are stored in settings → notifications, with a key and a boolean value for each channel, such as "push": true.

  • MediumPremium
    PostgreSQL

    Can we send email

    Before sending a campaign, Kotomarket needs to know which customers may actually receive email. The rule depends on whether there is an explicit value, a missing key, or no settings document at all.

  • MediumPremium
    PostgreSQL

    Delete Duplicate Webhooks, Keeping the Lowest ID

    The webhook_events table contains repeated deliveries of the same events. Each order event has its own event_key; for example, evt-pay-10 means a payment for order 10. Redeliveries use the same key.

  • MediumPremium
    PostgreSQL

    Explicit Status List Instead of Negation

    An operations report needs orders whose status is not paid. In this case, those are orders whose status is pending or cancelled. Rewrite the filter to list the required statuses explicitly.

  • MediumPremium
    PostgreSQL

    Searching by the start of a name

    An operator looks for a customer by typing the first letters of the name. The search walks ten thousand users with a full scan.

  • MediumPremium
    PostgreSQL

    Price Comparison Without Extra Multiplication

    The catalog currently compares twice the price with a threshold of 24, making searches less efficient. Rewrite the condition to compare price directly with the equivalent threshold of 12.

  • MediumPremium
    PostgreSQL

    Update a Product or Insert It If Missing

    You need to ensure that the product with id = 1 exists in the catalog and has the correct price. Set the price of the product with id = 1 in the products table to 15.00.

What you will practice

  • Write queries and joins using PostgreSQL syntax
  • Apply FILTER and DISTINCT ON where they fit
  • Work with JSONB and cast values using ::
  • Create and alter database tables with DDL
  • Read execution plans using EXPLAIN ANALYZE

Common mistakes

  1. 1. Using DISTINCT ON without the right sorting

    DISTINCT ON keeps one row for each distinct combination of expressions, but ORDER BY determines which row is selected. Without suitable sorting, you may get an arbitrary record instead of the latest or highest-valued one. Define which row should win within each group.

  2. 2. Assuming every type cast is safe

    PostgreSQL supports convenient casts using ::, but casting cannot turn invalid text into a valid number or date. An unexpected value may cause the query to fail. Check the source data type and consider which input formats are valid before applying a conversion.

  3. 3. Treating JSONB as plain text

    JSONB stores structured JSON data rather than an ordinary text string. When extracting nested values, it matters whether you need a JSON value or its text representation. Check the returned type and consider what should happen when a key or nested field is missing.

  4. 4. Assuming EXPLAIN ANALYZE only describes a query

    EXPLAIN ANALYZE actually executes the statement and collects runtime statistics. This matters especially for statements that modify data. When reading the output, compare estimated and actual row counts as well as execution times instead of focusing only on operation names.

Theory and courses

Frequently asked questions

Do these exercises run on a real PostgreSQL database?

Yes. Your queries execute in PostgreSQL rather than a simulated SQL environment. Their results are automatically compared with expected results using hidden data. This allows you to practice standard SQL concepts alongside PostgreSQL-specific syntax and behavior when the problem requires it.

Which PostgreSQL exercises are free?

Free PostgreSQL problems: 240. After signing in, the free plan lets you solve up to 3 new problems each day. Premium problems appear in the catalog but require a subscription. Problems also include hints and an AI mentor.

Are these PostgreSQL problems only about SELECT?

No. The collection covers SQL topics ranging from basic selections and joins to window functions, DDL, JSON, and query optimization. You can start with straightforward queries and move on to more specialized PostgreSQL features as you become comfortable with the fundamentals.

What are FILTER and DISTINCT ON used for?

FILTER applies a condition to an individual aggregate calculation, making it useful when calculating different metrics for the same groups. DISTINCT ON selects one row per distinct set of expressions. They solve different problems, and DISTINCT ON requires careful ordering to select the intended row.

What can EXPLAIN ANALYZE tell me?

It shows how PostgreSQL executed a statement, including actual row counts and timing information for plan operations. Comparing those figures with estimates can help identify expensive steps. Remember that EXPLAIN ANALYZE executes the statement, so use care with operations that change data.