Bird
Raised Fist0
PostgreSQLquery~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of range partitioning by date in PostgreSQL?
easy
A. To create random partitions without any order
B. To split data into parts based on date ranges for better management
C. To encrypt date columns for security
D. To combine all data into a single large table

Solution

  1. Step 1: Understand range partitioning concept

    Range partitioning divides data into segments based on continuous ranges, such as dates.
  2. Step 2: Identify the purpose of date-based partitioning

    Using date ranges helps organize data by time periods, improving query speed and management.
  3. Final Answer:

    To split data into parts based on date ranges for better management -> Option B
  4. Quick Check:

    Range partitioning by date = split data by date ranges [OK]
Hint: Range partitioning splits data by continuous date intervals [OK]
Common Mistakes:
  • Thinking partitioning combines data instead of splitting
  • Confusing partitioning with encryption
  • Assuming partitions are random, not range-based
2. Which of the following is the correct syntax to create a range partitioned table by a date column order_date in PostgreSQL?
easy
A. CREATE TABLE orders (id INT, order_date DATE) PARTITION BY RANGE (order_date);
B. CREATE TABLE orders PARTITION BY RANGE (order_date) (id INT, order_date DATE);
C. CREATE TABLE orders (id INT, order_date DATE) PARTITION BY LIST (order_date);
D. CREATE TABLE orders (id INT, order_date DATE) PARTITION BY HASH (order_date);

Solution

  1. Step 1: Check correct partitioning clause placement

    In PostgreSQL, PARTITION BY RANGE (column) comes after table columns definition.
  2. Step 2: Identify correct partition type for date ranges

    Range partitioning is used for continuous ranges like dates, so PARTITION BY RANGE is correct.
  3. Final Answer:

    CREATE TABLE orders (id INT, order_date DATE) PARTITION BY RANGE (order_date); -> Option A
  4. Quick Check:

    Syntax: columns then PARTITION BY RANGE [OK]
Hint: Define columns first, then PARTITION BY RANGE (date_column) [OK]
Common Mistakes:
  • Placing PARTITION BY before columns
  • Using LIST or HASH instead of RANGE for dates
  • Incorrect syntax order causing errors
3. Given the following partitioned table and partitions:
CREATE TABLE sales (id INT, sale_date DATE) PARTITION BY RANGE (sale_date);
CREATE TABLE sales_2023 PARTITION OF sales FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');
CREATE TABLE sales_2024 PARTITION OF sales FOR VALUES FROM ('2024-01-01') TO ('2025-01-01');

What will be the result of this query?
SELECT tableoid::regclass, * FROM sales WHERE sale_date = '2023-06-15';
medium
A. Returns rows from sales_2024 partition with sale_date '2023-06-15'
B. Returns no rows because '2023-06-15' is not in any partition
C. Returns rows from both partitions
D. Returns rows from sales_2023 partition with sale_date '2023-06-15'

Solution

  1. Step 1: Identify which partition contains '2023-06-15'

    The date '2023-06-15' falls between '2023-01-01' and '2024-01-01', so it belongs to sales_2023 partition.
  2. Step 2: Understand query behavior on partitioned tables

    Query on partitioned table routes to matching partition(s) based on WHERE clause; here, only sales_2023 matches.
  3. Final Answer:

    Returns rows from sales_2023 partition with sale_date '2023-06-15' -> Option D
  4. Quick Check:

    Date in sales_2023 range = rows from sales_2023 [OK]
Hint: Check date range to find correct partition for query [OK]
Common Mistakes:
  • Choosing wrong partition based on date
  • Assuming query scans all partitions
  • Ignoring partition boundaries
4. You try to create a partition for a range partitioned table by date with this command:
CREATE TABLE sales_2025 PARTITION OF sales FOR VALUES FROM ('2025-01-01') TO ('2024-12-31');

What is the problem with this statement?
medium
A. The TO date is earlier than the FROM date, causing a range error
B. Partition names cannot contain numbers
C. You must specify LIST partitioning, not RANGE
D. The sales table must be dropped before adding partitions

Solution

  1. Step 1: Check the FROM and TO values in partition definition

    The TO value '2024-12-31' is before the FROM value '2025-01-01', which is invalid for range partitions.
  2. Step 2: Understand partition range rules

    Range partitions require FROM value to be less than TO value to define a valid range.
  3. Final Answer:

    The TO date is earlier than the FROM date, causing a range error -> Option A
  4. Quick Check:

    FROM must be less than TO in range partitions [OK]
Hint: FROM date must be before TO date in range partitions [OK]
Common Mistakes:
  • Swapping FROM and TO dates
  • Thinking partition names cannot have numbers
  • Confusing range with list partitioning
5. You have a large sales table partitioned by month using range partitioning on sale_date. You want to add a new partition for March 2024. Which of the following commands correctly adds this partition?
hard
A. CREATE TABLE sales_2024_03 PARTITION OF sales FOR VALUES FROM ('2024-03-01') TO ('2024-03-31');
B. CREATE TABLE sales_2024_03 PARTITION OF sales FOR VALUES FROM ('2024-02-28') TO ('2024-03-31');
C. CREATE TABLE sales_2024_03 PARTITION OF sales FOR VALUES FROM ('2024-03-01') TO ('2024-04-01');
D. CREATE TABLE sales_2024_03 PARTITION OF sales FOR VALUES FROM ('2024-03-01') TO ('2024-03-30');

Solution

  1. Step 1: Understand range partition boundaries for months

    Range partitions use inclusive FROM and exclusive TO, so March 2024 is from '2024-03-01' up to but not including '2024-04-01'.
  2. Step 2: Check each option's date range correctness

    CREATE TABLE sales_2024_03 PARTITION OF sales FOR VALUES FROM ('2024-03-01') TO ('2024-04-01'); correctly uses FROM '2024-03-01' TO '2024-04-01'. Options B, C, and D have incorrect boundaries that either overlap or exclude days.
  3. Final Answer:

    CREATE TABLE sales_2024_03 PARTITION OF sales FOR VALUES FROM ('2024-03-01') TO ('2024-04-01'); -> Option C
  4. Quick Check:

    Range partitions: FROM inclusive, TO exclusive [OK]
Hint: Use TO date as first day of next month for monthly partitions [OK]
Common Mistakes:
  • Using TO date as last day of month (should be exclusive)
  • Overlapping partition ranges
  • Using incorrect FROM dates