Bird
0
0

Given the table items with a JSONB column attributes, what will this query return?

medium📝 query result Q13 of 15
PostgreSQL - JSON and JSONB

Given the table items with a JSONB column attributes, what will this query return?

SELECT id FROM items WHERE attributes @> '{"color": "red"}'::jsonb;

Assuming items contains:

id | attributes
---+-------------------------
1  | {"color": "red", "size": "M"}
2  | {"color": "blue", "size": "L"}
3  | {"color": "red"}
4  | {"size": "S"}
A[4]
B[2, 4]
C[1, 3]
D[1, 2, 3]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the query condition

    The query selects rows where attributes contain the JSON {"color": "red"}.
  2. Step 2: Check each row's attributes

    Rows 1 and 3 have "color": "red"; rows 2 and 4 do not.
  3. Final Answer:

    [1, 3] -> Option C
  4. Quick Check:

    Only rows with color red = [1, 3] [OK]
Quick Trick: Look for rows where JSON contains the key-value pair exactly [OK]
Common Mistakes:
  • Including rows without the key-value pair
  • Confusing @> with key existence operator
  • Ignoring the JSONB cast requirement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes