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

Joins-SQL

Uploaded by

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

Joins-SQL

Uploaded by

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

Lecturer : Damien Kettle Module : Database Administration

INNER JOIN
The INNER JOIN keyword return rows when there is at least one match in both tables.

SQL INNER JOIN Syntax


SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name

PS: INNER JOIN is the same as JOIN.

Inner Join Example


Create below tables on your Student Database using below DDL Statements:

CREATE TABLE Customers(


CustomerId int NOT NULL,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
City varchar(20) NOT NULL,
Country varchar(20) NOT NULL,
CONSTRAINT PK_Customers PRIMARY KEY (CustomerId))

CREATE TABLE Orders(


OrderId int NOT NULL,
CustomerId int NOT NULL,
Product varchar(50) NOT NULL,
Quantity int NOT NULL,
CONSTRAINT PK_Orders PRIMARY KEY (OrderId))

Insert data into the table using below INSERT Statements:

INSERT INTO Customers


(CustomerId, FirstName, LastName, City, Country)
VALUES
(1, 'Mark', 'Sheridan', 'Dublin', 'Ireland'),
(2, 'Joe', 'Dunphy', 'Galway', 'Ireland'),
(3, 'Ken', 'Moran', 'Cork', 'Ireland'),
(4, 'Philip', 'Moy', 'Dublin', 'Ireland'),
(5, 'Pat', 'Walsh', 'Waterford', 'Ireland')

INSERT INTO Orders


(OrderId, CustomerId, Product, Quantity)
VALUES
(1, 1, 'USB', 5),
(2, 2, 'Keyboard', 10),
(3, 1, 'Monitor', 10),
(4, 3, 'USB', 2),
(5, 9, 'USB', 2)
Lecturer : Damien Kettle Module : Database Administration

Customers Table:

Orders Table:

Now we want to only the customers with orders.

Construct and execute the following SELECT statement:

SELECT C.FirstName,
C.LastName,
O.OrderId,
O.Product
FROM
Customers C
INNER JOIN
Orders O
ON
C.CustomerId = O.CustomerId

You should get below result set:

The INNER JOIN keyword return rows when there is at least one match in both tables. If there are
rows in "Customers" that do not have matches in "Orders", those rows will NOT be listed.
Lecturer : Damien Kettle Module : Database Administration

LEFT JOIN

The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there
are no matches in the right table (table_name2).

SQL LEFT JOIN Syntax


SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

PS: In some databases LEFT JOIN is called LEFT OUTER JOIN.

Left Join Example


We want to list all customers whether they have an order or not.

Construct and execute below SQL statement:

SELECT C.FirstName,
C.LastName,
O.OrderId,
O.Product
FROM
Customers C
LEFT JOIN
Orders O
ON
C.CustomerId = O.CustomerId

You should get below result set:

The LEFT JOIN keyword returns all the rows from the left table (Customers), even if there are no
matches in the right table (Orders).
Lecturer : Damien Kettle Module : Database Administration

RIGHT JOIN

The RIGHT JOIN keyword returns all the rows from the right table (table_name2), even if
there are no matches in the left table (table_name1).

SQL RIGHT JOIN Syntax


SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

PS: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.

Right Join Example


We want to list all orders whether there customer exists in the Customers table or not.

Construct and execute below SQL statement:

SELECT C.FirstName,
C.LastName,
O.OrderId,
O.Product
FROM
Customers C
RIGHT JOIN
Orders O
ON
C.CustomerId = O.CustomerId

You should get below result set:


Lecturer : Damien Kettle Module : Database Administration

FULL JOIN
A FULL OUTER JOIN is neither "left" nor "right"— it's both! It includes all the rows from both of the
tables or result sets participating in the JOIN. When no matching rows exist for rows on the "left"
side of the JOIN, you see Null values from the result set on the "right." Conversely, when no
matching rows exist for rows on the "right" side of the JOIN, you see Null values from the result set
on the "left."

SQL FULL JOIN Syntax


SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name

Full Join Example


Now we want to list all the Customers who have Orders, and all the Orders whether they have a
Customer associated with them.

Construct and execute below SQL statement:

SELECT C.FirstName,
C.LastName,
O.OrderId,
O.Product
FROM
Customers C
FULL OUTER JOIN
Orders O
ON
C.CustomerId = O.CustomerId

You should get below result set:

You might also like