The JSONB existence operator (?) helps you quickly check if a specific key or string exists inside a JSONB column. It makes searching JSON data easy and fast.
JSONB existence (?) operator in PostgreSQL
jsonb_column ? 'key_or_string'The operator returns true if the key or string exists in the JSONB value.
It works only with JSONB data type, not plain JSON.
SELECT * FROM products WHERE attributes ? 'color';
SELECT * FROM users WHERE preferences ? 'dark_mode';
SELECT * FROM logs WHERE tags ? 'error';
This creates a table with JSONB data, inserts three items, and selects only those where the JSONB data has the key 'color'.
CREATE TABLE items (id SERIAL PRIMARY KEY, data JSONB); INSERT INTO items (data) VALUES ('{"name": "apple", "color": "red"}'), ('{"name": "banana", "taste": "sweet"}'), ('{"name": "carrot", "color": "orange"}'); SELECT id, data FROM items WHERE data ? 'color';
The operator only checks for top-level keys or strings, not nested ones.
Use double quotes inside JSON strings as usual, but single quotes around the key in SQL.
The JSONB existence operator (?) checks if a key or string exists in JSONB data.
It returns true or false and helps filter rows easily.
Works only on JSONB type and checks top-level keys or strings.
Practice
What does the PostgreSQL JSONB existence operator ? check for?
Solution
Step 1: Understand the operator's purpose
The?operator checks for the presence of a key or string in JSONB data.Step 2: Identify the scope of the check
It only checks the top-level keys or strings, not nested elements.Final Answer:
If a specified key or string exists at the top level in a JSONB column -> Option CQuick Check:
JSONB existence operator checks top-level keys [OK]
- Thinking it checks nested keys
- Confusing it with null or empty checks
- Assuming it works on JSON type (not JSONB)
- Believing it checks values instead of keys
Which of the following is the correct syntax to check if the key 'name' exists in a JSONB column data?
SELECT * FROM users WHERE data ? 'name';
Solution
Step 1: Check operator usage
The?operator requires the key as a string literal in single quotes.Step 2: Validate syntax correctness
SELECT * FROM users WHERE data ? 'name'; uses single quotes correctly; others either miss quotes or use invalid syntax.Final Answer:
SELECT * FROM users WHERE data ? 'name'; -> Option BQuick Check:
Key must be a single-quoted string [OK]
- Omitting quotes around the key
- Using double quotes instead of single quotes
- Using parameter syntax (:name) incorrectly
- Confusing key with column name without quotes
Given the table products with a JSONB column attributes containing {"color": "red", "size": "M"}, what will this query return?
SELECT id FROM products WHERE attributes ? 'color';
Solution
Step 1: Understand the query condition
The conditionattributes ? 'color'checks if the key 'color' exists in the JSONB column.Step 2: Determine the query result
It returns all rows where 'color' is a key, regardless of its value.Final Answer:
All product IDs where the 'color' key exists in attributes -> Option DQuick Check:
Operator '?' checks key existence, not value [OK]
- Thinking it filters by key value
- Assuming it returns rows without the key
- Confusing '?' with containment operator
- Expecting syntax error incorrectly
Identify the error in this query that checks if the key "status" exists in the JSONB column info:
SELECT * FROM orders WHERE info ? status;
Solution
Step 1: Check key syntax in operator
The key must be a string literal in single quotes when using the '?' operator.Step 2: Identify the error in the query
The query usesstatuswithout quotes, causing a syntax error or wrong interpretation.Final Answer:
Missing single quotes around the key 'status' -> Option AQuick Check:
Keys must be single-quoted strings [OK]
- Using unquoted keys
- Using double quotes instead of single quotes
- Thinking '?' works on non-JSONB columns
- Assuming column name is the problem
You want to find all rows in the events table where the JSONB column details contains either the key 'location' or the key 'venue'. Which query correctly uses the JSONB existence operator to achieve this?
Solution
Step 1: Understand the requirement
We want rows where either 'location' OR 'venue' keys exist in the JSONB column.Step 2: Choose the correct operator
The?|operator checks if any key in the array exists in the JSONB data.Step 3: Compare options
SELECT * FROM events WHERE details ?| array['location', 'venue']; uses?| array['location', 'venue'], which matches the requirement.Final Answer:
SELECT * FROM events WHERE details ?| array['location', 'venue']; -> Option AQuick Check:
Use '?|' for any key in array [OK]
- Using OR with multiple '?' instead of '?|'
- Using '?&' which requires all keys
- Using AND instead of OR logic
- Confusing single '?' with multiple keys
Ready to prove it?
Pass the test and this pattern moves unknown
