Download presentation
Presentation is loading. Please wait.
1
SQL: Multiple Tables CIT 381
2
Sample Schema
3
General Form
4
Naming Attributes
5
Renaming
6
FROM and Cross Product SELECT customers.company, customers.cust_num, orders.cust, orders.order_num FROM customers, orders; FROM creates the cross product of two (or more) tables. Here, every row of customers is paired with every row of orders. Note: this particular example is from the order processing database used in the SQL text. Since customers has 21 rows and orders has 30, the result of the above query returns 21*30 = 630 rows.
7
COMPANYCUST_NUMCUSTORDER_NUM AAA Associates21052111113012 AAA Associates21052108113058 AAA Associates21052107113034 AAA Associates21052102112968 AAA Associates21052120113048 AAA Associates21052103112983 AAA Associates21052103112963 AAA Associates21052108113055 AAA Associates21052118112992 AAA Associates21052124112997 AAA Associates21052118113013 AAA Associates21052118113051 AAA Associates21052112113045 AAA Associates21052101112989 AAA Associates21052111112975 AAA Associates21052106113065 AAA Associates21052103113027 AAA Associates21052114113024 AAA Associates21052124113062 AAA Associates21052118113049 AAA Associates21052103112987 AAA Associates21052114112979 AAA Associates21052106112993 AAA Associates21052117112961 AAA Associates21052111113057 AAA Associates21052112113007 AAA Associates21052113113042 AAA Associates21052107113036 AAA Associates21052109113069 AAA Associates21052108113003 Ace International21072118113051 Ace International21072118113013
8
Joins What we want to do is compute joins, matching the shared attribute in the two tables. stud_ssn in student stud_ssn in enroll This is by far the most important and common operation.
9
Computation of Joins FROM student s, enroll e drives all pairs of rows from the two tables into the condition. WHERE s.stud_ssn=e.stud_ssn picks out those pairs that match on the desired attribute. This is sometimes called a natural join.
10
Long Join Chains Find the name of all classes taken by Alice. (We must look in three tables.) SELECT c.class_dept, c.class_num, c.class_title FROM student s, enroll e, class c WHERE s.stud_name=‘Alice’ AND s.stud_ssn=e.stud_ssn AND e.class_crn=c.class_crn;
11
Joins in Access SELECT CUSTOMERS.COMPANY, CUSTOMERS.CUST_NUM, ORDERS.ORDER_NUM FROM CUSTOMERS INNER JOIN ORDERS ON CUSTOMERS.CUST_NUM = ORDERS.CUST; MS Access, while allowing the queries on previous slides, also allows for a non-standard syntax (Jet SQL). An inner join (what we’ve seen), is to be distinguished from an outer join, which we will see soon.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.