0
0
PostgreSQLquery~20 mins

Range partitioning by date in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Range Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Querying data from a date range partitioned table

Given a table sales partitioned by date ranges on the sale_date column, what will be the output of this query?

SELECT COUNT(*) FROM sales WHERE sale_date >= '2023-01-01' AND sale_date < '2023-04-01';

Assume the partitions are:

  • sales_2023_q1: 2023-01-01 to 2023-03-31
  • sales_2023_q2: 2023-04-01 to 2023-06-30

The sales_2023_q1 partition contains 1500 rows, and sales_2023_q2 contains 2000 rows.

A0
B3500
C2000
D1500
Attempts:
2 left
💡 Hint

Think about which partitions the query will scan based on the date range.

🧠 Conceptual
intermediate
1:30remaining
Understanding range partition boundaries

Which statement correctly describes how PostgreSQL range partitions handle boundary values for date partitions?

AThe partition includes rows where the partition key is &gt;= the start boundary and &lt;= the end boundary.
BThe partition includes rows where the partition key is &gt; the start boundary and &lt;= the end boundary.
CThe partition includes rows where the partition key is &gt;= the start boundary and &lt; the end boundary.
DThe partition includes rows where the partition key is &lt; the start boundary and &gt; the end boundary.
Attempts:
2 left
💡 Hint

Think about how ranges are usually defined in PostgreSQL partitions.

📝 Syntax
advanced
2:00remaining
Creating a range partitioned table by date

Which of the following SQL statements correctly creates a table events partitioned by range on the event_date column?

ACREATE TABLE events (id SERIAL PRIMARY KEY, event_date DATE NOT NULL) PARTITION BY RANGE (event_date);
BCREATE TABLE events (id SERIAL PRIMARY KEY, event_date DATE NOT NULL) PARTITION BY LIST (event_date);
CCREATE TABLE events (id SERIAL PRIMARY KEY, event_date DATE NOT NULL) PARTITION BY HASH (event_date);
DCREATE TABLE events (id SERIAL PRIMARY KEY, event_date DATE NOT NULL) PARTITION BY RANGE event_date;
Attempts:
2 left
💡 Hint

Check the syntax for partitioning by range in PostgreSQL.

🔧 Debug
advanced
2:30remaining
Diagnosing a partition constraint violation error

You created a range partition orders_2023_q1 for orders table with range from '2023-01-01' to '2023-04-01'. When inserting a row with order_date = '2023-04-01', you get a constraint violation error. Why?

ABecause the partition range is inclusive of the start date but exclusive of the end date, so '2023-04-01' does not belong in this partition.
BBecause the partition range is inclusive of both start and end dates, so '2023-04-01' is duplicated in two partitions.
CBecause the partition was created with a wrong data type for the date column.
DBecause the partition key column is missing from the insert statement.
Attempts:
2 left
💡 Hint

Recall how PostgreSQL defines range partition boundaries.

optimization
expert
3:00remaining
Optimizing queries on range partitioned tables by date

You have a large logs table partitioned by month on log_date. You want to optimize a query that fetches logs for March 2024. Which approach will best improve query performance?

ARemove the partitioning and store all logs in a single table with an index on <code>log_date</code>.
BAdd a WHERE clause filtering <code>log_date &gt;= '2024-03-01' AND log_date &lt; '2024-04-01'</code> to enable partition pruning.
CUse a JOIN with the partition table <code>logs_2024_03</code> directly instead of querying the parent table.
DAdd an index on <code>log_date</code> in the parent table only.
Attempts:
2 left
💡 Hint

Think about how PostgreSQL uses partition pruning with WHERE clauses.