﻿ SQL: List jobs of department ID 1001, not found in ID 2001

# SQL Exercise: List jobs of department ID 1001, not found in ID 2001

## SQL subqueries on employee Database: Exercise-19 with Solution

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

19. From the following table, write a SQL query to find the highest paid employees in the department MARKETING. Return complete information about the employees.

Sample table: employees

Sample table: department

Sample Solution:

``````SELECT *
FROM employees
WHERE salary IN
(SELECT max(salary)
FROM employees
WHERE dep_id IN
(SELECT d.dep_id
FROM department d
WHERE d.dep_name = 'MARKETING'));
``````

Sample Output:

``` emp_id | emp_name | job_name | manager_id | hire_date  | salary  | commission | dep_id
--------+----------+----------+------------+------------+---------+------------+--------
66928 | BLAZE    | MANAGER  |      68319 | 1991-05-01 | 2750.00 |            |   3001
(1 row)
```

Explanation:

The said query in SQL that selects all information about employees who work in the Marketing department and have the highest salary among all employees in the Marketing department from the employees table.

The subquery in the WHERE clause of main query that selects the maximum salary from the employees table where the dep_id is in the list of department IDs obtained from a subquery. The second subquery selects the dep_id column from the department table where the dep_name is equal to 'MARKETING'.

## Practice Online

Structure of employee Database:

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

Previous SQL Exercise: Find the details of highest paid employee.
Next SQL Exercise: List the grade 3 employees from Perth hired recently.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿