Challenge - 5 Problems
JSONB GIN Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Query output with GIN index on JSONB array
Given a PostgreSQL table
Assume the table has:
- id=1, tags=['organic', 'fresh']
- id=2, tags=['imported']
- id=3, tags=['organic', 'imported']
products with a JSONB column tags storing arrays of tags, and a GIN index on tags, what will be the output of this query?SELECT id FROM products WHERE tags @> '["organic"]';Assume the table has:
- id=1, tags=['organic', 'fresh']
- id=2, tags=['imported']
- id=3, tags=['organic', 'imported']
PostgreSQL
CREATE TABLE products (id SERIAL PRIMARY KEY, tags JSONB); INSERT INTO products (tags) VALUES ('["organic", "fresh"]'), ('["imported"]'), ('["organic", "imported"]'); CREATE INDEX idx_gin_tags ON products USING GIN (tags);
Attempts:
2 left
💡 Hint
The operator
@> checks if the JSONB array contains the specified element.✗ Incorrect
The query filters rows where the JSONB array
tags contains the element "organic". Rows with id 1 and 3 have "organic" in their tags, so they are returned.🧠 Conceptual
intermediate1:30remaining
Purpose of GIN index on JSONB columns
What is the main advantage of using a GIN index on a JSONB column that stores arrays in PostgreSQL?
Attempts:
2 left
💡 Hint
Think about what queries involving array elements need to be fast.
✗ Incorrect
GIN indexes are designed to speed up queries that check if a JSONB array contains certain elements, improving performance for containment operators.
📝 Syntax
advanced2:00remaining
Correct syntax to create a GIN index on a JSONB column
Which of the following SQL commands correctly creates a GIN index on the JSONB column
data in the table events?Attempts:
2 left
💡 Hint
GIN indexes require the USING GIN clause and the column name.
✗ Incorrect
Option C uses the correct syntax to create a GIN index on the JSONB column. Option C is invalid syntax because
jsonb_path_ops is specified incorrectly here.❓ optimization
advanced2:30remaining
Optimizing queries on JSONB arrays with GIN indexes
You have a table
users with a JSONB column preferences storing arrays of strings. You want to optimize queries that check if a user has a specific preference. Which index type and operator combination is best for this?Attempts:
2 left
💡 Hint
Think about which index supports containment queries on JSONB arrays.
✗ Incorrect
GIN indexes support fast containment queries using the
@> operator on JSONB arrays, making option D the best choice.🔧 Debug
expert3:00remaining
Why does this GIN index query return no rows?
You created a GIN index on the JSONB column
But it returns no rows, even though some rows have "urgent" in their arrays. What is the problem?
info in table documents. The column stores arrays of strings. You run this query:SELECT * FROM documents WHERE info @> '"urgent"';But it returns no rows, even though some rows have "urgent" in their arrays. What is the problem?
Attempts:
2 left
💡 Hint
Check the JSONB syntax for arrays in the query.
✗ Incorrect
The
@> operator expects a JSONB value on the right side. To check if an array contains "urgent", the right side must be a JSONB array: '["urgent"]'. Without brackets, it looks for a string, not an array element.