Skip to content

Instantly share code, notes, and snippets.

@DozenCoder
Created August 16, 2019 11:08
Show Gist options
  • Select an option

  • Save DozenCoder/d30307fd915e471dfff1d47628737753 to your computer and use it in GitHub Desktop.

Select an option

Save DozenCoder/d30307fd915e471dfff1d47628737753 to your computer and use it in GitHub Desktop.
Answer for twu-biblioteca-assignment

1.Who checked out the book 'The Hobbit’?

select name
from member
where id in (select member_id
             from book
                      inner join checkout_item on checkout_item.book_id = book.id
             where book.title = "The Hobbit");

Anand Beck

2.How many people have not checked out anything?

select count(*)
from member
where id not in (select DISTINCT(member_id) from checkout_item);

37

3.What books and movies aren't checked out?

select title
from book
where id not in (select distinct(book_id) from checkout_item where book_id not null)
union
select title
from movie
where id not in (select distinct(movie_id) from checkout_item where movie_id not null);

1984
Catcher in the Rye
Crouching Tiger, Hidden Dragon
Domain Driven Design
Fellowship of the Ring
Lawrence of Arabia
Office Space
Thin Red Line
To Kill a Mockingbird
Tom Sawyer

4.Add the book 'The Pragmatic Programmer', and add yourself as a member. Check out 'The Pragmatic Programmer'. Use your query from question 1 to verify that you have checked it out. Also, provide the SQL used to update the database.

insert into book
values (11, "The Pragmatic Programmer");

insert into member
values (43, "dabu zhang");

insert into checkout_item (member_id, book_id)
values (43, 11);

select name
from member
where id in (select member_id
             from book
                      inner join checkout_item on checkout_item.book_id = book.id
             where book.title = "The Pragmatic Programmer");

dabu zhang

5.Who has checked out more that 1 item?

select name
from member
where id in (select member_id from checkout_item group by member_id having count(member_id) > 1);

Anand Beck
Frank Smith

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment