SQL Interview Questions 2
SQL Interview Questions 2
Whati
sapr
imar
ykey
?
Apri
marykeyi
sacombi nati
onoffi
eldswhi
chuni
quelyspeci
fyarow.Thi
sisaspeci
alki
ndof
uni
quekey
,andithasi
mpl i
citNOTNULLconst
rai
nt.I
tmeans,Pri
marykeyval
uescannotbe
NULL.
7.Whati
sauni
quekey
?
AUniquekeyconst
rai
ntuni
quel
yident
if
iedeachr
ecor
dint
hedat
abase.Thi
spr
ovi
des
uni
quenessfort
hecol
umnorsetofcolumns.
APri
marykeyconst
rai
nthasaut
omat
icuni
queconst
rai
ntdef
inedoni
t.Butnot
,int
hecaseof
Uni
queKey.
Therecanbemanyuni
queconst
rai
ntdef
inedpert
abl
e,butonl
yonePr
imar
ykeyconst
rai
nt
defi
nedpert
abl
e.
8.Whati
saf
orei
gnkey
?
Aforeignkeyisonet
ablewhi
chcanberel
atedtothepr
imarykeyofanot
hert
able.Relat
ionshi
p
needst obecreat
edbet
weentwotabl
esbyrefer
enci
ngfor
eignkeywitht
hepri
mar ykeyof
anothertabl
e.
9.Whati
saj
oin?
Thisisakey
wordusedtoquer
ydat
afr
om moretabl
esbasedonther
elat
ionshi
pbet
weent
he
fi
eldsoft
hetabl
es.Key
splayamaj
orr
olewhenJOINsareused.
10.Whatar
ethet
ypesofj
oinandexpl
aineach?
Therearevar
ioust
ypesofjoi
nwhi
chcanbeusedt
oret
ri
evedat
aandi
tdependsont
he
rel
ati
onshi
pbetweentabl
es.
I
nnerJoi
n.
I
nnerj
oinr
etur
nrowswhent
her
eisatl
eastonemat
chofr
owsbet
weent
het
abl
es.
Ri
ghtJoi
n.
Rightjoi
nret
urnrowswhi char
ecommonbet weenthetabl
esandal
lrowsofRighthandsi
de
tabl
e.Simply,
itr
eturnsal
l t
herowsfrom t
her
ighthandsi
detabl
eeventhoughther
eareno
mat chesint
helefthandsidet
able.
Lef
tJoi
n.
Leftj
oinreturnrowswhichar
ecommonbet weent
hetabl
esandallr
owsofLef
thandsidet
able.
Simply,i
treturnsall
ther
owsfrom Lef
thandsi
det
abl
eeventhoughther
ear
enomat chesi
nthe
Righthandsi detabl
e.
Ful
lJoi
n.
Ful
ljoi
nret
urnrowswhentherearematchi
ngrowsi
nanyoneoft
hetables.Thismeans,i
t
ret
urnsal
lther
owsfrom t
helefthandsi
detabl
eandal
lther
owsfr
om therighthandsi
detabl
e.