JSON aggregation with JSON_AGG
📖 Scenario: You are managing a small online bookstore database. You want to collect all book titles by each author into a single JSON array for easy display on your website.
🎯 Goal: Create a query that groups books by author and aggregates their titles into a JSON array using JSON_AGG.
📋 What You'll Learn
Create a table called
books with columns id (integer), author (text), and title (text).Insert exactly these rows into
books: (1, 'Alice Walker', 'The Color Purple'), (2, 'George Orwell', '1984'), (3, 'Alice Walker', 'Meridian'), (4, 'George Orwell', 'Animal Farm').Write a query that selects
author and a JSON array of their title values using JSON_AGG.Group the results by
author.💡 Why This Matters
🌍 Real World
Aggregating related data into JSON arrays is useful for APIs and web applications that consume JSON data.
💼 Career
Database developers and backend engineers often use JSON aggregation to prepare data for frontend applications.
Progress0 / 4 steps