0
0
PostgreSQLquery~20 mins

GIN index for arrays and JSONB 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 output with GIN index on JSONB array
Given a PostgreSQL table 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);
A[1, 3]
B[2]
C[1, 2, 3]
D[]
Attempts:
2 left
💡 Hint
The operator @> checks if the JSONB array contains the specified element.
🧠 Conceptual
intermediate
1: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?
AIt speeds up containment queries like checking if an array contains a specific element.
BIt compresses the JSONB data to save disk space.
CIt automatically validates the JSONB structure for correctness.
DIt converts JSONB data into relational tables for faster joins.
Attempts:
2 left
💡 Hint
Think about what queries involving array elements need to be fast.
📝 Syntax
advanced
2: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?
ACREATE INDEX idx_gin_data ON events USING GIN (data jsonb_path_ops);
BCREATE INDEX idx_gin_data ON events USING HASH (data);
CCREATE INDEX idx_gin_data ON events USING GIN (data);
DCREATE INDEX idx_gin_data ON events USING BTREE (data);
Attempts:
2 left
💡 Hint
GIN indexes require the USING GIN clause and the column name.
optimization
advanced
2: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?
ACreate a GIST index on <code>preferences</code> and use the <code>~</code> operator.
BCreate a BTREE index on <code>preferences</code> and use the <code>=</code> operator.
CCreate a HASH index on <code>preferences</code> and use the <code>LIKE</code> operator.
DCreate a GIN index on <code>preferences</code> and use the <code>@></code> operator in queries.
Attempts:
2 left
💡 Hint
Think about which index supports containment queries on JSONB arrays.
🔧 Debug
expert
3:00remaining
Why does this GIN index query return no rows?
You created a GIN index on the JSONB column 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?
AThe index was created on the wrong column type; it must be on text, not JSONB.
BThe query is missing array brackets; it should be <code>@> '["urgent"]'</code> to match an array containing "urgent".
CGIN indexes do not support the <code>@></code> operator on JSONB columns.
DThe query should use the <code>=</code> operator instead of <code>@></code>.
Attempts:
2 left
💡 Hint
Check the JSONB syntax for arrays in the query.