w3resource

MySQL Date and Time Exercises: Get the first name and hire date from employees table where hire date between '1987-06-01' and '1987-07-30'

MySQL Date Time: Exercise-11 with Solution

Write a MySQL query to get the first name and hire date from employees table where hire date between '1987-06-01' and '1987-07-30'.

Sample table: employees


Code:

-- This SQL query retrieves the first name and hire date of employees hired within a specific date range.

SELECT 
    FIRST_NAME, -- Selecting the 'FIRST_NAME' column from the 'employees' table.
    HIRE_DATE -- Selecting the 'HIRE_DATE' column from the 'employees' table.
FROM 
employees -- Specifying the 'employees' table.
WHERE 
    HIRE_DATE  -- Filtering the rows based on the hire date being within a specific date range.
    BETWEEN '1987-06-01 00:00:00' -- Specifies the start of the date range.
    AND '1987-07-30  23:59:59'; -- Specifies the end of the date range.

Explanation:

  • This SQL query selects the first name and hire date of employees from the 'employees' table.
  • The WHERE clause filters the result set to include only records where the hire date falls within the specified date range.
  • The date range is defined using the BETWEEN operator, with the start date '1987-06-01 00:00:00' and the end date '1987-07-30 23:59:59'.
  • The query retrieves employees hired between June 1, 1987, and July 30, 1987, inclusive of both dates and all times within those dates.

Sample Output:

FIRST_NAME	HIRE_DATE
Steven		1987-06-17T04:00:00.000Z
Neena		1987-06-18T04:00:00.000Z
Lex		1987-06-19T04:00:00.000Z
Alexander	1987-06-20T04:00:00.000Z
Bruce		1987-06-21T04:00:00.000Z
David		1987-06-22T04:00:00.000Z
Valli		1987-06-23T04:00:00.000Z
Diana		1987-06-24T04:00:00.000Z
Nancy		1987-06-25T04:00:00.000Z
Daniel		1987-06-26T04:00:00.000Z
John		1987-06-27T04:00:00.000Z
Ismael		1987-06-28T04:00:00.000Z
Jose Manuel	1987-06-29T04:00:00.000Z
Luis		1987-06-30T04:00:00.000Z
Den		1987-07-01T04:00:00.000Z
Alexander	1987-07-02T04:00:00.000Z
Shelli		1987-07-03T04:00:00.000Z
Sigal		1987-07-04T04:00:00.000Z
Guy		1987-07-05T04:00:00.000Z
Karen		1987-07-06T04:00:00.000Z
Matthew		1987-07-07T04:00:00.000Z
Adam		1987-07-08T04:00:00.000Z
Payam		1987-07-09T04:00:00.000Z
Shanta		1987-07-10T04:00:00.000Z
Kevin		1987-07-11T04:00:00.000Z
Julia		1987-07-12T04:00:00.000Z
Irene		1987-07-13T04:00:00.000Z
James		1987-07-14T04:00:00.000Z
Steven		1987-07-15T04:00:00.000Z
Laura		1987-07-16T04:00:00.000Z
Mozhe		1987-07-17T04:00:00.000Z
James		1987-07-18T04:00:00.000Z
TJ		1987-07-19T04:00:00.000Z
Jason		1987-07-20T04:00:00.000Z
Michael		1987-07-21T04:00:00.000Z
Ki		1987-07-22T04:00:00.000Z
Hazel		1987-07-23T04:00:00.000Z
Renske		1987-07-24T04:00:00.000Z
Stephen		1987-07-25T04:00:00.000Z
John		1987-07-26T04:00:00.000Z
Joshua		1987-07-27T04:00:00.000Z
Trenna		1987-07-28T04:00:00.000Z
Curtis		1987-07-29T04:00:00.000Z
Randall		1987-07-30T04:00:00.000Z

Pictorial Presentation of the above query:

Pictorial: Query to get the first name and hire date from employees table where hire date between '1987-06-01' and '1987-07-30'

 

MySQL Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous:Write a MySQL query to get the DATE value from a given day (number in N).
Next:Write a MySQL query to display the current date in the specific format.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.