Challenge - 5 Problems
JSONB Existence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Check if a JSONB key exists in a column
Given a table products with a JSONB column
attributes, what is the output of this query?SELECT id FROM products WHERE attributes ? 'color';Assuming the table has these rows:
id | attributes
---+----------------------------
1 | {"color": "red", "size": "M"}
2 | {"weight": 10}
3 | {"color": "blue"}
4 | {"size": "L"}Attempts:
2 left
💡 Hint
The ? operator checks if the JSONB column contains the specified key at the top level.
✗ Incorrect
The query filters rows where the JSONB column 'attributes' contains the key 'color'. Rows 1 and 3 have 'color' as a key, so their ids are returned.
🧠 Conceptual
intermediate1:30remaining
Understanding the JSONB existence (?) operator behavior
Which statement correctly describes what the JSONB existence operator
? does in PostgreSQL?Attempts:
2 left
💡 Hint
Think about whether the operator looks inside nested objects or only top-level keys.
✗ Incorrect
The ? operator tests for the presence of a top-level key in a JSONB column. It does not search nested keys or values.
📝 Syntax
advanced1:30remaining
Identify the correct syntax for using the JSONB existence operator
Which of the following SQL queries correctly uses the JSONB existence operator to find rows where the JSONB column
data contains the key active?Attempts:
2 left
💡 Hint
Remember that the key must be a string literal in single quotes.
✗ Incorrect
The ? operator requires the key to be a string literal enclosed in single quotes. Option A is correct syntax.
❓ optimization
advanced2:00remaining
Optimizing queries using the JSONB existence operator
You want to speed up queries that use
WHERE attributes ? 'status' on a large table. Which index type is best to optimize this query?Attempts:
2 left
💡 Hint
Think about which index type supports key existence queries on JSONB.
✗ Incorrect
GIN indexes support fast existence checks on JSONB keys using the ? operator. B-tree and hash indexes do not support this efficiently.
🔧 Debug
expert2:00remaining
Diagnose the error in this JSONB existence operator query
A developer runs this query:
and gets an error. What is the cause?
SELECT * FROM orders WHERE details ? status;
and gets an error. What is the cause?
Attempts:
2 left
💡 Hint
Check how the key is written in the query.
✗ Incorrect
The ? operator requires the key to be a string literal in single quotes. Without quotes, PostgreSQL treats status as a column or variable, causing an error.