Inner and Outer Join SQL
Inner and Outer Join SQL
SQL JOIN is a clause that combines rows from two or more tables based on a related column between them.
The table's relationship is established by comparing the values in the columns. The purpose of using JOINs is
to retrieve data from multiple tables in a single query. There are four types of JOINs in SQL: INNER JOIN,
LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
INNER JOIN: Returns only the rows with matching values in both tables.
LEFT JOIN: Returns all the rows from the left table and matching rows from the right table.
RIGHT JOIN: Returns all the rows from the right table and matching rows from the left table.
FULL OUTER JOIN: Returns all the rows when a match in the left or right table.
Objectives
By the end of this lab, you'll be able to:
In this lab, you will use MySQL. MySQL is a Relational Database Management System (RDBMS) designed
to efficiently store, manipulate, and retrieve data.
To complete this lab, you will utilize MySQL relational database service available as part of IBM Skills
Network Labs (SN Labs) Cloud IDE. SN Labs is a virtual lab environment used in this course.
about:blank 1/5
28/03/2024, 17:08 about:blank
In this lab, you will run through some SQL practice problems that will provide hands-on experience with the
different kinds of join operations.
NOTE: This lab requires you to have all five of these tables of the HR database populated with sample data
on MySQL.
1. Open the phpMyAdmin interface from the Skills Network Toolbox in Cloud IDE.
2. Create a blank database named 'HR'. Use the script shared in the link below to create the required
tables.
Script_Create_Tables.sql
3. Download the files in the links below to your local machine (if not already done in previous labs).
Departments.csv
Jobs.csv
JobsHistory.csv
Locations.csv
Employees.csv
4. Use these files to the interface as data for respective tables in the 'HR' database.
JOINS
Let us see some examples of JOINS being used to query the data.
1. Retrieve the names and job start dates of all employees who work for department number 5.
about:blank 2/5
28/03/2024, 17:08 about:blank
We need to use the Inner join operation with the EMPLOYEES table as the left table and the JOB_HISTORY
table as the right table. The join will be made over employee ID, and the query response will be filtered for
the Department ID value 5.
The query for this question will be as shown below.
1. 1
2. 2
3. 3
4. 4
5. 5
Copied!
2. Retrieve employee ID, last name, department ID, and department name for all employees.
For this, you must use the Left Outer Join operation with the EMPLOYEES table as the left table and the
DEPARTMENTS table as the right table. The join will happen on the Department ID. The query will be
written as follows:
1. 1
2. 2
3. 3
4. 4
Copied!
3. Retrieve the First name, Last name, and Department name of all employees.
For this, you will use the Full Outer Join operation with the EMPLOYEES table as the left table and the
DEPARTMENTS table as the right table. A full outer join in MySQL is implemented as a UNION of left and
right outer joins. The query will be written as shown below.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
about:blank 3/5
28/03/2024, 17:08 about:blank
Copied!
Practice Problems
1. Retrieve the names, job start dates, and job titles of all employees who work for department number 5.
Hint
Solution
1. 1
2. 2
3. 3
4. 4
5. 5
1. select E.F_NAME,E.L_NAME, JH.START_DATE, J.JOB_TITLE
2. from EMPLOYEES as E
3. INNER JOIN JOB_HISTORY as JH on E.EMP_ID=JH.EMPL_ID
4. INNER JOIN JOBS as J on E.JOB_ID=J.JOB_IDENT
5. where E.DEP_ID ='5';
Copied!
2. Retrieve employee ID, last name, and department ID for all employees but department names for only
those born before 1980.
Hint
Solution
1. 1
2. 2
3. 3
4. 4
5. 5
1. SELECT E.EMP_ID, E.L_NAME, E.DEP_ID, D.DEP_NAME
2. FROM EMPLOYEES AS E
3. LEFT OUTER JOIN DEPARTMENTS AS D
4. ON E.DEP_ID = D.DEPT_ID_DEP
5. AND YEAR(E.B_DATE) < 1980;
Copied!
3. Retrieve the first name and last name of all employees but department ID and department names only
for male employees.
Hint
Solution
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
1. SELECT E.F_NAME, E.L_NAME, D.DEPT_ID_DEP, D.DEP_NAME
2. FROM EMPLOYEES AS E
3. LEFT OUTER JOIN DEPARTMENTS AS D
4. ON E.DEP_ID=D.DEPT_ID_DEP AND E.SEX = 'M'
about:blank 4/5
28/03/2024, 17:08 about:blank
5.
6. UNION
7.
8. SELECT E.F_NAME, E.L_NAME, D.DEPT_ID_DEP, D.DEP_NAME
9. from EMPLOYEES AS E
10. RIGHT OUTER JOIN DEPARTMENTS AS D
11. ON E.DEP_ID=D.DEPT_ID_DEP AND E.SEX = 'M';
Copied!
Conclusion
Congratulations! You have completed this lab and you are ready for the next topic.
Author(s)
Lakshmi Holla
Malika Singla
Abhishek Gagneja
Changelog
about:blank 5/5