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

Jan Lundeen
Jan Lundeen
5,887 Points

SQL Stage 4: Practice Problems: Find all loans that are overdue - not sure if my query is right.

Hi,

In Reporting for SQL, I was working on the section "SQL Stage 4: Practice Problems". In particular, I was trying to find all loans that are overdue. Here's my query:

SELECT * FROM loans where returned_on BETWEEN DATE('NOW',"- 2 days") AND DATE('now',"+2 days") AND returned_on IS null;

However, I'm not sure that it's correct since I got "Query returned no results". I looked at the table and no books are overdue. I would think I'd get zero, but I'm not sure. Any ideas?

Thanks,

Jan

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Jan.

I have edited your question to follow the guidelines for pasting the code on the forum: https://teamtreehouse.com/forum/posting-code-to-the-forum

;)

Jan Lundeen
Jan Lundeen
5,887 Points

Hi Vittorio,

I'm not sure exactly what you did. I looked in the link to the forum, but I can't find any non-compliant code. Can you help me out with this?

Thanks,

Jan

2 Answers

Your query can never return results because you're asking for records where returned_on is between two dates and where return_on IS NULL; you can't do both.

You probably meant for the dates to be the return_by field, but you really only need that to be greater than NOW.

SELECT * FROM loans WHERE return_by < DATE('now') AND returned_on IS null;
Jan Lundeen
Jan Lundeen
5,887 Points

Your rignt Ian. Thanks!

Jan