Date functions let you pull parts out of a date (YEAR, MONTH), measure gaps (DATEDIFF, TIMESTAMPDIFF), move a date forwards or backwards (INTERVAL) and format it for display (DATE_FORMAT). CURRENT_DATE and CURRENT_TIME return today's date and the current time.
Why the output has a date next to it: queries that use today's date give a different result every day, so their output below was produced on 23 November 2026. Run them yourself to see today's result.
YEAR(date) MONTH(date) DATEDIFF(later, earlier)
DATE_SUB(date, INTERVAL 4 YEAR)
TIMESTAMPDIFF(YEAR, start_date, end_date)
DATE_FORMAT(date, '%M %e, %Y')
These questions use the employees table (30 rows). Create it first with the copy-and-paste setup script. Here is the data, so you can check your answers by eye.
See the employees table
Table: employees
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1001 | James | Bond | 45300 | 1998-01-19 | M | 2020-11-29 | Marketing |
| 1002 | Robert | Lee | 69410 | 1998-09-02 | M | 2019-12-26 | IT |
| 1003 | Mary | Wood | 39518 | 1997-11-07 | F | 2019-09-12 | Business Development |
| 1004 | John | Cole | 36264 | 1999-02-01 | M | 2021-04-14 | HR |
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing |
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR |
| 1007 | David | Mills | 39400 | 1998-04-26 | M | 2021-10-17 | IT |
| 1008 | William | Hill | 26392 | 1999-10-21 | M | 2019-09-30 | Business Development |
| 1009 | Linda | Potter | 55231 | 1998-01-24 | F | 2021-08-07 | Finance |
| 1010 | Richard | Martin | 25600 | 1996-08-07 | M | 2021-03-23 | IT |
| 1011 | Susan | Hall | 54152 | 1999-12-27 | F | 2020-08-07 | Finance |
| 1012 | Joseph | Turner | 60556 | 1997-05-13 | M | 2019-09-02 | Marketing |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development |
| 1014 | Thomas | Ryan | 23676 | 1996-10-12 | M | 2022-03-02 | IT |
| 1015 | Charles | Williams | 57226 | 2000-01-29 | M | 2019-11-05 | NULL |
| 1016 | Jessica | Clark | NULL | 2000-01-25 | F | 2020-01-24 | IT |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing |
| 1020 | Daniel | Elliott | 44012 | 1999-01-21 | M | 2023-01-08 | IT |
| 1021 | David | Mills | NULL | 1998-01-24 | M | 2019-12-26 | IT |
| 1022 | Sarah | Harrison | 44692 | 1996-03-20 | F | 2020-05-03 | Finance |
| 1023 | Matthew | Hill | 41365 | 1998-05-16 | M | 2019-05-23 | IT |
| 1024 | Anthony | Miller | 56794 | 1996-06-30 | M | 2019-02-28 | Business Development |
| 1025 | Mark | Wood | 65120 | 1998-10-29 | M | 2020-07-30 | Business Development |
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL |
| 1027 | Barbara | Anderson | NULL | 1999-12-27 | F | 2021-03-14 | HR |
| 1028 | Jessica | Clark | 63836 | 1998-01-24 | F | 2021-05-23 | IT |
| 1029 | Nancy | Cole | 63669 | 2000-01-26 | F | 2019-12-26 | Finance |
| 1030 | Joe | Brooks | 44000 | 1999-08-05 | M | 2021-03-09 | Business Development |
How to use this page: read a question, write your own query, then open "Show the answer" and compare. Trying first is what makes it stick.
Question 1Retrieve all employees who joined the company in the year 2021.
Show the answer and the output
SELECT * FROM employees WHERE YEAR(joining_date) = 2021;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1004 | John | Cole | 36264 | 1999-02-01 | M | 2021-04-14 | HR |
| 1007 | David | Mills | 39400 | 1998-04-26 | M | 2021-10-17 | IT |
| 1009 | Linda | Potter | 55231 | 1998-01-24 | F | 2021-08-07 | Finance |
| 1010 | Richard | Martin | 25600 | 1996-08-07 | M | 2021-03-23 | IT |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development |
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL |
| 1027 | Barbara | Anderson | NULL | 1999-12-27 | F | 2021-03-14 | HR |
| 1028 | Jessica | Clark | 63836 | 1998-01-24 | F | 2021-05-23 | IT |
| 1030 | Joe | Brooks | 44000 | 1999-08-05 | M | 2021-03-09 | Business Development |
Question 2Get emp_no, first name and joining date. Also calculate the number of days between the joining date and the current date as 'days_worked'.
Show the answer and the output
SELECT emp_no, first_name, joining_date,
DATEDIFF(CURRENT_DATE(), joining_date) AS days_worked
FROM employees;Output (run on 23 November 2026)
| emp_no | first_name | joining_date | days_worked |
|---|---|---|---|
| 1001 | James | 2020-11-29 | 2185 |
| 1002 | Robert | 2019-12-26 | 2524 |
| 1003 | Mary | 2019-09-12 | 2629 |
| 1004 | John | 2021-04-14 | 2049 |
| 1005 | Jennifer | 2019-06-28 | 2705 |
| 1006 | Michael | 2022-06-09 | 1628 |
| 1007 | David | 2021-10-17 | 1863 |
| 1008 | William | 2019-09-30 | 2611 |
| 1009 | Linda | 2021-08-07 | 1934 |
| 1010 | Richard | 2021-03-23 | 2071 |
| 1011 | Susan | 2020-08-07 | 2299 |
| 1012 | Joseph | 2019-09-02 | 2639 |
Showing the first 12 of 30 rows.
Question 3Retrieve all the employees who joined in the month of June.
Show the answer and the output
SELECT * FROM employees WHERE MONTH(joining_date) = 6;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing |
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR |
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL |
Question 4Display the first name of the employees who were hired in the last 1 year.
Show the answer and the output
SELECT first_name FROM employees
WHERE joining_date >= CURRENT_DATE() - INTERVAL 1 YEAR;Output (run on 23 November 2026)
Empty set: the query ran and no rows matched.
This returns no rows today, because everyone in the sample data joined more than a year ago.
Question 5Calculate the total number of employees hired in each year.
Show the answer and the output
SELECT YEAR(joining_date) AS joining_year, COUNT(*) AS total_hired
FROM employees
GROUP BY YEAR(joining_date)
ORDER BY joining_year;Output
| joining_year | total_hired |
|---|---|
| 2019 | 12 |
| 2020 | 5 |
| 2021 | 9 |
| 2022 | 3 |
| 2023 | 1 |
Different from the PDF: Added ORDER BY joining_year so the years come out in order.
Question 6Find the employees who have been with the company for more than 4 years.
Show the answer and the output
SELECT * FROM employees
WHERE joining_date <= DATE_SUB(CURRENT_DATE(), INTERVAL 4 YEAR);Output (run on 23 November 2026)
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1001 | James | Bond | 45300 | 1998-01-19 | M | 2020-11-29 | Marketing |
| 1002 | Robert | Lee | 69410 | 1998-09-02 | M | 2019-12-26 | IT |
| 1003 | Mary | Wood | 39518 | 1997-11-07 | F | 2019-09-12 | Business Development |
| 1004 | John | Cole | 36264 | 1999-02-01 | M | 2021-04-14 | HR |
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing |
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR |
| 1007 | David | Mills | 39400 | 1998-04-26 | M | 2021-10-17 | IT |
| 1008 | William | Hill | 26392 | 1999-10-21 | M | 2019-09-30 | Business Development |
| 1009 | Linda | Potter | 55231 | 1998-01-24 | F | 2021-08-07 | Finance |
| 1010 | Richard | Martin | 25600 | 1996-08-07 | M | 2021-03-23 | IT |
| 1011 | Susan | Hall | 54152 | 1999-12-27 | F | 2020-08-07 | Finance |
| 1012 | Joseph | Turner | 60556 | 1997-05-13 | M | 2019-09-02 | Marketing |
Showing the first 12 of 29 rows.
Question 7Calculate the total salary expenditure for each month in the year 2021.
Show the answer and the output
SELECT DATE_FORMAT(joining_date, '%M') AS hire_month,
SUM(salary) AS total_expenditure
FROM employees
WHERE YEAR(joining_date) = 2021
GROUP BY MONTH(joining_date), hire_month
ORDER BY MONTH(joining_date);Output
| hire_month | total_expenditure |
|---|---|
| March | 69600 |
| April | 94510 |
| May | 63836 |
| June | 47030 |
| August | 55231 |
| October | 39400 |
Different from the PDF: Grouped by the month number and ordered by it, so the months come out in calendar order, not alphabetical.
Question 8Retrieve emp_no, first name and joining date of employees who have less than 3 years of work experience in the company.
Show the answer and the output
SELECT emp_no, first_name, joining_date,
TIMESTAMPDIFF(YEAR, joining_date, CURRENT_DATE()) AS experience
FROM employees
WHERE TIMESTAMPDIFF(YEAR, joining_date, CURRENT_DATE()) < 3;Output (run on 23 November 2026)
Empty set: the query ran and no rows matched.
This returns no rows today, because everyone in the sample data joined more than 3 years ago.
Different from the PDF: The PDF subtracted YEAR(joining_date) from YEAR(CURRENT_DATE()), which counts calendar years and is wrong for someone who joined in December. TIMESTAMPDIFF counts full years.
Question 9Change the format of the current date to May 26, 2023 style, as 'formatted_date'.
Show the answer and the output
SELECT DATE_FORMAT(CURRENT_DATE(), '%M %e, %Y') AS formatted_date;Output (run on 23 November 2026)
| formatted_date |
|---|
| November 23, 2026 |
Question 10Change the format of the current date to Friday, May 26, 2023 style, as 'formatted_date'.
Show the answer and the output
SELECT DATE_FORMAT(CURRENT_DATE(), '%W, %M %e, %Y') AS formatted_date;Output (run on 23 November 2026)
| formatted_date |
|---|
| Monday, November 23, 2026 |
Question 11Change the format of the current date to YYYY/MM/DD.
Show the answer and the output
SELECT DATE_FORMAT(CURRENT_DATE(), '%Y/%m/%d') AS formatted_date;Output (run on 23 November 2026)
| formatted_date |
|---|
| 2026/11/23 |
Question 12Display the current time in 12-hour format with AM/PM in parentheses, as 'formatted_time'. Example: 07:53:46 (PM).
Show the answer and the output
SELECT DATE_FORMAT(CURRENT_TIME(), '%h:%i:%s (%p)') AS formatted_time;Output (run on 23 November 2026)
| formatted_time |
|---|
| 08:55:49 (AM) |
Question 13Display the current time in 24-hour format without seconds (HH:MM), as 'formatted_time'.
Show the answer and the output
SELECT DATE_FORMAT(CURRENT_TIME(), '%H:%i') AS formatted_time;Output (run on 23 November 2026)
| formatted_time |
|---|
| 08:55 |
Question 14Display the current timestamp in the format Month DD, YYYY HH:MM:SS AM/PM, as 'formatted_timestamp'. Example: May 26, 2023 19:58:19 PM.
Show the answer and the output
SELECT DATE_FORMAT(CURRENT_TIMESTAMP(), '%M %d, %Y %T %p') AS formatted_timestamp;Output (run on 23 November 2026)
| formatted_timestamp |
|---|
| November 23, 2026 08:55:49 AM |
%T prints the time in 24-hour format, so adding %p shows "PM" next to a 24-hour time, as in the example in the question. Use %r for a proper 12-hour time.
Different from the PDF: Column renamed to formatted_timestamp, as the question asks.
Want these as PDFs? Download the question PDF or the answer PDF to practise offline.
