Basics Ql Statements

  • Upload
    bilo044

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

  • 8/20/2019 Basics Ql Statements

    1/42

    Chapter 1

    Writing Basic

    SQL Statements

    1

  • 8/20/2019 Basics Ql Statements

    2/42

    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'(luscommans

    Lesson Aim 

    To extract data from the database. you need to use the structured querylanguage (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

  • 8/20/2019 Basics Ql Statements

    3/42

    Capabilities of SQL S#L#C$ Statements

    Selection Projection

    Table1

    Table2

    Capabilities of SQL SELECT Statements

    & SELECT statement retrie#es information from the database. 'sing aSELECT 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 toselecti#ely restrict the ro"s that you see.

    %ro*ection You can use the ro*ection caability in SQL to choose thecolumns in a table that you "ant returned by your query. You can choose asfe" or as many columns of the table as you require.

    +oin You can use the *oin caability in SQL to bring together data that isstored in different tables by creating a lin, through a column that both thetables share. You "ill learn more about *oins in a later lesson.

    3

  • 8/20/2019 Basics Ql Statements

    4/42

    Basic S#L#C$ StatementSELECT [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 columnscolumn selects the named column.

    alias gi#es selected columns different headings.

    F ROM 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 andFROM 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.

  • 8/20/2019 Basics Ql Statements

    5/42

    Writing SQL Statements

    •SQL statements are not case sensitive)

    •SQL statements can be on one or more lines)•-ey!ors cannot be abbreviate or splitacross lines)•Clauses are usually place on separatelines)•$abs an inents are use to enhancereaability)

     *ritin+ SQL Statements

    'sing the follo"ing simle rules and guidelines you can construct #alidstatements 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 andcolumns are entered in lo"ercase.

    • 2ithin SQL$%lus. a SQL statement is entered at the SQL romt and the subsequentlines are numbered. This is called the SQL buffer. 3nly one statement can be currentat 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.

    !

  • 8/20/2019 Basics Ql Statements

    6/42

    Selecting All Columns

    S#L#C$ '

    *+O emp.

    EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

    5678 S9!T: CLE40 58;< =5>==8?; ?;;

  • 8/20/2019 Basics Ql Statements

    7/42

    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%T3 -&9E. and L3C. The tablecontains four ro"s one for each deartment.

    You can also dislay all columns in The table by listing all the columns afterthe SELECT ,ey"ord. /or examle the follo"ing SQL statement li,e theexamle 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

    %

  • 8/20/2019 Basics Ql Statements

    8/42

    Selecting Specific Columns

    S#L#C$ ename, eptno, hireate

    *+O emp.

    ENAME DEPTNO HIREDATE

    S9!T: ==8?;

    &LLE 6; ;=8?=

    2&4- 6;

  • 8/20/2019 Basics Ql Statements

    9/42

    Column 0eaing &efaults

    &efault justification

    -Left" &ate an character ata-+ight" /umeric ata

    &efault isplay" ppercase

    -

    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/20/2019 Basics Ql Statements

    10/42

    Arithmetic #%pressions

    Create e%pressions on /B#+ an &A$# ata by using arithmeticoperators)

    '-erator Descri-tion

    A..  

    S"btract

    ("lti-l3

    /  Di4i.e

     Arit5metic E6-ressions

    You may need to modify the "ay in "hich data is dislayed erformcalculations or loo, at "hatDif scenarios. This is ossible using arithmeticexressions &n arithmetic exression may contain column names constantnumeric #alues and the arithmetic oerators.

     Arit5metic '-erators

    The slide lists the arithmetic oerators a#ailable in SQL You can usearithmetic oerators in am clause of a SQL statement excet the /439clause

    1(

  • 8/20/2019 Basics Ql Statements

    11/42

    sing Arithmetic Operators

    S#L#C$ ename, sal, sal2344*+O emp.

    ENAME SAL SAL+300

    S9!T: ?;; ==;;

    &LLE =7;; =8;;

    2&4- =

  • 8/20/2019 Basics Ql Statements

    12/42

    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 clarifystatements)

    '-erator )ece#ence

    !f an arithmetic exression contains more than one oerator multilicationand 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 bee#aluated first.

    12

  • 8/20/2019 Basics Ql Statements

    13/42

    Oe!"to! P!ece#ence 

    S#L#C$ ename, sal, 19 ' sal 2 144*+O emp .

    ENA(E SAL 12SAL188

    SM+T &(( '%((

    ALLE- 1"(( 1'3((

    AR/ 12!( 1!1((

    0O-ES 2'%! 3!&((

    MART+- 12!( 1!1((

    LAE 2&!( 33((

    CLAR 2!( 2'!((

    SCOTT 3((( 3"1((+- !((( "(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 annualcomensation of emloys !t calculates the annual comensation as =<multilied by the monthly salary lus a oneDtime bonus of =;; oticethat multilication is erformed before addition.

    Note: 'se arentheses to reinforce the standard order of recedence andto imro#e clarity . /or examle the exression abo#e can be "ritten as(=

  • 8/20/2019 Basics Ql Statements

    14/42

    sing (aranthesis

    S#L#C$ ename, sal, 19 ' :sal 2 144;

    *+O emp.

    ENAME SAL $%&'SAL+$00(

    S9!T: ?;; =;?;;

    &LLE =7;;

  • 8/20/2019 Basics Ql Statements

    15/42

    &efining a /ull

  • 8/20/2019 Basics Ql Statements

    16/42

    /ull

  • 8/20/2019 Basics Ql Statements

    17/42

    &efining a Column Alias

    • R$n"$! " ,lun &$"*n)

    • I! u!$ul %& "lul"%,n!

    • I$*"%$l5 ,ll,! ,lun n"$6 ,/%,n"l AS

    +$5,.* '$%$$n ,lun n"$ "n* "l"!

    • R$7u.$! *,u'l$ 7u,%"%,n ".+! % ,n%"n!

    !/"$! ,. !/$"l &"."%$.! ,. ! "!$ !$n!%#$

    Col"mn Aliases

    2hen dislaying the result of a query. SQL$%lus normalMN uses the name ofthe selected column as the column heading. !n many cases this heading maynot be descriti#e and hence is difficult to understand. You can change acolumn heading by using a column alias.

    Secify the alias after the column in the SELECT list using a sace as asearator. y default alias headings aear in uercase. !f the aliascontains saces secial characters (such as O or ) or is case sensiti#eenclose the alias in double quotation mar,s (P P).

     

    1%

  • 8/20/2019 Basics Ql Statements

    18/42

    U!n) C,lun Al"!$! 

    S#L#C$ ename AS A , sal aa=*+O emp.

     AD (AA<

    SM+T &((

    ALLE- 1"((

    AR/ 12!(

    0O-ES 2'%!

    MART+- 12!(

    LAE 2&!(

    CLAR 2!(

    SCOTT 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 theemloyees. otice mat the otional &S ,ey"ord has been used before thecolumn 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 thecolumn aliases name and salary in lo"ercase "hereas the result of the

    query dislays the column headings in uercase. &s mentioned in die lastslide column headings aear in uercase by default.

    The second examle dislays the name and annual salary of all theemloyees. ecause &nnual Salary contains saces it has beenenclosed in double quotation mar,s. otice that the column heading inthe outut is exactly the same as the column alias.

     

    1&

  • 8/20/2019 Basics Ql Statements

    19/42

    Col"mn Aliases 9contin"e.:

    The second examle dislays the name and annual salary of all theemloyees. ecause &nnual Salary contains saces it has been

    enclosed in double quotation mar,s. otice that the column heading inthe outut is exactly the same as the column alias.

    S#L#C$ ename AS >A> , sal >aa= >*+O emp.

    A# Maa7SM+T &((

    ALLE- 1"((

    AR/ 12!(

    0O-ES 2'%!

    MART+- 12!(

    LAE 2&!(

    CLAR 2!(

    SCOTT 3(((

    +- !(((

    T4R-ER 1!((

    A/AMS 11((

    0AMES '!(

    FOR/ 3(((

    M+LLER 13((

    =@ ro"s selected.

     

    1'

  • 8/20/2019 Basics Ql Statements

    20/42

    Col"mn Aliases 9contin"e.:

    S#L#C$ ename >A?>, sal ' 19 >@?ll?7 cret>*+O emp .

    A#, -,)),. /c

    S9!T: 87;;

    &LLE =8

  • 8/20/2019 Basics Ql Statements

    21/42

    Concatenation Operator 

    Concatenates columns or character strings to othercolumns

    s represente by t!o vertical bars :  ;

    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 (  ) Columns on cither side of the oerator are

    combined to ma,e a single outut column.

    21

  • 8/20/2019 Basics Ql Statements

    22/42

    sing the Concatenation Operator 

    SELECT ename *ob &S Emloyees

      /439 em 1

    E)o1ee

    S9!T:CLE40

    &LLES&LES9&

    2&4-S&LES9&

    +3ES9&&BE4

    9&4T!S&LES9&

    L&0E9&&BE4

    CL&409&&BE4

    SC3TT&&LYST

    0!B%4ES!-ET

    T'4E4S&LES9&

    &-&9SCLE40

    +&9ESCLE40

    /34-&&LYST

    9!LLE4CLE40

    =@ ro"s selected.

    Concatenation '-erator 9contin"e.:

    !n the examle. E&9E and +3 are concatenated and they are gi#enthe alias Emloyees. otice that the emloyee number and *ob arecombined to ma,e a single outut column.

    The &S ,ey"ord before the alias name ma,es the SELECT clauseeasier to read.

    22

  • 8/20/2019 Basics Ql Statements

    23/42

    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

  • 8/20/2019 Basics Ql Statements

    24/42

    sing Literal Character Strings

    SELECT en"e 22 i " 22 22 4o5

    AS 6E)o1ee Det"i)67ROM e 8

    E)o1ee Det"i)

    S9!T:is a CLE40

    &LLEis a S&LES9&

    2&4-is a S&LES9&

    +3ESis a 9&&BE49&4T!is a S&LES9&

    L&0Eis a 9&&BE4

    CL&40is a 9&&BE4

    SC3TTis a &&LYST

    0!Bis a %4ES!-ET

    T'4E4is a S&LES9&

    &-&9Sis a CLE40

    +&9ESis a CLE40/34-is a &&LYST

    9!LLE4is a CLE40

    =@ ro"s selected.

    Literal C8aacte Stin9s 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 thesingle quotation mar,s m the SELECT statement. The saces imro#e thereadability of the outut

    2

  • 8/20/2019 Basics Ql Statements

    25/42

     Literal C8aacte Stin9s 5continue#6

    !n the follo"ing examle the name and salary for each emloyee isconcatenated "ith a literal to gi#e the returned ro"s more meaning.

    S#L#C$ ename E " E E1E E Ayl?7 cret F E sal Ayl?7

      *+O emp .

    A-LI9

    S9!T: = &ylR, cret J ?;;

    &LLE = &ylR, cret J =7;;

    2&4- = &ylR, cret J =

  • 8/20/2019 Basics Ql Statements

    26/42

    &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"

  • 8/20/2019 Basics Ql Statements

    27/42

    #liminating &uplicate +o!s#liminate uplicate ro!s by using the &S$/C$ 7ey!or in the S#L#C$clause,

    SELECT DISTINCT .e-tno

    %&'( em-/

    DEPTNO

    6;

  • 8/20/2019 Basics Ql Statements

    28/42

    SQL an SQL'(lus

    nteraction

    SQL an. SQLPl"s

    SOL is a command language for communication "ith the 3racle Ser#er fromany tool or alication. 3racle SQL contains many extensions. 2hen youenter a SQL statement it is stored in a art of memory called the SOL bufferand remains there until you enter a ne" statement.

    SOL*Plus is an 3racle tool that recogniHes and submits SQL statements tothe 3racle Ser#er or for cxecution and contains its o"n command language.

    S:L&P)u+ Se!;e!

    SQLPL7SComman.s

    Q"er3&es"lts

    %ormatte.&e-ort

    2&

    SQLStatements

    )"ffer SQLStatements

    %ormatte. '"t "t

  • 8/20/2019 Basics Ql Statements

    29/42

    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 maintainingsystems.

    • !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'

  • 8/20/2019 Basics Ql Statements

    30/42

    SQL Statements

  • 8/20/2019 Basics Ql Statements

    31/42

    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 anappen 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 hich you can do the follo"ingExecute SQL statements to retrie#e modify add. and remo#e data fromthe database /ormat erform calculations on. store and rint queryresults in the form of reorts Create scrit files to store SQL statementsfor reetitue use in the future

    SQL$%lus commands can be di#ided into the follouing main categories.

    C"tego!1  Pu!oe 

    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 maniulatethe SQL$%lus en#ironment and dislay column definitions 

    31

  • 8/20/2019 Basics Ql Statements

    32/42

    &isplaying $able Structure

    se the SQL'(lus SC+B# comman to isplay the structureof a table)

    -ESCU4!EV tablename

    &isplaying T"'l$ S%.u%u.$

    !n SQL$%lus you can dislay The structure of a table using the-ESC4!E command. The result of the command is to see the columnnames and datatyes as "ell as "hether a column must contain data.

    !n the syntax

    tablename is the name of any existing table #ie" or synonymaccessible to the user.

    DESC em- /

    /ame /ullG $ype

    EMPNO NOT NULL NUMBER849

    ENAME :ARCHAR28109

    ;OB :ARCHAR28

  • 8/20/2019 Basics Ql Statements

    33/42

    &isplaying $able Structure

    D!/l"5n) $able S%.u%u.$ 8,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 'LLindicates 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 Dec!ition

    N=MBER'>(  umber #alue ha#ing a maximum numberof digits the number of digits to theright of the decimal oint s

    *ARCHAR%'( WariableDlength character #alue ofmaximum siHe s

    DATE -ate and time #alue bet"een +anuary =.@5=< .C. and -ecember 6=.8888 &.-

    CHAR'( /ixedDlength character #alue of siHe s

     

    33

  • 8/20/2019 Basics Ql Statements

    34/42

    SQL'(lus #iting CommansAH((#/&I text

    CH0A/J#I 5ol 5ne!

    CH0A/J#I te%t

    CLH#A+I B**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 theSQL 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 '//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 terminatorcharacters (semicolon or slash) or ressing U4eturnV t"ice. You thensee the SQL romt.

    +nto#uction to 'racle> SQL an. PL?SQL 1@8 

    3

  • 8/20/2019 Basics Ql Statements

    35/42

    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 buffern  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 ahyhen (D).

    3!

  • 8/20/2019 Basics Ql Statements

    36/42

    SQLPl"s %ile Comman.s

    SA;E filename

    ET 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 theen#ironment format query results and manage files. You can use the commands identifiedin the follo"ing table

    Co"n# Dec!ition

    S&WfE+ filename U.extVU4E%UL&CL+&%%UE-VV

    Sa#es current contents of SQL buffer to afile. 'se &%%E- to add to an existing fileuse 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 saed fileto the SQL buffer. The default extension forthe filename is D sql.

    STA;RT< filename[ext] 4uns a re#iously sa#ed command file.

     filename 4uns a re#iously saed command file (sameas ST&4T).

    E- U!TV !n#o,es the editor and sa#es the buffercontents to a file named

    E/;+T< [filename[ext]] !n#o,es the editor to edit contents to a sa#edfile

    S)O;OL< ;=ilename;e>t

  • 8/20/2019 Basics Ql Statements

    37/42

    S"mmar3

    SELECT ;/+ST+-CT< *,column ;alias< , . . . B FROM table

    se SQL'(lus as an environment to"

    •#%ecute SQL statements

    •#it SQL statements

    S#L#C$ S%"%$$n%

    !n this lesson you ha#e learned about retrie#ing data from a database table "ith theSELECT 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= eD :o#s ae

    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%

  • 8/20/2019 Basics Ql Statements

    38/42

    (ractice Overvie!

    • Selecting all ata from ifferent tables

    • &escribing the structure of tables

    • (erforming arithmetic calculations an specifying columnnames

    • sing SQL'(lus eitor 

    P."%$ O#$.#$

    This is the first of many ractices. The solutions (if you require them) can befound in &endix &. %ractices are intended to introduce all toics co#eredin the lesson. Questions

  • 8/20/2019 Basics Ql Statements

    39/42

    P!"ctice $=.!nitiate a SQL$%lus session using the user !- and ass"ord ro#ided by theinstructor./alse6.2ill the SELECT statement execute successfully]

    True>/alse

     SHLI SELECT ename, Job, sal SalaD  FROM emp $

    @.2ill the SELECT statement execute successfully]

    True>/alse

     SHLI SELECT *  FROM sal9a#e

    (. There are four coding errors in this statement. Can you identify them]SHLI SELECT empno, ename

    SalaD > 12 A--4AL SALARK

      FROM emp$

    7. Sho" the structure of the -E%T table. Select all data from the -E%T table.

    N"e Nu))@ T1e

    -E%T3 '9E4(

  • 8/20/2019 Basics Ql Statements

    40/42

    (ractice 1 8,n%nu$*95. Sho" the structure of the E9% table. Create a query to dislay the name

     *ob. hire date and emloyee number for each emloyee "ith emloyeenumber aearing first. Sa#e your SQL statement to a file named pl).s)l.

    N"e Nu))@ T1e

    E9%3 3T 'LL '9E4(@)

    E&9E W&4C:&4

  • 8/20/2019 Basics Ql Statements

    41/42

    (ractice 1 :continue;

    8. Create a query to dislay unique *obs from the E9% table.

    B')

    A-ALKSTCLERMA-AER)RES+/E-TSALESMA-

    !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- Em-lo3ee Bob 0ire Date 

    %33' +- )RES+/E-T 1%-O&1 %"'& LAE MA-AER  (1 MAK &1 %%&2 CLAR MA-AER  ('04-&1 

    %!"" 0O-ESMA-AER

     (2 A)R &1

     %"! MART+- SALESMA-  2&SE)&1 %'' ALLE- SALESMA-  2(FE&1 %& T4R-ER SALESMA-  (&SE)&1 %'(( 0AMES CLER  (3/EC&1 %!21 AR/ SALESMA-  22FE&1 %'(2 FOR/ A-ALKST  (3/EC&1 %3"' SM+T CLER  1%/EC&( %%&& SCOTT A-ALKST  ('/EC&2 %&%" A/AMS CLER  12 0A- & 3 %'3 M+LLER CLER  2 3 0A- & 2 

    1 o:s selecte# 

    1

  • 8/20/2019 Basics Ql Statements

    42/42

    (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

    +-, )RES+/E-T LAE, MA-AER

    CLAR, MA-AER 0O-ES, MA-AER

    MART+-, SALESMA- ALLE-,

    SALESMA- T4R-ER, SALESMA-

    0AMES, CLER AR/, SALESMA-

    FOR/, A-ALKST SM+T, CLER

    SCOTT, A-ALKST A/AMS, CLER

    M+LLER, CLER 1 o:s selecte#.

    !f you "ant extra challenge comlete the follo"ing exercise

    =