0% found this document useful (0 votes)
17 views13 pages

CH 6

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

CH 6

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

University of Somalia (UNISO)

Faculty of Engineering and Computer Science

COURSE: PHP AND MYSQL


Chapter 6: Advanced SQL and
MYSQL(Cont.’)
Eng. Ismail Mohamed Jamal
MCSE (Master of Computer Science & Engineering)
Ondokuz Mayis University – Turkiye (OMU)
Email: [email protected]

1
Objectives

• After taking this chapter you should be able to:

 Database Design

 Performing joining

 Procedures

 Triggers

2
SQL JOIN

• What is a SQL join?


• A JOIN clause is used to combine rows from two or
more tables, based on a related column between them.
• (INNER) JOIN: Returns records that have matching values in both tables

• LEFT (OUTER) JOIN: Returns all records from the left table, and the matched
records from the right table

• RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table

• FULL (OUTER) JOIN: Returns all records when there is a match in either left or
right table

3
Cont.’

4
INNER JOIN

• INNER JOIN Syntax


• SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
• Let's look at a selection from the "Orders" table:
OrderID CustomerID OrderDate

10308 2 1996-09-18

10309 37 1996-09-19

10310 77 1996-09-20

5
Cont.’

• Then, look at a selection from the "Customers" table:


CustomerID CustomerName ContactName Country

1 Ali Abdi Ali Abdi Ali Germany

2 Aisha Kamaal Hassan Jaabir Hassan Mexico

3 Adna Mahad raage Mama Asli Mexico

6
Cont.’

• Notice that the "CustomerID" column in the "Orders" table


refers to the "CustomerID" in the "Customers" table. The
relationship between the two tables above is the
"CustomerID" column.

• Then, we can create the following SQL statement (that


contains an INNER JOIN), that selects records that have
matching values in both tables:

7
Cont.’

SELECT Orders.Order_id, Customer.customer_name,


Orders.order_date

FROM Orders

INNER JOIN Customer ON Orders.customer_id=Customer.customer_id;

8
LEFT JOIN

• The LEFT JOIN keyword returns all records from the left table
(table1), and the matched records from the right table (table2).
The result is NULL from the right side, if there is no match.

• LEFT JOIN Syntax

• SELECT column_name(s)
FROM table1
LEFT JOIN table2
Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.
ON table1.column_name = table2.column_name;
9
Cont.’

SELECT Customer.Customer_name, Orders.Order_id

FROM Customer

LEFT JOIN Orders ON Customer.customer_id = Orders.customer_id;

10
RIGHT JOIN

• The RIGHT JOIN keyword returns all records from the right table
(table2), and the matched records from the left table (table1). The
result is NULL from the left side, when there is no match.

• RIGHT JOIN Syntax

• SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

11
Cont.’

SELECT Orders.Order_id, Employee.lname, Employee.fname

FROM Orders

RIGHT JOIN Employee ON Orders.employee_id =


Employee.Employee_id;

12
Any Questions?

13

You might also like