SQL Exercise: List employees in ascending order on their experiences
[An editor is available at the bottom of the page to write and execute the scripts.]
80. From the following table, write a SQL query to list the employee ID, name, hire date, current date and experience of the employees in ascending order on their experiences.
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 | 2003-06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 | | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 2005-09-21 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 2001-01-13 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 2006-01-03 | IT_PROG | 9000.00 | 0.00 | 102 | 60 | | 104 | Bruce | Ernst | BERNST | 590.423.4568 | 2007-05-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 | | 105 | David | Austin | DAUSTIN | 590.423.4569 | 2005-06-25 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 2006-02-05 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 2007-02-07 | IT_PROG | 4200.00 | 0.00 | 103 | 60 | | 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 2002-08-17 | FI_MGR | 12008.00 | 0.00 | 101 | 100 | | 109 | Daniel | Faviet | DFAVIET | 515.124.4169 | 2002-08-16 | FI_ACCOUNT | 9000.00 | 0.00 | 108 | 100 | ......... | 206 | William | Gietz | WGIETZ | 515.123.8181 | 2002-06-07 | AC_ACCOUNT | 8300.00 | 0.00 | 205 | 110 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
Pictorial Presentation:
Sample Solution:
SELECT emp_id,
emp_name,
hire_date,
CURRENT_DATE,
age(CURRENT_DATE, hire_date) EXP
FROM employees
ORDER BY EXP ASC;
Sample Output:
emp_id | emp_name | hire_date | date | exp --------+----------+------------+------------+------------------------- 68736 | ADNRES | 1997-05-23 | 2018-02-01 | 20 years 8 mons 9 days 67858 | SCARLET | 1997-04-19 | 2018-02-01 | 20 years 9 mons 12 days 69324 | MARKER | 1992-01-23 | 2018-02-01 | 26 years 9 days 69062 | FRANK | 1991-12-03 | 2018-02-01 | 26 years 1 mon 29 days 69000 | JULIUS | 1991-12-03 | 2018-02-01 | 26 years 1 mon 29 days 68319 | KAYLING | 1991-11-18 | 2018-02-01 | 26 years 2 mons 13 days 66564 | MADDEN | 1991-09-28 | 2018-02-01 | 26 years 4 mons 3 days 68454 | TUCKER | 1991-09-08 | 2018-02-01 | 26 years 4 mons 23 days 67832 | CLARE | 1991-06-09 | 2018-02-01 | 26 years 7 mons 22 days 66928 | BLAZE | 1991-05-01 | 2018-02-01 | 26 years 9 mons 65646 | JONAS | 1991-04-02 | 2018-02-01 | 26 years 9 mons 29 days 65271 | WADE | 1991-02-22 | 2018-02-01 | 26 years 11 mons 7 days 64989 | ADELYN | 1991-02-20 | 2018-02-01 | 26 years 11 mons 9 days 63679 | SANDRINE | 1990-12-18 | 2018-02-01 | 27 years 1 mon 14 days (14 rows)
Explanation:
The said query in SQL that selects the emp_id, emp_name, hire_date, CURRENT_DATE, and EXP columns for each employee in the employees table, sorted in ascending order by their years of experience.
The age() function calculates the difference in years between the hire_date and CURRENT_DATE and the resulting column is aliased as EXP.
Go to:
PREV : Employees work as SALESMEN, sorted by their salary.
NEXT : Sort employees by designation, joining after 1991.
Practice Online
Sample Database: employees
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
