0
0
PostgreSQLquery~20 mins

JSONB existence (?) operator in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSONB Existence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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"}
A[1, 2, 3]
B[1, 3]
C[2, 4]
D[3, 4]
Attempts:
2 left
💡 Hint
The ? operator checks if the JSONB column contains the specified key at the top level.
🧠 Conceptual
intermediate
1:30remaining
Understanding the JSONB existence (?) operator behavior
Which statement correctly describes what the JSONB existence operator ? does in PostgreSQL?
AIt checks if a JSONB column contains a specific key at the top level.
BIt checks if a JSONB column contains a specific value anywhere inside the JSON.
CIt checks if a JSONB column contains a specific key or nested key at any depth.
DIt checks if a JSONB column contains a specific key and value pair.
Attempts:
2 left
💡 Hint
Think about whether the operator looks inside nested objects or only top-level keys.
📝 Syntax
advanced
1: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?
ASELECT * FROM users WHERE data ? 'active';
BSELECT * FROM users WHERE data ? active;
CSELECT * FROM users WHERE data ? "active";
DSELECT * FROM users WHERE data ? :active;
Attempts:
2 left
💡 Hint
Remember that the key must be a string literal in single quotes.
optimization
advanced
2: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?
AA hash index on the JSONB column
BA B-tree index on the JSONB column
CA GIN index on the JSONB column
DA full-text index on the JSONB column
Attempts:
2 left
💡 Hint
Think about which index type supports key existence queries on JSONB.
🔧 Debug
expert
2:00remaining
Diagnose the error in this JSONB existence operator query
A developer runs this query:
SELECT * FROM orders WHERE details ? status;

and gets an error. What is the cause?
AThe query is missing a FROM clause.
BThe JSONB column 'details' does not exist.
CThe ? operator cannot be used in WHERE clauses.
DThe key 'status' is not quoted as a string literal.
Attempts:
2 left
💡 Hint
Check how the key is written in the query.