0
0
PostgreSQLquery~20 mins

Sub-partitioning in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sub-partitioning Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of Query on Sub-partitioned Table

Given a table sales sub-partitioned by region and then by month, what will be the output of this query?

SELECT region, month, SUM(amount) FROM sales GROUP BY region, month ORDER BY region, month;
PostgreSQL
CREATE TABLE sales (
  id SERIAL PRIMARY KEY,
  region TEXT,
  month INT,
  amount NUMERIC
) PARTITION BY LIST (region);

CREATE TABLE sales_east PARTITION OF sales FOR VALUES IN ('East') PARTITION BY RANGE (month);
CREATE TABLE sales_east_jan PARTITION OF sales_east FOR VALUES FROM (1) TO (2);
CREATE TABLE sales_east_feb PARTITION OF sales_east FOR VALUES FROM (2) TO (3);

INSERT INTO sales (region, month, amount) VALUES
('East', 1, 100), ('East', 1, 150), ('East', 2, 200), ('East', 2, 50);
A[{"region": "East", "month": 1, "sum": 250}, {"region": "East", "month": 2, "sum": 300}]
B[{"region": "East", "month": 1, "sum": 250}, {"region": "East", "month": 2, "sum": 250}]
C[{"region": "East", "month": 1, "sum": 250}, {"region": "East", "month": 2, "sum": 200}]
D[{"region": "East", "month": 1, "sum": 100}, {"region": "East", "month": 2, "sum": 250}]
Attempts:
2 left
💡 Hint

Remember that SUM adds all amounts for each group.

📝 Syntax
intermediate
2:00remaining
Identify the Correct Syntax for Sub-partitioning

Which of the following SQL statements correctly creates a table orders partitioned by country and sub-partitioned by order_date range in PostgreSQL?

ACREATE TABLE orders (id INT, country TEXT, order_date DATE) PARTITION BY LIST (country, order_date);
B
CREATE TABLE orders (id INT, country TEXT, order_date DATE) PARTITION BY RANGE (country);
CREATE TABLE orders_us PARTITION OF orders FOR VALUES IN ('US') PARTITION BY LIST (order_date);
C
CREATE TABLE orders (id INT, country TEXT, order_date DATE) PARTITION BY LIST (country);
CREATE TABLE orders_us PARTITION OF orders FOR VALUES IN ('US') PARTITION BY RANGE (order_date);
D
CREATE TABLE orders (id INT, country TEXT, order_date DATE) PARTITION BY RANGE (order_date);
CREATE TABLE orders_us PARTITION OF orders FOR VALUES IN ('US') PARTITION BY LIST (country);
Attempts:
2 left
💡 Hint

Sub-partitioning means partitioning a partitioned table again by another column.

optimization
advanced
2:00remaining
Optimizing Query Performance on Sub-partitioned Tables

You have a large transactions table partitioned by region and sub-partitioned by transaction_date. Which indexing strategy will most improve query performance filtering by region and transaction_date?

ACreate local indexes on <code>transaction_date</code> for each sub-partition.
BCreate a global index on <code>transaction_date</code> only.
CCreate a composite index on (<code>region</code>, <code>transaction_date</code>) on the main table.
DCreate a composite index on (<code>transaction_date</code>, <code>region</code>) on each sub-partition.
Attempts:
2 left
💡 Hint

PostgreSQL does not support global indexes on partitioned tables.

🔧 Debug
advanced
2:00remaining
Debugging Partition Constraint Violation

You created a sub-partitioned table logs partitioned by server and sub-partitioned by log_date. Inserting a row with server = 'A' and log_date = '2024-07-01' causes a constraint violation. What is the most likely cause?

PostgreSQL
CREATE TABLE logs (
  id SERIAL PRIMARY KEY,
  server TEXT,
  log_date DATE,
  message TEXT
) PARTITION BY LIST (server);

CREATE TABLE logs_a PARTITION OF logs FOR VALUES IN ('A') PARTITION BY RANGE (log_date);
CREATE TABLE logs_a_july PARTITION OF logs_a FOR VALUES FROM ('2024-06-01') TO ('2024-06-30');
AThe <code>server</code> value 'A' is not allowed in the main partition.
BThe primary key constraint is violated because <code>id</code> is duplicated.
CThe <code>message</code> column is missing a NOT NULL constraint.
DThe row's <code>log_date</code> value '2024-07-01' does not fall into any sub-partition range.
Attempts:
2 left
💡 Hint

Check the ranges defined for sub-partitions.

🧠 Conceptual
expert
2:00remaining
Understanding Sub-partitioning Benefits

Which of the following best explains the main benefit of using sub-partitioning in a large database table?

AIt allows organizing data into smaller, more manageable pieces for faster query pruning and maintenance.
BIt automatically compresses data to save disk space without user intervention.
CIt merges multiple tables into one to simplify schema design.
DIt eliminates the need for indexes by optimizing full table scans.
Attempts:
2 left
💡 Hint

Think about how partitioning helps with performance and management.