{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chap8 - Chapter 8:Advanced SQL DML(Multiple Tables Jinie...

Info icon This preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 8:Advanced SQL DML (Multiple Tables) Jinie Pak Department of Information Systems UMBC
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Review Last class, we covered DML operations in a single table In the real world, you hardly work with a single table
Image of page 2
Learning Objectives Use the following SQL commands to manipulate multiple tables: UNION, DIFFERENCE, INTERSECT JOIN Learn the basics of nested query Use IN and EXISTS to perform nested queries Understand the difference between correlated and uncorrelated queries
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CASE: Video Shop CID CNAME CITY C1 C2 C3 C4 C5 Smith Jones Blake Clark Adams London Paris Paris London Athens FID FNAME PRICE F1 F2 F3 F4 F5 F6 Jurassic Park III The Others Senseless Dragon Heart 007 America Sweetheart 2.99 3.55 5.99 6.00 5.00 0.59 TYPE Action Horror Comedy Drama Action Comedy CID C1 C1 C1 C1 C1 C1 C2 C2 C3 C4 C4 C4 FID F1 F2 F3 F4 F5 F6 F1 F2 F2 F2 F4 F5 Date 1/3 10/2 9/4 1/2 1/1 8/1 1/3 1/4 12/2 1/2 1/3 11/4 FILM CUSTOMER RENT
Image of page 4
DML: PRODUCT Product: SQL SELECT * FROM A,B A B C 1 y $ 2 x % 3 y & D E “Tom” 3 “Eric” 1 X A B C D E 1 y $ “Tom” 3 2 x % “Tom” 3 3 y & “Tom” 3 1 y $ “Eric” 1 2 x % “Eric” 1 3 y & “Eric” 1 Table A Table B
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
DML: JOIN Equal Join SQL SELECT * FROM A,B WHERE A.a = B.e A B C 1 y $ 2 x % 3 y & D E “Tom” 3 “Eric” 1 X Table A Table B A B C D E 1 y $ “Tom” 3 2 x % “Tom” 3 3 y & “Tom” 3 1 y $ “Eric” 1 2 x % “Eric” 1 3 y & “Eric” 1
Image of page 6
DML: JOIN Join (Natural Join) SQL SELECT A.a, A.b, A.c, B.d FROM A,B WHERE A.a = B.e A B C 1 y $ 2 x % 3 y & D E “Tom” 3 “Eric” 1 X A B C D Table A Table B 3 y & “Tom” 1 y $ “Eric”
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
JOIN: SELECT Q 1. List info. about customers who rented F1 before SQL: SELECT * FROM CUSTOMER, RENT WHERE CID = CID AND FID = ‘F1’; ERROR at line 3: ORA-00918: column ambiguously defined
Image of page 8
JOIN: SELECT SQL: SELECT * FROM CUSTOMER, RENT WHERE CID = CID AND FID = ‘F1’; Attributes names must be qualified when confusion rises Whose attributes? Whose attributes?
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
JOIN: SELECT SQL: SELECT CUSTOMER.* FROM CUSTOMER, RENT WHERE CUSTOMER.CID = RENT.CID AND FID = ‘F1’; Do we need to specify FID?
Image of page 10
Practice: JOIN Q2. List name of films that were rented before Oct. 2009 SQL: FNAME -------------------- Beautiful Mind Pay it forward Chicken Run Beautiful Mind Green Miles Green Miles Green Miles E.T. Pay it forward
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Practice: JOIN Q2. List name of films that were rented before Oct. 2009 SQL: SELECT FILM.FNAME FROM FILM, RENT WHERE FILM.fid = RENT.fid AND rent_day < ‘1-OCT-2009’; FNAME -------------------- Beautiful Mind Pay it forward Chicken Run Beautiful Mind Green Miles Green Miles Green Miles E.T. Pay it forward
Image of page 12
JOIN: FROM Table Alias: To save time, we can assign aliases to tables: SELECT DISTINCT f .FNAME FROM FILM f , RENT r WHERE f.fid = r.fid AND rent_day < ‘1-OCT-2001’; Difference between table alias and attribute alias: Table alias can be used anywhere in the query to replace the original table names!
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Figure 8-2 Visualization of different join types with results returned in shaded area
Image of page 14
Processing Multiple Tables– Joins Join
Image of page 15

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern