Bird
0
0

You have a table books with columns author and title. How would you write a query to list each author with all their book titles concatenated, separated by commas?

hard📝 Application Q8 of 15
PostgreSQL - Aggregate Functions and GROUP BY
You have a table books with columns author and title. How would you write a query to list each author with all their book titles concatenated, separated by commas?
ASELECT author, STRING_AGG(title) FROM books;
BSELECT author, STRING_AGG(author, ', ') FROM books GROUP BY title;
CSELECT author, STRING_AGG(title, ';') FROM books;
DSELECT author, STRING_AGG(title, ', ') FROM books GROUP BY author;
Step-by-Step Solution
Solution:
  1. Step 1: Group by author to aggregate titles per author

    We need to group rows by author to combine their titles.
  2. Step 2: Use STRING_AGG on title with comma separator

    STRING_AGG(title, ', ') concatenates titles with commas.
  3. Final Answer:

    SELECT author, STRING_AGG(title, ', ') FROM books GROUP BY author; -> Option D
  4. Quick Check:

    Group by author + STRING_AGG titles with comma [OK]
Quick Trick: Group by author, aggregate titles with STRING_AGG [OK]
Common Mistakes:
  • Omitting GROUP BY author
  • Missing separator argument
  • Aggregating wrong column

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes