reset password
Author Message
cysun
Posts: 2935
Posted 12:27 Oct 20, 2015 |

A friend of mine recently went to an interview and here's one of the questions:

Given a table books(title, author, release_date) (the assumption here is that each book only has one author), write a query to list the two most recently released book of each author.

vsluong4
Posts: 87
Posted 21:18 Oct 20, 2015 |

According to this, I believe this will work

SELECT * FROM books

WHERE (SELECT COUNT(*) FROM books AS b

    WHERE books.author = b.author AND b.release_date >= books.release_date) <= 2;

Last edited by vsluong4 at 12:18 Oct 21, 2015.
cysun
Posts: 2935
Posted 21:47 Oct 20, 2015 |
vsluong4 wrote:

According to this, I believe this will work

SELECT * FROM books

WHERE (SELECT COUNT(*) FROM books AS b

    WHERE books.title = b.title AND books.author = b.author AND b.release_date >= books.release_date) <= 2;

I'm fairly certain this won't work, at least not with "books.title = b.title". You can prove me wrong by creating a table with some test data and actually running this query.

vsluong4
Posts: 87
Posted 13:00 Oct 21, 2015 |
Fixed