Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

SQL exercises on employee Database: List the employee id, name, hire_date, current date and experience of the employees in ascending order on their experiences

SQL employee Database: Exercise-80 with Solution

[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.

Pictorial Presentation:

SQL exercises on employee Database: List the employee id, name, hire_date, current date and experience of the employees in ascending order on their experiences

Sample table: employees


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)

Practice Online


Sample Database: employee

employee database structure

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

Previous: From the following table, write a SQL query to list the employees who works as a SALESMAN. Sort the result set in ascending order of annual salary. Return employee id, name, annual salary, daily salary of all the employees.
Next: From the following table, write a SQL query to list the employees in ascending order of designations of those joined after the second half of 1991.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



SQL: Tips of the Day

SQL Server SELECT into existing table.

INSERT INTO dbo.TABLETWO
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:

INSERT INTO dbo.TABLETWO
  (col1, col2)
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

Database: SQL Server

Ref: https://bit.ly/3y6tpA3