SQL Exercises: All employees in the Finance department
From the following tables, write a SQL query to find all those employees who work in the Finance department. Return department ID, name (first name), job ID and department name.
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 | | 110 | John | Chen | JCHEN | 515.124.4269 | 2005-09-28 | FI_ACCOUNT | 8200.00 | 0.00 | 108 | 100 | .................... +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
Sample table: departments
+---------------+----------------------+------------+-------------+ | DEPARTMENT_ID | DEPARTMENT_NAME | MANAGER_ID | LOCATION_ID | +---------------+----------------------+------------+-------------+ | 10 | Administration | 200 | 1700 | | 20 | Marketing | 201 | 1800 | | 30 | Purchasing | 114 | 1700 | | 40 | Human Resources | 203 | 2400 | | 50 | Shipping | 121 | 1500 | | 60 | IT | 103 | 1400 | | 70 | Public Relations | 204 | 2700 | | 80 | Sales | 145 | 2500 | | 90 | Executive | 100 | 1700 | | 100 | Finance | 108 | 1700 | ...... +---------------+----------------------+------------+-------------+
Sample Solution:
-- Selecting specific columns (department_id, first_name, job_id) from the 'employees' table (aliased as 'e')
-- and the 'department_name' from the 'departments' table (aliased as 'd')
SELECT e.department_id, e.first_name, e.job_id, d.department_name
-- Specifying the tables involved in the query ('employees' as 'e' and 'departments' as 'd')
FROM employees e, departments d
-- Joining the 'employees' and 'departments' tables based on the common 'department_id'
WHERE e.department_id = d.department_id
-- Filtering rows based on the condition that the 'department_name' is 'Finance'
AND d.department_name = 'Finance';
Sample Output:
department_id first_name job_id department_name 100 Nancy FI_MGR Finance 100 Daniel FI_ACCOUNT Finance 100 John FI_ACCOUNT Finance 100 Ismael FI_ACCOUNT Finance 100 Jose Manuel FI_ACCOUNT Finance 100 Luis FI_ACCOUNT Finance
Code Explanation:
The said query in SQL that select data from two tables: 'employees' and 'departments'. The query selects the "department_id", "first_name", "job_id", and "department_name" columns from both tables. The results are filtered using the "WHERE" clause, which states that the "department_id" column in the 'employees' table must match the "department_id" column in the 'departments' table, and the "department_name" column in the 'departments' table must be equal to the string "Finance".
Relational Algebra Expression:

Relational Algebra Tree:

Visual Presentation:

Alternative Statements:
Using INNER JOIN:
SELECT e.department_id, e.first_name, e.job_id, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id
WHERE d.department_name = 'Finance';
Using Subquery:
SELECT department_id, first_name, job_id,
(SELECT department_name FROM departments WHERE department_id = e.department_id) AS department_name
FROM employees e
WHERE department_id IN (SELECT department_id FROM departments WHERE department_name = 'Finance');
Using EXISTS Subquery:
SELECT department_id, first_name, job_id,
(SELECT department_name FROM departments WHERE department_id = e.department_id) AS department_name
FROM employees e
WHERE EXISTS (
SELECT 1
FROM departments d
WHERE e.department_id = d.department_id
AND d.department_name = 'Finance'
);
Practice Online
Query Visualization:
Duration:

Rows:

Cost:

Contribute your code and comments through Disqus.
Previous SQL Exercise: Salary of all employees who report to Payam.
Next SQL Exercise: Salary and reporting person id is 3000 and 121.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.