// strings reference

SQL @> JSON containment operator

payload @> '{"tags": ["pro"]}' is true when the right-hand document fits inside the left one. Containment, not equality, and GIN-indexable.

Kategorie
Strings
Returns
boolean
In this category
151 of 172

Syntax

jsonb @> jsonb
jsonb <@ jsonb

Each block is a separate form. The support table and the examples show which engine takes which.

Engine-Unterstützung

EngineSupportNotizen
SQL Server Not built in no equivalent short of OPENJSON shredding
PostgreSQL Ja Supported under this name.
MySQL Different name or syntax JSON_CONTAINS(target, candidate) means the same
SQLite Not built in no operator; emulate with json_each
Oracle Not built in no operator; JSON_EXISTS for paths

Beispiele

PostgreSQL

SELECT * FROM events
WHERE payload @> '{"tags": ["pro"]}';

PostgreSQL

SELECT * FROM events
WHERE payload->'user' @> '"matt"';

PostgreSQL

CREATE INDEX ON events USING gin (payload);

Gut zu wissen

  • @> is containment, not equality: '{"a":1,"b":2}' @> '{"a":1}' is true. The reversed order asks the opposite question; <@ is contained-in.
  • The operator exists for jsonb only: a json column or a text literal needs the ::jsonb cast, and the error names the types, not the fix.
  • Paired with a GIN index (jsonb_ops or jsonb_path_ops) it answers from the index; without one, every row is parsed and compared.
  • Array containment means all elements: tags @> '["a","b"]' requires both. For any-of matching on string elements, use ?|: tags ?| array['a', 'b'].

Questions about @> (jsonb)

@> or ->> comparisons?

@> for 'has this member/element' queries, indexable with GIN. ->> for comparing extracted text. Mixing them works but the index only helps @>.

Why does @> fail on my json column?

The containment operator is defined for jsonb. Cast both sides (::jsonb) or, better, migrate the column to jsonb; json cannot use GIN indexes either.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: PostgreSQL, MySQL.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.