Bird
0
0

Consider the query SELECT * FROM users WHERE data ? status; intended to check if the key 'status' exists in the JSONB column data. Why does this query fail?

medium📝 Debug Q6 of 15
PostgreSQL - JSON and JSONB

Consider the query SELECT * FROM users WHERE data ? status; intended to check if the key 'status' exists in the JSONB column data. Why does this query fail?

AThe JSONB column 'data' is not indexed
BThe key 'status' is not enclosed in single quotes, causing a syntax error
CThe operator <code>?</code> cannot be used in WHERE clauses
DThe table 'users' does not have a column named 'status'
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the query syntax

    The operator ? requires the key to be a string literal.
  2. Step 2: Identify the error

    The key status is not quoted, so PostgreSQL treats it as a column or identifier, causing failure.
  3. Final Answer:

    The key 'status' is not enclosed in single quotes, causing a syntax error -> Option B
  4. Quick Check:

    Keys must be quoted strings [OK]
Quick Trick: Always quote JSONB keys in ? operator [OK]
Common Mistakes:
  • Forgetting to quote the key string
  • Assuming operator ? works without quotes
  • Confusing column names with key strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes