Concept Flow - JSONB existence (?) operator
Start with JSONB column
Apply ? operator with key
Check if key exists in JSONB
Return TRUE
Return FALSE
The ? operator checks if a specified key exists in a JSONB column and returns true or false.
SELECT data ? 'name' AS has_name FROM users;
| Row | data (JSONB) | Key Checked | Condition (key exists?) | Result (TRUE/FALSE) |
|---|---|---|---|---|
| 1 | {"name": "Alice", "age": 30} | name | Exists | TRUE |
| 2 | {"age": 25, "city": "NY"} | name | Does not exist | FALSE |
| 3 | {"name": "Bob", "city": "LA"} | name | Exists | TRUE |
| 4 | {"city": "Chicago"} | name | Does not exist | FALSE |
| Variable | Start | Row 1 | Row 2 | Row 3 | Row 4 |
|---|---|---|---|---|---|
| data | N/A | {"name": "Alice", "age": 30} | {"age": 25, "city": "NY"} | {"name": "Bob", "city": "LA"} | {"city": "Chicago"} |
| key | N/A | name | name | name | name |
| result | N/A | TRUE | FALSE | TRUE | FALSE |
JSONB existence (?) operator: - Syntax: jsonb_column ? 'key' - Returns TRUE if 'key' exists at top-level in JSONB - Returns FALSE if 'key' is missing - Only checks top-level keys, not nested - Useful for quick key presence checks in JSONB columns
What does the PostgreSQL JSONB existence operator ? check for?
? operator checks for the presence of a key or string in JSONB data.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';
? operator requires the key as a string literal in single 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';
attributes ? 'color' checks if the key 'color' exists in the JSONB column.Identify the error in this query that checks if the key "status" exists in the JSONB column info:
SELECT * FROM orders WHERE info ? status;
status without quotes, causing a syntax error or wrong interpretation.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?
?| operator checks if any key in the array exists in the JSONB data.?| array['location', 'venue'], which matches the requirement.Pass the test and this pattern moves unknown