Skip to the content

Dates

SQL · Unit 14: Text and dates

Now, the neighborhood library. Which loan was the very first? Brian's, which has number 8, the highest, and is from December 20, 2025.

borrowed is a TEXT column. SQL doesn't know it holds dates, and sorts them character by character, the way it sorts names. And yet December 2025 comes out before January 2026. That's not luck. The YYYY-MM-DD format puts the biggest unit in front: first the year, then the month, and the day last. Written that way, the order of the characters is the order of the calendar.

SELECT id, member, borrowed
FROM loans
ORDER BY borrowed, id
LIMIT 4;

Returns

idmemberborrowed
8Brian2025-12-20
1Ann2026-01-02
2Luke2026-01-03
7Luke2026-01-08

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    loans has one loan from December 2025 and seven from January 2026, from the 2nd to the 13th. What does this query return?

  2. 2. Multiple choice

    Another library writes its dates the way people do on paper, month first, 12/20/2025 and 01/02/2026, also in a text column. Sorted from lowest to highest, which one comes first?

  3. 3. Find the bug

    The librarian asks for the loans from January 8 on. There are five. The query runs and returns nothing. Which line has the problem?

  4. 4. Complete the code

    Now the first week of the year, January 1 to 7. Fill in the blanks in the range.

  5. 5. Predict the output

    Loans 3, 5, and 6 haven't been returned, and their returned is NULL. Of the other five, two came back on January 14 and 15, and three before the 10th. What does this query return?

  6. 6. Multiple choice

    Outside this course, in a real library. The librarian saves this query and opens it every morning. What happens to the number it returns?

  7. 7. Put the lines in order

    The librarian goes over what went out before January 10, starting with the most recent. Put the query in order. Two lines don't belong.

  8. 8. Put the lines in order

    When did each member borrow a book for the first time? Put in order the query that gives each name with that date, from the longest-standing member to the newest. Two lines don't belong.

  9. 9. Write the code

    The librarian is putting together the January summary. Write a query with three columns, in this order: the loan's id, the member, and the day the book came back, for the loans that went out in January 2026 and have already been returned. Sort by the return day, earliest to latest.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →