Bird
0
0

Given a table products with a JSONB column attributes and a GIN index on it, what will this query return?

medium📝 query result Q13 of 15
PostgreSQL - JSON and JSONB
Given a table products with a JSONB column attributes and a GIN index on it, what will this query return?

SELECT * FROM products WHERE attributes @> '{"color": "red"}';
AAll rows where the JSONB column contains the key "color" with value "red"
BAll rows where the JSONB column contains any key named "color"
CAll rows where the JSONB column contains the value "red" anywhere
DSyntax error due to incorrect JSON format
Step-by-Step Solution
Solution:
  1. Step 1: Understand the @> operator

    The @> operator checks if the left JSONB contains the right JSONB as a subset, meaning the key-value pair must exist exactly.
  2. Step 2: Analyze the query condition

    The condition attributes @> '{"color": "red"}' means the JSONB column must have a key "color" with value "red".
  3. Final Answer:

    All rows where the JSONB column contains the key "color" with value "red" -> Option A
  4. Quick Check:

    @> means contains key-value pair exactly [OK]
Quick Trick: Use @> to find exact key-value pairs in JSONB [OK]
Common Mistakes:
  • Thinking @> matches only keys, not values
  • Assuming it matches any occurrence of value
  • Confusing JSON syntax causing errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes