data manipulation 21 after this lecture, you should be able to: use sql select statement...
TRANSCRIPT
Data Manipulation 2 1
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
Data Manipulation 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
Data Manipulation 2 3
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) ] ;
Data Manipulation 2 4
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, cityfrom s, spwhere s.sno = sp.sno and sp.pno = ‘p4’;
sname city ----- ------ Smith London Blake Paris
Join Operation
Data Manipulation 2 5
Who supplied blue parts?
Table S Table Psno | 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 snamefrom s, sp, pwhere s.sno = sp.sno and sp.pno = p.pno and color = 'blue' ;
sname-------- Jones
Data Manipulation 2 6
List the names and the colors of the parts supplied by the suppliers located in London.
select pname, colorfrom s, p, spwhere s.sno = sp.sno and sp.pno = p.pno and s.city = ‘London’ ;
pname | color -------------- nut | red bolt | green
Table S Table Psno | 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 SPsno | pno | qty---------------s1 | p1 | 300s1 | p2 | 250s2 | p3 | 200
Data Manipulation 2 7
Aliasing of Tables
Which parts are of the same color?
select px.pno, py.pno, px.colorfrom p px, p pywhere 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 PYpno | pname | color 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
Data Manipulation 2 8
Get the (distinct) names of the cities where both blue and green parts are located?
select distinct px.cityfrom p px, p pywhere 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
Data Manipulation 2 9
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 snamefrom s, sp spx, sp spywhere s.sno = spx.sno and spx.pno = ‘p1’ and s.sno = spy.sno and spy.pno = ‘p2’;
sname ----- Jones