w3resource

SQL Exercise: Employees with salary grades higher than MARKER

SQL subqueries on employee Database: Exercise-66 with Solution

[An editor is available at the bottom of the page to write and execute the scripts.]

66. From the following tables, write a SQL query to find those employees whose salary grade is greater than the grade of MARKER. Return complete information about the employees.

Sample table: employees


Sample table: salary_grade


Sample Solution:

SELECT *
FROM employees e,
     salary_grade s
WHERE e.salary BETWEEN s.min_sal AND s.max_sal
  AND s.grade >
    (SELECT s.grade
     FROM employees e,
          salary_grade s
     WHERE e.salary BETWEEN s.min_sal AND s.max_sal
       AND e.emp_name = 'MARKER') ;

Sample Output:

 emp_id | emp_name | job_name  | manager_id | hire_date  | salary  | commission | dep_id | grade | min_sal | max_sal
--------+----------+-----------+------------+------------+---------+------------+--------+-------+---------+---------
  64989 | ADELYN   | SALESMAN  |      66928 | 1991-02-20 | 1700.00 |     400.00 |   3001 |     3 |    1501 |    2100
  68454 | TUCKER   | SALESMAN  |      66928 | 1991-09-08 | 1600.00 |       0.00 |   3001 |     3 |    1501 |    2100
  66928 | BLAZE    | MANAGER   |      68319 | 1991-05-01 | 2750.00 |            |   3001 |     4 |    2101 |    3100
  67832 | CLARE    | MANAGER   |      68319 | 1991-06-09 | 2550.00 |            |   1001 |     4 |    2101 |    3100
  65646 | JONAS    | MANAGER   |      68319 | 1991-04-02 | 2957.00 |            |   2001 |     4 |    2101 |    3100
  67858 | SCARLET  | ANALYST   |      65646 | 1997-04-19 | 3100.00 |            |   2001 |     4 |    2101 |    3100
  69062 | FRANK    | ANALYST   |      65646 | 1991-12-03 | 3100.00 |            |   2001 |     4 |    2101 |    3100
  68319 | KAYLING  | PRESIDENT |            | 1991-11-18 | 6000.00 |            |   1001 |     5 |    3101 |    9999
(8 rows)

Explanation:

The said query in SQL that retrieves all employees and salary grade information from the 'employees' and 'salary_grade' table where the employee's salary falls within the salary range for a given grade and the grade is higher than the grade for the employee named 'MARKER'.

The WHERE clause filters the results to include only those rows where the employee's salary falls within the salary range for a grade obtained from a subquery.

The AND operator further filters the results to include only records where the grade is higher than the grade for the employee named 'MARKER'.

The subquery selects the grade for the employee named 'MARKER' from the 'employees' and 'salary_grade' tables and the AND operator in the subquery is used to select only the employee named 'MARKER'.

Practice Online


Structure of employee Database:

employee database structure

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

Previous SQL Exercise: Employees belong to the department where KAYLING works.
Next SQL Exercise: Grade, same as TUCKER or more experience than SANDRINE.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.