Easy Filtering rows Employees database

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

Press Run to see your query's result, or Check answer to compare it with the expected result.

Expected result

namehired_on
Lucas Moreau2020-09-21
Mei Chen2021-02-01
Omar Haddad2021-11-08
Aisha Bello2020-01-20

Rows can come in any order. Column names do not have to match.

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.

Want to try the query on MySQL or PostgreSQL? Open it in the SQL Online Compiler or SQLite Online.