Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Books in Good Condition and Renewal Rate

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

Your question is Books in Good Condition and Renewal Rate. Start with the requirements and the three 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

Meta Library tracks physical book copies and their current loan records. Write a PostgreSQL query to determine how many copies are in good condition and currently not returned, then calculate what percentage of those copies have been renewed more than two times.

Requirements

  1. Join the book, copy, and loan tables.
  2. Include only copies where condition is good and returned_at is NULL.
  3. Return the eligible copy count, the count renewed more than two times, and that count as a percentage of eligible copies, rounded to two decimal places.
  4. Treat a NULL renewal count as zero and avoid division-by-zero errors.

Schema

books
ColumnTypeDescription
book_idPKINTUnique identifier for a book title
titleVARCHAR(200)Book title
authorVARCHAR(150)Author name
book_copies
ColumnTypeDescription
copy_idPKINTUnique identifier for a physical copy
book_idINTBook title associated with the copy
conditionVARCHAR(20)Current physical condition
loans
ColumnTypeDescription
loan_idPKINTUnique identifier for a loan
copy_idINTPhysical copy on loan
returned_atDATEDate returned, or null for an active loan
renewal_countINTNumber of times the loan was renewed
Tablesbooksbook_copiesloans
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results