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

  • MediumPremium
    PostgreSQL

    Split customers into low / mid / high spenders

    Marketing wants three customer segments by total paid revenue: «low» — under 200, «mid» — 200 up to 700 (700 not included), «high» — 700 and above.

  • MediumPremium
    PostgreSQL

    Run a quarterly tier reshuffle in one query

    Once a quarter billing reshuffles tiers: customers whose total paid revenue is 1000 or above move to «gold», 300–999 to «silver», below 300 stay on «free».

  • MediumPremium
    PostgreSQL

    Calculate Comment Thread Depth

    You're a product analyst at a video streaming service. Discussions under videos form branching threads: someone can reply to a comment, and another person can reply to that reply.

  • MediumPremium
    PostgreSQL

    Show the customer's name next to the total

    On the «high-value customers» dashboard customers currently show up as raw identifiers, and the product manager is complaining: it's unclear who's actually the big spender.

  • MediumPremium
    PostgreSQL

    Annual cost of the CEO's reporting line

    HR wants to know what the CEO's reporting line costs the company — the sum of salaries of every report at any level of the hierarchy. The CEO themselves is not included, only their team.

  • MediumPremium
    PostgreSQL

    Upsert the Time Zone Setting

    The settings table follows this policy: update the value if the key already exists; otherwise, insert a new row. Add the setting with key = 'timezone' and value = 'UTC' using this policy.

  • MediumPremium
    PostgreSQL

    Breadcrumb Paths for Travel Destinations

    You're a data engineer at an online travel agency. Destinations form a tree: continent → country → region → city, but branches can have different depths.

  • MediumPremium
    PostgreSQL

    Compare average ticket size — free vs paid

    Billing wants to compare how customers buy across pricing tiers: one average ticket on «free», another on «gold», a third on «silver» — the figure feeds the upsell forecast.

  • MediumPremium
    PostgreSQL

    Total Direct and Indirect Reports

    HR wants the size of each employee's «empire» right next to them — the total number of reports at any depth, not just the direct ones.

  • MediumPremium
    PostgreSQL

    Delete Expired User Sessions

    A nightly job removes expired sessions from the sessions table. Delete all rows where expires_at is earlier than the current time.

  • MediumPremium
    PostgreSQL

    All Subsidiaries in a Corporate Group

    You're a compliance analyst at a fintech group. A regulator has requested the full consolidation scope: every company directly or indirectly owned by 'Meridian Capital Group'.

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.