Bird
0
0

What will be the result of this query?

medium📝 query result Q5 of 15
PostgreSQL - JSON and JSONB
What will be the result of this query?
SELECT data->'items'->>0 FROM orders WHERE id=5;
Assuming data contains JSON array under key 'items'.
ANULL
BJSON object of the first element in 'items' array
CError due to invalid operator usage
DText value of the first element in 'items' array
Step-by-Step Solution
Solution:
  1. Step 1: Analyze operator chaining

    data->'items' extracts the JSON array as JSON, then ->>0 extracts the first element as text.
  2. Step 2: Understand array indexing with ->>

    Using ->>0 extracts the element at index 0 as text, so the output is the text value of the first array element.
  3. Final Answer:

    Text value of the first element in 'items' array -> Option D
  4. Quick Check:

    Chained -> and ->> extract nested JSON text [OK]
Quick Trick: Chain -> for JSON, ->> for text extraction [OK]
Common Mistakes:
  • Expecting JSON output from ->> operator
  • Using ->> on JSON array without index
  • Assuming error from chaining

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes