0% found this document useful (0 votes)
61 views

Basics QL Statements

This document provides an overview of basic SQL statements and concepts: - It describes the capabilities and components of SQL SELECT statements such as selection, projection, and joins. - It demonstrates the structure of a basic SELECT statement and explains how to select all columns, specific columns, and apply column aliases. - It covers data formatting in query results and how to use arithmetic operators to modify and calculate values in the SELECT clause. - It also discusses operator precedence and how to use parentheses to control evaluation order in expressions.

Uploaded by

harsha429
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Basics QL Statements

This document provides an overview of basic SQL statements and concepts: - It describes the capabilities and components of SQL SELECT statements such as selection, projection, and joins. - It demonstrates the structure of a basic SELECT statement and explains how to select all columns, specific columns, and apply column aliases. - It covers data formatting in query results and how to use arithmetic operators to modify and calculate values in the SELECT clause. - It also discusses operator precedence and how to use parentheses to control evaluation order in expressions.

Uploaded by

harsha429
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Chapter 1

Writing Basic
SQL Statements
1
Objectives
After completing this lesson, you shoul be able to o the
follo!ing"
List the capabilities of SQL S#L#C$ statements
#%ecute a basic S#L#C$ statement
&ifferentiate bet!een SQL statements an SQL'(lus
commans
Lesson Aim
To extract data from the database. you need to use the structured query
language (SQL) SELECT statement You may need to restrict the columns
that are dislayed. This lesson describes all !he SQL statements that you need
to erform these actions.
You may "ant to create SELECT statements that can be used time and time
again. Tins lesson also co#ers the use of SQL$%lus commands to execute
SQL statements
2
Capabilities of SQL S#L#C$
Statements
Selection Projection
Table
1
Table
2
Capabilities of SQL SELECT Statements
& SELECT statement retrie#es information from the database. 'sing a
SELECT statement( you can do the follo"ing)
Selection) You can use the selection caability in SQL to choose the ro"s in
a table that you "ant returned by a query. You can use #arious criteria to
selecti#ely restrict the ro"s that you see.
%ro*ection) You can use the ro*ection caability in SQL to choose the
columns in a table that you "ant returned by your query. You can choose as
fe" or as many columns of the table as you require.
+oin) You can use the *oin caability in SQL to bring together data that is
stored in different tables by creating a lin, through a column that both the
tables share. You "ill learn more about *oins in a later lesson.
3
Basic S#L#C$ Statement
SELECT [DISTINCT] {! col"mn [alias]!##!$
%&'( table;
S#L#C$ ientifies what columns)
*+O, ientifies which table)
)asic SELECT Statement
!n its simlest form( a SELECT statement must include the follo"ing
& SELECT clause( "hich secifies the columns to be dislayed.
A FROM clause, "hich secifies the table containing the columns
listed in the SELECT clause.
!n the syntax)
SELECT is a list of one or more columns.
-!ST!.CT suresses dulicates.
$ selects all columns
column selects the named column.
alias gi#es selected columns different headings.
FROM table secifies the table containing the columns.
Note: Throughout this course( the "ords ,ey"ord( clause( and statement are
used.
& keyword refers to an indi#idual SQL element. /or examle( SELECT and
FROM are ,ey"ords.
& clause is a art of an SQL statement. /or examle. SELECT empno,
ename, is a clause.
& statement is a combination of t"o or more clauses. /or examle. SELECT
* FROM emp is a SQL statement.

Writing SQL Statements


SQL statements are not case sensitive)
SQL statements can be on one or
more lines)
-ey!ors cannot be abbreviate or split
across lines)
Clauses are usually place on separate
lines)
$abs an inents are use to enhance
reaability)
*ritin+ SQL Statements
'sing the follo"ing simle rules and guidelines( you can construct #alid
statements that are both easy to read and easy to edit)
SQL statements are not case sensiti#e( unless indicated.
SQL statements can be entered on one or many lines.
0ey"ords cannot be slit across lines or abbre#iated.
Clauses are usually laced on searate lines for readability and ease of
editing.
Tabs and indents can be used to ma,e code more readable.
0ey"ords tyically are entered in uercase1 all other "ords( such as table names and
columns( are entered in lo"ercase.
2ithin SQL$%lus. a SQL statement is entered at the SQL romt( and the subsequent
lines are numbered. This is called the SQL buffer. 3nly one statement can be current
at any time "ithin the buffer.
Executing SQL Statements
%lace a semicolon (1) at the end of the last clause.
%lace a slash on the last line in the buffer.
%lace a slash at the SQL romt
!ssue a SQL$%lus 4'0 command at the SQL romt.
!
Selecting All Columns
S#L#C$ '
*+O, emp.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
5678 S9!T: CLE40 58;< =5>=<>=8?; ?;; <;
5@88 &LLE. S&LES9&. 578? <;>;<>=8?= =7;; 6;; 6;
5A<= 2&4- S&LES9&. 578? <<>;<>=8?= =<A; A;; 6;
5A77 +3.ES 9&.&BE4 5?68 ;<>;@>=8?= <85A <;
57A@ 9&4T!. S&LES9&. 578? <?>;8>=8?= =<A; =@;; 6;
578? CL&0E 9&.&BE4 5?68 ;=>;A>=8?= <?A; 6;
55?< CL&40 9&.&BE4 5?68 ;8>;7>=8?= <@A; =;
55?? SC3TT &.&LYST 5A77 ;8>=<>=8?< 6;;; <;
5?68 0!.B %4ES!-E.T =5>==>=8?= A;;; =;
5?@@ T'4.E4 S&LES9&. 578? ;?>;8>=8?= =A;; ; 6;
5?57 &-&9S CLE40 55?? =<>;=>=8?6 ==;; <;
58;; +&9ES CLE40 578? ;6>=<>=8?= 8A; 6;
58;< /34- &.&LYST 5A77 ;6>=<>=8?= 6;;; <;
586@ 9!LLE4 CLE40 55?< <6>;=>=8?< =6;; =;
=@ ro"s selected.
"
Selectin+ All Col"mns! All &o,s
You can dislay all columns of data in a table by follo"ing the SELECT
,ey"ord "ith an asteris, ($). !n the examle on the slide( the deartment
table contains three columns) -E%T.3( -.&9E. and L3C. The table
contains four ro"s( one for each deartment.
You can also dislay all columns in The table by listing all the columns after
the SELECT ,ey"ord. /or examle( the follo"ing SQL statement( li,e the
examle on the slide( dislays all columns and all ro"s of the -E%T table)
SELECT #eptno, #name, loc
FROM #ept$
&#($/O &/A,# LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
%
Selecting Specific Columns
S#L#C$ ename, eptno, hireate
*+O, emp.
ENAME DEPTNO HIREDATE
S9!T: <; =5>=<>=8?;
&LLE. 6; <;>;<>=8?=
2&4- 6; <<>;<>=8?=
+3.ES <; ;<>;@>=8?=
9&4T!. 6; <?>;8>=8?=
CL&0E 6; ;=>;A>=8?=
CL&40 =; ;8>;7>=8?=
SC3TT <; ;8>=<>=8?<
0!.B =; =5>==>=8?=
T'4.E4 6; ;?>;8>=8?=
&-&9S <; =<>;=>=8?6
+&9ES 6; ;6>=<>=8?=
/34- <; ;6>=<>=8?=
9!LLE4 =; <6>;=>=8?<
=@ ro"s selected.
Selectin+ S-ecific Col"mns! All &o,s
You can use the SELECT statement to dislay secific columns of the table by
secifying the column names( searated by commas. The examle on the
slide dislays all the names( deartment numbers and hiredates from the -E%T
table.
!n the SELECT clause( secifiy the columns that you "ant to see( in the order
in "hich you "ant them to aear in the outut. /or examle( to dislay detno
before ename( you use the follo"ing statement.
SELECT .e-tno! ename
%&'( em-/
&#($/O #/A,#
20 SMITH
30 ALLEN
&
Column 0eaing &efaults
&efault justification
-Left" &ate an character ata
-+ight" /umeric ata
&efault isplay" 1ppercase
-
Col"mn 0ea.in+ Defa"lts
Character column heading and data as "ell as date column heading and data are left
*ustified "ithin a column "idth. .umber headings and data are rightD*ustified.
S#L#C$ ename, hireate, sal
*+O, emp
ENAME HIREDATE SAL
S9!T: =5>=<>=8?; ?;;
&LLE. <;>;<>=8?= =7;;
2&4- <<>;<>=8?= =<A;
+3.ES ;<>;@>=8?= <85A
9&4T!. <?>;8>=8?= =<A;
CL&0E ;=>;A>=8?= <?A;
CL&40 ;8>;7>=8?= <@A;
SC3TT ;8>=<>=8?< 6;;;
0!.B =5>==>=8?= A;;;
T'4.E4 ;?>;8>=8?= =A;;
&-&9S =<>;=>=8?6 ==;;
+&9ES ;6>=<>=8?= 8A;
/34- ;6>=<>=8?= 6;;;
9!LLE4 <6>;=>=8?< =6;;
=@ ro"s selected.
Character and date column headings can be truncated( but number headings can not
be truncated. The column headings aear in uercase by default. You can
o#erride the column heading dislay "ith an alias. Column aliases are co#ered later
in this lesson.
'
Arithmetic #%pressions
Create e%pressions on /1,B#+ an &A$# ata by using arithmetic
operators)
'-erator Descri-tion
1 A..
2 S"btract
("lti-l3
/ Di4i.e
Arit5metic E6-ressions
You may need to modify the "ay in "hich data is dislayed( erform
calculations( or loo, at "hatDif scenarios. This is ossible using arithmetic
exressions &n arithmetic exression may contain column names( constant
numeric #alues( and the arithmetic oerators.
Arit5metic '-erators
The slide lists the arithmetic oerators a#ailable in SQL You can use
arithmetic oerators in am clause of a SQL statement excet the /439
clause
1(
1sing Arithmetic Operators
S#L#C$ ename, sal, sal2344
*+O, emp.
ENAME SAL SAL+300
S9!T: ?;; ==;;
&LLE. =7;; =8;;
2&4- =<A; =AA;
+3.ES <85A 6<5A
9&4T!. =<A; =AA;
CL&0E <?A; 6=A;
CL&40 <@A; <5A;
SC3TT 6;;; 66;;
0!.B A;;; A6;;
T'4.E4 =A;; =?;;
&-&9S ==;; =@;;
+&9ES 8A; =<A;
/34- 6;;; 66;;
9!LLE4 =6;; =7;;
=@ ro"s selected.
7sin+ Arit5metic '-erators
The examle in tin) slide uses the addition oerator to calculate a salary
increaseDof E6;; for all emloyees and dislays a ne" S&LF6;; column in
the outut.
.ote that the resultant calculated column S&LF6;; is not a ne" column in
the E9% table) it is for dislay only. Cy default( the name of a ne" column
comes from the calculation that generated itGin this case. Sal F 6;;.
Note: SQL$%lus ignores blan, saces before and after the arithmetic
oerator.
11
Operator (receence
' 5 2 6
,ultiplication an ivision ta7e priority over aition an subtraction)
Operators of the same priority are evaluate from left to right)
(arentheses are use to force prioriti8e evaluation an to clarify
statements)
'-erator )*ece#ence
!f an arithmetic exression contains more than one oerator( multilication
and di#ision are e#aluated first. !f oerators "ithin an exression are of same
riority( then e#aluation is done from left to right
You can use arentheses to force the exression "ithin arentheses to be
e#aluated first.
12
Oe!"to! P!ece#ence
S#L#C$ ename, sal, 19 ' sal 2 144
*+O, emp .
ENA(E SAL 12SAL1188
SM+T, &(( '%((
ALLE- 1"(( 1'3((
.AR/ 12!( 1!1((
0O-ES 2'%! 3!&((
MART+- 12!( 1!1((
1LA2E 2&!( 33((
CLAR2 2!( 2'!((
SCOTT 3((( 3"1((
2+-3 !((( "(1((
T4R-ER 1!(( 1&1((
A/AMS 11(( 133((
0AMES '!( 11!((
FOR/ 3((( 3"1((
M+LLER 13(( 1!%((
=@ ro"s selected.
'-erator Prece.ence 9contin"e.:
The examle on the slide dislays the name( salary( and annual
comensation of emloys !t calculates the annual comensation as =<
multilied by the monthly salary( lus a oneDtime bonus of E =;; .otice
that multilication is erformed before addition.
Note: 'se arentheses to reinforce the standard order of recedence and
to imro#e clarity . /or examle( the exression abo#e can be "ritten as
(=<$sal)F=;; "ith no change in the result.

13
1sing (aranthesis
S#L#C$ ename, sal, 19 ' :sal 2 144;
*+O, emp.
ENAME SAL $%&'SAL+$00(
S9!T: ?;; =;?;;
&LLE. =7;; <;@;;
2&4- =<A; =7<;;
+3.ES <85A 678;;
9&4T!. =<A; =7<;;
CL&0E <?A; 6A@;;
CL&40 <@A; 6;7;;
SC3TT 6;;; 65<;;
0!.B A;;; 7=<;;
T'4.E4 =A;; =8<;;
&-&9S ==;; =@@;;
+&9ES 8A; =<7;;
/34- 6;;; 65<;;
9!LLE4 =6;; =7?;;
=@ ro"s selected.
7sin+ Parent5eses
You can o#erride the rules of recedence by using parentheses to
secify the order in "hich oerators are executed.
The examle on the slide dislays the name( salary( and annual
comensation of emloyees. !t calculates the annual comensation
as monthly salary lus a monthly bonus of E=;;. multilied by =<.
Cecause of the arentheses( addition ta,es riority o#er
multilication.

1
&efining a /ull <alue
A null ! " #"lu$ %&"% ! un"#"l"'l$( un"!!)n$*( un+n,-n( ,. n"//l0"'l$1
A null ! n,% %&$ !"2$ "! 3$., ,. " 'l"n+ !/"0$.
S#L#C$ ename, job, comm
*+O, emp
ENAME JOB COMM
S9!T: CLE40
&LLE. S&LES9&. 6;;
2&4- S&LES9&. A;;
+3.ES 9&.&BE4
9&4T!. S&LES9&. =@;;
CL&0E 9&.&BE4
CL&40 9&.&BE4
SC3TT &.&LYST
0!.B %4ES!-E.T
T'4.E4 S&LES9&. ;
&-&9S CLE40
+&9ES CLE40
/34- &.&LYST
9!LLE4 CLE40
=@ ro"s selected.
Nu)) *")ue+ !f a ro" lac,s the data #alue for a articular column( that
#alue is said to be mill, or to contain null.
& null #alue is a #alue that is una#ailable( unassigned. un,no"n( or
inalicable. & null #alue is not the same as Hero or a sace. Iero is a
number( and a sace is a character.
Columns of any datatye can contain null #alues( unless the column "as
defined as .3T.'LL or as %4!9&4Y 0EY "hen the column "as
created.
!n the C399 column in the E9% table( you notice that only a
S&LES9&. can earn commission. 3ther emloyees are not entitled to
earn commission. & null #alue reresents that fact. Turner( "ho is a
salesman( does not earn any commission. .otice that his commission is
Hero and not null.
1!
/ull <alues
in Arithmetic #%pressions
Arithmetic e%pressions containing a null value evaluate to null)
SELECT ename( =< $ sal F comm
/439 em
2:E4E ename JK0!.BK
ENAME $%&SAL+COMM
0!.B
-ull ;al"es 5continue#6
!f any column #alue in an arithmetic exression is null( the result is null.
/or examle( if you attemt to erform di#ision "ith Hero( you get an
error. :o"e#er( if you di#ide a number by null( the result is a null or
un,no"n.
!n the examle on the slide( emloyee 0!.B is not in S&LES9&. and
docs not get any commission. Cecause the C399 column in the
arithmetic exression is null( the result is null.
/or more information( see Oracle Server SOL Reference. 4elease ?(
LElements of SQL.L
1"
&efining a Column Alias
R$n"2$! " 0,lu2n &$"*n)
I! u!$4ul -%& 0"l0ul"%,n!
I22$*"%$l5 4,ll,-! 0,lu2n n"2$6 ,/%,n"l AS
+$5-,.* '$%-$$n 0,lu2n n"2$ "n* "l"!
R$7u.$! *,u'l$ 7u,%"%,n 2".+! 4 % 0,n%"n!
!/"0$! ,. !/$0"l 0&"."0%$.! ,. ! 0"!$ !$n!%#$
Col"mn Aliases
2hen dislaying the result of a query. SQL$%lus normalMN uses the name of
the selected column as the column heading. !n many cases( this heading may
not be descriti#e and hence is difficult to understand. You can change a
column heading by using a column alias.
Secify the alias after the column in the SELECT list using a sace as a
searator. Cy default( alias headings aear in uercase. !f the alias
contains saces( secial characters (such as O or E)( or is case sensiti#e(
enclose the alias in double quotation mar,s (P P).


1%
U!n) C,lu2n Al"!$!
S#L#C$ ename AS A , sal ,aa=
*+O, emp.
AD (AA<
SM+T, &((
ALLE- 1"((
.AR/ 12!(
0O-ES 2'%!
MART+- 12!(
1LA2E 2&!(
CLAR2 2!(
SCOTT 3(((
2+-3 !(((
T4R-ER 1!((
A/AMS 11((
0AMES '!(
FOR/ 3(((
M+LLER 13((
=@ ro"s selected.

Col"mn Aliases 9contin"e.:
The first examle dislays the name and the monthly salary of all the
emloyees. .otice mat the otional &S ,ey"ord has been used before the
column alias name. The result of the query "ould be the same "hether the
&S ,ey"ord is used or not. &lso notice that the SQL statement has the
column aliases( name and salary( in lo"ercase( "hereas the result of the
query dislays the column headings in uercase. &s mentioned in die last
slide( column headings aear in uercase by default.
The second examle dislays the name and annual salary of all the
emloyees. Cecause &nnual Salary contains saces( it has been
enclosed in double quotation mar,s. .otice that the column heading in
the outut is exactly the same as the column alias.

1&
Col"mn Aliases 9contin"e.:
The second examle dislays the name and annual salary of all the
emloyees. Cecause &nnual Salary contains saces( it has been
enclosed in double quotation mar,s. .otice that the column heading in
the outut is exactly the same as the column alias.
S#L#C$ ename AS >A> , sal >,aa= >
*+O, emp.
A# Maa7
SM+T, &((
ALLE- 1"((
.AR/ 12!(
0O-ES 2'%!
MART+- 12!(
1LA2E 2&!(
CLAR2 2!(
SCOTT 3(((
2+-3 !(((
T4R-ER 1!((
A/AMS 11((
0AMES '!(
FOR/ 3(((
M+LLER 13((
=@ ro"s selected.

1'
Col"mn Aliases 9contin"e.:
S#L#C$ ename >A?>, sal ' 19 >@?ll?7 Acret>
*+O, emp .
A#, -,)),. /c
S9!T: 87;;
&LLE. =8<;;
2&4- =A;;;
+3.ES 6A5;;
9&4T!. =A;;;
CL&0E 6@<;;
CL&40 <8@;;
SC3TT 67;;;
0!.B 7;;;;
T'4.E4 =?;;;
&-&9S =6<;;
+&9ES ==@;;
/34- 67;;;
9!LLE4 =A7;;
=@ ro"s selected.
2(
Concatenation Operator
Concatenates columns or character strings to other
columns
Bs represente by t!o vertical bars : CC ;
Creates a resultant column that is a character e%pression
Concatenation '-erator
You can lin, columns to other columns( arithmetic exressions( or
constant #alues to create a character exression by using the
concatenation oerator ( QQ ) Columns on cither side of the oerator are
combined to ma,e a single outut column.
21
1sing the Concatenation Operator
SELECT ename QQ *ob &S LEmloyeesL
/439 em 1
E0)o1ee+
S9!T:CLE40
&LLE.S&LES9&.
2&4-S&LES9&.
+3.ES9&.&BE4
9&4T!.S&LES9&.
CL&0E9&.&BE4
CL&409&.&BE4
SC3TT&.&LYST
0!.B%4ES!-E.T
T'4.E4S&LES9&.
&-&9SCLE40
+&9ESCLE40
/34-&.&LYST
9!LLE4CLE40
=@ ro"s selected.
Concatenation '-erator 9contin"e.:
!n the examle. E.&9E and +3C are concatenated( and they are gi#en
the alias Emloyees. .otice that the emloyee number and *ob are
combined to ma,e a single outut column.
The &S ,ey"ord before the alias name ma,es the SELECT clause
easier to read.
22
Literal Character Strings
A literal is a character, e%pression, or number inclue in the S#L#C$
list)
&ate an character literal values must be enclose !ithin single
Duotation mar7s)
#ach character string is output once for each ro! returne)
Literal C5aracter Strin+s
& literal is any character( exression( or number included in die SELECT
list that is not a column name or a column alias. !t is rinted for each ro"
returned. Literal strings of freeDformat text can be included in the query
result and are treated the same as a column in the SELECT list.
-ate and character literals must be enclosed "ithin single quotation
mar,s ( K K ) 1 number literals must not.
23
1sing Literal Character Strings
SELECT en"0e 22 3i+ "3 22 3 3 22 4o5
AS 6E0)o1ee Det"i)+6
7ROM e0 8
E0)o1ee Det"i)+
S9!T:is a CLE40
&LLE.is a S&LES9&.
2&4-is a S&LES9&.
+3.ESis a 9&.&BE4
9&4T!.is a S&LES9&.
CL&0Eis a 9&.&BE4
CL&40is a 9&.&BE4
SC3TTis a &.&LYST
0!.Bis a %4ES!-E.T
T'4.E4is a S&LES9&.
&-&9Sis a CLE40
+&9ESis a CLE40
/34-is a &.&LYST
9!LLE4is a CLE40
=@ ro"s selected.
Literal C8a*acte* St*in9s 5continue#6
The examle on the slide dislays names and *obs of all emloyees. The
column has the heading Emloyee -etails .otice the saces bet"een the
single quotation mar,s m the SELECT statement. The saces imro#e the
readability of the outut
2

Literal C8a*acte* St*in9s 5continue#6
!n the follo"ing examle( the name and salary for each emloyee is
concatenated "ith a literal to gi#e the returned ro"s more meaning.
S#L#C$ ename CC E " E CC E1E CC E Ayl?7 Acret F E CC sal Ayl?7
*+O, emp .
A-LI9
S9!T: ) = &ylR, Scret J ?;;
&LLE. ) = &ylR, Scret J =7;;
2&4- ) = &ylR, Scret J =<A;
+3.ES ) = &ylR, Scret J <85A
9&4T!. ) = &ylR, Scret J =<A;
CL&0E ) = &ylR, Scret J <?A;
CL&40 ) = &ylR, Scret J <@A;
SC3TT ) = &ylR, Scret J 6;;;
0!.B ) = &ylR, Scret J A;;;
T'4.E4 ) = &ylR, Scret J =A;;
&-&9S ) = &ylR, Scret J ==;;
+&9ES ) = &ylR, Scret J 8A;
/34- ) = &ylR, Scret J 6;;;
9!LLE4 ) = &ylR, Scret J =6;;
=@ ro"s selected.
2!
&uplicate +o!s
$he efault isplay of Dueries is all ro!s, incluing uplicate ro!s)
SELECT #eptno
FROM emp$
/E)T-O
1(
3(
1(
2(

1 *o:s selecte#.
/uplicate Ro:s
'nless you indicate other"ise. SQL$%lus dislays die results of a query "ithout
eliminating dulicate ro"s. The examle on the slide dislays all the deartment
numbers from the E9% table. .otice that the deartment numbers are reeated.
2"
#liminating &uplicate +o!s
#liminate uplicate ro!s by using the &BS$B/C$ 7ey!or in the S#L#C$
clause,
SELECT DISTINCT .e-tno
%&'( em-/
DEPTNO
6;
<;
=;
D"-licate &o,s 9contin"e.:
To eliminate dulicate ro"s in the result( include the -!ST!.CT ,ey"ord m
the SELECT clause immediately after the SELECT ,ey"ord. !n the examle
on the slide( the E9% table actually contains fourteen ro"s but there are only
three unique deartment numbers in the table.
You can secify multile columns after the -!ST!.CT qualifier. The
-!ST!.CT qualifier affects all the selected columns( and the result reresents
a distinct combination of the columns
SELECT DISTINCT .e-tno! job
%&'( em-/
DEPTNO JOB
<; CLE40
6; S&LES9&.
<; 9&.&BE4
6; CLE40
=; %4ES!-E.T
6; 9&.&BE4
=; CLE40
=; 9&.&BE4
<; &.&LYST
8 ro"s selected.
2%
SQL an SQL'(lus
Bnteraction
SQL an. SQLPl"s
SOL is a command language for communication "ith the 3racle Ser#er from
any tool or alication. 3racle SQL contains many extensions. 2hen you
enter a SQL statement( it is stored in a art of memory called the SOL buffer
and remains there until you enter a ne" statement.
SOL*Plus is an 3racle tool that recogniHes and submits SQL statements to
the 3racle Ser#er or for cxecution and contains its o"n command language.
S:L&P)u+
Se!;e!
SQLPL7S
Comman.s
Q"er3
&es"lts
%ormatte.
&e-ort
2&
SQL
Statements
)"ffer SQL
Statements
%ormatte.
'"t-"t
7e"tu!e+ o< S:L
Can be used by a range of users( including those "ith little or no
rogramming exerience.
!s a nonrocedural language.
4educes the amount of time required for creating and maintaining
systems.
!s an EnglishDli,e language.
/eatures of SQLK%lus
&ccets ad hoc entry of statements
&ccets SQL inut from files
%ro#ides a line editor for modifying SQL statements
Controls en#ironmental settings
/ormats query results into a basic reort.
&ccesses local and remote databases
2'
SQL Statements <ersus
SQL'(lus Commans
SQL
SQLPl"s
& language &n en#ironment
&.S! standard 3racle rorietary
0ey"ord cannot be abbre#iated 0ey"ords can be abbre#iated
Statements maniulate data and table
definitions in the database
Commands do not allo" maniulation of
#alues in the databasese
SQL an. SQLPl"s
9contin"e.:
The follo"ing table comares SQL and SQL$%lus)
SQL SQLPl"s
!s a language for communicating Ser#er to
access data
4ecogniHes the ser#er
!s the 3racle
!s entered one line at a time) not stored in
the SQL buffer
3(
Overvie! of SQL'(lus
Log in to SQL'(lus)
&escribe the table structure)
#it your SQL statement)
#%ecute SQL from SQL'(lus)
Save SQL statements to files an
appen SQL statements to files)
#%ecute save files)
Loa commans from file to buffer to eit)
SQL=Pl"s
SQL$%lus is ail en#ironment tn TThich you can do the follo"ing)
Execute SQL statements to retrie#e( modify( add. and remo#e data from
the database /ormat( erform calculations on. store( and rint query
results in the form of reorts Create scrit files to store SQL statements
for reetitue use in the future
SQL$%lus commands can be di#ided into the follouing main categories.
C"tego!1 Pu!o+e
En#ironment &ffects the general beha#ior of SQL statements for the session
/ormat /ormats query results
/ile maniulation Sa#es( loads( and runs scrit files
Execution Sends SQL statements from SQL buffer to 3racieS Ser#er
Edit 9odifies SQL statements in the buffer
!nteraction &l,ms you to create and ass #ariables to SQL statements(
rint #ariable #alues( and rint messages to the screen
9iscellaneous :as #arious commands to connect to the database( maniulate
the SQL$%lus en#ironment( and dislay column definitions
31
&isplaying $able Structure
1se the SQL'(lus &#SC+BB# comman to isplay the structure
of a table)
-ESCU4!CEV tablename
&isplaying T"'l$ S%.u0%u.$
!n SQL$%lus( you can dislay The structure of a table using the
-ESC4!CE command. The result of the command is to see the column
names and datatyes as "ell as "hether a column must contain data.
!n the syntax)
tablename is the name of any existing table( #ie"( or synonym
accessible to the user.
DESC em- /
/ame /ullG $ype
EMPNO NOT NULL NUMBER849
ENAME :ARCHAR28109
;OB :ARCHAR28<9
MGR NUMBER849
HIREDATE DATE
SAL NUMBER8=(29
COMM NUMBER8=(29
DEPTNO NUMBER829
DESC .e-t/
/ame /ullG $ype
DEPTNO NUMBER829
DNAME :ARCHAR28149
LOC :ARCHAR28139
DESC sal+ra.e /
/ame /ullG $ype
GRADE NUMBER
LOSAL NUMBER
HISAL NUMBER
32
&isplaying $able Structure
D!/l"5n) $able S%.u0%u.$ 80,n%nu$*9
The examle on the slide dislays the information about the structure of the
-E%T table. !n the result)
Null? indicates "hether a column must contain data) .3T .'LL
indicates that a column must contain data
Type dislays the datatye for a column
The datatyes are described m the follo"ing table)
D"t"t1e De+c!ition
N=MBER'>+(
.umber #alue ha#ing a maximum number
of digits ( the number of digits to the
right of the decimal oint s
*ARCHAR%'+(
WariableDlength character #alue of
maximum siHe s
DATE
-ate and time #alue bet"een +anuary =.
@5=< C.C. and -ecember 6=.8888 &.-
CHAR'+(
/ixedDlength character #alue of siHe s

De+c!ition
33
SQL'(lus #iting Commans
AH((#/&I text
CH0A/J#I 5ol 5ne!
CH0A/J#I te%t
CLH#A+I B1**H#+I
&#L
&#Ln
&#L m n
SQL=Pl"s E#itin9 Comman#s
SQLTlus commands arc entered one line at a time and are not stored in the
SQL buffer.
Command -escrition
&U%%E.-V tet &dds text to the end of the current line
CU:&.BEV>ci>e>>X"9D Changes old text to mK" in the current line
CU:&.BEV>text > -eletes tet from the current line
CLUE&4V C'//UE4V -eletes all lines from the SQL buffer
-EL -eletes current line
Buidelines
!f you ress U4eturnV before comleting a command. SQL$%lus
romts you "ith a line number.
You terminate the SQL buffer by either entering one of the terminator
characters (semicolon or slash) or ressing U4eturnV t"ice. You then
see the SQL romt.
+nt*o#uction to 'racle> SQL an. PL?SQL 12@8

3
SQLPl"s E.itin+ Comman.s
I[NP7T]
I[NP7T] text
L[IST]
L[IST] n
L[IST] m n
&[7N]
n
n text
8 text
SQLPl"s E.itin+ Comman.s 9contin"e.:
Command -escrition
!U.%' TV !nserts an indefinite number of lines
!U.%'TV tet !nserts a line consisting of rf.#r
!.U!STV Lists all lines in the S3L buffer
LU!STV n Lists one line (secified b# n!
LU!STV m n Lists a ranae of lines "m to n!
4U'.l -isla#s and runs the current SQL statement m the buffer
n Secifies the line to ma,e the current line
n tet 4elaces line n "ith tet
; tet !nserts a line before line =
You can enter onh one SQL$%!us command er SQL romt.
SQL$%lus commands are not stored in the buffer To continue a
SQL$%lus command on the next line( end the current line "ith a
hyhen (D).
3!
SQLPl"s %ile Comman.s
SA;E filename
AET filename
STA&T filename
@filename
EDIT filename
SP''L filename
SQLPl"s %ile Comman#s
SQL statements communicate "ith the 3racle Ser#er. SQL$%lus commands control the
en#ironment( format query results( and manage files. You can use the commands identified
in the follo"ing table
Co00"n# De+c!ition
S&WfE+ filename U.extV
U4E%UL&CL+&%%UE.-VV
Sa#es current contents of SQL buffer to a
file. 'se &%%E.- to add to an existing file)
use 4E%L&CE to o#er"rite an existing file.
The default extension is D sql.
#$% filename.extV 2rites the contents of a re#iously saTed file
to the SQL buffer. The default extension for
the filename is D sql.
STA;RT< filename[ext]
4uns a re#iously sa#ed command file.
Y filename 4uns a re#iously saTed command file (same
as ST&4T).
E- U!TV !n#o,es the editor and sa#es the buffer
contents to a file named
E/;+T< [filename[ext]]
!n#o,es the editor to edit contents to a sa#ed
file
S)O;OL< ;=ilename;e>t<< ?
OFF?O4T
Stores query results in a file. 3// closes the
sool file and sends the file results to the
system rinter
E@+T
Lea#es SQL$%L'S

3"
S"mmar3
SELECT ;/+ST+-CT< A*,column ;alias< , . . . B FROM tableC
1se SQL'(lus as an environment to"
#%ecute SQL statements
#it SQL statements
S#L#C$ S%"%$2$n%
!n this lesson( you ha#e learned about retrie#ing data from a database table "ith the
SELECT statement. The syntax is as follo"s)
SELECT U-!ST!.CTV Z $ ( column[name UaliasV( \N
/439 table[name 1
+n t8is sDnta>,t8e *oles o= EeD :o*#s a*eF
SELECT is a list of at least one column
/+ST+-CT suresses dulicates
$ selects all columns
ColumnGname selects the named column
Alias gi#es selected column a different heading
FROM secifies the table containing the columns
3%
(ractice Overvie!
Selecting all ata from ifferent tables
&escribing the structure of tables
(erforming arithmetic calculations an specifying column
names
1sing SQL'(lus eitor
P."0%0$ O#$.#$-
This is the first of many ractices. The solutions (if you require them) can be
found in &endix &. %ractices are intended to introduce all toics co#ered
in the lesson. Questions <D@ are aerDbased.
!n any ractice( there may be &if you have time& or &if you want etra
challen'e& questions. -o these only if you ha#e comleted all other
questions "ithin the allocated time and "ould li,e a further challenge to
your s,ills.
Ta,e the ractice slo"ly and recisely. You can exeriment "ith sa#ing and
running command files. !f you ha#e any questions at any time( attract the
instructorKs attention.
P"e!?B"+e# :ue+tion+
/or questions <D@ circle either True or /alse.
3&
P!"ctice $
=.!nitiate a SQL$%lus session using the user !- and ass"ord ro#ided by the
instructor.
<.SQL$%lus commands access the database.
True>/alse
6.2ill the SELECT statement execute successfully]
True>/alse
SHLI SELECT ename, Job, sal Sala*D
FROM emp $
@.2ill the SELECT statement execute successfully]
True>/alse
SHLI SELECT *
FROM sal9*a#e

(. There are four coding errors in this statement. Can you identify them]
SHLI SELECT empno, ename
Sala*D > 12 A--4AL SALARK
FROM emp$
7. Sho" the structure of the -E%T table. Select all data from the -E%T table.
N"0e Nu))@ T1e
-E%T.3 .'9CE4(<)
-.&9E W&4C:&4<(=@)
L3C W&4C:&4<(=6)
DEPTNO DNAME LOC
=; &CC3'.T!.B .E2 Y340
<; 4ESE&4C: -&LL&S
6; S&LES C:!C&B3
@; 3%E4&T!3.S C3ST3.
3'
(ractice 1 80,n%nu$*9
5. Sho" the structure of the E9% table. Create a query to dislay the name(
*ob. hire date( and emloyee number for each emloyee( "ith emloyee
number aearing first. Sa#e your SQL statement to a file named pl)*.s)l.
N"0e Nu))@ T1e
E9%.3 .3T .'LL .'9CE4(@)
E.&9E W&4C:&4<(=;)
+3C W&4C:&4<(8)
9B4 .'9CE4(@)
:!4E-&TE -&TE
S&L .'9CE4(5(<)
C399 .'9CE4(5(<)
-E%T.3 .'9CE4(<)
?. 4un your query in the file lq5.sql.
E(PN' ENA(E B') 0I&EDATE
%3"' SM+T, CLER2 1%C12C1'&(
%'' ALLE- SALESMA- 2(C(2C1'&1
%!21 .AR/ SALESMA- 22C(2C1'&1
%!"" 0O-ES MA-A3ER (2C(C1'&1
%"! MART+- SALESMA- 2&C('C1'&1
%"'& 1LA2E MA-A3ER (1C(!C1'&1
%%&2 CLAR2 MA-A3ER ('C("C1'&1
%%&& SCOTT A-ALKST ('C12C1'&2
%&3' 2+-3 )RES+/E-T 1%C11C1'&1
%& T4R-ER SALESMA- (&C('C1'&1
%&%" A/AMS CLER2 12C(1C1'&3
%'(( 0AMES CLER2 (3C12C1'&1
%'(2 FOR/ A-ALKST (3C12C1'&1
%'3 M+LLER CLER2 23C(1C1'&2
=@ ro"s selected.


(
(ractice 1 :continue;
8. Create a query to dislay unique *obs from the E9% table.
B')
A-ALKST
CLER2
MA-A3ER
)RES+/E-T
SALESMA-
!f you ha#e time( comlete the follo"ing exercises)
=;. Loadplc+*.s)l into the SQL buffer. .ame the column headings Em O( Emloyee( +ob(
and :ire -ate( resecti#ely. 4erun your query.
Em- C Em-lo3ee Bob 0ire Date
%33' 2+-3 )RES+/E-T 1%L-OML&1
%"'& 1LA2E MA-A3ER (1 LMAKL &1
%%&2 CLAR2 MA-A3ER ('L04-L&1
%!"" 0O-ES MA-A3ER (2 LA)RL &1
%"! MART+- SALESMA- 2&LSE)L&1
%'' ALLE- SALESMA- 2(LFE1L&1
%& T4R-ER SALESMA- (&LSE)L&1
%'(( 0AMES CLER2 (3L/ECL&1
%!21 .AR/ SALESMA- 22LFE1L&1
%'(2 FOR/ A-ALKST (3L/ECL&1
%3"' SM+T, CLER2 1%L/ECL&(
%%&& SCOTT A-ALKST ('L/ECL&2
%&%" A/AMS CLER2 12 L0A-L & 3
%'3 M+LLER CLER2 2 3L 0A-L & 2
1 *o:s selecte#
1
(ractice 1 :continue;
==. -islay the name concatenated "ith the *ob. searated by a comma
and sace( and name the column Emloyee and Title.
EmploDee an# Title
2+-3, )RES+/E-T 1LA2E, MA-A3ER
CLAR2, MA-A3ER 0O-ES, MA-A3ER
MART+-, SALESMA- ALLE-,
SALESMA- T4R-ER, SALESMA-
0AMES, CLER2 .AR/, SALESMA-
FOR/, A-ALKST SM+T,, CLER2
SCOTT, A-ALKST A/AMS, CLER2L
M+LLER, CLER2 1 *o:s selecte#.
!f you "ant extra challenge( comlete the follo"ing exercise)
=<. Create a query to dislay all the data from the E9% table. Searate
each column by a comma. .ame the column T:E 3'T%'T.
T:E 3'T%'T
%33',2+-3,)RES+/E-T, ,1%L-OML&1,!(((, , 1(
%"'3,1LA2E,MA-A3ER,%&3',(1LMAKL31, 23!(, ,3(
%%32,CLAR2,MA-A3ER,%33',('L04-L31, 2!(, , 1(
%!"",0O-ES, MA-A3ER,%33',(2LA)RL31, 2'%!, , 2(
%"!,MART+-,SALESMA-,%"'&,2&LSE)L&1, 12!(, 1((,3(
%'',ALLE-,SALESMA-,%"'&,2(LFE1L&1, 1"((,3((,3(
%3,T4R-ER, SALESMA-,%"'&,(&LSE)L&1, 1!((, (,3(
%'((,0AMES,CLER2,%"'&,(3L/ECL&1,'!(,,3(
%!21,.AR/,SALESMA-,%"'&,22LFE1L&1,12!(, !((,3(
%'(2,FOR/,A-ALKST,%!"",(3L/ECL&1, 3(((, , 2(
%3"',SM+T,,CLER2,%'(2,1%L/ECL&(,&((,,2(
%%&&,SCOTT,A-ALKST,%!"",('L/ECL&2,3(((,,2(

You might also like