Investigating Duplicate Active Subscriptions
This is the first step in investigating subscription problems. Find users who have more than one subscription with status = 'active'.
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 problemProblems found: 460
Reset filtersThis is the first step in investigating subscription problems. Find users who have more than one subscription with status = 'active'.
The warehouse operator takes fresh paid orders into work — the ones that arrived last. Paid orders are more than half the table: the filter narrows almost nothing, while the sort is expensive.
A user can have multiple addresses, but only one can be the primary address.
The customer card shows their last twenty orders. A large customer has fifteen hundred of them, and all of them get sorted for the sake of twenty rows. Remove both the full scan and the sort.
The catalog flags products that appear at least once in order_items to distinguish purchased products from those with no demand.
Every API request checks whether the user has an active subscription. This is the most frequent query in the system. Active subscriptions are a third of the table; the user is one.
The finance report gathers the month's paid orders. Paid orders are more than half the table, so storing all of them in an index is pointless — yet a full scan for the sake of one week is expensive…
The order lines table must prevent you from adding a line for an order that does not exist in the database.
Step 3 of 5 in the payment incident investigation. Support will manually review orders with status = 'paid' that have no payments with status = 'success'.
The payment provider delivers the same event several times. Before doing any work the handler checks what has already arrived under this key.
The report is written with WITH, and the author marked it MATERIALIZED just in case.
The refund screen shows only the successful payments of an order — accounting is not interested in the rest.
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.
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.
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.
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.
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.
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.
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.
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.
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.