Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Manipulation 21 After this lecture, you should be able to:  Use SQL SELECT statement effectively to retrieve the data from multiple related tables.

Similar presentations


Presentation on theme: "Data Manipulation 21 After this lecture, you should be able to:  Use SQL SELECT statement effectively to retrieve the data from multiple related tables."— Presentation transcript:

1 Data Manipulation 21 After this lecture, you should be able to:  Use SQL SELECT statement effectively to retrieve the data from multiple related tables.  Join Operation  Aliasing of Tables  Brainstorm on Assignment 3 (Part 1). Data Manipulation 2

2 2 Review: Get the S#'s, cities, and statuses of the suppliers in Athens or Paris. Sort the resultant table in the descending order of statuses. sno | city | status ----------------------- s3 | Paris | 30 s5 | Athens | 30 s2 | Paris | 10 select sno, city, status from s where city in (‘Athens’, ‘Paris’) order by status desc; Table S sno | sname | status | city ----------------------------- s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris s4 | Clark | 20 | London s5 | Adams | 30 | Athens

3 Data Manipulation 23 Select Statements  Select columns – Projection  From – Database tables  Where -- Selection condition  Group By – Partitioning data into groups  Having -- Group selection condition  Order By -- Sorting Select [Distinct] column(s) From table(s) Where condition [ Group By field(s) ] [ Having condition ] [ Order By field(s) ] ;

4 Data Manipulation 24 Get the names and cities of the suppliers who supplied P4. Table S Table SP sno | sname | status | city sno | pno | qty ----------------------------- --------------- s1 | Smith | 20 | London s1 | p1 | 300 s2 | Jones | 10 | Paris s1 | p4 | 200 s3 | Blake | 30 | Paris s2 | p3 | 400 s3 | p4 | 100 select sname, city from s, sp where s.sno = sp.sno and sp.pno = ‘p4’; sname city ----- ------ Smith London Blake Paris Join Operation

5 Data Manipulation 25 Who supplied blue parts? Table S Table P sno | sname | sts | city pno | pname | color | wgt | city -------------------------- ---------------------------------- s1 | Smith | 20 | London p1 | nut | red | 12 | London s2 | Jones | 10 | Paris p2 | bolt | green | 17 | Paris p3 | screw | blue | 17 | Rome Table SP sno | pno | qty --------------- s1 | p1 | 300 s2 | p3 | 200 select sname from s, sp, p where s.sno = sp.sno and sp.pno = p.pno and color = 'blue' ; sname -------- Jones

6 Data Manipulation 26 List the names and the colors of the parts supplied by the suppliers located in London. select pname, color from s, p, sp where s.sno = sp.sno and sp.pno = p.pno and s.city = ‘London’ ; pname | color -------------- nut | red bolt | green Table S Table P sno | sname | sts | city pno | pname | color | wgt | city -------------------------- ---------------------------------- s1 | Smith | 20 | London p1 | nut | red | 12 | London s2 | Jones | 10 | Paris p2 | bolt | green | 17 | Rome p3 | screw | blue | 17 | Rome Table SP sno | pno | qty --------------- s1 | p1 | 300 s1 | p2 | 250 s2 | p3 | 200

7 Data Manipulation 27 Aliasing of Tables Which parts are of the same color? select px.pno, py.pno, px.color from p px, p py where px.color = py.color and px.pno < py.pno ; px.pno | py.pno | color ----------------------- p2 | p3 | blue p1 | p4 | red Table P pno | pname | color -------------------- p1 | nut | red p2 | screw | blue p3 | cam | blue p4 | cog | red Table P PX Table P PY pno | pname | color -------------------- p1 | nut | red p1 | nut | red p2 | screw | blue p2 | screw | blue p3 | cam | blue p3 | cam | blue p4 | cog | red p4 | cog | red

8 Data Manipulation 28 Get the (distinct) names of the cities where both blue and green parts are located? select distinct px.city from p px, p py where px.color = ‘blue’ and py.color = ‘green’ and px.city = py.city; City -------- London Rome Table P pno | pname | color | wgt | city ---------------------------------- p1 | nut | blue | 12 | London p2 | bolt | green | 17 | Rome p3 | screw | blue | 15 | Rome p4 | cog | green | 18 | London p5 | cam | red | 19 | Athens

9 Data Manipulation 29 Get the names of the suppliers who supplied p1 and p2. Table S Table SP sno | sname | status | city sno | pno | qty ----------------------------- --------------- s1 | Smith | 20 | London s1 | p1 | 300 s2 | Jones | 10 | Paris s1 | p3 | 200 s3 | Blake | 30 | Paris s2 | p1 | 400 s2 | p2 | 100 s2 | p3 | 50 s3 | p2 | 80 select sname from s, sp spx, sp spy where s.sno = spx.sno and spx.pno = ‘p1’ and s.sno = spy.sno and spy.pno = ‘p2’; sname ----- Jones


Download ppt "Data Manipulation 21 After this lecture, you should be able to:  Use SQL SELECT statement effectively to retrieve the data from multiple related tables."

Similar presentations


Ads by Google