Concept Flow - JSONB containment (@>) operator
Start with two JSONB values
Check if left JSONB contains right JSONB
Return TRUE
End
The operator checks if the left JSONB value contains the right JSONB value and returns true or false accordingly.
SELECT '{"a":1, "b":2}'::jsonb @> '{"a":1}'::jsonb AS contains;
| Step | Left JSONB | Right JSONB | Containment Check | Result |
|---|---|---|---|---|
| 1 | {"a":1, "b":2} | {"a":1} | Does left contain right? | TRUE |
| 2 | {"a":1, "b":2} | {"b":3} | Does left contain right? | FALSE |
| 3 | {"a":1, "b":{"c":3}} | {"b":{"c":3}} | Does left contain right? | TRUE |
| 4 | {"a":1, "b":{"c":3}} | {"b":{"c":4}} | Does left contain right? | FALSE |
| Variable | Start | Check 1 | Check 2 | Check 3 | Check 4 |
|---|---|---|---|---|---|
| Left JSONB | {"a":1, "b":2} | {"a":1, "b":2} | {"a":1, "b":2} | {"a":1, "b":{"c":3}} | {"a":1, "b":{"c":3}} |
| Right JSONB | {"a":1} | {"b":3} | {"b":{"c":3}} | {"b":{"c":4}} | {"b":{"c":4}} |
| Result | TRUE | FALSE | TRUE | FALSE |
JSONB containment (@>) operator: - Syntax: left_jsonb @> right_jsonb - Returns TRUE if left contains right JSONB exactly - Works recursively on nested JSON objects - Useful for checking presence of key-value pairs - Returns FALSE if any key or value mismatches
What does the PostgreSQL @> operator do when used with JSONB data?
@> operator in PostgreSQL is used to check containment between JSONB values.Which of the following is the correct syntax to check if a JSONB column data contains the JSON {"key": "value"}?
SELECT * FROM table WHERE data ___ '{"key": "value"}'::jsonb;@> operator checks if the left JSONB contains the right JSONB.data contains the JSON, use data @> '{"key": "value"}'::jsonb.Given the table items with a JSONB column attributes, what will this query return?
SELECT id FROM items WHERE attributes @> '{"color": "red"}'::jsonb;Assuming items contains:
id | attributes
---+-------------------------
1 | {"color": "red", "size": "M"}
2 | {"color": "blue", "size": "L"}
3 | {"color": "red"}
4 | {"size": "S"}attributes contain the JSON {"color": "red"}.Identify the error in this query that tries to find rows where info JSONB column contains {"active": true}:
SELECT * FROM users WHERE info @> '{active: true}';active should be "active".::jsonb cast, but the main syntax error is the missing quotes around the key.You want to find all rows in products where the details JSONB column contains {"features": {"waterproof": true}} and also the price is less than 100. Which query is correct?
@> operator checks if details contains the given JSON. The JSON must have keys in double quotes and be cast to jsonb.AND price < 100 to filter by price.Pass the test and this pattern moves unknown