SQL Exercise: Employees according to the department in ASC order
[An editor is available at the bottom of the page to write and execute the scripts.]
84. From the following table, write a SQL query to find the employees according to the department in ascending order. Return name, job name, department, salary, and grade.
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 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
Sample table: department
departmentid | name | head
--------------+------------------+------
1 | General Medicine | 4
2 | Surgery | 7
3 | Psychiatry | 9
Sample table: salary_grade
grade | min_sal | max_sal
-------+---------+---------
1 | 800 | 1300
2 | 1301 | 1500
3 | 1501 | 2100
4 | 2101 | 3100
5 | 3101 | 9999
Pictorial Presentation:
Sample Solution:
SELECT e.emp_name,
e.job_name,
d.dep_name,
e.salary,
s.grade
FROM employees e,
department d,
salary_grade s
WHERE e.dep_id = d.dep_id
AND e.salary BETWEEN s.min_sal AND s.max_sal
ORDER BY e.dep_id ;
Sample Output:
emp_name | job_name | dep_name | salary | grade ----------+-----------+-----------+---------+------- KAYLING | PRESIDENT | FINANCE | 6000.00 | 5 CLARE | MANAGER | FINANCE | 2550.00 | 4 MARKER | CLERK | FINANCE | 1400.00 | 2 SANDRINE | CLERK | AUDIT | 900.00 | 1 SCARLET | ANALYST | AUDIT | 3100.00 | 4 ADNRES | CLERK | AUDIT | 1200.00 | 1 FRANK | ANALYST | AUDIT | 3100.00 | 4 JONAS | MANAGER | AUDIT | 2957.00 | 4 BLAZE | MANAGER | MARKETING | 2750.00 | 4 JULIUS | CLERK | MARKETING | 1050.00 | 1 ADELYN | SALESMAN | MARKETING | 1700.00 | 3 WADE | SALESMAN | MARKETING | 1350.00 | 2 MADDEN | SALESMAN | MARKETING | 1350.00 | 2 TUCKER | SALESMAN | MARKETING | 1600.00 | 3 (14 rows)
Explanation:
The said query in SQL that retrieves the employee name, job name, department name, salary, and salary grade for each employee who falls within a certain salary range and belongs to a department, and orders the results by department ID.
A join happens by matching the 'employees' and 'department' tables based on the department ID column and the 'employees' and 'salary_grade' tables matches the salary falls within the salary range of the salary grade. This orders the results by the department ID.
Go to:
PREV : List the employees along with grades in ascending order.
NEXT : Sort on highest salary of employees except CLERK.
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.
