Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Reporting with SQL Date and Time Functions Practice Session

Sean Flanagan
Sean Flanagan
33,235 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!

4 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
PLUS
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;

-- Find all loans that are overdue.

SELECT return_by, date("Now") AS todays_date, ROUND(julianday("Now") - julianday(return_by),2) AS daydiff from loans WHERE daydiff > 0