0% found this document useful (0 votes)
1K views

Advanced SQL Interview Questions and Answers

sql intreview

Uploaded by

vara421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Advanced SQL Interview Questions and Answers

sql intreview

Uploaded by

vara421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

6/23/2015

AdvancedSQLInterviewQuestionsandAnswers
Translate:

SelectLanguage

Search

Home

AdvancedSQLInterviewQuestionsandAnswers

Java

HerearesomecomplexSQLinterviewproblemsthatareforpeoplewhoarelookingfor
moreadvancedandchallengingquestions,alongwiththeanswersandcomplete

C/C++
Databases/SQL
SQLInterviewQuestions

explanations.Trytofigureouttheanswertothequestionsyourselfbeforereadingthe
answers.
Supposewehave2tablescalledOrdersandSalespersonshownbelow:
Salesperson

Innervs.Outerjoins

Orders

SQLKeyDefinition

ID Name Age Salary

Number order_date cust_id salesperson_id Amount

DifferencesbetweenPrimaryand
ForeignKeys

Abe

61

140000

10

8/2/96

540

Bob

34

44000

20

1/30/99

1800

SecondaryKey

Chris

34

40000

30

7/14/95

460

SimplekeyinSQL

Dan

41

52000

40

1/29/98

2400

SuperkeyExample

Ken

57

115000

50

2/3/98

600

WhatisReferentialIntegrity

11 Joe

38

38000

60

3/2/98

720

70

5/6/98

150

NaturalKeyInDatabase

Havingvs.Whereclause
Howdodatabaseindexeswork?
Whatisaselfjoin?

NowsupposethatwewanttowriteSQLthatmustconformtotheSQLstandard.

ExampleofDISTINCTinSQL
Retrieveuniquerowswithout
DISTINCT

Wewanttoretrievethenamesofallsalespeoplethathavemorethan1order
fromthetablesabove.YoucanassumethateachsalespersononlyhasoneID.

PracticeInterviewQuestion1
PracticeInterviewQuestion1
continued
PracticeInterviewQuestion1
continued
PracticeInterviewQuestion2
AdvancedSQLInterviewQuestions
andAnswers
AdvancedSQLInterviewQuestions
andAnswersPart2

Ifthatisthecase,thenwhat(ifanything)iswrongwiththefollowingSQL?:

SELECTName
FROMOrders,Salesperson
WHEREOrders.salesperson_id=Salesperson.ID
GROUPBYsalesperson_id
HAVINGCOUNT(salesperson_id)>1

PracticeInterviewQuestion2
continued

TheanswerandexplanationtoadvancedSQLquestion1

DataMiningvs.DataWarehousing

ThereisdefinitelysomethingwrongwiththeSQLabove,anditisprobablysomethingthat
mostbeginnerSQLprogrammersmaynotnotice.TheproblemisthattheSQLStandard
saysthatwecannotselectacolumnthatisnotpartofthegroupbyclauseunlessitis
alsocontainedwithinanaggregatefunction.IfwetrytoruntheSQLaboveinSQL
Server,wewouldgetanerrorthatlookslikethis:

Ternary/ThreevaluedLogicinSQL
FindMaximumValueWithoutUsing
Aggregate
SQLInjectionExampleandTutorial
SQLInjectionPrevention
BlindSQLInjectionExample
ParameterizedQueriesvsPrepared
Statements
PreparedStatementExample
Differencebetweenafulljoinandan
innerjoin?
Differencebetweenaleftouterjoin
andrightouterjoin?

Column'Name'isinvalidintheselectlistbecauseitis
notcontainedineitheranaggregatefunctionor
theGROUPBYclause.

Youmightbeconfusednow,soletsexplainwhatthaterrormeansinplainEnglishand
throughsomesimpleexamples.Themostimportantthingyoushouldtakeoutofthis
discussionisunderstandingexactlywhywegetthaterror,andhowtoavoidit.There
isagoodreasonfortheerrorreadontounderstandwhy.

Differencebetweenaleftjoinanda
leftouterjoin?

YoucanseeinthebadSQLabovethattheNamecolumnisclearlynotalsoapartofthe
groupbystatement,norisitcontainedwithinanaggregatefunction(likeSUM,MAX,etc).

SQL:HavingvsGroupBy

Astheerrorabovesuggests,wecanfixtheerrorbyeitherwrappingtheNamecolumn
insideanaggregatefunctionoraddingittotheGroupByclause.

SQL:GroupBywithmultiplecolumns
SQLSelectDistinctandOrderBy
SQLOrderBydefaultsortorder

SoifwewanttowriteSQLthatcomplieswiththestandard,thenwecouldwrite
somethinglikethisbyaddingtheNamecolumntotheGroupBy:

Derivedtablevssubquery

http://www.programmerinterview.com/index.php/databasesql/advancedsqlinterviewquestionsandanswers/

1/5

6/23/2015

AdvancedSQLInterviewQuestionsandAnswers

CorrelatedvsUncorrelatedSubquery
FindnthhighestsalarySQL
CardinalityinSQL
SelectivityinSQLDatabases
CardinalityversusSelectivity

SELECTName
FROMOrders,Salesperson
WHEREOrders.salesperson_id=Salesperson.ID
GROUPBYsalesperson_id,Name
weaddedthenamecolumntothegroupby,andnowitworks!
HAVINGCOUNT(salesperson_id)>1

Clusteredvs.NonClusteredIndex
Pageversusblock

TheSQLabovewillrunjustfinewithoutgivinganyerror.

DatabaseLocking
LockEscalation

WecouldalsofixtheproblembyputtingtheNamecolumninanyaggregatefunction,and
thensimplymakethatapartofourselectstatement.So,wecouldjustwritethisSQL

DatabaseDeadlockExample

instead,anditwouldbeperfectlylegalaccordingtotheSQLstandard.Wechosetouse

Whatisadatabasetransaction?

theMAXaggregatefunction,butanyotheraggregatewouldworkjustfineaswell:

SQLServerTransaction
OracleTransaction
MySQLTransaction
DB2Transaction
ConcurrentUpdateProblem

SELECTMAX(Name)putnameinanaggregatefunction
FROMOrders,Salesperson
WHEREOrders.salesperson_id=Salesperson.ID
GROUPBYsalesperson_id
HAVINGCOUNT(salesperson_id)>1

HowtoTuneDatabasePerformance
DatabaseFullTableScan
Whatiscostbasedoptimization?
HowtotuneSQLqueries
SQLIndexPerformance

AddingtheNamecolumntothegroupby,orwrappingtheNamecolumninanaggregate
willcertainlyfixtheerrorbutitsveryimportanttonotethatbothofthosethingswill
changethedatathatisreturnedtoastatethatyoumaynotwant.

Whydoestheselectedcolumnhavetobeinthegroupby
clauseorpartofanaggregatefunction?

Whatisabitmapindex?
OracleIndexesExamples
Systemprivilegesvs.object
privileges
SQLGrant

So,nowyouunderstandhowtofixtheerrorbutdoyouunderstandwhyitisa
probleminthefirstplace?Well,youshouldbecausethatisthemostimportant
thingtounderstand!So,letsexplainsomemoreaboutwhySQLgivesthaterror
shownabove.

SQLRevoke

Firstoff,letstalkalittlebitmoreaboutaggregatefunctions.Youprobablyknowwhat
aggregatefunctionsinSQLareweusedoneintheexampleabove.Incaseyouforgot,

SQLCreateUser

aggregatefunctionsareusedtoperformamathematicalfunctiononthevaluesinsidea

DatabaseRoles

givencolumn,whichispassedintotheaggregatefunction.Herearesomeofthe
commonlyusedaggregatefunctions:

SQLCASEStatement
SQLSearchedCASEStatement
SQLInlineView
RANK()versusDENSE_RANK()
Javascript
PHP

AVG()Returnstheaveragevalue
COUNT()Returnsthenumberofrows
FIRST()Returnsthefirstvalue
LAST()Returnsthelastvalue
MAX()Returnsthelargestvalue
MIN()Returnsthesmallestvalue
SUM()Returnsthesum

DataStructures
DesignPatternQuestions
ExcelInterviewQuestions

ToillustratewhytheSQLstandardsaysthataselectedcolumnhastobeinthegroupby
clauseorpartofanaggregatefunction,letsuseanotherexample.Supposewehave
sometablescalledStarbucks_StoresandStarbucks_Employees.Incaseyoudontalready
know,Starbucksisapopularcoffeeshop/cafeintheUSA:

HTML5
Networking
OperatingSystems
Recursion

Starbucks_Employees

Starbucks_Stores

ID Name Age HourlyRate StoreID

store_id city

Abe

61

14

10

10

SanFrancisco

Bob

34

10

30

20

LosAngeles

Chris

34

40

30

SanFrancisco

ApacheInterviewQuestions

Dan

41

11

50

40

LosAngeles

General/Miscellaneous

Ken

57

11

60

50

SanFrancisco

11 Joe

38

13

70

60

NewYork

70

SanFrancisco

NonTechnicalQuestions
InterviewinginIndia

Now,giventhetablesaboveletssaythatwewritesomeSQLlikethis:
WorkingAsaSoftwareEngineer
FinancialAnalystQuestions

SELECTcount(*)asnum_employees,HourlyRate
FROMStarbucks_EmployeesJOINStarbucks_Stores

http://www.programmerinterview.com/index.php/databasesql/advancedsqlinterviewquestionsandanswers/

2/5

6/23/2015

AdvancedSQLInterviewQuestionsandAnswers

JobAdviceForProgrammers

ONStarbucks_Employees.StoreID=Starbucks_Stores.store_id
GROUPBYcity

Puzzles
AssortmentofKnowledge
AmericanVocabulary
TechnicalVocabulary
ScienceQuestions

ItlooksliketheSQLabovewouldjustreturnthenumberofStarbucksemployeesineach
city,alongwiththeHourlyRatebecauseitwillgrouptheemployeesbasedonwhatever
citytheyworkin(thankstothegroupbycitystatement).
Subscribetoournewsletterformorefreeinterviewquestions.

Theproblemwithselectinganonaggregatecolumnthatisnot
inthegroupby
ButtherealquestionhereiswhatexactlywouldbereturnedfortheHourlyRateinthe
SQLabove?Woulditreturneveryemployeeshourlyrateseparatedbycommas?Sincewe
groupbycity,willitreturnthehighesthourlyrateforeachcity?Willitreturnthehourly
rateasadistinctlist,sothose2guysmaking11dollarsanhourwillhavethe11returned
onlyonce?

Email

Theproblemhereisthatwedonotknowwhatwillbereturnedbecausewearenot
Country

UnitedStates

specificenoughwithwhatweareaskingforintheSQL!Ifwhatweareaskingfor
isnotspecificenough,thentheSQLprocessorwillnotknowwhattoreturn.
ThisiswhyalmostalldatabaseimplementationsreturnanerrorwhentheSQLaboveis
run(withthenotableexceptionofMySQL)andthisiswhytheSQLdoesnotconformto
theStandard.InSQLServerrunningtheSQLabovewillreturnthesameerrorthatwe
showedearlier.
LetsexplainevenfurtherincasetheproblemwiththatSQLisnotcrystalclear.Theorder

ProgrammerInterview

ofoperationsinwhichthingswillhappenwiththeSQLaboveis:

Like 23,854

1.The2tablesarejoinedontheconditionthatthe
Starbucks_Employees.StoreIDcolumnvalueisequaltothe
Starbucks_Stores.store_idcolumnvalues.
2.Groupsarethencreatedforeachcitywhichmeansthat
eachdistinctcitywillhaveit'sown"group".So,therewill
beatotalof3groupsoneeachforSanFrancisco,NewYork,
andLosAngeles.
3.Thedataweareinterestedinisselectedfromeachgroup
thatiscreatedinstep2.

Becauseweendupwithdifferentgroupsbasedonthecity,whenweselectacount(*),
thatwillfindthetotalcountofrowsineachandeverygroup.But,theproblemisthat
whenweselectHourlyRate,therewillbemultiplevaluesfortheHourlyRatewithineach
group.Forexample,forthegroupcreatedbythecityofSanFranciscotherewillbe4
differentvaluesfortheHourlyRate14,10,11,and13.
SothequestioniswhichvalueoftheHourlyRateshouldbeselectedfromeachgroup?
Well,itcouldbeanyoneofthosevalueswhichiswhythatSQLresultsinanerror.
ThisisbecausewhatweareaskingforisNOTspecificenoughhopefullythisiscrystal
clearnowtoyou.
IfthesameHourlyRatewerepartofanaggregatefunctionlikeMAXthenitwouldsimply
returnthehighestHourlyRatewithineachgroup.Andthatiswhyhavinganaggregate
functionwouldfixtheSQLerrorbecauseonlyonevaluewillbeselectedfromanygiven
group.
So,thisSQLisperfectlyfinebecausewearemorespecificinwhatweaskforbutthis
SQLwouldonlyworkforyouifyouactuallywantthehighestHourlyRateforeachcity:

SELECTcount(*)asnum_employees,MAX(HourlyRate)
FROMStarbucks_EmployeesJOINStarbucks_Stores
ONStarbucks_Employees.StoreID=Starbucks_Stores.store_id
GROUPBYcity

Fixtheerrorbyaddingcolumntothegroupclause
AnotherwaytofixtheerroristosimplyaddtheHourlyRatecolumntothegroupby
clause.ThisalsomeansthathavingtheHourlyRatecolumnwrappedinaggregatefunction
isnolongernecessary.SoyoucouldwritesomeSQLlikethisanditwouldfixtheerror:

http://www.programmerinterview.com/index.php/databasesql/advancedsqlinterviewquestionsandanswers/

3/5

6/23/2015

AdvancedSQLInterviewQuestionsandAnswers
SELECTcount(*)asnum_employees,HourlyRate
FROMStarbucks_EmployeesJOINStarbucks_Stores
ONStarbucks_Employees.StoreID=Starbucks_Stores.store_id
GROUPBYcity,HourlyRate

Thiswouldthencreategroupsbasedontheuniquecombinationofthevaluesinthe
HourlyRateandCitycolumns.Thismeansthattherewillbeadifferentgroupforeach
HourlyRateandCitycombinationso$11,SanFranciscoand$11,LosAngeleswillbe2
differentgroups.Ifyouneedtoreadupmoreonthistopicthenyoucangohere:Group
ByWithMultipleColumns
WiththeSQLabove,eachgroupwillonlyhaveonevaluefortheHourlyRate,whichalso
meansthattherewillbenoambiguityorconfusionwhenselectingtheHourlyRatesince
thereisonlypossiblevaluetoselect.Itisnowveryclearthatoneandonlyone
HourlyRatevaluecanbereturnedforeachgroup.

Addingthecolumntothegroupbyclausefixestheerrorbut
willalterthedatathatisreturned
But,oneveryimportantthingtonoteisthateventhoughaddingthecolumntothe
groupbywillfixtheerror,itwillalsochangethegroupsthatarecreated.Thismeansthat
thedatareturnedwillbecompletelydifferentfromwhatwasreturnedbefore.So,the
count(*)functionwillnolongerreturnthecountofemployeesinagivencity,andwill
insteadreturnthenumberofrowsineachgroupcreatedbytheuniquecombinationof
theHourlyRateandcitycolumns.

MySQLselectingnonaggregatecolumnsnotinthegroupby
OneveryimportantthingthatyoushouldknowisthatMySQLactuallyallowsyouto
havenonaggregatedcolumnsintheselectlisteveniftheyarenotapartofthegroupby
clause(aquicksidenote:anonaggregatedcolumnissimplyacolumnthatisnot
wrappedwithinanaggregatefunction).Whatthismeansisthatyouwillnotreceivean
errorifyoutrytorunanyofthebadSQLaboveinMySQL.Thereasonitisallowedin
MySQLisbecauseMySQLassumesthatyouknowwhatyouaredoinganditdoes
actuallymakesenseinsomescenarios.Forinstance,letsreferbacktotheSQLthatwe
startedwith:

SELECTName
FROMOrders,Salesperson
WHEREOrders.salesperson_id=Salesperson.ID
GROUPBYsalesperson_id
HAVINGCOUNT(salesperson_id)>1

ThereasontheoriginalSQLcode(presentedabove)worksjustfineinMySQLisbecause
thereisa1to1mappingofsalespersonnametoIDmeaningthatforeveryunique
salespersonIDthereisonlyonepossiblename.Anotherwayofsayingthatisthateach
salespersoncanonlyhaveonename.Sowhenwecreategroups(whichisdoneinthe
GROUPBYsalesperson_id)basedonthesalespersonID,eachgroupwillonlyhaveone
andonlyonename.
ThisSQLwillalsorunjustfineinMySQLwithoutreturninganerror:

SELECTcount(*)asnum_employees,HourlyRate
FROMStarbucks_EmployeesJOINStarbucks_Stores
ONStarbucks_Employees.StoreID=Starbucks_Stores.store_id
GROUPBYcity

But,eventhoughthecodeabovewillnotreturnanerror,theHourlyRatethatisreturned
byMySQLwillbesomearbitrary(random)valuewithineachgroup.Thisisbecausewhen
wecreateeachgroupbasedonthecity,eachgroupcanhavedifferentvaluesforthe
HourlyRate.
Inotherwords,thereisnoonetoonemappingbetweentheHourlyRateandthecitylike
wehadbeforewiththesalespersonIDandthename.So,becausewearenotbeing
specificastowhichHourlyRatewewant,MySQLwillreturnanarbitraryvalue.For
instance,inthegroupcreatedbythecityofSanFrancisco,MySQLcouldreturnthe
HourlyRateforanyemployeewhoworksinSanFranciscowhetheritis14,10,11,or
13wedontreallyknowsinceitisarbitrary/randominMySQL.
Thatconcludespart1ofourmoredifficultandcomplexSQLquestions.Clickonnextto

http://www.programmerinterview.com/index.php/databasesql/advancedsqlinterviewquestionsandanswers/

4/5

6/23/2015

AdvancedSQLInterviewQuestionsandAnswers
checkoutthenextquestionthatsapartofouradvancedSQLinterviewquestionslist.

Hiring?JobHunting?PostaJOBoryourRESUMEonourJOBBOARD>>
Follow@programmerintvw
+7 Recommend this on Google

FOLLOWVaroonSahgal,AuthorofProgrammerInterview on

Previous...

Next...

WouldyouliketothankProgrammerInterview.comforbeingahelpfulfreeresource?Thenwhynottellafriendaboutus,orsimplyaddalink
tothispagefromyourwebpageusingtheHTMLbelow.
Linktothispage:

<ahref="http://www.programmerinterview.com/index.php/databasesql/advancedsqlinterviewquestionsandanswers/">ProgrammerandSoftwareInterviewQuestionsandAnswers
Pleasebookmarkwithsocialmedia,yourvotesarenoticedandappreciated:

Like 23,854peoplelikethis.

Copyright2015|ProgrammerJobBoard|IndiaJobBoardforProgrammers|About

http://www.programmerinterview.com/index.php/databasesql/advancedsqlinterviewquestionsandanswers/

WebsiteDesignedbyNayaPixel.com

5/5

You might also like