0
0
PostgreSQLquery~20 mins

Indexing JSONB with GIN in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSONB GIN Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Query performance with GIN index on JSONB

Given a table products with a details column of type JSONB, which query will use the GIN index created as CREATE INDEX idx_gin_details ON products USING GIN (details); to efficiently find products where the color is "red"?

PostgreSQL
CREATE TABLE products (id SERIAL PRIMARY KEY, details JSONB);
CREATE INDEX idx_gin_details ON products USING GIN (details);

-- Which query uses the GIN index efficiently?
ASELECT * FROM products WHERE details @> '{"color": "red"}';
BSELECT * FROM products WHERE details->>'color' = 'red';
CSELECT * FROM products WHERE details ? 'color';
DSELECT * FROM products WHERE details->'color' = 'red';
Attempts:
2 left
💡 Hint

GIN indexes on JSONB support containment operators like @>.

🧠 Conceptual
intermediate
1:30remaining
Understanding GIN index behavior on JSONB

What does a GIN index on a JSONB column index internally to speed up queries?

AIt indexes only the top-level keys of the JSONB document.
BIt indexes only numeric values inside the JSONB document.
CIt indexes all keys and values inside the JSONB document separately.
DIt indexes the entire JSONB document as a single string.
Attempts:
2 left
💡 Hint

Think about how GIN indexes support containment queries.

📝 Syntax
advanced
1:30remaining
Correct syntax for creating a GIN index on JSONB path ops

Which of the following is the correct syntax to create a GIN index on a JSONB column data using the jsonb_path_ops operator class?

ACREATE INDEX idx_data_path ON mytable USING GIN (data jsonb_path_ops);
BCREATE INDEX idx_data_path ON mytable USING GIN (data jsonb_path_ops());
C;)spo_htap_bnosj atad( NIG GNISU elbatym NO htap_atad_xdi XEDNI ETAERC
DREATE INDEX idx_data_path ON mytable USING GIN (data jsonb_path_ops);
Attempts:
2 left
💡 Hint

Operator classes are specified without parentheses.

optimization
advanced
2:00remaining
Choosing the right GIN operator class for JSONB indexing

You want to optimize queries that check if a JSONB column contains specific key-value pairs but do not need to query array elements inside JSON. Which GIN operator class should you choose?

Ajsonb_path_ops
Bjsonb_array_ops
Cjsonb_ops
Djsonb_full_ops
Attempts:
2 left
💡 Hint

One operator class is smaller and faster but supports fewer query types.

🔧 Debug
expert
2:30remaining
Diagnosing why a GIN index is not used for a JSONB query

You created a GIN index on a JSONB column info using CREATE INDEX idx_info ON users USING GIN (info);. However, the query SELECT * FROM users WHERE info->>'status' = 'active'; does not use the index and is slow. Why?

AThe index was created on the wrong column; it should be on <code>info->>'status'</code>.
BThe operator <code>->></code> does not use the GIN index; only containment operators like <code>@></code> do.
CGIN indexes do not support JSONB columns, only B-tree indexes do.
DThe query syntax is invalid and causes the planner to ignore the index.
Attempts:
2 left
💡 Hint

Consider which JSONB operators are supported by GIN indexes.