Sub topic 4
Filtering with `@>`, `?`, and Related Operators
Assume you know basic SELECT, WHERE, and JOIN syntax, and that the orders table from the previous chapter exists with a jsonb column named details. The examples use documents shaped like this:
{
"status": "paid",
"total": 42.5,
"items": [{"sku": "A1", "qty": 2}],
"shipping": {"city": "Oslo"}
}Containment and existence operators let you filter JSONB documents without extracting every value into a separate SQL expression.
Containment with `@>`
The @> operator asks whether the JSONB value on the left contains the JSONB value on the right. For objects, every key and value in the right-hand document must appear in the left-hand document. Extra keys on the left are allowed.
SELECT id, customer
FROM orders
WHERE details @> '{"status": "paid"}'::jsonb;This finds orders whose details object has "status": "paid". The cast makes the right-hand value explicitly jsonb. PostgreSQL can often infer the type from context, but an explicit cast makes the query easier to read.
Containment can check nested objects and arrays:
SELECT id, customer
FROM orders
WHERE details @> '{"shipping": {"city": "Oslo"}}'::jsonb;
SELECT id, customer
FROM orders
WHERE details @> '{"items": [{"sku": "A1", "qty": 2}]}'::jsonb;The array condition means that an array element matching the supplied object exists. It does not require the array to contain only that element.
The reverse operator, <@, asks whether the left document is contained in the right document:
SELECT id
FROM orders
WHERE '{"status": "paid"}'::jsonb <@ details;These operators compare JSONB structure and values. They do not treat the JSON text as an ordinary string.
Checking for keys
The ? operator checks whether a top-level key or array string exists:
SELECT id, customer
FROM orders
WHERE details ? 'shipping';This checks only the top level. It does not find city, because city is nested inside shipping.
For nested existence, first navigate to the nested object:
SELECT id, customer
FROM orders
WHERE details -> 'shipping' ? 'city';The related operators check several keys:
| Operator | Meaning | Example | Column | Column |
|---|---|---|---|---|
| `?` | At least one named key exists | `details ? 'status'` | ||
| `? | ` | At least one key from an array exists | `details ? | array['status', 'state']` |
| `?&` | Every key from an array exists | `details ?& array['status', 'total']` |
SELECT id
FROM orders
WHERE details ?| array['status', 'state'];
SELECT id
FROM orders
WHERE details ?& array['status', 'total'];These operators test key names, not whether a key has a particular value. To test a value, use containment or extract and compare:
SELECT id
FROM orders
WHERE details @> '{"status": "paid"}'::jsonb;Combining conditions
JSONB predicates work with normal SQL logic:
SELECT id, customer
FROM orders
WHERE details @> '{"status": "paid"}'::jsonb
AND details ? 'items'
AND (details ->> 'total')::numeric >= 25;This query requires a paid order, an items key, and a total of at least 25. Use parentheses when combining AND and OR, because SQL operator precedence can otherwise produce a different condition than intended.
How the operators guide a query
Choose containment when the required JSON structure matters. Choose existence operators when only the presence of keys matters. Later, a GIN index can support many containment and existence queries.
Exercise
Write a query that returns orders containing a top-level status key and whose status is pending.
Show answer
SELECT id FROM orders WHERE details ? 'status' AND details @> '{"status": "pending"}'::jsonb;