Bird
0
0

Given the table products with a JSONB column attributes containing {"color": "red", "size": "M"}, what will this query return?

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

Given the table products with a JSONB column attributes containing {"color": "red", "size": "M"}, what will this query return?

SELECT id FROM products WHERE attributes ? 'color';
ASyntax error due to incorrect operator usage
BAll product IDs where the value of 'color' is 'red'
CAll product IDs where the 'color' key does not exist
DAll product IDs where the 'color' key exists in attributes
Step-by-Step Solution
Solution:
  1. Step 1: Understand the query condition

    The condition attributes ? 'color' checks if the key 'color' exists in the JSONB column.
  2. Step 2: Determine the query result

    It returns all rows where 'color' is a key, regardless of its value.
  3. Final Answer:

    All product IDs where the 'color' key exists in attributes -> Option D
  4. Quick Check:

    Operator '?' checks key existence, not value [OK]
Quick Trick: Remember '?' checks keys, not values [OK]
Common Mistakes:
  • Thinking it filters by key value
  • Assuming it returns rows without the key
  • Confusing '?' with containment operator
  • Expecting syntax error incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes