Bird
0
0

Why does this query cause an error?

medium📝 Debug Q7 of 15
PostgreSQL - JSON and JSONB
Why does this query cause an error?
SELECT data->>'name' FROM users WHERE data->'age' > 25;
Adata->>'name' is invalid syntax.
BCannot compare JSON object with integer directly.
CMissing FROM clause.
DJSON keys must be in double quotes.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze WHERE clause

    data->'age' returns JSON object, which cannot be compared to integer 25.
  2. Step 2: Correct approach

    Use data->>'age' to get text, then cast to integer for comparison.
  3. Final Answer:

    Cannot compare JSON object with integer directly. -> Option B
  4. Quick Check:

    Compare JSON text or cast, not JSON object [OK]
Quick Trick: Use ->> to get text before comparing JSON values [OK]
Common Mistakes:
  • Comparing JSON object directly to number
  • Confusing -> and ->> operators
  • Ignoring need for casting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes