Bird
0
0

Given the table users(id SERIAL PRIMARY KEY, data JSONB) with data:

medium📝 query result Q13 of 15
PostgreSQL - Advanced Features
Given the table users(id SERIAL PRIMARY KEY, data JSONB) with data:
{"name": "Alice", "age": 30} in the data column, what does this query return?
SELECT data->>'name' FROM users WHERE data->>'age' = '30';
AReturns all data rows regardless of age.
BReturns the age 30 as a number.
CReturns the name 'Alice' for users aged 30.
DReturns an error due to wrong JSON syntax.
Step-by-Step Solution
Solution:
  1. Step 1: Understand JSONB operators in the query

    The operator ->> extracts JSON object field as text. The WHERE clause filters rows where age equals '30' as text.
  2. Step 2: Analyze query result

    The SELECT returns the 'name' field as text for rows matching age '30'. So it returns 'Alice'.
  3. Final Answer:

    Returns the name 'Alice' for users aged 30. -> Option C
  4. Quick Check:

    data->>'name' with age filter = 'Alice' [OK]
Quick Trick: ->> extracts text from JSONB fields [OK]
Common Mistakes:
  • Confusing -> and ->> operators
  • Expecting numeric type instead of text
  • Ignoring WHERE filter on JSONB field

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes