Dates in a range with BETWEEN
Question
Show the name and hired_on date of employees hired in 2020 or 2021.
employeesid, name, salary, department_id, manager_id, hired_on
departmentsid, name, city
Your query
Ctrl+Enter to run
Result
Expected result
| name | hired_on |
|---|---|
| Lucas Moreau | 2020-09-21 |
| Mei Chen | 2021-02-01 |
| Omar Haddad | 2021-11-08 |
| Aisha Bello | 2020-01-20 |
Hint
BETWEEN includes both ends of the range. Dates are written as 'YYYY-MM-DD'.
Show the solution
solution.sql
Run this code
SELECT name, hired_on
FROM employees
WHERE hired_on BETWEEN '2020-01-01' AND '2021-12-31';
Why it works
BETWEEN low AND high is the same as >= low AND <= high. Dates stored as 'YYYY-MM-DD' text sort correctly, so the comparison works in SQLite.
Common mistake
If the column held a date and a time (2021-12-31 14:00), BETWEEN ... '2021-12-31' would miss that last day. Then use hired_on < '2022-01-01' instead.