Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Join and Aggregation

EasySQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL Join and Aggregation. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Cotality's Knowledge Library stores authors and their published books in separate tables. Write a PostgreSQL query that reports how many books each author has written.

Requirements

  1. Return every author, including authors who have no matching books.
  2. Count books using the book primary key, so authors with no books appear with a count of zero.
  3. Return the author ID, author name, and book count, ordered by book count descending and author name ascending for ties.

Schema

authors
ColumnTypeDescription
author_idPKINTEGERUnique author identifier
author_nameVARCHAR(100)Author's display name
books
ColumnTypeDescription
book_idPKINTEGERUnique book identifier
author_idINTEGERReference to authors.author_id; NULL indicates no assigned author
book_titleVARCHAR(200)Title of the book
Tablesauthorsbooks
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results