Post on 14-Jan-2016
description
transcript
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 1/45
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc !llrights reser"e#
Chapter 4
Query Formulation with SQL
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 2/45
4-2
Outline
Background
Getting started
Joining tables Summariing tables
!roblem sol"ing guidelines
#d"anced problems
$ata manipulation statements
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 3/45
4-3
%hat is SQL&
Structured Query Language
Language 'or database de'inition(
manipulation( and control
)nternational standard
Standalone and embedded usage
)ntergalactic database speak
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 4/45
4-4
SQL Statements
Statement Chapter
C*+#,+ ,#BL+ -( ./
S+L+C, -( 0( .1
)2S+*,( 3!$#,+ -( .1
$+L+,+ -( 0( .1
C*+#,+ )+% .1
C*+#,+ ,*)GG+* ..G*#2,( *+O5+ .4
CO66),( *OLLB#C5 .7
C*+#,+ ,8!+ ./
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 5/45
4-5
SQL Standardiation
*elati"ely simple standard9 SQL:/; and
re"ision <SQL:/0=
6odestly comple> standard9 SQL:0?
Comple> standards9 SQL9.000 and
SQL9?11-
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 6/45
4-6
SQL Con'ormance
2o o''icial con'ormance testing
endor claims about con'ormance
*easonable con'ormance on Core SQL Large "ariance on con'ormance outside o'
Core SQL
$i''icult to write portable SQL code outsideo' Core SQL
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 7/45
4-7
S+L+C, Statement O"er"iew
SELECT @list o' column e>pressionsA
FROM @list o' tables and oin operationsA
WHERE @list o' logical e>pressions 'or rowsA
GROUP BY @list o' grouping columnsA HAVING @list o' logical e>pressions 'or groupsA
ORDER BY @list o' sorting speci'icationsA
+>pression9 combination o' columns( constants(operators( and 'unctions
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 8/45
4-8
3ni"ersity $atabase
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 9/45
4-9
First S+L+C, +>amples
Example 1
SELECT * FROM Faculty
Example 2 (Access)
SELECT *
FROM Faculty WHERE FacSSN = '!"#$%&('
Example 3
SELECT FacF)+tNa,-. FacLa+tNa,-. FacSalay
FROM Faculty
Example 4
SELECT FacF)+tNa,-. FacLa+tNa,-. FacSalay
FROM Faculty
WHERE FacSalay / 0%%% AND FacRa12 = 'PROF'
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 10/45
4-10
3sing +>pressions
Example 5 (Access)
SELECT FacF)+tNa,-. FacLa+tNa,-. FacC)ty.
FacSalay*$3$ AS I1c-a+-4Salay.
FacH)-Dat-
FROM FacultyWHERE y-a5FacH)-Dat-6 / $&&0
Example 5 (Oracle)
SELECT FacF)+tNa,-. FacLa+tNa,-. FacC)ty.
FacSalay*$3$ AS I1c-a+-4Salay.FacH)-Dat-
FROM Faculty
WHERE t781u,9-5t78c:a5FacH)-Dat-. 'YYYY'66
/ $&&0
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 11/45
4-11
)ne>act 6atching
• Match against a pattern: LIKE peratr
• !se meta characters t speci"# patterns
$ %il&car& (' r )
$ An# single character ( r *)
Example + (Access)
SELECT *
FROM O;;-)1<
WHERE C7u+-N7 LIE 'IS*'
Example + (Oracle)
SELECT *
FROM O;;-)1<
WHERE C7u+-N7 LIE 'IS>'
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 12/45
4-12
3sing $ates
• ,ates are n-m.ers
• ,ate cnstants an& "-nctins are nt stan&ar&
Example / (Access)
SELECT FacF)+tNa,-. FacLa+tNa,-. FacH)-Dat-
FROM Faculty
WHERE FacH)-Dat- BETWEEN ?$@$@$&&&?
AND ?$#@"$@#%%%?
Example / (Oracle)SELECT FacF)+tNa,-. FacLa+tNa,-. FacH)-Dat-
FROM Faculty
WHERE FacH)-Dat- BETWEEN '$a1$&&&'
AND '"$D-c#%%%'
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 13/45
4-13
Other Single ,able +>amples
Example 0: esting "r n-ll al-es
SELECT O;;-N7. C7u+-N7
FROM O;;-)1<
WHERE FacSSN IS NULL AND O;;T-, = 'SUMMER'
AND O;;Y-a = #%%0
Example : Mixing A, an& O
SELECT O;;-N7. C7u+-N7. FacSSN
FROM O;;-)1<WHERE 5O;;T-, = 'FALL' AND O;;Y-a = #%%6
OR 5O;;T-, = 'WINTER' AND O;;Y-a = #%%06
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 14/45
4-14
Join Operator
6ost databases ha"e many tables
Combine tables using the oin operator
Speci'y matching condition Can be any comparison but usually
!5 F5 most common oin condition
*elationship diagram use'ul when combining
tables
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 15/45
4-15
Join +>ample
FacSSN FacName
...:..:.... oe
???:??:???? sue
---:--:---- sara
OfferNo FacSSN
.... ...:..:....
???? ???:??:????
---- ...:..:....
FacSSN FacName OfferNo
...:..:.... oe ....
???:??:???? sue ????
...:..:.... oe ----
Faculty
Offern!
Natural "on of Offern! an#
Faculty
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 16/45
4-16
Cross !roduct Style
• List ta.les in the 6OM cla-se
• List 7in cn&itins in the %8EE cla-se
Example 19 (Access)
SELECT O;;-N7. C7u+-N7. FacF)+tNa,-.
FacLa+tNa,-
FROM O;;-)1<. Faculty
WHERE O;;T-, = 'FALL' AND O;;Y-a = #%%AND FacRa12 = 'ASST' AND C7u+-N7 LIE 'IS*'
AND Faculty3FacSSN = O;;-)1<3FacSSN
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 17/45
4-17
Join Operator Style
• !se IE OI an& O ;e#<r&s
• 6OM cla-se cntains 7in peratins
Example 11 (Access)
SELECT O;;-N7. C7u+-N7. FacF)+tNa,-.
FacLa+tNa,-
FROM O;;-)1< INNER OIN Faculty
ON Faculty3FacSSN = O;;-)1<3FacSSN WHERE O;;T-, = 'FALL' AND O;;Y-a = #%%
AND FacRa12 = 'ASST' AND C7u+-N7 LIE 'IS*'
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 18/45
4-18
2ame Quali'ication
#mbiguous column re'erence 6ore than one table in the Duery contains a
column re'erenced in the Duery
#mbiguity determined by the Duery not thedatabase
3se column name alone i' Duery is notambiguous
Quali'y with table name i' Duery isambiguous
*eadability "ersus writability
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 19/45
4-19
Summariing ,ables
*ow summaries important 'or decision:making
tasks
*ow summary
*esult contains statistical <aggregate= 'unctions Conditions in"ol"e statistical 'unctions
SQL keywords
#ggregate 'unctions in the output list G*O3! B89 summary columns
E#)2G9 summary conditions
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 20/45
4-20
G*O3! B8 +>amples
Example 12: =r-ping n a single cl-mn
SELECT FacRa12. AVG5FacSalay6 AS A<Salay
FROM Faculty
GROUP BY FacRa12
Example 13: < an& gr-p cn&itins
SELECT St4Ma7. AVG5St4GPA6 AS A<Ga
FROM Stu4-1t
WHERE St4Cla++ IN 5'R'. 'SR'6
GROUP BY St4Ma7
HAVING AVG5St4GPA6 / "3$
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 21/45
4-21
SQL Summariation *ules
Columns in S+L+C, and G*O3! B8
S+L+C,9 non aggregate and aggregate
columns
G*O3! B89 list all non aggregate columns
%E+*+ "ersus E#)2G
*ow conditions in %E+*+
Group conditions in E#)2G
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 22/45
4-22
Summariation and Joins
• ><er"-l cm.inatin
• List 7in cn&itins in the %8EE cla-se
Example 14: List the n-m.er " st-&ents enrlle& in each "all 2993
""ering?
SELECT O;;-)1<3O;;-N7.
COUNT5*6 AS Nu,Stu4-1t+
FROM E17ll,-1t. O;;-)1<WHERE O;;-)1<3O;;-N7 = E17ll,-1t3O;;-N7
AND O;;Y-a = #%%0
GROUP BY O;;-)1<3O;;-N7
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 23/45
4-23
Conceptual +"aluation !rocess
F*O6 ,ables9
Cross !roduct and
Join Operations
G*O3!
B8&
*estriction
on %E+*+
Conditions
Sort on
G*O3! B8
Columns
Compute
#ggregates
and *educe
+ach Group
to . *ow
O*$+*
B8&
SortColumns in
O*$+* B8
.
?
-
4
;
8es
2o
8es
2o
Finish
*estriction
on E#)2G
Conditions
7
!roect
Columns in
S+L+C,
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 24/45
4-24
Conceptual +"aluation
Lessons *ow operations be'ore group operations
F*O6 and %E+*+ be'ore G*O3! B8 and
E#)2G
Check row operations 'irst
Grouping occurs only one time
3se small sample tables
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 25/45
4-25
Conceptual +"aluation !roblem
Example 15: List the n-m.er " ""erings ta-ght in 299+ .#
"ac-lt# ran; an& &epartment? Excl-&e cm.inatins " "ac-lt#
ran; an& &epartment <ith less than t< ""erings ta-ght?
SELECT FacRa12. FacD-t.
COUNT5*6 AS Nu,O;;-)1<+
FROM Faculty. O;;-)1<
WHERE O;;-)1<3FacSSN = Faculty3FacSSNAND O;;Y-a = #%%0
GROUP BY FacRa12. FacD-t
HAVING COUNT5*6 / $
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 26/45
4-26
Query Formulation !rocess
>r.lem
@tatement
>r.lem
@tatement
,ata.ase
epresentatin
,ata.ase Lang-age
@tatement
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 27/45
4-27
Critical Questions
%hat tables&
Columns in output
Conditions to test <including oin conditions=
Eow to combine the tables& 3sually oin !5 to F5
6ore comple> ways to combine
)ndi"idual rows or groups o' rows&
#ggregate 'unctions in output
Conditions with aggregate 'unctions
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 28/45
4-28
+''iciency Considerations
Little concern 'or e''iciency
)ntelligent SQL compilers
Correct and non redundant solution
2o e>tra tables
2o unnecessary grouping
3se E#)2G 'or group conditions only
Chapter / pro"ides additional tips 'or
a"oiding ine''icient S+L+C, statements
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 29/45
4-29
#d"anced !roblems
Joining multiple tables
Sel' oins
Grouping a'ter oining multiple tables
,raditional set operators
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 30/45
4-30
Joining ,hree ,ables
+>ample .;9 List Leonard inces teaching schedule in 'all?117H For each course( list the o''ering number( coursenumber( number o' units( days( location( and timeH
SELECT O;;-N7. O;;-)1<3C7u+-N7. O;;Day+.
C+U1)t+. O;;L7cat)71. O;;T),-
FROM Faculty. C7u+-. O;;-)1<
WHERE Faculty3FacSSN = O;;-)1<3FacSSN
AND O;;-)1<3C7u+-N7 = C7u+-3C7u+-N7
AND O;;Y-a = #%% AND O;;T-, = 'FALL'AND FacF)+tNa,- = 'L-71a4'
AND FacLa+tNa,- = 'V)1c-'
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 31/45
4-31
Joining Four ,ables
+>ample .9 List Bob 2orberts course schedule in spring ?11;HFor each course( list the o''ering number( course number( days(location( time( and 'aculty nameH
SELECT O;;-)1<3O;;-N7. O;;-)1<3C7u+-N7.
O;;Day+. O;;L7cat)71. O;;T),-. FacF)+tNa,-. FacLa+tNa,-
FROM Faculty. O;;-)1<. E17ll,-1t. Stu4-1t
WHERE O;;-)1<3O;;-N7 = E17ll,-1t3O;;-N7
AND Stu4-1t3St4SSN = E17ll,-1t3St4SSN
AND Faculty3FacSSN = O;;-)1<3FacSSN
AND O;;Y-a = #%%0 AND O;;T-, = 'SPRING'
AND St4F)+tNa,- = 'BOB'
AND St4La+tNa,- = 'NORBERT'
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 32/45
4-32
Sel':Join
Join a table to itsel' 3sually in"ol"e a sel':re'erencing
relationship
3se'ul to 'ind relationships among rows o'the same table Find subordinates within a preset number o'
le"els
Find subordinates within any number o' le"elsreDuires embedded SQL
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 33/45
4-33
Sel':Join +>ample
+>ample ./9 List 'aculty members who ha"e a highersalary than their super"isorH List the social securitynumber( name( and salary o' the 'aculty and super"isorH
SELECT Su93FacSSN. Su93FacLa+tNa,-. Su93FacSalay. Su3FacSSN.
Su3FacLa+tNa,-. Su3FacSalay
FROM Faculty Su9. Faculty Su
WHERE Su93FacSu-)+7 = Su3FacSSN
AND Su93FacSalay / Su3FacSalay
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 34/45
4-34
6ultiple Joins Between ,ables
+>ample .09 List the names o' 'aculty members and thecourse number 'or which the 'aculty member teaches thesame course number as his or her super"isor in ?11;H
SELECT FacF)+tNa,-. FacLa+tNa,-. O$3C7u+-N7
FROM Faculty. O;;-)1< O$. O;;-)1< O#
WHERE Faculty3FacSSN = O$3FacSSN
AND Faculty3FacSu-)+7 = O#3FacSSNAND O$3O;;Y-a = #%%0 AND O#3O;;Y-a = #%%0
AND O$3C7u+-N7 = O#3C7u+-N7
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 35/45
4-35
6ultiple Column Grouping
+>ample ?19 List the course number( the o''ering number(
and the number o' students enrolledH Only include courses
o''ered in spring ?11;H
SELECT C7u+-N7. E17ll,-1t3O;;-N7.
C7u1t5*6 AS Nu,Stu4-1t+
FROM O;;-)1<. E17ll,-1t
WHERE O;;-)1<3O;;-N7 = E17ll,-1t3O;;-N7
AND O;;Y-a = #%%0 AND O;;T-, = 'SPRING'
GROUP BY E17ll,-1t3O;;-N7. C7u+-N7
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 36/45
4-36
,raditional Set Operators
A !IO
A IE@EB
A MI!@
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 37/45
4-37
3nion Compatibility
*eDuirement 'or the traditional set
operators
Strong reDuirement
Same number o' columns
+ach corresponding column is compatible
!ositional correspondence
#pply to similar tables by remo"ing
columns 'irst
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 38/45
4-38
SQL 32)O2 +>ample
+>ample ?.9 *etrie"e basic data about all uni"ersity people
S+L+C, FacSS2 #S SS2( FacFirst2ame #S First2ame(
FacLast2ame #S Last2ame( FacCity #S City(
FacState #S State
F*O6 Faculty
32)O2
S+L+C, StdSS2 #S SS2( StdFirst2ame #S First2ame( StdLast2ame #S Last2ame( StdCity #S City(
StdState #S State F*O6 Student
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 39/45
4-39
Oracle )2,+*S+C, +>ample
+>ample ??9 Show teaching assistants( 'aculty who arestudentsH Only show the common columns in the resultH
SELECT FacSSN AS SSN. FacF)+tNa,- AS
F)+tNa,-. FacLa+tNa,- AS La+tNa,-.FacC)ty AS C)ty. FacStat- AS Stat-
FROM Faculty
INTERSECT
SELECT St4SSN AS SSN. St4F)+tNa,- AS F)+tNa,-. St4La+tNa,- AS La+tNa,-.
St4C)ty AS C)ty. St4Stat- AS Stat-
FROM Stu4-1t
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 40/45
4-40
Oracle 6)23S +>ample
+>ample ?-9 Show 'aculty who are not students <pure'aculty=H Only show the common columns in the resultH
SELECT FacSSN AS SSN. FacF)+tNa,- AS
F)+tNa,-. FacLa+tNa,- AS La+tNa,-.FacC)ty AS C)ty. FacStat- AS Stat-
FROM Faculty
MINUS
SELECT St4SSN AS SSN. St4F)+tNa,- AS F)+tNa,-. St4La+tNa,- AS La+tNa,-.
St4C)ty AS C)ty. St4Stat- AS Stat-
FROM Stu4-1t
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 41/45
4-41
$ata 6anipulation Statements
)2S+*,9 adds one or more rows
3!$#,+9 modi'ies one or more rows
$+L+,+9 remo"es one or more rows
3se S+L+C, statement to )2S+*,
multiple rows
3!$#,+ and $+L+,+ can use a %E+*+clause
2ot as widely used as S+L+C, statement
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 42/45
4-42
)2S+*, +>ample
+>ample ?49 )nsert a row into the Student table supplying
"alues 'or all columnsH
INSERT INTO Stu4-1t5St4SSN. St4F)+tNa,-. St4La+tNa,-.
St4C)ty. St4Stat-. St4). St4Cla++.
St4Ma7. St4GPA6
VALUES5'&&&&&&&&&'.'OE'.'STUDENT'.'SEATAC'.
'WA'.'&%!#$$#$'.'FR'.'IS'. %3%6
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 43/45
4-43
3!$#,+ +>ample
+>ample ?79 Change the maor and class o'Eomer %ellsH
UPDATE Stu4-1tSET St4Ma7 = 'ACCT'.
St4Cla++ = 'SO'
WHERE St4F)+tNa,- = 'HOMER'AND St4La+tNa,- = 'WELLS'
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 44/45
4-44
$+L+,+ +>ample
+>ample ?;9 $elete all )S maors who are seniorsH
DELETE FROM Stu4-1t
WHERE St4Ma7 = 'IS'
AND St4Cla++ = 'SR'
7/18/2019 Chapter04 Rev
http://slidepdf.com/reader/full/chapter04-rev 45/45
Summary
SQL is a broad language
S+L+C, statement is comple>
3se problem sol"ing guidelines
Lots o' practice to master Duery
'ormulation and SQL