JSONB containment operator @>
Last updated: 2026-11-04
JSONB containment operator @>
@> reports whether the left jsonb contains the right jsonb. Containment is recursive and structural: every key/value pair of an object must be matched on the left, and every element of the right array must be present in the left array. It is the workhorse for filtering jsonb and far more efficient than comparing keys one by one. The reverse relation is <@.
| Kind | operator |
|---|---|
| Example Language | SQL |
📝 Syntax
jsonb @> jsonb -- left contains right
jsonb <@ jsonb -- left is contained by right
Precedence: @> is in the "any other operator" tier, below arithmetic, above comparison, and at the same level as -> and ->>. `doc @> '{"a":1}'` is therefore one boolean expression that can go straight into WHERE or be combined with AND/OR. Write the right-hand side as a jsonb constant so it is not confused with text.
⚙️ Parameters
| left | The left jsonb, which may contain the right side; if NULL the result is NULL. |
|---|---|
| right | The right jsonb, the content being looked for; if NULL the result is NULL. |
Returns:The boolean true, false, or NULL when either operand is NULL.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
SELECT id, doc @> '{"active": true}' AS has_active, doc @> '{"user": {"name": "Ada"}}' AS has_nested FROM t_contains ORDER BY id
id | has_active | has_nested
---+------------+-----------
1 | true | true
2 | false | false
(2 rows)
SELECT id, doc @> '{"user": {"tags": ["db"]}}' AS has_array_item FROM t_contains ORDER BY id
id | has_array_item
---+---------------
1 | true
2 | false
(2 rows)
📌 Key Points
- NULL propagation: the result is NULL when either operand is NULL.
- Object containment requires every key to exist with an equal value; array containment requires every right-hand element to appear on the left.
- Element order in the right array does not matter, and the right array must be a subset of the left one.
- The reverse relation is <@, which is the same test with the operands swapped.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QHow does @> differ from =?
A= checks whether two jsonb values are exactly equal (same keys, values and structure); @> only requires the left side to contain the right, a subset test. `{"a":1,"b":2}` @> `{"a":1}` is true, while = between them is false.