Download presentation
Presentation is loading. Please wait.
Published byAllan Cooper Modified over 9 years ago
2
Hassan Tariq
3
MULTIPLE TABLES: SQL provides a convenient operation to retrieve information from multiple tables.SQL provides a convenient operation to retrieve information from multiple tables. This operation is called join.This operation is called join. The join operation will combine the tables into one large table with all possible combinations (Math: Cartesian Product), and then it will filter the rows of this combined table to yield useful information.The join operation will combine the tables into one large table with all possible combinations (Math: Cartesian Product), and then it will filter the rows of this combined table to yield useful information.
4
INNER JOIN 1 LEFT OUTER JOIN 2 RIGHT OUTER JOIN 3 FULL OUTER JOIN 4
10
9 Connect two or more tables: PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi Product Company CNameStockPriceCountry GizmoWorks25USA Canon65Japan Hitachi15Japan What is the Connection between them ?
11
10 Product (pname, price, category, manufacturer) Company (cname, stockPrice, country) 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 Join between Product and Company
12
11 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi Product Company CnameStockPriceCountry GizmoWorks25USA Canon65Japan Hitachi15Japan PNamePrice SingleTouch$149.99 SELECT PName, Price FROM Product, Company WHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200
13
12 Product (pname, price, category, manufacturer) Company (cname, stockPrice, country) Find all countries that manufacture some product in the ‘Gadgets’ category. SELECT Country FROM Product, Company WHERE Manufacturer=CName AND Category=‘Gadgets’
14
13 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi Product Company CnameStockPriceCountry GizmoWorks25USA Canon65Japan Hitachi15Japan Country ?? What is the problem ? What’s the solution ? SELECT Country FROM Product, Company WHERE Manufacturer=CName AND Category=‘Gadgets’
15
NATURAL JOIN / INNER JOIN A Natural Join is a join operation that joins two tables bytheir common column. This operation is similar to the setting relation of two tables. SELECT a.comcol, a.col1, b.col2, expr1, expr2 ; FROM table1 a, table2 b ; WHERE a.comcol = b.comcol
16
NATURAL JOIN / INNER JOIN Musicid 980 1 typeStudent idnameclass Productidnameclasstype Same id Join eg. 25 Make a list of students and the instruments they learn. (Natural Join) eg. 25 Make a list of students and the instruments they learn. (Natural Join)
17
SELECT s.class, s.name, s.id, m.type ; FROM student s, music m ; WHERE s.id=m.id ORDER BY class, name NATURAL JOIN / INNER JOIN Result eg. 25 Make a list of students and the instruments they learn. (Natural Join) eg. 25 Make a list of students and the instruments they learn. (Natural Join)
18
eg. 26 Find the number of students learning piano in each class. eg. 26 Find the number of students learning piano in each class. NATURAL JOIN Three Parts : (1)Natural Join. (2)Condition: m.type="Piano" (3)GROUP BY class
19
NATURAL JOIN Music Student Product Join Condition m.type= "Piano" Group By class eg. 26 eg. 26
20
eg. 26 Find the number of students learning piano in each class. eg. 26 Find the number of students learning piano in each class. SELECT s.class, COUNT(*) ; FROM student s, music m ; WHERE s.id=m.id AND m.type="Piano" ; GROUP BY class ORDER BY class NATURAL JOIN Result
21
An Outer Join is a join operation that includes rows that have a match, plus rows that do not have a match in the other table. Used when rows from one table should be part of the result there are no related rows in a second table Direction must be specified – Left/Right specify which table has the rows which should always be included – Full specifies that rows from both tables should be included even if no match between rows
22
Left Outer Join returns all matched rows, plus all unmatched rows from the table on the left of the join clause (use nulls in fields of non-matching tuples) SELECT s.sid, s.name, r.bid FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid Returns all sailors & information on whether they have reserved boats
24
Right Outer Join returns all matched rows, plus all unmatched rows from the table on the right of the join clause SELECT r.sid, b.bid, b.name FROM Reserves r RIGHT OUTER JOIN Boats b ON r.bid = b.bid Returns all boats & information on which ones are reserved.
26
Full Outer Join returns all (matched or unmatched) rows from the tables on both sides of the join clause SELECT r.sid, b.bid, b.name FROM Reserves r FULL OUTER JOIN Boats b ON r.bid = b.bid Returns all boats & all information on reservations
27
Note: in this case it is the same as the ROJ because bid is a foreign key in reserves, so all reservations must have a corresponding tuple in boats.
28
Typically used to generate lots of data quickly Match each row from table 1 with every row from table 2 Result is (table 1 row count)*(table 2 row count) Using table list: SELECT * FROM Publishers, Titles Using CROSS JOIN keywords: SELECT * FROM Publishers CROSS JOIN Titles
29
A subquery must be enclosed in the parenthesis. A subquery must be put in the right hand of the comparison operator A subquery cannot contain a ORDER-BY clause. A query can contain more than one sub-queries.
30
3 Subquery Types 1. Single-row subquery - where the subquery returns only one row. 2. Multiple-row subquery - where the subquery returns multiple rows. 3. Multiple column subquery - where the subquery returns multiple columns. Another name for these query types is: Correlated Subquery.
31
Correlated Subqueries Are dependent on the their outer query* Will be executed many times while it’s outer queries is being processed, running once for each row selected by the outer query. Can be in the HAVING OR WHERE clauses
32
SELECT s.store_name, sl.store_sales, FROM store s, sales sl WHERE s.store_key = sl.store_key and sl.store_sales >=( SELCECT AVG(store_sales) FROM sales)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.