MySQL Date and Time Exercises: Query to get the department ID, year, and number of employees joined
MySQL Date Time: Exercise-21 with Solution
Write a MySQL query to get the department ID, year, and number of employees joined.
Sample table: employees
+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | 100 | Steven | King | SKING | 515.123.4567 | 1987-06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 | | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 1987-06-18 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 1987-06-19 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 1987-06-20 | IT_PROG | 9000.00 | 0.00 | 102 | 60 | | 104 | Bruce | Ernst | BERNST | 590.423.4568 | 1987-06-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 | | 105 | David | Austin | DAUSTIN | 590.423.4569 | 1987-06-22 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 1987-06-23 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 1987-06-24 | IT_PROG | 4200.00 | 0.00 | 103 | 60 | | 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 1987-06-25 | FI_MGR | 12000.00 | 0.00 | 101 | 100 | .......... | 206 | William | Gietz | WGIETZ | 515.123.8181 | 1987-10-01 | AC_ACCOUNT | 8300.00 | 0.00 | 205 | 110 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
Code:
-- This SQL query calculates the count of employees hired in each department for each year, ordered by department ID.
SELECT
DEPARTMENT_ID, -- Selecting the 'DEPARTMENT_ID' column from the 'employees' table.
DATE_FORMAT(HIRE_DATE,'%Y'), -- Formatting the hire date to extract the year and selecting it as 'YEAR'.
COUNT(EMPLOYEE_ID) -- Counting the number of employees in each department for each year.
FROM
employees -- Specifying the 'employees' table.
GROUP BY
DEPARTMENT_ID, DATE_FORMAT(HIRE_DATE, '%Y') -- Grouping the result set by department ID and year of hire date.
ORDER BY
DEPARTMENT_ID; -- Ordering the result set by department ID.
Explanation:
- This SQL query calculates the count of employees hired in each department for each year, ordered by department ID.
- It selects the department ID, extracts the year from the hire date, and counts the number of employees for each department and year combination.
- The result set is grouped by department ID and year of hire date using the GROUP BY clause.
- The ORDER BY clause ensures that the result set is ordered by department ID.
Sample Output:
DEPARTMENT_ID DATE_FORMAT(HIRE_DATE,'%Y') COUNT(EMPLOYEE_ID) 0 1987 1 10 1987 1 20 1987 2 30 1987 6 40 1987 1 50 1987 45 60 1987 5 70 1987 1 80 1987 34 90 1987 3 100 1987 6 110 1987 2
Pictorial Presentation of the above query:

Go to:
PREV :Write a MySQL query to get first name, hire date and experience of the employees.
NEXT :MySQL string
MySQL Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
