0% found this document useful (0 votes)
403 views4 pages

Nested vs Correlated Subqueries in SQL

The document discusses the difference between nested and correlated subqueries in Oracle SQL and PL/SQL. A nested subquery executes first and provides results that are used by the outer query. A correlated subquery executes once for each row returned by the outer query, using the outer row to determine if the inner query results should be included. The document provides examples of each type of subquery to illustrate their differences.

Uploaded by

nikunjpatel90
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)
403 views4 pages

Nested vs Correlated Subqueries in SQL

The document discusses the difference between nested and correlated subqueries in Oracle SQL and PL/SQL. A nested subquery executes first and provides results that are used by the outer query. A correlated subquery executes once for each row returned by the outer query, using the outer row to determine if the inner query results should be included. The document provides examples of each type of subquery to illustrate their differences.

Uploaded by

nikunjpatel90
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

2/14/2017 OracleSQL&PL/SQL:DifferencebetweenNested&CorrelatedSubqueries

34 More NextBlog CreateBlog SignIn

OracleSQL&PL/SQL
Oracle,SQLServer,SQL,PL/SQL,SQLInterviewQuestions,SQL/PLSQLInterviewQuestions,SQL
Joins,MultipleChoiceQuestionsonSQLandSQLUnion

RSS
34
Feed
OracleSQL&PL/SQL
IfyoulikereadingthisOracleSQL/PLSQLblog,pleasehelp
DifferencebetweenNested& increasetheblogs'visibilitybyclickingonthe'g+1'button.

CorrelatedSubqueries
OracleSQL OraclePL/SQL
Therearetwomaintypesofsubqueriesnestedand
[Link],whenthesubqueryis SQLIntroduction PL/SQLIntroduction
executedfirst,anditsresultsareinsertedintoWhereclauseof
SQLSyntax PL/SQLProcedures
[Link],
wherethemainqueryisexecutedfirstandthesubqueryis SQLSelect PL/SQLFunctions
executedforeveryrowreturnedbythemainquery.[Via:SqlBy SQLInsert PL/SQLCollections
Leon] SQLUpdate PL/SQLRecords
SQLDelete PL/SQLTableBased
NestedSubqueries Records
Asubqueryisnestedwhenyouarehavingasubqueryinthe SQLJoins
SQLExamplesofJoins PL/SQLProgrammer
whereorhavingclauseofanothersubquery.
DefinedRecords
[Link]
Gettheresultofallthestudentswhoareenrolledinthesame Joins PL/SQLCursorBased
Records
courseasthestudentwithROLLNO12. SQLGroupBy
PL/SQLTables
Select* SQLGroupByExamples
PL/SQLVarrays
Fromresult SQLHaving
PL/SQLNestedTables
whererollnoin(selectrollno SQLIN
fromstudent PL/SQLLoops
SQLNULLs
wherecourseid=(selectcourseid PL/SQLTriggers
FunctionalDependencies
fromstudent PL/SQLCursors
whererollno=12)); Normalization
PL/SQLImplicitCursors
ACIDProperties
Theinnermostsubquerywillbeexecutedfirstandthenbasedon PL/SQLExplicitCursors
SQLSubQueries
itsresultthenextsubquerywillbeexecutedandbasedonthat PL/SQLREFCursors
[Link] SQLQueriesWith
Examples PL/SQLCursorForLoop
candothenestingisimplementationdependent.
SQLViews PL/SQLCursorswith
Parameters
CorrelatedSubquery Insert,Update,Delete
Views PL/SQLWhereCurrentOf
andForUpdate
ACorrelatedSubqueryisonethatisexecutedaftertheouter SQLJoinViews
[Link] ExamplesofPL/SQL
SQLInlineViews Cursors
[Link]
SQLNthHighestSalary PL/SQLExceptions
executionisasfollows:
SQLSecondHighestSalary PL/SQLInterview
SQLDifferenceTruncate Questions
Theouterqueryreceivesarow. /Delete SQLPL/SQLSitemap
Foreachcandidaterowoftheouterquery,the
SQLDifferenceTruncate
subquery(thecorrelatedsubquery)isexecuted /Drop
once.
SQLDifferenceHAVING/ SQLServer
Theresultsofthecorrelatedsubqueryareusedto
WHERE MultipleChoice
determinewhetherthecandidaterowshouldbepart
SQLDifferenceCAST/ Questions
oftheresultset.
CONVERT
Theprocessisrepeatedforallrows.
SQLMultipleChoice
SQLDifferenceNOTIN/
Questions
NOTEXIST
CorrelatedSubqueriesdifferfromthenormalsubqueriesinthat SQLDifferenceIN/
thenestedSELECTstatementreferesbacktothetableinthe EXISTS
firstSELECTstatement. SQLDifferenceUNION/
UNIONALL

[Link] 1/4
2/14/2017 OracleSQL&PL/SQL:DifferencebetweenNested&CorrelatedSubqueries
Tofindoutthenamesofallthestudentswhoappearedinmore SQLDifferenceNested/
thanthreepapersoftheiroptedcourse,theSQLwillbe CorrelatedSubquery
SQLREPLACE
Selectname
fromstudentA SQLTOP
Where3<(selectcount(*) SQLLIKE
fromresultb SQLSELECTINTO
[Link]=[Link]);
SQLCREATETABLE

Inotherwords,acorrelatedsubqueryisonewhosevalue SQLCREATETABLE
dependsuponsomevariablethatreceivesitsvalueinsome (MoreExamples)
[Link] SQLALTERTABLE
evalutedinabottomtoupmanner,[Link] SQLDifferenceviews/
[Link] Materializedviews
[Link] SQLCount
[Link]
SQLUpdate
evaluatedrepeatedly,onceforeachvalueofthevariablein
question,insteadofonceandforall. SQLClustered/Non
ClusteredIndexes
SQLDeleteDuplicate
Records
CorrelatedsubqueriesimprovetheSQLperformance
SQLDifference
when: Unique/PrimaryKey

Onlyafewrowsareretrievedthroughtheouterqueryand SQLGETDATE()
especiallywhenouterquery'sWHEREclause,whichlimitsthe SQLDATEDIFF()
numberofrowsretrieved,canuseanindex. SQLDATEADD()
SQLDATEPART()
SQLConvert()
Thecorrelatedinnerqueriesareperformedthroughanindex
SQLSUM()
[Link]
theinnerqueryisperformedislargeandtheindexscanhasto SQLAVG()
retrieveonlyasmallpercentageofitsrows. SQLMIN()
SQLMAX()
SQLInsertIntoSelect
Inothercasesviewsandjoinsarelikelytobemoreefficient. SQLInsertMultiple
Buttheonlysurewaytosaywhichmethodisefficientisto ValuesInTable
analyzetheexecutionplanofthequeryandtheresourcesused SQLReferentialIntegrity
byit.[via:IntroductiontoDatabaseManagementSystemsBy
SQLNotNullConstraint
IsrdGroup]
SQLUniqueConstraint
SQLPrimaryKey
Constraint
SQLForeignKey
Constraint
NewerPost Home OlderPost SQLDefaultConstraint
SQLCheckConstraint
SQLROLLUP
SQLCUBE
SQLSTUFF()
SQLCount_Big
SQLBinary_Checksum
SQLChecksum_AGG
SQLIndexInclude
SQLCoveredQuery
SQLIdentity
SQLsp_columns
SQLDiffLocal/Global
TemporaryTables
SQLStoredProcedure
SQLsp_who
SQLSession
SQLDynamicSQL
SQLSQLServer
ExecutionPlan
SQLsp_executesql
SQLDifference
Execute/Execute()
SQLCaseExpression

[Link] 2/4
2/14/2017 OracleSQL&PL/SQL:DifferencebetweenNested&CorrelatedSubqueries
SQLXMLVariable
Example
SQLPivot
SQLMerge
ExampleofSQLMerge
ExampleofMergeOutput
Clause
SQLCompute/Compute
by
SQLDiff
SCOPE_IDENTITY/
IDENT_CURRENT/
@@IDENTITY
SQLCoalesce
SQLImportDatainto
SQLServerTable
SQLDeleteDuplicate
RecordsinSQLServerTable
SQLCheckIfaColumn
ExistsinSQLServerTable
SQL
Has_perms_by_name
SQLDifferencebetween
Logins/Users
SQLDiffbetweenTable
Variable/TempTable
SQLSQLServer2012
features
SQLSERVERPROPERTY
SQLFindPrimaryKey/
ForeignKeyinTable
SQLSequence
SQLColumnstore
Indexes
SQLDifferencePrimary/
CandidateKey
SQLThrowSQLServer
2012
SQLDifferenceGUID/
INT
SQLServer2012
Enhancements
SQLDiffbetweenCross
Join/FullOuterJoin
SQLNTILE()
SQLObjectDependencies
SQLEmailReminderon
UpdatingTable
SQLObjectDefinition
SQLServerReport
Builder3
SQLServerUserDefined
Roles
SQLServerFiletables
SQLServerContained
Databases
SQLServerUpdatefrom
Select
SQLServerXMLMethods
SQLServerAdda
ColumntoTable
SQLServerInsert
MultipleRows
SQLServerAddIdentity
ColumntoTable
SQLServerIndexes
SQLServerDiff
Functions/StoredProcedures

[Link] 3/4
2/14/2017 OracleSQL&PL/SQL:DifferencebetweenNested&CorrelatedSubqueries
SQLComparison
OperatorsAny/All
SQLDifferencePrimary/
ForeignKey
SQLDifferenceReplace/
Stuff
SQLROW_NUMBER()
SQLIntersect
SQLServerLocal
Variables
SQLServerUpdatewith
Join
SQLServerDifference
Merge/HashJoin
SQLServerDifference
Update/ExclusiveLock
SQLServerDifference
Instance/Database
SQLServerDifference
Datetime2/Datetime
SQLServerDifference
Disable/DropIndexes
SQLServerDifference
Decimal/Float
SQLServerDATEDIFF
withExamples
SQLServerDifference
Raiserror/Throw
SQLServerCheckIf
ColumnExistsinTable
SQLServerQueryExcel
UsingLinkedServer
SQLServerWaitFor
SQLServerDifference
Rank,Dense_Rank&
Row_Number
SQLServerPATINDEX
Examples
SQLInterviewQuestions
SQLJoinInterview
Questions

PoweredbyBlogger.

[Link] 4/4

You might also like