Bird
0
0

Which query correctly uses the JSONB existence operator to achieve this?

hard📝 Application Q15 of 15
PostgreSQL - JSON and JSONB

You want to find all rows in the events table where the JSONB column details contains either the key 'location' or the key 'venue'. Which query correctly uses the JSONB existence operator to achieve this?

ASELECT * FROM events WHERE details ?| array['location', 'venue'];
BSELECT * FROM events WHERE details ?& array['location', 'venue'];
CSELECT * FROM events WHERE details ? array['location', 'venue'];
DSELECT * FROM events WHERE details ? 'location' AND details ? 'venue';
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want rows where either 'location' OR 'venue' keys exist in the JSONB column.
  2. Step 2: Choose the correct operator

    The ?| operator checks if any key in the array exists in the JSONB data.
  3. Step 3: Compare options

    SELECT * FROM events WHERE details ?| array['location', 'venue']; uses ?| array['location', 'venue'], which matches the requirement.
  4. Final Answer:

    SELECT * FROM events WHERE details ?| array['location', 'venue']; -> Option A
  5. Quick Check:

    Use '?|' for any key in array [OK]
Quick Trick: Use '?|' with array for checking multiple keys [OK]
Common Mistakes:
  • Using OR with multiple '?' instead of '?|'
  • Using '?&' which requires all keys
  • Using AND instead of OR logic
  • Confusing single '?' with multiple keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes