Predict the output
What does the containment query return?
CREATE TABLE items (id int, attrs jsonb);
INSERT INTO items VALUES
(1, '{"color": "red", "size": "M"}'),
(2, '{"color": "blue"}'),
(3, '{"color": "red"}');
SELECT id FROM items
WHERE attrs @> '{"color": "red"}'
ORDER BY id;Hint 1 of 1
Does @> require an exact match?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 1 and 3
Correct. Both contain color red; extra keys don't matter.
@> asks whether the stored document contains the given one. Rows 1 and 3 both have color red; row 1's extra size key doesn't matter.
Why not the others?
- B
3only. Containment allows extra keys in the stored value. - C
1only. Item 3 also has color red. - D All three. Item 2 is blue.
| id |
|---|
| 1 |
| 3 |
(2 rows)