Bird
0
0

Why does this query fail?

medium📝 Debug Q7 of 15
PostgreSQL - JSON and JSONB

Why does this query fail?
SELECT * FROM users WHERE profile @> '{"age": 30}';
Given that profile is of type JSON, not JSONB.

AThe JSON string is invalid
BThe query is correct and should work
Cprofile column must be cast to text
DThe @> operator works only with JSONB, not JSON
Step-by-Step Solution
Solution:
  1. Step 1: Understand operator compatibility

    The @> operator is defined for JSONB type, not JSON.
  2. Step 2: Identify type mismatch

    Since profile is JSON, the operator cannot be applied directly, causing failure.
  3. Final Answer:

    @> operator requires JSONB type, not JSON -> Option D
  4. Quick Check:

    @> works only with JSONB = B [OK]
Quick Trick: Cast JSON to JSONB before using @> operator [OK]
Common Mistakes:
  • Assuming JSON and JSONB are interchangeable
  • Ignoring type requirements of operators
  • Thinking JSON string syntax is the issue

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes