Professional Documents
Culture Documents
L1.1 SQL (Joins)
L1.1 SQL (Joins)
Topics to be covered
Foreign Key Basics
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:
Company
Products
Joins
Example 1:
Solution:
Example:
Example:
Person ( name, address, worksfor)
Company( name, address)
Inner Join
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
;
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 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.
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.