CodeEraFree roadmap

SQL Practice / Date and time functions

SQL Date and Time Functions: practice questions with answers

14 questions with solved queries and real output. Outputs checked on MariaDB 10.11 (MySQL-compatible) on 23 November 2026.

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1001JamesBond453001998-01-19M2020-11-29Marketing
1002RobertLee694101998-09-02M2019-12-26IT
1003MaryWood395181997-11-07F2019-09-12Business Development
1004JohnCole362641999-02-01M2021-04-14HR
1005JenniferGill489411996-06-05F2019-06-28Marketing
1006MichaelWatts238001999-04-16M2022-06-09HR
1007DavidMills394001998-04-26M2021-10-17IT
1008WilliamHill263921999-10-21M2019-09-30Business Development
1009LindaPotter552311998-01-24F2021-08-07Finance
1010RichardMartin256001996-08-07M2021-03-23IT
1011SusanHall541521999-12-27F2020-08-07Finance
1012JosephTurner605561997-05-13M2019-09-02Marketing
1013MaryGill582461999-01-21F2021-04-30Business Development
1014ThomasRyan236761996-10-12M2022-03-02IT
1015CharlesWilliams572262000-01-29M2019-11-05NULL
1016JessicaClarkNULL2000-01-25F2020-01-24IT
1017NancyParker669001996-10-24F2022-01-09Finance
1018BettyBarker352881998-11-13F2019-12-26HR
1019ChristopherGibson593411998-04-23M2019-05-02Marketing
1020DanielElliott440121999-01-21M2023-01-08IT
1021DavidMillsNULL1998-01-24M2019-12-26IT
1022SarahHarrison446921996-03-20F2020-05-03Finance
1023MatthewHill413651998-05-16M2019-05-23IT
1024AnthonyMiller567941996-06-30M2019-02-28Business Development
1025MarkWood651201998-10-29M2020-07-30Business Development
1026ElizabethWalker470301998-08-11F2021-06-18NULL
1027BarbaraAndersonNULL1999-12-27F2021-03-14HR
1028JessicaClark638361998-01-24F2021-05-23IT
1029NancyCole636692000-01-26F2019-12-26Finance
1030JoeBrooks440001999-08-05M2021-03-09Business 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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1004JohnCole362641999-02-01M2021-04-14HR
1007DavidMills394001998-04-26M2021-10-17IT
1009LindaPotter552311998-01-24F2021-08-07Finance
1010RichardMartin256001996-08-07M2021-03-23IT
1013MaryGill582461999-01-21F2021-04-30Business Development
1026ElizabethWalker470301998-08-11F2021-06-18NULL
1027BarbaraAndersonNULL1999-12-27F2021-03-14HR
1028JessicaClark638361998-01-24F2021-05-23IT
1030JoeBrooks440001999-08-05M2021-03-09Business 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_nofirst_namejoining_datedays_worked
1001James2020-11-292185
1002Robert2019-12-262524
1003Mary2019-09-122629
1004John2021-04-142049
1005Jennifer2019-06-282705
1006Michael2022-06-091628
1007David2021-10-171863
1008William2019-09-302611
1009Linda2021-08-071934
1010Richard2021-03-232071
1011Susan2020-08-072299
1012Joseph2019-09-022639

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1005JenniferGill489411996-06-05F2019-06-28Marketing
1006MichaelWatts238001999-04-16M2022-06-09HR
1026ElizabethWalker470301998-08-11F2021-06-18NULL

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_yeartotal_hired
201912
20205
20219
20223
20231

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1001JamesBond453001998-01-19M2020-11-29Marketing
1002RobertLee694101998-09-02M2019-12-26IT
1003MaryWood395181997-11-07F2019-09-12Business Development
1004JohnCole362641999-02-01M2021-04-14HR
1005JenniferGill489411996-06-05F2019-06-28Marketing
1006MichaelWatts238001999-04-16M2022-06-09HR
1007DavidMills394001998-04-26M2021-10-17IT
1008WilliamHill263921999-10-21M2019-09-30Business Development
1009LindaPotter552311998-01-24F2021-08-07Finance
1010RichardMartin256001996-08-07M2021-03-23IT
1011SusanHall541521999-12-27F2020-08-07Finance
1012JosephTurner605561997-05-13M2019-09-02Marketing

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_monthtotal_expenditure
March69600
April94510
May63836
June47030
August55231
October39400

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.