0
0
PostgreSQLquery~10 mins

JSONB containment (@>) operator in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
PostgreSQL
SELECT '{"a":1, "b":2}'::jsonb @> '{"a":1}'::jsonb AS contains;
This query checks if the JSONB on the left contains the JSONB on the right and returns true or false.
Execution Table
StepLeft JSONBRight JSONBContainment CheckResult
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
💡 All checks complete; results show if left JSONB contains right JSONB.
Variable Tracker
VariableStartCheck 1Check 2Check 3Check 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}}
ResultTRUEFALSETRUEFALSE
Key Moments - 3 Insights
Why does '{"a":1, "b":2}' @> '{"a":1}' return TRUE?
Because the left JSONB contains the key-value pair {"a":1} exactly, as shown in execution_table row 1.
Why does '{"a":1, "b":2}' @> '{"b":3}' return FALSE?
Because the value for key "b" in the left JSONB is 2, which does not match 3 in the right JSONB, as shown in execution_table row 2.
Does the operator check nested JSON objects?
Yes, it checks nested objects recursively, as shown in execution_table rows 3 and 4 where nested objects are compared.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of checking '{"a":1, "b":2}' @> '{"a":1}'?
ANULL
BFALSE
CTRUE
DError
💡 Hint
Refer to execution_table row 1 under the Result column.
At which step does the containment check return FALSE because of a value mismatch?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Check execution_table rows where Result is FALSE and see the reason.
If the right JSONB is '{"b":{"c":3}}', what is the containment result when left JSONB is '{"a":1, "b":{"c":3}}'?
AFALSE
BTRUE
CNULL
DError
💡 Hint
Look at execution_table row 3 for nested object containment.
Concept Snapshot
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
Full Transcript
The JSONB containment operator (@>) in PostgreSQL checks if the left JSONB value contains the right JSONB value. It returns TRUE if all key-value pairs in the right JSONB exist in the left JSONB, including nested objects. The execution steps show examples where the operator returns TRUE or FALSE based on matching keys and values. This operator is useful to filter JSONB data by presence of specific parts. The visual quiz tests understanding of containment results and nested checks.