Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

SQL

Topics to be covered
 Foreign Key Basics

 Multiple table query (Joins)


◎ Quality assurance and standards
◎ Quality planning and control
Foreign Key Basics
Foreign Key Constraints
 Suppose we have the following schema:

Students (sid: varchar(5), name : string ,gpa : float)


Enrolled (Student__________id: varchar(5),cid:varchar(5), grade :
string)

 And we want to impose the following constraint:


“A student must appear in the Students table to enroll in a class"

Students Enrolled
sid name gpa Studentid cid grade
Foreign Key?
88999 Bob 3.2 77888 234 A
77888 Mary 3.8 77888 235 B
Declaring Foreign Keys
Example:

Students (sid: varchar(5), name : string ,gpa : float)


Enrolled (Student__________id: varchar(5),cid:varchar(5), grade :
string)

CREATE TABLE Enrolled (


student_id CHAR(20),
cid CHAR(20),
grade CHAR(10),
PRIMARY KEY (student_id, cid),
FOREIGN KEY (student_id) REFERENCES Students(sid)
)
Foreign Keys and Update Operations

Students (sid: varchar(5), name : string ,gpa : float)


Enrolled (Student__________id: varchar(5),cid:varchar(5), grade :
string)

 What if we insert a tuple into Enrolled with no corresponding student?


 INSERT is rejected (foreign keys are constraints)!

 What if we delete a student?


SQL Joins
SQL
Example: Company and Products table

Company

Products
Joins
Example 1:

Product (PName, Price, Category, Manufacturer)


Company (CName, StockPrice, Country)

 Ex: Find all products under $200 manufactured in Japan;


return their names and prices.
Joins
Product(PName, Price, Category, Manufacturer)
Company(CName, StockPrice, Country)

 Ex: Find all products under $200 manufactured in Japan;


return their names and prices.

Solution:

SELECT PName, Price


FROM Product, Company
WHERE Manufacturer = CName
AND Country=‘Japan’
AND Price <= 200
Joins
Product(PName, Price, Category, Manufacturer)
Company(CName, StockPrice, Country)

 Ex: Find all products under $200 manufactured in Japan;


return their names and prices.

SELECT PName, Price


FROM Product, Company
WHERE Manufacturer = CName
AND Country=‘Japan’
AND Price <= 200

◎ A join between tables returns all unique combinations of their tuples


which meet some specified join condition
Tuple Variable Ambiguity in Multitable

Example:

Person ( name, address, worksfor)


Company( name, address)

Which address are


we referring to?

SELECT DISTINCT name, address


FROM Person, Company
WHERE worksfor = name
Which name are
we referring to?
Tuple Variable Ambiguity in Multitable

Example:
Person ( name, address, worksfor)
Company( name, address)

SELECT DISTINCT Person.name, Person . address


FROM Person, Company
WHERE Person.worksfor = Company.name

Both equivalent ways


to solve the variable
ambiguity problem
SELECT DISTINCT p.name, p.address
FROM Person p, Company c
WHERE p.worksfor = c.name
Joins
There are two type of Joins:

 Inner Join

 Outer Join – (Includes: Left, Right, and Full Outer joins.)


Joins: Inner Join
INNER Join
 The join operation combines data from two tables by forming pairs of
related rows where the matching columns in each table have the same
value.
 If one row of a table is unmatched, the row is omitted from the result
table.
 If we want to include unmatched rows we use outerjoin.
 The Outer join retains rows that do not satisfy the join condition.
 There are three types of Outer join: Left, Right, and Full Outer joins.
Joins: Inner Join
INNER JOIN
The INNER JOIN keyword selects records that have matching values in
both tables.

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

By default, joins are INNER JOINS


Joins
Equivalents of writing basic joins
Product(PName, Price, Category, Manufacturer)
Company(CName, StockPrice, Country)

SELECT PName, Price


FROM Product, Company
WHERE Manufacturer = CName
AND Country=‘Japan’
AND Price <= 200

By default, joins are INNER JOINS


Equivalent to:
SELECT PName, Price
FROM Product
JOIN Company ON Manufacturer = Cname
AND Country=‘Japan’
WHERE Price <= 200
Example of simple join operation

SELECT b.*, p.*


FROM Branch1 b, PropertyForRent1 p
WHERE b.bCity = p.pCity;

;
Resulting table:
Joins
Left Outer join
 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.

Syntax

SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
Joins
Left Outer join
Example: List all branch offices and any properties that are in the same
city.

SELECT b.*, p.*


FROM Branch b
LEFT JOIN PropertyForRent p ON b.bCity = p.pCity;
Joins
Right outer join
 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
Syntax

SELECT column_name(s)
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
Joins
Right outer join
Example: List all properties and any branch offices that are in the same
city.

SELECT b.*, p.*


FROM Branch b
RIGHT JOIN PropertyForRent p ON b.bCity = p.pCity;
Joins
Full outer join
 Returns records when there is a match in either the left or right table

Syntax:

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2 ON table1.column_name =
table2.column_name;
Joins
Full outer join
 Returns records when there is a match in either the left or right table

Example: List the branch offices and properties that are in the same city
along with any unmatched branches or properties.

SELECT b.*, p.*


FROM Branch1 b ,
FULL JOIN PropertyForRent1 p ON b.bCity = p.pCity;
Sorting the Joins
How to sort joins
Example:
 For each branch office, list the numbers and names of staff who
manage properties and the properties that they manage.

SELECT s.branchNo, s.staffNo, fName, lName, propertyNo


FROM Staff s, PropertyForRent p
WHERE s.staffNo = p.staffNo
ORDER BY s.branchNo, s.staffNo, propertyNo;

You might also like