Handling Dates Part 1
Part of the Fundamentals section of Coddy's SQL journey — lesson 31 of 72.
Dates in SQL have a specific format and we can query them similarly to numbers. To write a date in a query we will use the format: YYYY-MM-DD (2012-09-24)
To query dates we can use all the operators we already know - bigger, smaller, or equal within the WHERE keyword:
SELECT * FROM table1
WHERE col1 > '2011-01-13'We can even use the BETWEEN keyword:
SELECT * FROM table1
WHERE col1 BETWEEN '2010-01-01' AND '2010-02-25'Challenge
EasyAvailable tables and columns:
<strong>games</strong>:<strong>id</strong>,<strong>date</strong>
Fetch all of the game ids that are not played in the winter.
Winter dates are: 2022-12-21 - 2023-03-20. Only these days should not be included.
Sort them by the date in descending order and rename the column to game
Cheat sheet
Dates in SQL use the format YYYY-MM-DD (e.g., 2012-09-24).
Query dates using comparison operators:
SELECT * FROM table1
WHERE col1 > '2011-01-13'Use BETWEEN for date ranges:
SELECT * FROM table1
WHERE col1 BETWEEN '2010-01-01' AND '2010-02-25'Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4More Keywords
The IN keywordThe BETWEEN keywordThe LIKE keywordThe AS keywordRecap - Cellphone Models2Conditions
Conditions BasicsThe AND keywordThe OR keywordThe NOT keywordMultiple Conditions CombinedParenthesisBooleans5Arithmetic Operations
Mathematical OperatorsMathematical ColumnsThe Modulo OperationThe ROUND() Function3Specific Return Format
Null valuesSort Results Part 1Sort Results Part 2Recap - Cyber Security FirmLimit number of recordsRecap - Vehicle Factory6Intro Challenges
Recap - Parliamentary ElectionRecap - Police Criminal ArrestRecap - Bar Beverage ContainerRecap - Engineer new columns