SQL Exercise: List the details of the employees working at PERTH
[An editor is available at the bottom of the page to write and execute the scripts.]
60. From the following table, write a SQL query to list the details of the employees working at PERTH.
Sample table: employees
Sample table: department
Sample Solution:
SELECT *
FROM employees
WHERE dep_id IN
(SELECT dep_id
FROM department
WHERE department.dep_location = 'PERTH');
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 64989 | ADELYN | SALESMAN | 66928 | 1991-02-20 | 1700.00 | 400.00 | 3001 65271 | WADE | SALESMAN | 66928 | 1991-02-22 | 1350.00 | 600.00 | 3001 66564 | MADDEN | SALESMAN | 66928 | 1991-09-28 | 1350.00 | 1500.00 | 3001 68454 | TUCKER | SALESMAN | 66928 | 1991-09-08 | 1600.00 | 0.00 | 3001 69000 | JULIUS | CLERK | 66928 | 1991-12-03 | 1050.00 | | 3001 (6 rows)
Explanation:
The said query in SQL that retrieves all employee records from the 'employees' table where the department ID of the employee matches a department ID from the 'department' table that has a location of 'PERTH'.
The WHERE clause filters the results to include only those departments where the department ID of the employee is in a subquery.
The subquery returns a list of department IDs from the 'department' table that are located in Perth.
Practice Online
Structure of employee Database:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: Display the unique department of the employees.
Next SQL Exercise: Employees of grade 2 and 3 who belongs to a city.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics