Vous êtes sur la page 1sur 24

SQL THROUGH SQL * PLUS

SQL THROUGH SQL * PLUS

Objectives
At the end of this unit, you will be able to: Lea n to w ite and use a wide !a iety of SQL and SQL*Plus "o##ands alon$ with "onditions and ules of synta%& Unde stand the 'u 'ose of ea"h SQL and SQL* Plus "o##and&

T y out e%a#'les usin$ SQL and SQL*Plus "o##ands&

Topics
& (ata et ie!al state#ents&

SQL THROUGH SQL * PLUS

Data Retrieval Statements


(ata et ie!al state#ents a e f e*uently used to e%t a"t e"o ds f o# the database& A !a iety of S+L+,T state#ents "an be "onst u"ted to et ie!e and dis'lay e"o ds f o# one o #o e tables& SELECT Statement Statement Purpose Syntax S+L+,T Pe fo #s a *ue y and sele"ts ows and "olu#ns f o# one o #o e tables o !iews S+L+,T-ALL . (/ST/0,T1 2-table&1* . e%' -alias1,34 5RO6 table -alias1-,table-alias11&& -7H+R+ "ondition1 -,O00+,T 89 "ondition -START 7/TH "ondition11 -GROUP 89 2e%' -,e%' 13-HA:/0G "ondition11 -OR(+R 89 2e%' . 'os 4-AS, . (+S,1 -,2e%' . 'os4 -AS, . (+S,1131 -2U0/O0 . /0T+RS+,T . 6/0US 4 *ue y 1 -5OR UP(AT+ O5 "olu#n -,"olu#n13-0O7A/T11 s'e"ifies the "olu#ns, e%' essions, o "onstants to et ie!e& #a;es S+L+,T dis'lay all ows ' odu"ed by the *ue y& #a;es S+L+,T only du'li"ate ows& #a;es S+L+,T dis'lay all "olu#ns of the table<s= s'e"ified by 5RO6& s'e"ifies the table o !iew to be sele"ted f o#& s'e"ifies a "ondition to be used to sele"t ows and o >oin tables& s'e"ifies that ows a e to be et ie!ed in a t ee st u"tu e o de & identifies the ow<s= to be used as the oot<s= of the t ee& dis'lay su##a y info #ation about $ ou's of ows that has sa#e !alue in one o #o e fields

Where SELECT ALL D ST !CT " #RO$ table W%ERE CO!!ECT &' START W T% (RO)P &'

SQL THROUGH SQL * PLUS

)! O! ORDER &'

"o#bines the esults of two S+L+,T "o##ands into a sin$le esult& s'e"ifies o de fo et ie!al de'endin$ on e%' ession o 'osition of "olu#n&

#OR )PDATE O# lo";s the sele"ted ows of the table& !OWA T #a;es S+L+,T te #inate athe than wait&

Example S*L CO$$A!D Result

To dis'lay the na#e of all de'a t#ents& SQL@S+L+,T dna#e 5RO6 de'tA (0A6+ BBBBBBBBBBBBBBB A,,OU0TS R+S+AR,H SAL+S OP+RAT/O0S +(U,AT/O0 R+S+AR,H C ows sele"ted&

Example S*L Comman+ Result

To dis'lay U0/QU+ (+PART6+0TS&

de'a t#ent

na#e

unde

headin$

SQL@S+L+,T (/ST/0,T dna#e D(+PART6+0TSE 5RO6 de'tA (+PART6+0TS BBBBBBBBBBBBBBBBBBBBBBB A,,OU0TS +(U,AT/O0 OP+RAT/O0S R+S+AR,H SAL+S F ows sele"ted

Example S*L Comman+

To dis'lay all data f o# table SQL@S+L+,T * 5RO6 e#'A


F

SQL THROUGH SQL * PLUS

Result +6P0O +0A6+ BBBBBBBBBBBB BBBBBBBBBBB H?CI S6/TH HFII ASHA HL)1 7AS/6 HLCC RAGA HCLF 6AR/A HCIM 8/LL HHM) SUG/T HHMM SU0/L HM?I ,HR/S HMFF T/0A HMHC AG/T HIKK GA,J HIK) A0/L HI?F 6/LA0 1F ows sele"ted& 7H+R+ "lause o'tion of S+L+,T Statement Purpose SELECT ,--W%ERE The 7H+R+ "lause in the S+L+,T state#ent is used to et ie!e set of e"o ds de'endin$ on the sea "h "ondition& The sea "h "ondition "o#' ises of: B ,olu#n na#e o e%' ession o "onstant B ,o#'a ison o'e ato B +%' ession o "olu#n na#e o "onstant Syntax S+L+,T -ALL . (/ST/0,T 1 2table&1* . e%' ession -alias1,34 5RO6 table-alias1 -,table-alias11&& -7H+R+ "ondition1 The followin$ se"tion $i!es the list of !a ious o'e ato s that a e used in the sea "h "ondition of 7H+R+ "lause of S+L+,T state#ent& GO8 BBBBBBBBBBBBBBBBBB ,L+RJ SAL+S6A0 SAL+S6A0 6A0AG+R SAL+S6A0 6A0AG+R 6A0AG+R A0AL9ST PR+S/(+0T SAL+S6A0 ,L+RJ ,L+RJ A0AL9ST ,L+RJ 6GR H/R+(AT+ SAL ,O66 (+PT0O BBBBBBBB BBBBBBBBBBBBBBBBB BBBBBBBBB BBBBBBBBBBB BBBBBBBBBBB HIK) 1HB(+,BIL MKK )K HCIM )KB5+8BIL 1CKK ?KK ?K HCIM ))B5+8BIL 1)LK LKK ?K HM?I K)BAPRBIL )IHL )K HCIM )MBS+PBIL 1)LK 1FKK ?K HM?I K1B6A9BIL )MLK ?K HM?I KIBGU0BIL )FLK 1K HLCC KIB(+,BIC ?KKK )K 1HB0O:BIL LKKK )K HCIM KMBS+PBIL 1LKK ?K HHMM 1)BGA0BIC 11KK )K HCIM K?B(+,BIL ILK ?K HLCC K?B(+,BIL ?KKK )K HHM) )?BGA0BIC 1?KK 1K

SQL THROUGH SQL * PLUS

Comparison Operator The "o#'a ison o'e ato s used in the sea "h "ondition of 7H+R+ "lause a e $i!en below Operator . / . 01 2. 1 1. 0 0. &ETWEE!,A!D, !OT &ETWEE!,A!D, !3list4 !OT ! 3list4 S !)LL S !OT !)LL L 5E !OT L 5E Example S*L Comman+ Description +*ual to 0ot e*ual to G eate than G eate than o e*ual to Less than Less than o e*ual to 8etween two !alues 0ot between two !alues +*ual to any one of the !alues in the list 0ot e*ual to any one of the !alues in the list /s a null !alue /s not a null !alue 6at"h a "ha a"te 'atte n usin$ wild"a d "ha a"te s (oes not #at"h a "ha a"te 'atte n usin$ wild"a d "ha a"te s

To dis'lay e#'loyee na#e, sal, >ob and de'tno fo all e#'loyees in de'a t#ent ?K& SQL@S+L+,T ena#e,sal,>ob,de'tno ) 5RO6 e#' ? 7H+R+ de'tnoN?KA +0A6+ BBBBBBBBBBBBB ASHA 7AS/6 6AR/A 8/LL T/0A GA,J SAL BBBBBBBB 1CKK 1)LK 1)LK )MLK 1LKK ILK GO8 (+PT0O BBBBBBBBBBBBBBBBBB BBBBBBBBBBBB SAL+S6A0 ?K SAL+S6A0 ?K SAL+S6A0 ?K 6A0AG+R ?K SAL+S6A0 ?K ,L+RJ ?K

Result

C ows sele"ted&

SQL THROUGH SQL * PLUS

Logical Operators The lo$i"al o'e ato s used to "o#bine se!e al sea "h "onditions& Operator A!D OR !OT Example Description (is'lays ow only if both "onditions a e t ue& (is'lays ow e!en if one of the "onditions is t ue& (is'lays ow if the "ondotion is false&

To dis'lay e#'loyee na#e,>ob,sala y,de'a t#ent nu#be fo those e#'loyees in de'a t#ent )K who ea n )KKK o #o e, as well as all e#'loyees in de'a t#ent ?K& SQL@S+L+,T ena#e,>ob,sal,de'tno ) 5RO6 e#' ? 7H+R+ sal@N)KKK A0( de'tnoN)K OR de'tnoN?KA +0A6+ BBBBBBBBBBBBB ALL+0 7AR( GO0+S 6ART/0 8LAJ+ S,OTT TUR0+R GA6+S 5OR( GO8 BBBBBBBBBBBBBBBBBB SAL+S6A0 SAL+S6A0 6A0AG+R SAL+S6A0 6A0AG+R A0AL9ST SAL+S6A0 ,L+RJ A0AL9ST SAL BBBBBBBBBBBBBBB 1CKK 1)LK )IHL 1)LK )MLK ?KKK 1LKK ILK ?KKK (+PT0O BBBBBBBBB ?K ?K )K ?K ?K )K ?K ?K )K

S*L Comman+ Result

I ows sele"ted& Example S*L Comman+ Result 0A6+ GO8 SAL BBBBBBBBBBBBBBBB BBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBB GO0+S 6A0AG+R )IHL 8LAJ+ 6A0AG+R )MLK S,OTT A0AL9ST ?KKK 5OR( A0AL9ST ?KKK F ows sele"ted& (+PT0O BBBBBBBBBBBBB )K ?K )K )K To dis'lay e#'loyee na#e, >ob, sala y, de'a t#ent nu#be fo those e#'loyees in de'a t#ent )K o ?K who ea n )KKK o #o e& SQL@ S+L+,T ena#e,>ob,sal,de'tno ) 5RO6 e#' ? 7H+R+ sal@N)KKK A0(<de'tnoN)K OR de'tnoN?K=A

SQL THROUGH SQL * PLUS

Special Operators Operator ! &ETWEE! L 5E Description ,he";in$ a !alue in a set of !alues& ,he";in$ a !alue within a an$e& 6at"hin$ a 'atte n f o# a "olu#n&

Example S*L Comman+

To dis'lay e#'loyee na#e,>ob fo all "le ;s and analysts& SQL@S+L+,T ena#e, >ob ) 5RO6 e#' ? 7H+R+ >ob /0 <O,L+RJP,PA0AL9STP =A +0A6+ BBBBBBBBBBB S6/TH SU0/L AG/T GA,J A0/L 6/LA0 GO8 BBBBBBB ,L+RJ A0AL9ST ,L+RJ ,L+RJ A0AL9ST ,L+RJ

Result

C ows sele"ted& Example S*L Comman+ To dis'lay e#'loyee nu#be ,na#e, de'a t#ent nu#be fo those hi ed between Ganua y 1,1IIL and Ganua y 1,1IIC& SQL@S+L+,T e#'no,ena#e,de'tno, ) hi edate 5RO6 e#' ? 7H+R+ hi edate 8+T7++0 OK1BGA0BILP F A0( OK1BGA0BICPA +6P0O BBBBBBBBBBBBBBB H?CI HFII HL)1 HLCC HCLF +0A6+ BBBBBBBBBBBBBBBB S6/TH ASHA 7AS/6 RAGA 6AR/A
M

Result

(+PT0O BBBBBBBBBBBBBBBB )K ?K ?K )K ?K

H/R+(AT+ BBBBBBBBBBBBBBBB 1HB(+,BIL )KB5+8BIL ))B5+8BIL K)BAPRBIL )MBS+PBIL

SQL THROUGH SQL * PLUS

HCIM HHM) HM?I HMFF HIKK HIK)

8/LL SUG/T ,HR/S T/0A GA,J A0/L

?K 1K 1K ?K ?K )K

K1B6A9BIL KIBGU0BIL 1HB0O:BIL KMBS+PBIL K?B(+,BIL K?B(+,BIL

11 ows sele"ted& Wildcard Characters The 7ild"a d "ha a"te s a e used fo "ha a"te st in$ "o#'a ison with 'atte n #at"hin$, "o##only used to #at"h st in$s with "o#'a ison o'e ato s li;e 8+T7++0, 0OT 8+T7++0, L/J+, 0OT L/J+& Operator 6 73)n+erscore4 Example S*L Comman+ Description 6at"hes any nu#be of "ha a"te s& 6at"hes any sin$le "ha a"te s&

To dis'lay na#e, >ob, sala y fo those whose na#e be$ins with lette A& SQL@ S+L+,T ena#e,>ob,sal ) 5RO6 e#' ? 7H+R+ ena#e L/J+ QARQA E!A$E BBBBBBBBBBBBBB ALL+0 A(A6S 8O& BBBBBBBBB SAL+S6A0 ,L+RJ SAL BBBBBBBBB 1CKK 11KK

Result

) ows sele"ted& Example S*L Comman+ To dis'lay e#'loyee na#e, >ob, sala y, de'a t#ent nu#be fo e#'loyees whose na#e has / as the se"ond "ha a"te & SQL@ S+L+,T ena#e,>ob,sal,de'tno ) 5RO6 e#' ? 7H+R+ ena#e L/J+ QS/RQA +0A6+ BBBBBBBBBBBBBB J/0G 6/LL+R GO8 BBBBBBBBBBBBB PR+S/(+0T ,L+RJ SAL (+PT0O BBBBBBBB BBBBBBBBB LKKK 1K 1?KK 1K

Result

) ows sele"ted&
I

SQL THROUGH SQL * PLUS

Working with NULL values The NULL values are not 0 or a blank , it represents an unknown or inapplicable value !t cannot be compared using relational or logical operators so the special operator " IS# is used with the ke$word NULL to locate NULL values Example S*L Comman+ To dis'lay e#'loyee na#e, "o##ission fo e#'loyees who a e not eli$ible fo "o##ission& SQL@ S+L+,T ena#e,"o## ) 5RO6 e#' ? 7H+R+ "o## /S 0ULLA +0A6+ BBBBBBBBBB S6/TH GO0+S 8LAJ+ ,LARJ S,OTT J/0G A(A6S GA6+S 5OR( 6/LL+R ,O66 BBBBBBBBBB

Result

1K ows sele"ted&

Using %&pressions with columns A ith#eti" "o#'utations "an be done on nu#e i" "olu#ns&Alias na#es "an be $i!en to "olu#ns o e%' essions on *ue y out'uts& Example S*L Comman+ List the na#e, sala y and P5 a#ount of all the e#'loyees <P5 is "al"ulated as 1KR of sala y= SQL@ S+L+,T ena#e,sal, sal*&1 ) 5RO6 e#' A

1K

SQL THROUGH SQL * PLUS

Result

+0A6+ BBBBBBBBBB S6/TH ALL+0 7AR( GO0+S 6ART/0 8LAJ+ ,LARJ S,OTT J/0G TUR0+R A(A6S GA6+S 5OR( 6/LL+R

SAL SAL*&1 BBBBBBBBBB BBBBBBBBBB MKK MK 1CKK 1CK 1)LK 1)L )IHL )IH&L 1)LK 1)L )MLK )ML )FLK )FL ?KKK ?KK LKKK LKK 1LKK 1LK 11KK 11K ILK IL ?KKK ?KK 1?KK 1?K

1F ows sele"ted& 7e "an ' o!ide an alias to the "olu#n Osal *&1P as OP5P S*L Comman+ SQL@ S+L+,T ena#e,sal, sal*&1 P5 ) 5RO6 e#' A

Result

+0A6+ BBBBBBBBBB S6/TH ALL+0 7AR( GO0+S 6ART/0 8LAJ+ ,LARJ S,OTT J/0G TUR0+R A(A6S GA6+S 5OR( 6/LL+R

SAL P5 BBBBBBBBBB BBBBBBBBBB MKK MK 1CKK 1CK 1)LK 1)L )IHL )IH&L 1)LK 1)L )MLK )ML )FLK )FL ?KKK ?KK LKKK LKK 1LKK 1LK 11KK 11K ILK IL ?KKK ?KK 1?KK 1?K

1F ows sele"ted&

11

SQL THROUGH SQL * PLUS

O'(%' )* clause option o+ S%L%CT Statement Purpose S+L+,T3&&OR(+R 89 The OR(+R 89 ,lause in the S+L+,T state#ent is used to et ie!e set of e"o ds de'endin$ on the sea "h "ondition of 7H+R+ "lause and dis'lay the esult set in as"endin$ o des"endin$ o de of the s'e"ified "olu#ns& S+L+,T -ALL . (/ST/0,T 1 2-table&1 * . e%' ession -alias1,3&4 5RO6 table-alias1 -,table -alias11&& -7H+R+ "ondition1 -OR(+R 89 2e%' . 'os4 -AS, . (+S, 1 -,2e%' . 'os4 -AS, . (+S,113&1 To dis'lay na#e and sala ies of e#'loyees in de'a t#ent )K in as"endin$ o de of sala y& SQL@ S+L+,T ena#e,sal,de'tno 5RO6 e#' 7H+R+ de'tnoN)K OR(+R 89 SALA +0A6+ BBBBBBBBBBBBBBBB S6/TH A(A6S GO0+S S,OTT 5OR( SAL BBBBBBBBBBBBBBB MKK 11KK )IHL ?KKK ?KKK (+PT0O BBBBBBBBBBBBBB )K )K )K )K )K

Syntax

Example S*L Comman+

Result

L ows sele"ted& Example To dis'lay na#e and sala ies of e#'loyees in de'a t#ent )K in as"endin$ o de of sala y, usin$ the 'osition of "olu#n in the synta%& SQL@ S+L+,T ena#e,sal,de'tno ) 5RO6 e#' ? 7H+R+ de'tnoN)K F OR(+R 89 )A

S*L Comman+

1)

SQL THROUGH SQL * PLUS

Result +0A6+ BBBBBBBBBBBBBBBB S6/TH A(A6S GO0+S S,OTT 5OR( SAL (+PT0O BBBBBBBBB BBBBBBBBB MKK )K 11KK )K )IHL )K ?KKK )K ?KKK )K

L ows sele"ted& Example To dis'lay nu#be , na#e, de'a t#ent nu#be , sala y of all e#'loyees& O de the esult by de'a t#ent nu#be and in des"endin$ o de of sala y& SQL@ S+L+,T e#'no,ena#e,de'tno,sal ) 5RO6 e#' ? OR(+R 89 de'tno,sal (+S,A +6P0O BBBBBBBBBBBBBBB HM?I HHM) HI?F HHMM HIK) HLCC HMHC H?CI HCIM HFII HMFF HL)1 HCLF HIKK +0A6+ BBBBBBBBBBBBBBBB J/0G ,LARJ 6/LL+R S,OTT 5OR( GO0+S A(A6S S6/TH 8LAJ+ ALL+0 TUR0+R 7AR( 6ART/0 GA6+S (+PT0O BBBBBBBBBBBB 1K 1K 1K )K )K )K )K )K ?K ?K ?K ?K ?K ?K SAL BBBBBBBBB LKKK )FLK 1?KK ?KKK ?KKK )IHL 11KK MKK )MLK 1CKK 1LKK 1)LK 1)LK ILK

S*L Comman+

Result

1F ows sele"ted&

1?

SQL THROUGH SQL * PLUS

CONN%CT )* , ST-'T W!T. option o+ S%L%CT Statement Purpose S+L+,T3&,O00+,T 89 3&&START 7/TH The ,O00+,T 89 o'tion of S+L+,T state#ent etu ns ows in a hie a "hi"al o de & This o'tion "an be used only with tables whi"h have sel+/re+erential integrit$ "onst aint& S+L+,T -ALL . (/ST/0,T1 2-table&1* . e%' ession -alias1,34 5RO6 table -alias1-,table-alias11&& -7H+R+ "ondition1 -,O00+,T 89 "ondition -START 7/TH "ondition11 To et ie!e the e"o ds f o# e#' table in a t ee st u"tu e, sta tin$ with RAGA and "onne"tin$ ea"h e"o dPs 6GR field to the ' e!ious e"o dPs +6P0O field& SQL@ S+L+,T ena#e,e#'no,>ob,de'tno,#$ ,le!el ) 5RO6 e#' ? ,O00+,T 89 PR/OR e#'noN#$ F START 7/TH ena#eNQRAGAQA +0A6+ BBBBBBBBBBB RAGA SU0/L AG/T A0/L S6/TH +6P0O BBBBBBBBBBBB HLCC HHMM HMHC HIK) H?CI GO8 (+PT0O 6GR L+:+L BBBBBBBBBBBBBBBB BBBBBBBBBBBB BBBBBBBBB BBBBBBBBB 6A0AG+R )K HM?I 1 A0AL9ST )K HLCC ) ,L+RJ )K HHMM ? A0AL9ST )K HLCC ) ,L+RJ )K HIK) ?

Syntax

Example

S*L Comman+

Result

L ows sele"ted&

1F

SQL THROUGH SQL * PLUS

UN!ON, !NT%'S%CT, 0!NUS option o+ S%L%CT Statement Purpose Syntax SELECT,-)! O! 9 !TERSECT 9 $ !)S To "o#bine info #ation of si#ila ty'e f o# #o e than one table& S+L+,T -ALL . (/ST/0,T 1 2-table&1 * . e%' ession -alias1,34 5RO6 table-alias1-,table-alias11&& -2U0/O0 . /0T+RS+,T . 6/0US4 *ue y1 The set o'e ato s U0/O0, U0/O0 ALL, /0T+RS+,T and 6/0US 'ut "e tain est i"tions on the *ue ies they o'e ate& 1& The *ue ies #ust sele"t the sa#e nu#be of "olu#ns and "o es'ondin$ "olu#ns #ust be of the sa#e ty'e& )& The *ue ies should not efe to any "olu#n of lon$ ty'e& ?& The OR(+R 89 "lause should be used only on"e at the end of the *ue y and not at the end of ea"h S+L+,T& F& The OR(+R 89 "lause should not s'e"ify the "olu#ns by na#e, instead should s'e"ify the "olu#ns by 'osition& (is'lay all data f o# table de't& SQL@S+L+,T * 5RO6 de'tA (+PT0O BBBBBBBBBBBBBBBB 1K )K ?K FK (0A6+ BBBBBBBBBBBBBBBBBB A,,OU0T/0G R+S+AR,H SAL+S OP+RAT/O0S LO, BBBBBBBBBBBBB 0+7 9ORJ (ALLAS ,H/,AGO 8OSTO0

!ote:

Example S*L Comman+ Result

F ows sele"ted&

Example S*L Comman+

To dis'lay distin"t !alues etu ned by eithe *ue y& SQL@S+L+,T dna#e 5RO6 de't U0/O0 ) S+L+,T dna#e 5RO6 de't1A

1L

SQL THROUGH SQL * PLUS

Result

(0A6+ BBBBBBBBBBBBBBBBBBB A,,OU0TS +(U,AT/O0 OP+RAT/O0S R+S+AR,H SAL+S L ows sele"ted&

Example S*L Comman+ Result

To dis'lay all the !alues etu ned by both *ue ies& SQL@S+L+,T dna#e 5RO6 de't U0/O0 ALL ) S+L+,T dna#e 5RO6 de't1A (0A6+ ;;;;;;;;;;;;;;;; A,,OU0TS R+S+AR,H SAL+S OP+RAT/O0S +(U,AT/O0 R+S+AR,H A,,OU0TS OP+RAT/O0S R+S+AR,H SAL+S 1K ows sele"ted&

Example S*L Comman+ Result

To dis'lay "o##on !alues whi"h a e ' esent in the !alues etu ned by both the *ue ies& SQL@S+L+,T dna#e 5RO6 de't /0T+RS+,T ) S+L+,T dna#e 5RO6 de't1A (0A6+ BBBBBBBBBBBBBBBBB A,,OU0TS OP+RAT/O0S R+S+AR,H SAL+S F ows sele"ted&
1C

SQL THROUGH SQL * PLUS

Example S*L Comman+ Result

To dis'lay !alues etu ned by the fi st *ue y whi"h a e not ' esent in the !alues etu ned by the se"ond *ue y& SQL@S+L+,T dna#e 5RO6 de't 6/0US ) S+L+,T dna#e 5RO6 de't1A (0A6+ BBBBBBBBBBBBB +(U,AT/O0 1 ow sele"ted&

Example S*L Comman+ Result

To dis'lay !alues etu ned by the fi st *ue y whi"h a e not ' esent in the !alues etu ned by the se"ond *ue y& SQL@S+L+,T dna#e 5RO6 de't 6/0US ) S+L+,T * 5RO6 de't1A (+PT0O BBBBBBBBBBBBBBBB LK CK (0A6+ LO, BBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBB +(U,AT/O0 8O68A9 +S+AR,H 8A0GALOR+

) ows sele"ted&

-ggregate 1unctions The a$$ e$ate fun"tions ' odu"e a sin$le !alue fo an enti e $ ou' o table& ,OU0T SU6 6AT 6/0 A:G (ete (ete (ete (ete (ete #ine the nu#be of ows o non 0ULL "olu#n !alues #ines the su# of all sele"ted "olu#ns #ines the la $est of all sele"ted !alues of a "olu#n #ines the s#allest of all sele"ted !alues of a "olu#n #ines the a!e a$e of all non 0ULL !alues of a "olu#n

/n the abo!e fun"tions , 0ULLs a e i$no ed& Example S*L Comman+ To dis'lay the nu#be of e#'loyees wo ;in$ with the "o#'any& SQL@S+L+,T ,OU0T<*= 5RO6 e#'A

1H

SQL THROUGH SQL * PLUS

Result

,OU0T<*= BBBBBBBBBBBB 1F

Example S*L Comman+ Result

To dis'lay the total sala ies 'ayable to e#'loyees SQL@S+L+,T SU6<sal= 5RO6 e#'A SU6<SAL= BBBBBBBBBB )IK)L

Example S*L Comman+

To dis'lay the #a%i#u# sala y of e#'loyee wo ;in$ as a sales#an& SQL@S+L+,T 6AT<sal= 5RO6 e#' ) 7H+R+ >ob N OSAL+S6A0PA 6AT<SAL= BBBBBBBBBB 1CKK

Result

Example S*L Comman+ Result

To dis'lay the #ini#u# sala y of e#'loyee SQL@S+L+,T 6/0<sal= 5RO6 e#' 6/0<SAL= BBBBBBBBBB MKK

1M

SQL THROUGH SQL * PLUS

Example S*L Comman+

To dis'lay the a!e a$e sala y and nu#be of e#'loyees wo ;in$ in de'a t#ent )K& SQL@S+L+,T A:G<sal= , ,OU0T<*= 5RO6 e#' ) 7H+R+ de'tno N )KA A:G<SAL= ,OU0T<*= BBBBBBBBBB BBBBBBBBBB )1HL L

Result

2'OU3 )* clause option o+ S%L%CT Statement Purpose S+L+,T3&&GROUP 89 The GROUP 89 ,lause in the S+L+,T state#ent is used to di!ide the ows in a table into s#alle $ ou's& ,onditional et ie!al of ows f o# a $ ou'ed esult is 'ossible with the HA:/0G "lause& S+L+,T -ALL . (/ST/0,T 1 2-table&1 * . e%' ession -alias1,3&4 5RO6 table-alias1 -,table -alias11&& -7H+R+ "ondition1 - GROUP 89 2e%' . 'os4 1 -HA:/0G "ondition 1 -OR(+R 89 2e%' . 'os4 -AS, . (+S, 1 -,2e%' . 'os4 -AS, . (+S,113&1 List the de'a t#ent nu#be s and nu#be of e#'loyees in ea"h de'a t#ent& SQL@S+L+,T de'tno, ,OU0T<*= 5RO6 e#' ) GROUP 89 de'tnoA (+PT0O ,OU0T<*= BBBBBBBBBB BBBBBBBBBB 1K ? )K L ?K C

Syntax

Example S*L Comman+

Result

1I

SQL THROUGH SQL * PLUS

Example S*L Comman+

List the >obs and the nu#be of e#'loyees in ea"h >ob&The esult should be in des"endin$ o de of the e#'loyees& SQL@S+L+,T >ob, ,OU0T<*= 5RO6 e#' ) GROUP 89 >ob ? OR(+R 89 ) (+S, A GO8 ,OU0T<*= BBBBBBBBB BBBBBBBBBB ,L+RJ F SAL+S6A0 F 6A0AG+R ? A0AL9ST ) PR+S/(+0T 1 List the total sala y, #a%i#u# and #ini#u# sala y and the a!e a$e sala y of e#'loyees >ob wise, fo de'a t#ent nu#be )K only& SQL@S+L+,T >ob, SU6<sal=, A:G<sal=, 6AT<sal=, 6/0<sal= ) ) 5RO6 e#' 7H+R+ de'tno N )K ? GROUP 89 >obA GO8 SU6<SAL= A:G<SAL= 6AT<SAL= 6/0<SAL= BBBBBBBBB BBBBBBBBBB BBBBBBBBBB BBBBBBBBBB BBBBBBBBBB A0AL9ST CKKK ?KKK ?KKK ?KKK ,L+RJ 1IKK ILK 11KK MKK 6A0AG+R )IHL )IHL )IHL )IHL

Result

Example

S*L Comman+

Result

The .-4!N2 Clause Purpose Ha!in$ "lause is used to s'e"ify whi"h $ ou's a e to be dis'layed, that is , est i"t the $ ou's that a e etu ned on the basis of a$$ e$ate fun"tions& List the a!e a$e sala y fo all de'a t#ents e#'loyin$ #o e than fi!e 'eo'le& SQL@S+L+,T de'tno, A:G<sal= 5RO6 e#' ) GROUP 89 de'tno ? Ha!in$ "ount<*= @ LA

Example S*L Comman+

)K

SQL THROUGH SQL * PLUS

Result

(+PT0O A:G<SAL= BBBBBBBBBB BBBBBBBBBB ?K 1LCC&CCCCH

Di<<erences bet=een (roup &y %avin> Clause an+ Or+er &y? Where Clause (roup &y? %avin> Clause Or+er &y? Where Clause G ou' by "lause $ ou's the ows into s#alle $ ou's a""o din$ to the $i!en *ue y and then shows the out'ut in a 'a ti"ula se*uen"e& Ha!in$ "lause is used fo "onditional et ie!al of ows f o# a $ ou'ed esult O de by "lause i#'oses an o de on the esult of a *ue y 7he e ,lause with O de by is used fo "onditional et ie!al of indi!idual ows&

E+it S*L " Plus Statements


The SQL state#ents e#ains within the SQL buffe until anothe SQL state#ents o!e w ites it& Afte ente in$ a SQL state#ent, you "an edit the state#ent with a !a iety of SQL* Plus +dit "o##ands& Comman+ L@ STA A@PPE!DA text Description List the "o#'lete SQL state#ent SQL@L A''end te&t to the end of the "u ent line SQL@A , de'tno

C@%A!(EA Re'la"es fi st o""u en"e of old5te&t in the Bol+7textBne=7textB "u ent line with new5te&t SQL@,Uena#e,>obUe#'no,sala yU DEL @!P)TA e%it /n'ut #ode n e%it SQL@/ OR(+R 89 ena#e Re'la"e the "o#'lete "ontents of line n with Te%t SQL@) 5RO6 e#' (elete the "u ent line SQL@(+L /n'ut one o #o e lines of te%t afte the lineA ' ess return on a blan; line to

)1

SQL THROUGH SQL * PLUS

Explicitly execute the S*L statements Comman+ U RU0 Description +%e"ute the "ontents of the SQL buffe 5i st dis'lay and then e%e"ute the "u ent SQL state#ents

#ile $anipulation Comman+s So#e of the #ost "o##only used SQL * Plus file #ani'ulation "o##ands alon$ wih the synta% is $i!en below& Comman+ Purpose Syntax SACE Sa!e SQL "o##ands in the buffe to a file& SA:+ filena#e -R+PLA,+1 where R+PLA,+ e'la"es the "ontents of an e%istin$ file& /f the file does not e%ist,R+PLA,+ " eates the file&

Comman+ Purpose Syntax

START +%e"ute a sa!ed file with the START "o##and& START filena#e

Comman+ Purpose Syntax

(ET Ret ie!e the "ontents of a file into the SQL buffe to edit it with the G+T "o##and& G+T filena#e

))

SQL THROUGH SQL * PLUS

Comman+ Purpose Syntax

SPOOL Sa!e "o##ands to ' odu"e a e'o t as well as its out'ut within o'e atin$ syste# files& SPOOL -filena#e . OUT . O551 where

<ilename O## O)T

#a;es SQL s'oolin$ dis'layed out'ut to the na#ed file& #a;es SQL sto' s'oolin$& #a;es SQL sto' s'oolin$ and aslo ' ints the file&

)?

SQL THROUGH SQL * PLUS

Summary :;
The S+L+,T state#ent is used to *ue y database tables& ,onditional et ie!al of ows f o# a table is done by s'e"ifyin$ the "ondition in the 7H+R+ "lause of the S+L+,T state#ent& 0ULL !alues e' esents an un;nown o ina''li"able !alue& The esult f o# a *ue y "an be so ted in eithe des"endin$ o as"endin$ o de <default= usin$ the OR(+R 89 "lause with S+L+,T state#ent& A$$ e$ate fun"tions a e used to ' odu"e su##a iVed esults& ,OU0T, SU6, 6AT, 6/0 and A:G a e few of the a$$ e$ate fun"tions& G ou'in$ the esult of a *ue y based on "olu#ns is done usin$ the GROUP 89 "lause and "onditional et ie!al of the $ ou'ed esult is a"hie!ed by s'e"ifyin$ the HA:/0G "lause& /n the absen"e of the GROUP 89 "lause, the enti e table is t eated as a sin$le $ ou'& HA:/0G "lause est i"ts the dis'lay of $ ou's on the basis of a$$ e$ate fun"tions& GROUP 89 "lause $ ou's the ows into s#alle $ ou's whe e as OR(+R 89 "lause a an$es the out'ut in 'a ti"ula o de based on the "olu#ns of the table& The lo$i" of Ha!in$ "lause is elated to the esults of $ ou' fun"tions, whe e as 7he e ,lause wo ;s on indi!idual ows&

)F

Vous aimerez peut-être aussi