0% found this document useful (0 votes)
40 views

Understanding JOINS - Lab

Uploaded by

arnela.sokolic1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Understanding JOINS - Lab

Uploaded by

arnela.sokolic1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

IT 101 - Database Design and Implementation

9. Understanding Joins

9. Understanding Joins
In MySQL, the JOIN statement is a technique of connecting data between several tables in a
database based on the values of common fields in those tables. The same column name and data
type are generally present in the tables being linked as common values (primary and foreign keys).

Types of Joins:

● INNER JOIN – returns only matching rows


● OUTER JOIN – returns matching the unmatched rows
● CROSS JOIN – returns all rows from the LeftTable combined with all rows from the
RightTable.
● Self Join

From this lab, for the practical part, we will work with sakila, employee and classic models
databases.

To import the employee database, you first need to download the file from the link provided. Then
you should unzip the file. After that open the employees.sql file, and modify this source part by
providing the full path to that file.

So, after changing the source part and adding the file path to this part of code, you need to save all
changes and then open MySql Command Client Line.
IT 101 - Database Design and Implementation
9. Understanding Joins

In MySQL Command Client Line you should type this: source yourpathtofile/employees.sql;

Don’t forget to change the path to your employees.sql file. After this the database should be
successfully imported.

INNER JOIN
Only common matched records are retrieved using INNER JOINS. The INNER JOIN clause
restricts records retrieval from Table A and Table B to those that satisfy the join requirement. It is
the most often used JOIN type.

SELECT *
FROM products p
INNER JOIN productlines p2
ON p.productLine = p2.productLine
IT 101 - Database Design and Implementation
9. Understanding Joins

LEFT JOIN

LEFT JOINS allow you to get all entries from Table A and those from Table B that meet the join
criteria. NULL values are displayed for records from Table A that do not fit the criteria.

SELECT *
FROM products p
LEFT JOIN productlines p2
ON p.productLine = p2.productLine

RIGHT JOIN

RIGHT JOINS allow you to get all entries from Table B and those from Table A that meet the join
criteria. The NULL values are presented for the records from Table B that do not fit the criteria.
IT 101 - Database Design and Implementation
9. Understanding Joins

SELECT *
FROM products p
RIGHT JOIN productlines p2
ON p.productLine = p2.productLine

CROSS JOIN

The MySQL CROSS JOIN, commonly known as a cartesian join, returns all possible row
combinations from each table. If no extra condition is provided, the result set is obtained by
multiplying each row of table A with all rows in table B.
IT 101 - Database Design and Implementation
9. Understanding Joins

SELECT *
FROM products p
CROSS JOIN productlines p2

SELF JOIN
A self join allows a table to be joined to itself, enabling comparisons between rows within the same
table. This facilitates the retrieval of related records or hierarchical data within a single dataset.
IT 101 - Database Design and Implementation
9. Understanding Joins

SELECT e1.employeeNumber AS employeeNumber,


e1.lastName AS lastName,
e1.firstName AS firstName,
e1.jobTitle AS jobTitle,
e2.employeeNumber AS managerEmployeeNumber,
e2.lastName AS managerLastName,
e2.firstName AS managerFirstName,
e2.jobTitle AS managerJobTitle
FROM employees e1
JOIN employees e2 ON e1.reportsTo = e2.employeeNumber;

Why are Joins useful?

● JOINS allow you to get data from two or more linked database tables. They are very
valuable since they save time over running queries one by one to achieve the same results.

● MySQL is more efficient, meaning when using JOINS, MySQL performs better since joins
are performed via indexing.

● Using JOINS lowers server load. Because you execute one query, you get better and faster
results.
IT 101 - Database Design and Implementation
9. Understanding Joins

TASKS:

For the first 2 tasks use classic models database.

1. Retrieve a list of customers whose city is Madrid along with details of their orders. Include
customer information such as customer name as name, and city, along with order details
such as order date, total amount, and status. Display the results ordered by the order date
in descending order where order status is ‘In Process’.

2. Retrieve orders for customer Kelvin where the quantity of a specific product in the order is
50 or more, displaying the product name, customer's first and last name, order number,
order date, amount paid, quantity ordered, and price each, arranging the results in
descending order based on the order date.

3. For this task you should import the sakila database. Retrieve all customers whose city is
Abu Dhabi. ( note: you should get city from city table)
IT 101 - Database Design and Implementation
9. Understanding Joins

4. Retrieve all female Senior Engineer employees that have salaries bigger than 135000. For
this task you should use the employees.sql database and you are required to use the
keyword ‘using’ when joining tables.

You might also like