Bird
0
0

Given the table products with columns category and name, what is the output of this query?

medium📝 query result Q13 of 15
PostgreSQL - Aggregate Functions and GROUP BY
Given the table products with columns category and name, what is the output of this query?
SELECT category, JSON_AGG(name) FROM products GROUP BY category ORDER BY category;
A[{"category":"Books","json_agg":["Novel","Comics"]}, {"category":"Toys","json_agg":["Car","Doll"]}]
B[{"category":"Books","json_agg":"Novel,Comics"}, {"category":"Toys","json_agg":"Car,Doll"}]
C[{"category":"Books","json_agg":["Novel"]}, {"category":"Toys","json_agg":["Car"]}]
D[{"category":"Books","json_agg":null}, {"category":"Toys","json_agg":null}]
Step-by-Step Solution
Solution:
  1. Step 1: Understand GROUP BY with JSON_AGG

    The query groups rows by category and collects all product names per category into a JSON array.
  2. Step 2: Analyze expected JSON_AGG output

    JSON_AGG returns an array of names per category, so each category has a JSON array of product names, not a string or null.
  3. Final Answer:

    [{"category":"Books","json_agg":["Novel","Comics"]}, {"category":"Toys","json_agg":["Car","Doll"]}] -> Option A
  4. Quick Check:

    JSON_AGG groups names into arrays = [{"category":"Books","json_agg":["Novel","Comics"]}, {"category":"Toys","json_agg":["Car","Doll"]}] [OK]
Quick Trick: JSON_AGG returns arrays, not comma-separated strings [OK]
Common Mistakes:
  • Expecting JSON_AGG to return strings instead of arrays
  • Ignoring GROUP BY effect
  • Assuming null results without data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes