Bird
0
0

How can you find rows where a JSONB column data contains a nested JSON object {"user":{"id":123}}?

hard📝 Application Q9 of 15
PostgreSQL - JSON and JSONB

How can you find rows where a JSONB column data contains a nested JSON object {"user":{"id":123}}?

ASELECT * FROM table WHERE data @> '{"user":{"id":123}}'::jsonb;
BSELECT * FROM table WHERE data @> '{"user.id":123}'::jsonb;
CSELECT * FROM table WHERE data ? 'user.id';
DSELECT * FROM table WHERE data @> '{"id":123}'::jsonb;
Step-by-Step Solution
Solution:
  1. Step 1: Use nested JSON structure in containment

    The JSONB containment operator supports nested objects, so the nested JSON must be written as is.
  2. Step 2: Eliminate incorrect options

    Dot notation like {"user.id":123} is invalid JSON, the ? operator checks keys only, and {"id":123} checks only top-level key.
  3. Final Answer:

    Use nested JSON object in @> operator as in option A -> Option A
  4. Quick Check:

    Nested JSONB containment uses nested JSON syntax = A [OK]
Quick Trick: Write nested JSON exactly for @> containment [OK]
Common Mistakes:
  • Using dot notation inside JSON
  • Checking keys only with ? operator
  • Ignoring nested structure in containment

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes