Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Databases Reporting with SQL Date and Time Functions Practice Session

Sean Flanagan
Sean Flanagan
33,232 Points

Loans overdue

Hi. How's my solution for this challenge?

SELECT * FROM loans WHERE returned_on > return_by;

If i remember correctly you are looking to query for all the books loaned out that are overdue. If this is the case then your query is almost perfect. You should also consider any books that were returned on the same day they were due -> Hint: [ There is room in your query somewhere for an "equality" check ;) ].

You Got This!

3 Answers

I think we want loans with a return_by date which is in the past and with no returned_on date (book hasn't been returned.) This seemed to work:

SELECT * FROM loans WHERE return_by < DATE("now") AND returned_on IS NULL;

Yassin Chiguer
Yassin Chiguer
5,228 Points

The following query worked for me. Checks your current date (now) to yesterday's date (-1 day) for the return_by value. That's my understanding from the tutorial.

select * from loans where return_by = DATE("now", "-1 day")

Marcus Grant
Marcus Grant
Courses Plus Student 2,546 Points

Since I noticed that the dayys in this challenge are dynamic, I came up with the following which works:

SELECT * FROM loans WHERE return_by < DATE("now");

Since the book might be due to returned today, it won't count as it's not overdue. However, if the return date is null and is any day before today I will get the correct list of overdue books.

Marcus, I believe you need to include "AND returned_on IS NULL;" as Troy mentioned in his comment. If you don't, books that have a return_by date prior to today's date but have already been returned will show up as overdue books.

SELECT * FROM loans WHERE return_by < DATE("now") AND returned_on IS NULL;