Bird
Raised Fist0
PostgreSQLquery~20 mins

List partitioning by category 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
🎖️
List Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of inserting data into a category partitioned table

Given a PostgreSQL table products partitioned by LIST on the category column, what will be the output of the following query?

INSERT INTO products (id, name, category) VALUES (1, 'Apple', 'Fruit') RETURNING *;

Assume partitions exist for categories 'Fruit' and 'Vegetable'.

PostgreSQL
CREATE TABLE products (id INT, name TEXT, category TEXT) PARTITION BY LIST (category);
CREATE TABLE products_fruit PARTITION OF products FOR VALUES IN ('Fruit');
CREATE TABLE products_vegetable PARTITION OF products FOR VALUES IN ('Vegetable');
A1 | Apple | NULL
BError: no partition for category 'Fruit'
C1 | Apple | Fruit
D1 | Apple | Vegetable
Attempts:
2 left
💡 Hint

Check if the partition for the category exists before inserting.

🧠 Conceptual
intermediate
1:30remaining
Understanding list partitioning behavior

Which statement best describes how PostgreSQL routes data when using LIST partitioning by category?

AData is routed randomly to any partition regardless of the key value.
BData is routed to partitions based on matching the partition key value exactly in the partition's value list.
CData is routed to partitions based on range intervals of the key value.
DData is routed to the default partition only.
Attempts:
2 left
💡 Hint

Think about how LIST partitioning differs from RANGE partitioning.

📝 Syntax
advanced
2:30remaining
Correct syntax for creating a list partitioned table

Which of the following SQL statements correctly creates a table orders partitioned by LIST on the order_type column with partitions for 'Online' and 'InStore'?

A
CREATE TABLE orders (id INT, order_type TEXT) PARTITION BY LIST (order_type);
CREATE TABLE orders_online PARTITION OF orders FOR VALUES IN ('Online');
CREATE TABLE orders_instore PARTITION OF orders FOR VALUES IN ('InStore');
B
CREATE TABLE orders (id INT, order_type TEXT) PARTITION BY RANGE (order_type);
CREATE TABLE orders_online PARTITION OF orders FOR VALUES IN ('Online');
CREATE TABLE orders_instore PARTITION OF orders FOR VALUES IN ('InStore');
C
CREATE TABLE orders (id INT, order_type TEXT) PARTITION BY LIST (order_type);
CREATE TABLE orders_online PARTITION OF orders FOR VALUES FROM ('Online') TO ('Online');
CREATE TABLE orders_instore PARTITION OF orders FOR VALUES FROM ('InStore') TO ('InStore');
D
CREATE TABLE orders (id INT, order_type TEXT) PARTITION BY LIST (order_type);
CREATE TABLE orders_online PARTITION OF orders FOR VALUES IN ('Online', 'InStore');
Attempts:
2 left
💡 Hint

Check the partitioning method and the syntax for partition value lists.

optimization
advanced
2:00remaining
Optimizing queries on list partitioned tables

You have a large sales table partitioned by LIST on region. Which query will most efficiently use partition pruning to scan only the 'North' region partition?

ASELECT * FROM sales WHERE region <> 'North';
BSELECT * FROM sales WHERE region LIKE '%North%';
CSELECT * FROM sales WHERE region IN ('North', 'South');
DSELECT * FROM sales WHERE region = 'North';
Attempts:
2 left
💡 Hint

Partition pruning works best with exact matches on the partition key.

🔧 Debug
expert
3:00remaining
Diagnosing insert failure on list partitioned table

Given a table employees partitioned by LIST on department with partitions for 'HR' and 'IT', the following insert fails:

INSERT INTO employees (id, name, department) VALUES (10, 'John Doe', 'Finance');

What is the most likely cause of this error?

ANo partition exists for the 'Finance' department, so the insert fails.
BThe 'Finance' value is misspelled in the insert statement.
CThe table is partitioned by RANGE, not LIST, causing the error.
DThe 'department' column is missing from the table definition.
Attempts:
2 left
💡 Hint

Check if a partition exists for the inserted category value.

Practice

(1/5)
1. What is the main purpose of list partitioning by category in PostgreSQL?
easy
A. To split a table into parts based on specific category values
B. To combine multiple tables into one large table
C. To encrypt data in the table for security
D. To create temporary tables for faster queries

Solution

  1. Step 1: Understand list partitioning concept

    List partitioning divides a table into smaller parts based on specific category values.
  2. Step 2: Identify the main purpose

    This helps organize data and speeds up queries by focusing on relevant partitions.
  3. Final Answer:

    To split a table into parts based on specific category values -> Option A
  4. Quick Check:

    List partitioning = split by category [OK]
Hint: List partitioning splits tables by category values [OK]
Common Mistakes:
  • Confusing list partitioning with table joins
  • Thinking it encrypts data
  • Assuming it merges tables
2. Which of the following is the correct syntax to create a list partitioned table by category in PostgreSQL?
easy
A. CREATE TABLE sales PARTITION BY GROUP (category);
B. CREATE TABLE sales PARTITION BY RANGE (category);
C. CREATE TABLE sales PARTITION BY HASH (category);
D. CREATE TABLE sales PARTITION BY LIST (category);

Solution

  1. Step 1: Identify partition type syntax

    List partitioning uses PARTITION BY LIST in PostgreSQL.
  2. Step 2: Match correct syntax

    Only CREATE TABLE sales PARTITION BY LIST (category); uses PARTITION BY LIST with the column name correctly.
  3. Final Answer:

    CREATE TABLE sales PARTITION BY LIST (category); -> Option D
  4. Quick Check:

    List partitioning syntax = PARTITION BY LIST [OK]
Hint: Use PARTITION BY LIST for list partitioning [OK]
Common Mistakes:
  • Using PARTITION BY RANGE instead of LIST
  • Using PARTITION BY HASH incorrectly
  • Using non-existent PARTITION BY GROUP
3. Given the following setup:
CREATE TABLE products (
  id SERIAL PRIMARY KEY,
  name TEXT,
  category TEXT
) PARTITION BY LIST (category);

CREATE TABLE products_electronics PARTITION OF products FOR VALUES IN ('electronics');
CREATE TABLE products_clothing PARTITION OF products FOR VALUES IN ('clothing');

INSERT INTO products (name, category) VALUES ('Phone', 'electronics'), ('Shirt', 'clothing');

SELECT * FROM products WHERE category = 'electronics';

What will the SELECT query return?
medium
A. No rows because category is filtered incorrectly
B. All rows from both partitions
C. Only rows where category is 'electronics', here the 'Phone' row
D. An error because partitions are not queried directly

Solution

  1. Step 1: Understand partition filtering

    Query filters category = 'electronics', so only that partition is scanned.
  2. Step 2: Check inserted data

    Only 'Phone' has category 'electronics', so only that row is returned.
  3. Final Answer:

    Only rows where category is 'electronics', here the 'Phone' row -> Option C
  4. Quick Check:

    Partition filter returns matching rows [OK]
Hint: Query filters partition by category value [OK]
Common Mistakes:
  • Expecting all rows without filter
  • Thinking query causes error
  • Assuming partitions must be queried separately
4. Consider this incorrect partition creation:
CREATE TABLE orders (
  id SERIAL PRIMARY KEY,
  product TEXT,
  category TEXT
) PARTITION BY LIST (category);

CREATE TABLE orders_electronics PARTITION OF orders FOR VALUES IN ('electronics', 'gadgets');

What is the error in this partition definition?
medium
A. FOR VALUES IN must list values as separate strings, not combined
B. Partitions cannot have multiple values in FOR VALUES IN clause
C. The partition table name is invalid
D. The parent table cannot be partitioned by LIST

Solution

  1. Step 1: Check FOR VALUES IN syntax

    FOR VALUES IN expects a list of values as separate strings, e.g. ('electronics', 'gadgets').
  2. Step 2: Identify error in given code

    The code uses multiple values in one partition which is not allowed in PostgreSQL list partitioning.
  3. Final Answer:

    Partitions cannot have multiple values in FOR VALUES IN clause -> Option B
  4. Quick Check:

    Each partition must have exactly one value [OK]
Hint: Each partition must have a single value in FOR VALUES IN clause [OK]
Common Mistakes:
  • Using one string with commas inside
  • Misnaming partition tables
  • Thinking LIST partitioning is not allowed
5. You want to create a list partitioned table events by event_type with partitions for 'login', 'logout', and 'purchase'. Which of the following is the correct way to create the partitions and insert a new 'purchase' event?
hard
A. CREATE TABLE events PARTITION BY LIST (event_type); CREATE TABLE events_login PARTITION OF events FOR VALUES IN ('login'); CREATE TABLE events_logout PARTITION OF events FOR VALUES IN ('logout'); CREATE TABLE events_purchase PARTITION OF events FOR VALUES IN ('purchase'); INSERT INTO events (event_type) VALUES ('purchase');
B. CREATE TABLE events PARTITION BY RANGE (event_type); CREATE TABLE events_login PARTITION OF events FOR VALUES FROM ('login') TO ('logout'); INSERT INTO events (event_type) VALUES ('purchase');
C. CREATE TABLE events PARTITION BY LIST (event_type); CREATE TABLE events_all PARTITION OF events FOR VALUES IN ('login', 'logout', 'purchase'); INSERT INTO events (event_type) VALUES ('purchase');
D. CREATE TABLE events PARTITION BY HASH (event_type); CREATE TABLE events_hash PARTITION OF events FOR VALUES IN (1); INSERT INTO events (event_type) VALUES ('purchase');

Solution

  1. Step 1: Choose correct partition type and syntax

    List partitioning by event_type requires PARTITION BY LIST and partitions with FOR VALUES IN for each category.
  2. Step 2: Verify partitions and insert

    CREATE TABLE events PARTITION BY LIST (event_type); CREATE TABLE events_login PARTITION OF events FOR VALUES IN ('login'); CREATE TABLE events_logout PARTITION OF events FOR VALUES IN ('logout'); CREATE TABLE events_purchase PARTITION OF events FOR VALUES IN ('purchase'); INSERT INTO events (event_type) VALUES ('purchase'); correctly creates partitions for 'login', 'logout', and 'purchase' and inserts a 'purchase' event into the parent table.
  3. Final Answer:

    Option A with correct list partitions and insert -> Option A
  4. Quick Check:

    List partitions per category + insert into parent [OK]
Hint: Create partitions per category, insert into parent table [OK]
Common Mistakes:
  • Using RANGE or HASH instead of LIST
  • Creating one partition for all values
  • Inserting into partitions directly instead of parent