Download presentation
Presentation is loading. Please wait.
Published byMarylou Horn Modified over 9 years ago
1
JOINS cis 407 Inner join Right and left outer join Full join Cross join
2
Cross Product P PS 101 nut 101 acme 102 bolt 101 wacky 102 acme PxPS 101 nut101 acme 101 nut 101 wacky 101 nut 102 acme 102 bolt101 acme 102 bolt 101 wacky 102 bolt 102 acme SQL: select companyName, shipCity from customers, order But only want result rows where part numbers match
3
Inner Joins (typically what is meant by join) Select * from customers c inner join [order details] od on c.customerID = od.customerID Old method Select * from customers c, [order details] od where c.customerID = od.customerID
4
Example #1 Select s.companyName, p.productName from products p join suppliers s on p.supplierid = s.supplierid where s.city = ‘city’ and p.discontinued = TRUE ‘ON’ specifies how relations joined ‘where’ constrains what is in result
5
Example #2 Select c.companyName, p.ProductName from suppliers s join products p on s.supplierID = p.supplierID join [order details] od on p.productID = od.productID join orders o on od.orderid = o.orderid join customers c on o.customerID = c.customerID where c.city = s.city Generate an equivalent English sentence
6
OUTER JOIN A left or right outerjoin is something like a combination of a join and a crossproduct. (pg 90) a left outer join includes all the information from the table on the left. There many be a lot of nulls in the result.
7
Example Select companyName, orderID from company c inner join order o on c.customer = o.customer –Only customers that have ordered products will be included in the result. Select companyName, orderID from company c left outer join order o on c.customer = o.customer –Includes all customers will null orderIDs in nothing ordered. –Outer joins useful in finding orphan records
8
Null does not match Null A value of null does not join to a value of null. Why? –x = null will not work –X is null will work Select c.customerid, companyname from customer c left outerjoin orders o on c.customerid = o.customerid where o.customerid is null
9
Other joins Full outerjoin combines left and right outerjoin Cross join is cross product. –May be useful in populating test database
10
Other examples Select * from customers c, orders o where c.customerid = o.customerid Select * from customers c, orders o where c.customerid != o.customerid
11
UNION Select companyName from customers where city = ‘NY’ union select companyName from Customers where city = ‘Pocky’ Union selects must have the same number of columns. Must be the same domain for results to make much sense. (pg 105) See example pg 106
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.