You are on page 1of 28

SQL Interview Questions For Software Testers

Software testing - Questions and Answers - SQL Interview


Questions
SQL recognizes 4 general type of Data
Character Strings
These are sentences sy!"ols or "oth
#A$C%A$& and C%A$
'ath functions can not "e done at the
character string data(
)*'+$IC Data
DAT+ Ti!esta!p )u!"ers and Integer
,--L+A)S
,oolean values are either True or .alse(
)*LLS
A )*LL #alue indicates that nothing e/ists in
that field
Allowing )*LL values can "e decided at the
ti!e of creating the ta"le(
0hat is an -perator1
-perators are a !eans "y which SQL can !anipulate
nu!"ers and strings or test for e2uality
.our types of -perator
Arith!etic
$ange
+2uality and
Logical
$D,'S provides a lot of "uilt in .unctions to perfor!
an operation( And it is an e/cellent tool inside a
2uery(
S+L+CT C-*)T345 .$-' ta"le one6
An +/pression is a special state!ent that returns a
value
4 types of +/pressions
,oolean
The ,oolean +/pression returns the True7.alse
$esult
)u!eric
It generally returns a )u!"er
A#835
S*'35
Character
Character e/pressions are used to test for
values of a string
Date +/pressions
Date and Ti!esta!ps
Q( 0hat is a pri!ary 9ey1
A( :ri!ary 9ey ; +ach row of the data in a ta"le uni2uely
identified "y a :ri!ary <ey The colu!n 3colu!ns5 that has
co!pletely uni2ue data throughout the ta"le is 9nown as the
pri!ary 9ey field(
pri!ary 9ey also called a pri!ary 9eyword is a 9ey in a
relational data"ase that is uni2ue for each record( It is a
uni2ue identifier such as a driver license nu!"er
telephone nu!"er 3including area code5 or vehicle
identification nu!"er 3#I)5( A relational data"ase !ust
always have one and only one pri!ary 9ey( :ri!ary 9eys
typically appear as colu!ns in relational data"ase ta"les(
Primary Key
A primary key is a property given to a ta"le colu!n
that distinguishes that record apart fro! each
There are = Types of :ri!ary <ey
Si!ple :ri!ary <ey
Co!posite :ri!ary <ey
Surrogate :ri!ary <ey
Si!ple :ri!ary <ey
It uses only one field to identify a record(
Co!posite :ri!ary <ey
'ultiple fields >oined together to identify a
record in a ta"le(
Surrogate <ey
*ni2ue running se2uence nu!"er is generated to
identify a record(
Q( 0hat is the !ain role of a pri!ary 9ey in a ta"le1
A( The !ain role of a pri!ary 9ey in a data ta"le is to
!aintain the internal integrity of a data ta"le(
Q( 0hat are foreign 9eys1
A(.oreign 9ey also called a foreign 9eyword in a data"ase
ta"le is a 9ey fro! another ta"le that refers to 3or
targets5 a specific 9ey usually the pri!ary 9ey in the
ta"le "eing used( A pri!ary 9ey can "e targeted "y !ultiple
foreign 9eys fro! other ta"les( ,ut a pri!ary 9ey does not
necessarily have to "e the target of any foreign 9eys(
A( .oreign <ey ; ?ou can logically relate data fro! !ultiple
ta"les using .oreign <eys
Q( Can a ta"le have !ore than one foreign 9ey defined1
A( A ta"le can have any nu!"er of foreign 9eys defined( It
can have only one pri!ary 9ey defined(
Q( 0hat is difference "etween *)IQ*+ and :$I'A$? <+?
constraints1
A ta"le can have only one :$I'A$? <+? whereas there can "e
any nu!"er of *)IQ*+ 9eys(
The colu!ns that co!pose :< are auto!atically define )-T
)*LL whereas a colu!n that co!pose a *)IQ*+ <+? can have
null values(
Q( Can a pri!ary 9ey contain !ore than one colu!ns1
?es( :ri!ary 9ey created on !ore than one colu!n is called
co!posite pri!ary 9ey(
Constraints
The -racle Server uses constraints to prevent invalid data
entry into ta"les(
?ou can use constraints to do the following;
@ +nforce rules on the data in a ta"le whenever a row is
inserted updated or deleted fro! that
ta"le( The constraint !ust "e satisfied for the operation to
succeed(
@ :revent the deletion of a ta"le if there are dependencies
fro! other ta"les
@ :rovide rules for -racle tools such as -racle Developer
CONSTRAINTS
PRIMARY CONSTRAINTS ; :$I'A$?*)IQ*+C%+C<$+.+$+)C+S5
SECONARY CONSTRAINTS ! )-T )*LLD+.A*LT5
C-)ST$AI)TS ,AS+D -) & L+#+LS C-L*') L+#+L and TA,L+ L+#+L
E"# For SECONARY CONSTRAINTS
AAAAAAAAAAAAAAAAAAAAAAA
B( )-T )*LL C-)ST$AI)T
C$+AT+ TA,L+ +':C3 +)- )*',+$3=5 )-T )*LL
+)A'+ #A$C%A$&3BD556
&( D+.A*LT C-)ST$AI)T
C$+AT+ TA,L+ +':C3+)- )*',+$3=5 )-T )*LL
+)A'+ #A$C%A$&3BD5D-E DAT+ D+.A*LT S?SDAT+56
=( :$I'A$? C-)ST$AI)T3C-L*') L+#+L5
*)IQ*+
C$+AT+ TA,L+ +':C3 +)- )*',+$3=5 )-T )*LL C-)ST$AI)T
*)I+':
*)IQ*++)A'+ #A$C%A$&3BD556
:$I'A$? <+?
C$+AT+ TA,L+ +':C3 +)- )*',+$3=5 C-)ST$AI)T :<+':C
:$I'A$? <+?+)A'+
#A$C%A$&3BD556
4( C%+C< C-)ST$AI)T
C$+AT+ TA,L+ ,A)<3 AC)- )*',+$3&5 C-)ST$AI)T :<,A)<
:$I'A$?
<+?ACT?:+ #A$C%A$&3&5 C-)ST$AI)T C<,A)< C%+C<
3ACT?:+ I)
3FS,FFCAFF$DF55AC)A'+ #A$C%A$&3BD5A'-*)T
)*',+$34556
G( $+.+$+)C+S
C$+AT+ TA,L+ +':C3 +)- )*',+$3=5 C-)ST$AI)T :<+C
:$I'A$? <+?E-, #A$C%A$&3BD5+)A'+
#A$C%A$&3BD5'8$ )*',+$345 $+.+$+)C+S +':C3+)-556
H( $+.+$+)C+S3$+.+$I)8 T- DI..+$+)T TA,L+5
C$+AT+ TA,L+ D+:TC3D+:T)- )*',+$3&5 C-)ST$AI)T :<D)-
:$I'A$? <+? D)A'+ #A$C%A$&3BD5 L-C #A$C%A$&3BD556
C$+AT+ TA,L+ +':C3 +':)- )*',+$345+)A'+
#A$C%A$&3BD5
SAL )*',+$3I&5D+:T)- )*',+$3&5 C-)ST$AI)T .<D)-
$+.+$+)C+S D+:TC3D+:T)-556
I( TA,L+ L+#+L C-)ST$AI)TS
*)IQ*+ TA,L+ L+#+L
C$+AT+ TA,L+ ,A)<3 AC)- )*',+$3=5ACT?:+ #A$C%A$&3BD5
,AL )*',+$3I&5:LAC+ #A$C%A$&3BD5C-)ST$AI)T
*)I,A)< *)IQ*+3AC)-ACT?:+556
:$I'A$? <+?3TA,L+ L+#+L5
C$+AT+ TA,L+ ,A)<3 AC)- )*',+$3&5 ACT?:+ #A$C%A$&3&5
C-)ST$AI)T C<,A)< C%+C< 3ACT?:+ I)
3FS,FFCAFF$DF55A'-*)T )*',+$5
ATA Inte"rity Constraints
Constraint es$ri%tion
Descri"e the different type of Integrity Constraints
supported "y -$ACL+ 1
)-T )*LL Constraint - Disallows )*LLs in a ta"leFs
colu!n(
*)IQ*+ Constraint - Disallows duplicate values in a
colu!n or set of colu!ns(
:$I'A$? <+? Constraint - Disallows duplicate values and
)*LLs in a colu!n or set of colu!ns(
.-$+I8) <+? Constrain - $e2uire each value in a colu!n or
set of colu!ns !atch a value in a related ta"leFs *)IQ*+ or
:$I'A$? <+?(
C%+C< Constraint - Disallows values that do not
satisfy the logical e/pression of the constraint(
0hat is difference "etween *)IQ*+ constraint and :$I'A$? <+?
constraint1
A colu!n defined as *)IQ*+ can contain )*LLs while a
colu!n defined as :$I'A$? <+? canFt contain )ulls(
0hat are the Li!itations of a C%+C< Constraint 1
The condition !ust "e a ,oolean e/pression evaluated
using the values in the row "eing inserted or updated and
canFt contain su"2ueries se2uence the S?SDAT+*ID*S+$ or
*S+$+)# SQL functions or the pseudo colu!ns L+#+L or
$-0)*'(
0hat is the !a/i!u! nu!"er of C%+C< constraints that can "e
defined on a colu!n 1
)o Li!it(
Q 0hat is an Inde/ 1
An Inde/ is an optional structure associated with a ta"le
to have direct access to rows which can "e created to
increase the perfor!ance of data retrieval( Inde/ can "e
created on one or !ore colu!ns of a ta"le(
Inde/es are auto!atically !aintained and used "y -$ACL+(
Changes to ta"le data are auto!atically incorporated into
all relevant inde/es(
In&e'es
Inde/ will i!prove the throughput of the SQL Query(
The Inde/ should "e created "ased on the fre2uently
used colu!ns in the 0%+$+ clause(
C$+AT+ I)D+J e!pKid/ -) e!p3e!pKna!e56
S+L+CT ena!edeptKidsal!gr
.$-' +':
0%+$+ ena!e li9e LSunMN6

Q 0hat is the Su"2uery 1
A Su"2uery is a 2uery whose return values are used in
filtering conditions of the !ain 2uery(
Q( 0hat is correlated su"-2uery 1
A Correlated su"K2uery is a su"K2uery which has reference
to the !ain 2uery(
Q( 0hat is an Integrity Constraint 1
A Integrity constraint is a rule that restricts
values to a colu!n in a ta"le(
Q( 0hat is $eferential Integrity 1
A 'aintaining data integrity through a set of rules that
restrict the values of one or !ore colu!ns of the ta"les
"ased on the values of pri!ary 9ey or uni2ue 9ey of the
referenced ta"le(
Q( what is Case .unction
Case facilitates conditional in2uires "y doing the wor9 of
an if-then-else state!ent
CASE TATEMENT
In SQL case wor9s with either the select or update clauses(
It provides when-then-else functionality 30%+) this_happens
T%+) do_this5 also 9nown as nested I.-T%+)-+LS+ - I.
conditional state!ents
S+L+CT ena!e
CAS+ 0%+) sal O D A)D SAL PQ BDDDDD T%+) B
0%+) sal O BDDDDD A)D SAL P &GDDDD T%+) &
0%+) sal O &GDDDD A)D SAL P GDDDDDD T%+) =
+LS+ CC
+)D AS e!pKcategory
.$-' +':
Q( Decode function
Decode ; facilitates conditional in2uires "y doing the wor9
of a case or if then else state!ent
S+L+CT supplierKna!e
decode3supplierKid BDDDD FI,'F
BDDDB F'icrosoftF
BDDD& F%ewlett :ac9ardF
F8atewayF5 result
.$-' suppliers6
Q( %ow you will avoid duplicating records in a 2uery1
A ,y using DISTI)CT
Q( 0hat is difference "etween $ena!e and Alias1
$ena!e is a per!anent na!e given to a ta"le or colu!n
whereas Alias is a te!porary na!e given to a ta"le or colu!n
which do not e/ist once the SQL state!ent is e/ecuted(

Q( 0hat is a view 1
A view is a virtual ta"le "ased on one or !ore ta"les(
0hy *se views 1
To restrict data access
@ To !a9e co!ple/ 2ueries easy
@ To provide data independence
@ To present different views of the sa!e data
Q( 0hat are the advantages of #iews 1
@ #iews restrict access to the data "ecause the view can
display selective colu!ns fro! the ta"le(
@ #iews can "e used to !a9e si!ple 2ueries to retrieve the
results of co!plicated 2ueries( .or e/a!ple views can "e
used to 2uery infor!ation fro! !ultiple ta"les without the
user 9nowing how to write a >oin state!ent(
@ #iews provide data independence for ad hoc users and
application progra!s( -ne view can "e used to retrieve data
fro! several ta"les(
@ #iews provide groups of users access to data according to
their particular criteria(
:rovide an additional level of ta"le security "y
restricting access to a predeter!ined set of rows and
colu!ns of a ta"le(
%ide data co!ple/ity(
Si!plify co!!ands for the user(
:resent the data in a different perpecetive fro! that of the
"ase ta"le(
Store co!ple/ 2ueries(
Q( 0hat are various privileges that a user can grant to
another user1
S+L+CT
C-))+CT
$+S-*$C+S
Q( 0hat is sche!a1
A sche!a is collection of data"ase o">ects of a *ser(
Q( what is Ta"le 1
A ta"le is the "asic unit of data storage in an -$ACL+
data"ase( The ta"les of a data"ase hold all of the user
accessi"le data( Ta"le data is stored in rows and colu!ns(
Q( Do #iew contain Data1
#iews do not contain or store data(
Q( Can a #iew "ased on another #iew 1
?es(
Q( 0hat is a Se2uence 1
A se2uence generates a serial list of uni2ue nu!"ers for
nu!erical colu!ns of a data"aseFs ta"les(
Q( 0hat is a Synony! 1
A synony! is an alias for a ta"le view se2uence or progra!
unit(
There are two types of Synony!s :rivate and :u"lic(
A :rivate Synony!s can "e accessed only "y the owner(
A :u"lic synony!s can "e accessed "y any user on the
data"ase(
Synony!s are used to ; 'as9 the real na!e and owner of an
o">ect(
:rovide pu"lic access to an o">ect
:rovide location transparency for ta"lesviews or progra!
units of a re!ote data"ase(
Si!plify the SQL state!ents for data"ase users(
Q( 0hat is difference "etween T$*)CAT+ R D+L+T+ 1
T$*)CAT+ co!!its after deleting entire ta"le i(e( can not
"e rolled "ac9( Data"ase triggers do not fire on T$*)CAT+
D+L+T+ allows the filtered deletion( Deleted records can "e
rolled "ac9 or co!!itted(
Data"ase triggers fire on D+L+T+(
A&vanta"es of COMMIT an& ROLL(ACK Statements
0ith C-''IT and $-LL,AC< state!ents you can;
@ +nsure data consistency
@ :review data changes "efore !a9ing changes per!anent
@ 8roup logically related operations
Q( Difference "etween S*,ST$ and I)ST$ 1
I)ST$ 3StringBString&3n3!55
I)ST$ returns the position of the !th occurrence of
the string & in
stringB( The search "egins fro! nth position of stringB(
S*,ST$ 3StringB n!5
S*,ST$ returns a character string of size ! in stringB
starting fro! nth postion of stringB(
Q( +/plain *)I-) 'I)*S *)I-) ALL I)T+$S+CT 1
I)T+$S+CT returns all distinct rows selected "y "oth
2ueries(
'I)*S - returns all distinct rows selected "y the first
2uery "ut not "y the second(
*)I-) - returns all distinct rows selected "y either 2uery
*)I-) ALL - returns all rows selected "y either
2uery including all duplicates(
)nion
The union clause places two separate 2ueries together
for!ing one ta"le( A union wor9s "est when using two
ta"les with si!ilar colu!ns "ecause each colu!n !ust
have the sa!e data type
S+L+CT dno .$-' e!p
*)I-)
S+L+CT dno .$-' dept6
*)I-) ALL selects all rows fro! each ta"le and co!"ines
the! into a single ta"le
The Difference "etween *)I-) and *)I-) ALL
The difference "etween *nion and *nion all is that
*nion all will not eli!inate duplicate rows instead it
>ust pulls all rows fro! all ta"les fitting your 2uery
specifics and co!"ines the! into a ta"le
Q( 0hat is $-0ID 1
$-0ID is a pseudo colu!n attached to each row of a
ta"le( It is BS character long "loc9no rownu!"er are the
co!ponents of $-0ID(
Q( 0hat is the fastest way of accessing a row in a ta"le 1
*sing $-0ID(
Q( 0hat is difference "etween C%A$ and #A$C%A$& 1
0hat is the !a/i!u! SIT+ allowed for each type 1
C%A$ pads "lan9 spaces to the !a/i!u! length( #A$C%A$&
does not pad "lan9 spaces( .or C%A$ it is &GG and &DDD for
#A$C%A$&(
Q( %ow !any L-)8 colu!ns are allowed in a ta"le 1 Is it
possi"le to use L-)8 colu!ns in 0%+$+ clause or -$D+$ ,? 1
A -nly one L-)8 colu!ns is allowed( It is not possi"le to
use L-)8 colu!n in 0%+$+ or -$D+$ ,? clause(
Q( 0hat is Data"ase Lin9 1
A data"ase lin9 is a na!ed o">ect that descri"es a UpathU
fro! one data"ase to another(
:rivate Data"ase Lin9 :u"lic Data"ase Lin9 R )etwor9
Data"ase Lin9(
:rivate data"ase lin9 is created on "ehalf of a specific
user( A private data"ase lin9 can "e used only when
the owner of the lin9 specifies a glo"al o">ect na!e in a
SQL state!ent or in the definition of the ownerFs views or
procedures(
:u"lic data"ase lin9 is created for the special user group
:*,LIC( A pu"lic data"ase lin9 can "e used when any
user in the associated data"ase specifies a glo"al o">ect
na!e in a SQL state!ent or o">ect definition(
)etwor9 data"ase lin9 is created and !anaged "y a networ9
do!ain service( A networ9 data"ase lin9 can "e used
when any user of any data"ase in the networ9 specifies a
glo"al o">ect na!e in a SQL state!ent or o">ect
definition(
Q( 0hich is !ore faster - I) or +JISTS1
+JISTS is !ore faster than I) "ecause +JISTS returns a
,oolean value whereas I) returns a value(
Q( 0hat is a >oin1
A( Eoin is a process of retrieve pieces of data fro!
different sets 3ta"les5 and returns the! to the user or
progra! as one VWX>oinedVW collection of data(
*oin + )nion
E-I)
The join clause co!"ines colu!ns of one ta"le to
that of another to create a single ta"le
A >oin 2uery does not alter either ta"le "ut
te!porarily co!"ines data fro! each ta"le to "e
viewed as a single ta"le
= different types of Eoin
Inner
Left
$ight
Inner ,oin
An inner >oin returns all rows that result in a !atch
such as the e/a!ple a"ove(
S+L+CT a(ena!e"(dna!ee(sale(!gr
.$-' e!p a
dept "
0%+$+ a(dno Q "(dno
Ty%es of *oins
- E.ui,oins
- Non/e.ui,oins
- Outer ,oins
- Se0f ,oins
- Cross ,oins
- Natura0 ,oins
- Fu00 or outer ,oins
E.ui,oins
To deter!ine an e!ployeeNs depart!ent na!e you co!pare the
value in the D+:A$T'+)TKID
colu!n in the +':L-?++S ta"le with the D+:A$T'+)TKID values
in the D+:A$T'+)TS ta"le( The relationship "etween the
+':L-?++S and D+:A$T'+)TS ta"les is an equijoinYthat is
values in the D+:A$T'+)TKID colu!n on "oth ta"les !ust "e
e2ual( .re2uently this type of >oin involves pri!ary and
foreign 9ey co!ple!ents(
Note! +2ui>oins are also called simple joins or inner joins(
Non/E.ui,oins
A non-e2ui>oin is a >oin condition containing so!ething
other than an e2uality operator(
The relationship "etween the +':L-?++S ta"le and the
E-,K8$AD+S ta"le has an
e/a!ple of a non-e2ui>oin( A relationship "etween the two
ta"les is that the SALA$?
colu!n in the +':L-?++S ta"le !ust "e "etween the values in
the L-0+STKSALA$?
and %I8%+STKSALA$? colu!ns of the E-,K8$AD+S ta"le( The
relationship is
o"tained using an operator other than e2uals 3Q5(
SELECT e#0ast1name2 e#sa0ary2 ,#"ra&e10eve0
FROM em%0oyees e2 ,o31"ra&es ,
45ERE e#sa0ary
(ET4EEN ,#0owest1sa0 AN ,#6i"6est1sa07
Outer ,oin ; to also see rows that do not !eet the >oin
condition
Returnin" Re$or&s wit6 No ire$t Mat$6 wit6 Outer *oins
If a row does not satisfy a >oin condition the row will not
appear in the 2uery result( .or e/a!ple in the e2ui>oin
condition of +':L-?++S and D+:A$T'+)TS ta"les e!ployee
8rant does not appear "ecause there is no depart!ent ID
recorded for her in the +':L-?++S ta"le( Instead of seeing
&D e!ployees in the result set you see BC records(
S+L+CT e(lastKna!e e(depart!entKid d(depart!entKna!e
.$-' e!ployees e depart!ents d
0%+$+ e(depart!entKid Q d(depart!entKid6
SELECT e#0ast1name2 e#&e%artment1i&2 &#&e%artment1name
FROM em%0oyees e2 &e%artments &
45ERE e#&e%artment1i&89: ; &#&e%artment1i& 7
Se0f *oin !
*oinin" a Ta30e to Itse0f
So!eti!es you need to >oin a ta"le to itself( To find the
na!e of each e!ployeeNs !anager you need to >oin the
+':L-?++S ta"le to itself or perfor! a self >oin( .or
e/a!ple to find the na!e of 0halenNs !anager you need to;
@ .ind 0halen in the +':L-?++S ta"le "y loo9ing at the
LASTK)A'+ colu!n(
@ .ind the !anager nu!"er for 0halen "y loo9ing at the
'A)A8+$KID colu!n( 0halenNs !anager nu!"er is BDB(
@ .ind the na!e of the !anager with +':L-?++KID BDB "y
loo9ing at the LASTK)A'+ colu!n( <ochharNs e!ployee nu!"er
is BDB so <ochhar is 0halenNs !anager(In this process you
loo9 in the ta"le twice( The first ti!e you loo9 in the
ta"le to find 0halen in the LASTK)A'+ colu!n and 'A)A8+$KID
value of BDB( The second ti!e you loo9 in the +':L-?++KID
colu!n to find BDB and the LASTK)A'+ colu!n to find <ochhar(
S+L+CT wor9er(lastKna!e ZZ N wor9s for N
ZZ !anager(lastKna!e
.$-' e!ployees wor9er e!ployees !anager
0%+$+ wor9er(!anagerKid Q !anager(e!ployeeKid 6
Left Outer *oin !
This 2uery retrieves all rows in the +':L-?++S ta"le which
is the left ta"le even if there is no !atch in the
D+:A$T'+)TS ta"le(This 2uery was co!pleted in earlier
releases as follows;
S+L+CT e(lastKna!e e(depart!entKid d(depart!entKna!e
.$-' e!ployees e depart!ents d
0%+$+ d(depart!entKid 3[5 Q e(depart!entKid6
S+L+CT e(lastKna!e e(depart!entKid d(depart!entKna!e
.$-' e!ployees e
L+.T -*T+$ E-I) depart!ents d
-) 3e(depart!entKid Q d(depart!entKid5 6
Left Outer *oin Synta'
A Left >oin returns all rows of the left of the conditional
even if there is no right colu!n to !atch
S+L+CT a(eno a(ena!ed(dna!e
.$-' e!p a L+.T -*T+$ E-I) dept "
-) a(dno Q "(dno6
Ri"6t Outer *oin !
S+L+CT e(lastKna!e e(depart!entKid d(depart!entKna!e
.$-' e!ployees e
$I8%T -*T+$ E-I) depart!ents d
-) 3e(depart!entKid Q d(depart!entKid5 6
E'am%0e of RI<5T O)TER *OIN
This 2uery retrieves all rows in the D+:A$T'+)TS ta"le
which is the right ta"le even if there is no !atch in the
+':L-?++S ta"le(
This 2uery was co!pleted in earlier releases as follows;
S+L+CT e(lastKna!e e(depart!entKid d(depart!entKna!e
.$-' e!ployees e depart!ents d
0%+$+ d(depart!entKid Q e(depart!entKid 3[56
Ri"6t Outer *oin
A $ight >oin returns all rows of the $ight of the
conditional even if there is no right colu!n to !atch
S+L+CT a(eno a(ena!e"(dno"(dna!e
.$-' e!p a $I8%T -*T+$ E-I) dept "
-) a(dno Q "(dno6
.ull outer >oin
SELECT e#0ast1name2 e#&e%artment1i&2 &#&e%artment1name
FROM em%0oyees e
F)LL O)TER *OIN &e%artments &
ON 8e#&e%artment1i& ; &#&e%artment1i&: 7
E'am%0e of F)LL O)TER *OIN
This 2uery retrieves all rows in the +':L-?++S ta"le even
if there is no !atch in the
D+:A$T'+)TS ta"le( It also retrieves all rows in the
D+:A$T'+)TS ta"le even if there is no !atch in the
+':L-?++S ta"le(
Cartesian Pro&u$ts
A Cartesian product results in all co!"inations of rows
displayed( This is done "y either o!itting the 0%+$+ clause
or specifying the C$-SS E-I) clause(
Ta30e A0iases
@ Ta"le aliases speed up data"ase access(
@ Ta"le aliases can help to 9eep SQL code s!aller "y
conserving !e!ory(
Q( 0hat 9inds of >oins do you 9now1 8ive e/a!ples(
A( 0e have self >oin outer >oint 3L+.T $I8%T5 cross-
>oin 3 Cartesian product n4! rows returned5
+/p;
outer >oint
S+L+CT +!ployee()a!e Depart!ent( Dept)a!e
.$-' +!ployee Depart!ent
0%+$+ +!ployee(+!ployeeKID Q Depart!ent(+!ployeeKID6
cross->oin
S+L+CT 4 .$-' ta"leB ta"le&6
self >oin
S+L+CT eB(na!e Z ZVW\ VW] Z Z e&(ena!e .$-' e!p eB e!p
e& 0%+$+ eB( e!pKno Q e&(e!pKno6
Q( %ow do you add record to a ta"le1
A( I)S+$T into ta"leKna!e #AL*+S 3VW]AL+JVW\ ==
VW]'VW\56
Q( %ow do you add a colu!n to a ta"le1
A( ALT+$ TA,L+ Depart!ent ADD 3A8+ )*',+$56
Q( %ow do you change value of the field1
A( *:DAT+ +':Kta"le set nu!"er Q &DD where ite!K!un"er Q
VW]CDVW\6
update na!eKta"le set status Q Fena"leF where phone Q
F4BHBBB&&&&F6
update S+$#IC+Kta"le set $+Q*+STKDAT+ Q toKdate 3F&DDH-D=-D4
DC;&CF Fyyyy-!!-dd hh&4;'IF5 where phone Q F4BHBBB&&&&F6
0hat does C-''IT do 1
C-''IT !a9es per!anent the changes resulting fro! all SQL
state!ents in the transaction( The changes !ade "y the SQL
state!ents of a transaction "eco!e visi"le to other user
sessions transactions that start only after transaction
is co!!itted(
0hat does $-LL,AC< do 1
$-LL,AC< retracts any of the changes resulting fro! the
SQL state!ents in the transaction
Q( 0hat is the highest value that can "e stored in a ,?T+
data field1
A( The highest value that can "e stored in a ,?T+ field is
&GG( or fro! -B&S to B&I( ,yte is a set of ,its that
represent a single character( *sually there are S ,its in a
,yte so!eti!es !ore depending on how the !easure!ent is
"eing !ade( +ach Char re2uires one "yte of !e!ory and can
have a value fro! D to &GG 3or D to BBBBBBBB in "inary5(
0hat is a :rocedure 1
A :rocedure consist of a set of SQL and :L7SQL state!ents
that are grouped together as a unit to solve a specific
pro"le! or perfor! a set of related tas9s(
Q( 0hat is a stored procedure1
A( A procedure is a group of :L7SQL state!ents that can "e
called "y a na!e( :rocedures do not return values they
perfor! tas9s(
Q( Descri"e how )*LLs wor9 in SQL1
A( The )*LL is how SQL handles !issing values(
Arifth!etic operation with )*LL in SQL will return a
)*LL(
0hat is a S)A:S%-T 1
Snapshots are read-only copies of a !aster ta"le located
on a re!ote node which is periodically refreshed to
reflect changes !ade to the !aster ta"le(
B=G( 0hat is a S)A:S%-T L-8 1
A snapshot log is a ta"le in the !aster data"ase that is
associated with the !aster ta"le( -$ACL+ uses a snapshot
log to trac9 the rows that have "een updated in the
!aster ta"le( Snapshot logs are used in updating the
snapshots "ased on the !aster ta"le(
Q( 0hat is )or!alization1
A( The process of ta"le design is called nor!alization(
Q( 0hat is referential integrity constraints1
A( $eferential integrity constraints are rules
that are partnof the ta"le in a data"ase sche!a(
0hat is Data"ase Trigger 1
A Data"ase Trigger is procedure 3set of SQL and :L7SQL
state!ents5 that is auto!atically e/ecuted as a result
of an insert inupdate to or delete fro! a ta"le(
Q( 0hat are the uses of Data"ase Trigger 1
Data"ase triggers can "e used to auto!atic data
generation audit data !odifications enforce co!ple/
Integrity constraints and custo!ize co!ple/ security
authorizations(
Q( 0hat are the differences "etween Data"ase Trigger
and Integrity constraints 1
A declarative integrity constraint is a state!ent a"out
the data"ase that is always true( A constraint applies to
e/isting data in the ta"le and any state!ent that
!anipulates the ta"le(
A trigger does not apply to data loaded "efore the
definition of the trigger therefore it does not
guarantee all data in a ta"le confor!s to the rules
esta"lished "y an associated trigger(
A trigger can "e used to enforce transitional
constraints where as a declarative integrity constraint
cannot "e used(
Q( 0hich of the following 0%+$+ clauses will return only
rows
that have a )*LL in the :erDie!+/penses colu!n1
A( 0%+$+ :erDie!+/penses PO
,( 0%+$+ :erDie!+/penses IS )*LL
C( 0%+$+ :erDie!+/penses Q )*LL
D( 0%+$+ :erDie!+/penses )-T I) 345
A( , is correct ^_` 0hen searching for a )*LL value in
a colu!n you !ust
use the 9eyword IS( )o 2uotes are re2uired around the
9eyword )*LL(
Q( ?ou issue the following 2uery;
S+L+CT .irst)a!e .$-' StaffList
0%+$+ .irst)a!e LI<+FKAMF
0hich na!es would "e returned "y this 2uery1 Choose all
that apply(
A( Allen
,( CLA$<
C( EAC<S-)
D( David
A( C is correct ^_` Two wildcards are used with the
LI<+ operator(
The underscore 3K5 stands for any one character of any
case and the percent sign 3M5 stands for any nu!"er of
characters of any case including none( ,ecause this string
starts with an underscore rather than a percent sign it
wonFt
return Allen or Clar9 "ecause they represent zero and two
characters "efore the UAU( If the LI<+ string had "een UMA
MU
"oth of these values would have "een returned(
David was not returned "ecause all non-wild card characters
are case sensitive( Therefore only strings
with an uppercase UAU as their second letter are returned
Q( 0rite a SQL S+L+CT 2uery that only returns each city only
once fro! Students ta"le1
Do you need to order this list with an -$D+$ ,? clause1
A( S+L+CT DISTI)CT City .$-' Students6
The Distinct 9eyword auto!atically sorts all data
in ascending order( %owever if you want the data
sorted in descending order you have to use an -$D+$ ,?
clause
Q( 0rite a SQL S+L+CT sa!ple of the concatenation operator(
A( S+L+CT Last)a!e ZZFF ZZ .irst)a!e City .$-' Students6
Q( %ow to rena!e colu!n in the SQL S+L+CT 2uery1
A( S+L+CT Last)a!e ZZFF ZZ .irst)a!e
AS UStudent )a!eU City AS U%o!e CityU
U.$-' Students-$D+$ ,? UStudent )a!eU
Q( 0rite SQL S+L+CT e/a!ple how you li!iting the rows
returned with a 0%+$+ clause(
A( S+L+CT InstructorID Salary .$-' Instructors
0%+$+ Salary O G4DD A)D Salary P HHDD6
Q( 0rite SQL S+L+CT 2uery that returns the first and
last na!e of each instructor the Salary
and gives each of the! a nu!"er(
A( S+L+CT .irst)a!e Last)a!e Salary $-0)*' .$-'
Instructors6
Q( 0hich of the following functions can "e used only with
nu!eric values1
3Choose all that apply(5
A( A#8
,( 'I)
C( L+)8T%
D( S*'
+( $-*)D
A( A and D ^_` -nly A and D are correct( The 'I)
function
wor9s with any character nu!eric or date datatype(
The L+)8T% function is a character function that returns
the nu!"er of letters in a character value( The $-*)D
function wor9s with "oth nu!eric and date values(
Q( 0hich function do you use to re!ove all padded
characters
to the right of a character value in a colu!n with a char
datatype1
A( $T$I'
,( $:AD
C( T$I'
A( C ^_` The T$I' function is used to re!ove padded
spaces(
LT$I' and $T$I' functions were included in earlier versions
of -racle "ut -racle Si has replaced the! with a single
T$I' function
Q( 0hich state!ent do you use to eli!inate padded spaces
"etween the !onth and day values in a function
T-KC%A$3S?SDAT+F'onth DD ????F5 1
A( To re!ove padded spaces you use the Uf!U
prefi/ "efore the date ele!ent that contains the spaces(
T-KC%A$3S?SDAT+Ff!'onth DD ????F5
Q( Is the 0%+$+ clause !ust appear always "efore the 8$-*:
,? clause in SQL S+L+CT 1
A( ?es(
The proper order for SQL S+L+CT
clauses is; S+L+CT .$-' 0%+$+ 8$-*: ,? %A#I)8 -$D+$ ,?(
-nly the S+L+CT and .$-' clause are !andatory(
Q( %ow -racle e/ecutes a state!ent with nested su"2ueries1
A( 0hen -racle e/ecutes a state!ent with nested su"2ueries
it always e/ecutes the inner!ost 2uery first( This 2uery
passes its results to the ne/t 2uery and so on until it
reaches the outer!ost 2uery( It is the outer!ost 2uery that
returns a result set(
Q( 0hich operator do you use to return all of the rows
fro! one 2uery e/cept rows are returned in a second 2uery1
A( ?ou use the 'I)*S operator to return all rows fro! one
2uery e/cept where duplicate rows are found in a second
2uery( The *)I-) operator returns all rows fro! "oth 2ueries
!inus duplicates( The *)I-) ALL operator returns all rows
fro! "oth 2ueries including duplicates( The I)T+$S+CT
operator returns only those rows that e/ist in "oth 2ueries(
Q( 0hich of the following state!ents are Data 'anipulation
Language co!!ands1
A( I)S+$T
,( *:DAT+
C( 8$A)T
D( T$*)CAT+
+( C$+AT+
A( A and , ^_` The I)S+$T and *:DAT+ state!ents are
Data 'anipulation Language 3D'L5 co!!ands(
8$A)T is a Data Control Language 3DCL5 co!!and(
T$*)CAT+ and C$+AT+ are Data Definition Language 3DDL5
co!!ands
Q( 0hat is -racle loc9ing1
A( -racle uses loc9ing !echanis!s to protect data fro!
"eing destroyed "y concurrent transactions(
Q( 0hat -racle loc9 !odes do you 9now1
A( -racle has two loc9 !odes; shared or e/clusive(
Shared loc9s are set on data"ase resources so that !any
transactions can access the resource( +/clusive loc9s are
set on resources that ensure one transaction has e/clusive
access to the data"ase resource
Q( 0hat is 2uery opti!ization1
A( Query opti!ization is the part of the 2uery process in
which the data"ase syste! co!pares different 2uery
strategies and chooses the one with the least e/pected cost
Q( 0hat is transaction1
A( A transaction is a collection of applications
code and data"ase !anipulation code "ound into an
indivisi"le unit of e/ecution(it consists fro!;
,+8I)-T$A)SACTI-) )a!e Code +)D T$A)SACTI-) )a!e
Q( 0hat data"ases do you 9now1
Infor!i/
D,&
SQL
-racle
Q( +/plain SQL S+L+CT e/a!ple;
select >(.IL+K)*' fro! D,Kna!e(>o" > D,Kna!e(address a
where >(E-,KT?:+ QFCF
A)D >(C-':A)?K)A'+ Q FT+STHF
A)D >(-..IC+KID Q FBDBF
A)D >(ACTI#+KI)D Q F?F
A)D a(ADD$+SSKSTAT*SKID Q F%F
A)D a(-..IC+KID Q FBDBF
A)D a(.IL+K)*' Q >(.IL+K)*'
order "y >(.IL+K)*'6
Answer; > and a aliases for ta"le na!es( this is outer
>oint select stata!ent fro! two ta"les(
Q( Descri"e so!e Conversion .unctions that you 9now
A( T-KC%A$ converts a nu!"er 7 date to a string(
T-KDAT+ converts a string 3representing a date5 to
a date(
T-K)*',+$ converts a character string containing
digits to a nu!eric data type it accepts one para!eter
which is a colu!n value or a string literal
Q( In what se2uence SQL state!ent are processed1
A( The clauses of the su"select are processed in the
following se2uence 3D,&5;
B( .$-' clause
&( 0%+$+ clause
=( 8$-*: ,? clause
4( %A#I)8 clause
G( S+L+CT clause
H( -$D+$ ,? clause
I( .+TC% .I$ST clause
Q( 0hat is a pseudo colu!n( 8ive so!e e/a!ples1
It is a colu!n that is not an actual colu!n in the ta"le(
+a/!ple *S+$ *ID S?SDAT+ $-0)*' $-0ID )*LL A)D L+#+L(
Q( Suppose a custo!er ta"le is having different colu!ns li9e
custo!er no pay!ents(
0hat will "e the 2uery to select top three !a/ pay!ents1
S+L+CT custo!erKno pay!ents fro! custo!er CB
0%+$+ =PQ3S+L+CT C-*)T345 fro! custo!er C& 0%+$+ CB(pay!ent
PQ C&(pay!ent5
Q( .ind out nth highest salary fro! e!p ta"le
S+L+CT DISTI)CT 3a(sal5 .$-' +': A
0%+$+ R) Q 3S+L+CT C-*)T 3DISTI)CT 3"(sal55 .$-' +': ,
0%+$+ a(salPQ"(sal56
Q( 0hat are the difference "etween DDL D'L and DCL
co!!ands1
DDL is Data Definition Language state!ents( So!e e/a!ples;
C$+AT+ - to create o">ects in the data"ase
ALT+$ - alters the structure of the data"ase
D$-: - delete o">ects fro! the data"ase
T$*)CAT+ - re!ove all records fro! a ta"le including all
spaces allocated for
the records are re!oved
C-''+)T - add co!!ents to the data dictionary
8$A)T - gives userFs access privileges to data"ase
$+#-<+ - withdraw access privileges given with the 8$A)T
co!!and

D'L is Data 'anipulation Language state!ents( So!e e/a!ples;
S+L+CT - retrieve data fro! the a data"ase
I)S+$T - insert data into a ta"le
*:DAT+ - updates e/isting data within a ta"le
D+L+T+ - deletes all records fro! a ta"le the space for
the records re!ain
CALL - call a :L7SQL or Eava su"progra!
+J:LAI) :LA) - e/plain access path to data
L-C< TA,L+ - control concurrency

DCL is Data Control Language state!ents( So!e e/a!ples;
C-''IT - save wor9 done
SA#+:-I)T - identify a point in a transaction to which you
can later roll "ac9
$-LL,AC< - restore data"ase to original since the last
C-''IT
S+T T$A)SACTI-) - Change transaction options li9e what
roll"ac9 seg!ent to use
Can we drop a colu!n fro! a ta"le1
yes( ALT+$ TA,L+ ta"leKna!e D$-: C-L*') colu!nKna!e6
Q( Descri"e so!e 8roup .unctions that you 9now
A( B5 The C-*)T function tells you how !any rows were in the
result set(
S+L+CT C-*)T345 .$-' T+STI)8(QA
&5 The A#8 function tells you the average value of a
nu!eric colu!n(
S+L+CT 'AJ3SALA$?5 .$-' T+STI)8(QA
=5 The 'AJ and 'I) functions tell you the !a/i!u! and
!ini!u! value of a nu!eric colu!n(
S+L+CT 'I)3SALA$?5 .$-' T+STI)8(QA
45 The S*' function tells you the su! value of a
nu!eric colu!n(
S+L+CT S*'3SALA$?5 .$-' T+STI)8(QA
8roup functions; 8roup functions operate on sets of rows to
give one result per group
Count function ; CO)NT8=: returns t6e num3er of rows in a
ta30e#
S+L+CT C-*)T345.$-' e!ployees
0%+$+ depart!entKid Q GD6
5avin" $0ause
If you restrict rows "ased on the result of a group
function you !ust have a 8$-*: ,? clause as well as the
%A#I)8 clause(
S+L+CT depart!entKid 'AJ3salary5
.$-' e!ployees
8$-*: ,? depart!entKid
%A#I)8 'AJ3salary5OBDDDD 7
Query Synta'
0rite a 2uery to display the na!e who is getting the !a/
salary fro! +': ta"le
S+L+CT ena!e .$-' +':
0%+$+ salQ3S+L+CT 'AJ3sal5 .$-' +':5
Select ena!e !a/3 sal5 fro! e!p 3 0rong5
0rite a 2uery to display the e!ployee na!e and respective
!anager na!e in the +!p ta"le
S+L+CT a(ena!e "(ena!e .$-' +': a +': "
0%+$+ a('8$Q "(+':)-
0rite a 2uery to display the e!ployee na!e and respective
!anager na!e in the +!p ta"le including the !anager is null
S+L+CT a(ena!e ZZF is wor9ing under FZZ"(ena!e
.$-' +': a+': "
0%+$+ a(!grQ"(e!pno
*)I-)
S+L+CT FThere is no !anager to FZZena!e .$-' +':
0%+$+ !gr IS )*LL
0rite a 2uery to su! all the positive and negative values in
the colu!n
Ta"le na!e ta"B
C-LB
B
&
-B
-&
>:To "et ?2@2N ma'imum sa0aries wit6 $om%0ete row
select 4 fro! e!p where salQ3select !in3sal5 fro!3
select 4 fro! 3select 4 fro! e!p order "y sal desc5 where
rownu!PQ&55
3$eplace & with the desired )u!"er5
?: To "et ? Ma' Sa0ary wit6 $om%0ete row
select 4 fro! e!p where salQ
3select !a/3sal5 !/ fro! e!p where 3Sal5 P3select !a/3sal5
fro! e!p55
A:To <et To% N rows
select 4 fro! 3select 4 fro! e!p order "y sal desc5 where
rownu!PQG55
3replace G with the desired )u!"er5
B:To <et Nt6 Row
select 4 fro! e!p where IQ3Select count3$owid5 fro! e!p /
where
e!p(rowidOQ/(rowid5
3$eplace I with the Desired )u!"er5
C:To <et Em%0oyees w6ose sa0ary is "reater t6e t6e av"
sa0ary of &e%t
select e!pnoe!p(deptnosala(av fro! e!p
3select Deptnoavg3sal5 av fro! e!p group "y deptno5 a
where a(deptnoQe!p(deptno and e!p(salOa(av
-ther way
S+L+CT 4 .$-' +': 0%+$+ SALO3s+L+CT A#83SAL5 .$-' +': J
0%+$+
J(D+:T)-Q+':(D+:T)-5 -$D+$ ,? +':)-
D:e0etion of u%0i$ate Re$or&s
Delete fro! e!p where rowid in3select !in3rowid5 fro! e!p /
where
/(rowidQe!p(rowid5
//Se0e$t Case E'am%0e
select ena!esal case when sal "etween GDD and BDDD then
FDF
when sal "etween BDDB and BGDD then FCF
when sal "etween BGDB and &GDD then F,F
else FAF end $an9 fro! e!p salgrade where
e!p(sal "etween losal and hisal
//Rows from em% an& Sa0"ra&e ta30es
select ena!esalgrade fro! e!psalgrade where
e!p(sal "etween losal and hisal
//)se of RanE to Retervie to% rows
select e(4ran935 over3order "y sal desc5ran9 fro!
e!p e where rownu!PQG
Q( Descri"e T-KDAT+ function(
A( The T-KDAT+ function returns a ti!esta!p fro! a character
string that has "een interpreted using a character
te!plate(T-KDAT+ is a synony! for TI'+STA':K.-$'AT(
0rite a synta/ for ToKDate function
ToKdate3F&DD=7DI7DCF Fyyyy7!!7ddF5 would return a date
value of Euly C &DD=(
ToKdate3FDIDCD=F F''DD??F5 would return a date value of
Euly C &DD=(
ToKdate3F&DD&D=BGF Fyyyy!!ddF5 would return a date value of
'ar BG &DD&(
S+L+CT T-KDAT+3FEanuary BGFF'-)T% DDF5 USa!pleU .$-' D*AL

You might also like