Each Author's Home Library
Problem
A library network records which libraries each author has borrowing rights at.
```
Membership
+-------------+---------+
| Column | Type |
+-------------+---------+
| author_id | int |
| library_id | int |
| home_flag | varchar |
+-------------+---------+
(author_id, library_id) is the primary key.
home_flag is one of ('Y','N'). 'Y' marks the author's home library; 'N' means it is a secondary library.
```
Authors may hold memberships at several libraries. When an author belongs to more than one, exactly one row is flagged `'Y'` as the home library. When an author belongs to only one library, that single row is flagged `'N'` but it is still their home library.
Report each author together with their home `library_id`. In any order.
Tables
Example rows — the live problem includes the full dataset.
| author_id | library_id | home_flag |
|---|---|---|
| 1 | 1 | N |
| 2 | 1 | Y |
| 2 | 2 | N |
Expected output
Your answer should return 4 rows with the columns author_id, library_id.
Starter code (SQL)
SELECT *
FROM Membership;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock