Schema and sample data
| emp_id | name | hire_date | department |
|---|---|---|---|
| 1 | Ava Torres | 2021-03-15 | Engineering |
| 2 | Ben Ncube | 2022-01-01 | Sales |
| 3 | Cara Singh | 2023-06-20 | Engineering |
| 4 | Deng Liu | 2020-11-05 | Support |
| 5 | Emma Rossi | 2022-09-12 | Sales |
| 6 | Farid Khan | 2024-02-28 | Support |
Show setup SQL (copy to run)
CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
hire_date DATE NOT NULL,
department VARCHAR(30)
);
INSERT INTO Employees (emp_id, name, hire_date, department) VALUES
(1, 'Ava Torres', '2021-03-15', 'Engineering'),
(2, 'Ben Ncube', '2022-01-01', 'Sales'),
(3, 'Cara Singh', '2023-06-20', 'Engineering'),
(4, 'Deng Liu', '2020-11-05', 'Support'),
(5, 'Emma Rossi', '2022-09-12', 'Sales'),
(6, 'Farid Khan', '2024-02-28', 'Support');
From the Employees table, return the name and hire_date of every employee hired on or after 2022-01-01. Sort the result by hire_date from earliest to latest.
Expected result
| name | hire_date |
|---|---|
| Ben Ncube | 2022-01-01 |
| Emma Rossi | 2022-09-12 |
| Cara Singh | 2023-06-20 |
| Farid Khan | 2024-02-28 |
Show hint
You need a WHERE clause that compares hire_date against the cutoff date. Because the wording says "on or after", the comparison is >= so an employee hired exactly on 2022-01-01 must be kept. Write the literal as a quoted string in YYYY-MM-DD form.
Try to write the query yourself before you open this.
Show solution and explanation
SELECT name, hire_date
FROM Employees
WHERE hire_date >= '2022-01-01'
ORDER BY hire_date ASC;
The WHERE hire_date >= '2022-01-01' filter keeps only rows whose hire date falls on or after the cutoff. Ava Torres (2021-03-15) and Deng Liu (2020-11-05) are hired before the cutoff, so they are dropped. Ben Ncube was hired exactly on 2022-01-01, and because the comparison is >= (not a strict >), that row stays. Reading "on or after" as a strict greater-than is the most common mistake here.
SQL Server compares a DATE column against a string literal by implicitly converting the string to a date. The YYYY-MM-DD format is the safe, unambiguous choice because it is interpreted the same way regardless of the server language or regional settings. Other formats like 01/02/2022 can be read as either January 2 or February 1.
ORDER BY hire_date ASC then sorts the four surviving rows from earliest to latest. ASC is the default, so it could be omitted, but stating it makes the intent explicit. If you needed a bounded range instead of an open one, you would add an upper bound, for example hire_date >= '2022-01-01' AND hire_date < '2023-01-01'.