Lecture 7-SQL-DML Part 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

SQL

Data Manipulation Language

DML

Part 3
Joins
A relational database consists of multiple related tables linked together using
common columns which are known as foreign key columns.
Because of this, data in each table is incomplete from the business perspective.

Neamat El Tazi 2
Joins
To get complete orders’ information, you need to query data from both orders and
orderdetails tables.
A join is a method of linking data between one (self-join) or more tables based on
values of the common column between the tables.

Neamat El Tazi 3
Join Types
There are four types of Joins:

1. Inner join
2. Left Outer join
3. Right Outer join
4. Cross join
To join tables, you use the cross join, inner join, left join, or right join clause for the
corresponding type of join.
The join clause is used in the SELECT statement appeared after the FROM clause.

Neamat El Tazi 4
Joins Examples
Members
Table

Movies
Table

Neamat El Tazi 5
INNER JOIN
The inner JOIN is used to return rows from both tables that satisfy the given
condition.
Suppose, you want to get list of members who have rented movies together with
titles of movies rented by them. You can simply use an INNER JOIN for that, which
returns rows from both tables that satisfy the given conditions.

Neamat El Tazi 6
Joins Examples
Members
Table

Movies
Table

Neamat El Tazi 7
INNER JOIN

Neamat El Tazi 8
INNER JOIN

Neamat El Tazi 9
Example of the Sample Database
Who is the sales representative employee of each
customer?

select customerNumber, customerName,


employees.firstName as “Sales Rep Name”

from customers, employees

where customers.salesRepEmployeeNumber =
employees.employeeNumber;

Neamat El Tazi 10
LEFT JOIN
It can detect records having no match in joined table.
It returns NULL values for records of joined table if no match is
found.

Neamat El Tazi 11
LEFT JOIN
Assume now you want to get titles of all movies together with names of members who
have rented them. It is clear that some movies have not been rented by anyone.

We can simply use LEFT JOIN for this purpose.

Neamat El Tazi 12
LEFT JOIN
The LEFT JOIN returns all the rows from the table on the left even if no matching rows
have been found in the table on the right.
Where no matches have been found in the table on the right, NULL is returned.

Neamat El Tazi 13
Joins Examples
Members
Table

Movies
Table

Neamat El Tazi 14
LEFT JOIN

Neamat El Tazi 15
RIGHT JOIN
RIGHT JOIN is obviously the opposite of LEFT JOIN.

The RIGHT JOIN returns all the columns from the table on the right even if no
matching rows have been found in the table on the left.

Where no matches have been found in the table on the left, NULL is returned.

Neamat El Tazi 16
RIGHT JOIN
In our example, let's assume that you need to get names of members and
movies rented by them.
Now we have a new member who has not rented any movie yet.

Neamat El Tazi 17
What is the Difference Between
These Two RIGHT JOINs?

Neamat El Tazi 18
RIGHT JOIN

Neamat El Tazi 19
RIGHT JOIN

Neamat El Tazi 20
Full Join (Full Outer Join)
A full outer join is a method of combining tables so that the result includes
unmatched rows of both tables.

SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Full Join (Full Outer Join)
CROSS JOIN
Cross JOIN is a simplest form of JOINs which matches each row from one
database table to all rows of another.

In other words, it gives us combinations of each row of first table with all
records in second table.

Neamat El Tazi 23
CROSS JOIN
Suppose we want to get all member records against all the movie records, we can use
the script shown below to get our desired results.

Neamat El Tazi 24
Joins Examples
Members
Table

Movies
Table

Neamat El Tazi 25
CROSS JOIN

Neamat El Tazi 26
JOINS SUMMARY
• JOINS allow us to combine data from more than one table into a single result set.

• INNER JOINS only return rows that meet the given criteria.

• OUTER JOINS (Left join and Right join) can also return rows where no matches
have been found. The unmatched rows are returned with the NULL keyword.

• The major JOIN types include Inner, Left join, Right join and Cross JOINS.

Neamat El Tazi 27
Set Operations: Difference
Get the students who didn’t register in a course

SELECT *
FROM Student

WHERE Student.SSN not in

(SELECT SSN from Registered)

28
Set Operations: Intersect
Get SSN for students registered in subjects from CS and subjects from IS together

Select Registered.SSN
from Registered
where registered.CrsCode like 'CS%'

intersect

select Registered.SSN
from Registered
where registered.CrsCode like 'IS%'

29
Set Operations: Union
Get students names in CS department and all student names who registered in CS courses

SELECT Student.Name
FROM Student , Registered
WHERE Student.SSN=registered.SSN
AND Registered.Crscode LIKE 'CS%‘

UNION

SELECT Student.Name
FROM Student
WHERE Student.Major = 'CS'

30
Questions
Search and return all order numbers with their products numbers and names.
Try it as Cross join and Inner join and see the difference.

Inner Join:

Select orderdetails.orderNumber,
products.productCode, products.productName
From products, orderdetails
Where products. productCode =
orderdetails.productCode

Neamat El Tazi 31
32
Other Questions for review
1. Get order numbers with their products and the ordered quantities
2. Get employees names working in San Francisco
3. Return all employees names that serve customers in San Francisco
4. Return the names of the customers who paid more than 100,000 in any check
5. Return all customers names and put their amount of payment if they paid more than 100,000. If
they didn't pay it, just put their name
6. Return all employees names that worked with customers who paid more than 100,000 in any
check. (Hint: Join 3 tables)
7. Retrieve each employee with his manager’s name. (Hint: Self Join)

Neamat El Tazi 33
Review Questions
1. Get order numbers with their products and the ordered quantities

select orderNumber, orderdetails.quantityOrdered, products.productName

from orderdetails, products

where orderdetails.productCode = products.productCode;

Neamat El Tazi 34
Review Questions
2. Get employees names working in San Francisco

select firstName, lastName, city

from employees inner join offices

on employees.officeCode = offices.officeCode

where city = "San Francisco";

Neamat El Tazi 35
Review Questions
3. Return all employees names that serve customers in San Francisco

select employees.firstName, employees.lastName

from employees Inner join customers

on customers.salesRepEmployeeNumber = employees.employeeNumber

where customers.city = "San Francisco";

Neamat El Tazi 36
Review Questions
4. Return the names of the customers who paid more than 100,000 in any check

select customerName, amount

from customers, payments

where customers.customerNumber = payments.customerNumber

and amount>100000;

Neamat El Tazi 37
Review Questions
5. Return all customers names and put their amount of payment if they paid more than 100,000. If they
didn't pay it, just put their name

select customerName, amount

from customers left join payments

on customers.customerNumber = payments.customerNumber

where amount>100000

Neamat El Tazi 38
Review Questions
6. Return all employees names that worked with customers who paid more than 100,000 in any
check. (Hint: Join 3 tables)
select customerName, amount

from employees, customers, payments

where employees. employeeNumber =


customers.salesRepEmployeeNumber And

customers.customerNumber = payments.customerNumber

and amount>100000;

Remember you need (number of tables -1) join conditions

39
Review Questions
7. Retrieve each employee name and number with his manager’s name. (Hint: Self Join)

Select E.employeeNumber, E.firstName, M.firstName

From employees as E, employees as M

Where E.reportsTo = M.employeeNumber;

40
Any Questions?

41

You might also like