Sei sulla pagina 1di 11

11/30/2017 Subqueries in SQL | Correlated Subquery | Difference Between SubQuery and Correlated Subquery

SQL WORLD
GET YOUR SQL QUERY FAST AND FURIOUS..

HOME COMPLEX SQL QUERIES EXAMPLES ORACLE OBIEE UNIX MICROSOFT SQL POSTGRESQL

MYSQL SIEBEL ABOUT US CONTACT US

Subqueries in SQL | Correlated


SEARCH YOUR TOPIC HERE

Subquery | Di erence Between SEARCH …

SubQuery and Correlated Subquery


 February 25, 2017  admin  SQL Di erence Between, SQL Interview Questions, SQL CATEGORIES
Tutorials  16
BI Tutorials

Oracle Errors

SQL Complex Queries

SQL Di erence Between

SQL Interview Questions

SQL Interview Questions For Freshers

SQL Joins

SQL Performance Tuning

Inner and Outer Query SQL Tutorials

http://www.complexsql.com/subqueries-correlated-subquery/ 1/11
11/30/2017 Subqueries in SQL | Correlated Subquery | Difference Between SubQuery and Correlated Subquery

Subquery: Unix Tutorials

Subquery is query within query.The output of outer query is assigned to the column which is RECENT POSTS
used in where condition of outer query.The subquery output is returning only one output
value and based on that output value the outer query is executed.Subqueries are used in
Oracle Errors | ORA-00936: missing
various real life scenarios like report development,Application logic
expression | ORA-00936
development,Performance tuning of query.
Oracle Errors | ORA-00604: error occurred at
recursive SQL level 1 | ORA-00604

PLSQL Interview Questions | Important


PLSQL Interview Questions

Hierarchical queries | Hierarchical Queries


with Examples

Inner and Outer Query OBIEE Interview Questions | Oracle Business


Intelligence Interview Questions

“Subquery is Query within a query in which the value of inner query is assigned to the Oracle Errors | ORA-12560:TNS:protocol
adapter error | ORA-12560
outer query and outer query is executed…”
Oracle Errors | ORA-01000: maximum open
cursors exceeded | ORA-01000
Types of Subqueries:
Oracle Errors | ORA-03113: end-of- le on
1.Single Row Subquery communication channel | ORA-03113

2.Multirow Subquery Oracle Errors | ORA-01722: invalid number |


ORA-01722
3.Correlated Subquery ORA-00600: internal error code, arguments:
[%s], [%s],[%s], [%s], [%s] | ORA-00600 |
4.Nested SubQueries
ORA-00600: internal error code

5.Scalar Subqueries Oracle Errors | ORA-12154: TNS:could not


resolve the connect identi er speci ed |

http://www.complexsql.com/subqueries-correlated-subquery/ 2/11
11/30/2017 Subqueries in SQL | Correlated Subquery | Difference Between SubQuery and Correlated Subquery

CLICK HERE TO GET 20 MOST IMPORTANT QUESTIONS FOR TECH MAHINDRA ORA-12154

Query to nd Duplicate Records in Table? |


1.Single Row Subquery with real life example:
Query to Delete Duplicate Records from

When Query within a query or subquery returns only one row then these type of queries are table

called as single row subqueries. Single row comparison operator is used to compare the two Interview Questions for Accenture | SQL
queries.The most widely used operator for single row subquery is Equal to operator(=).Here Interview Questions for Accenture |
We need to make sure that the query is returning only one value.Here we are able to use Accenture SQL Interview Questions
Max,Min,AVG like fuctions which will return only one value.
Di erence between SQL and NoSQL | SQL vs
Real life Scenario: NoSQL | SQL Vs NoSQL with Examples

Interview Questions for Deloitte | SQL


Write a query to nd highest salared Employee from Employee table.
Interview Questions for Deloitte | Interview
Questions Asked in Deloitte | Deloitte
Select Employee_No,Employee_Name from Employee Interview Questions

SQL Interview Questions for Cognizant (CTS)


where Salary=(Select max(Salary) from Employee);
| Important SQL Interview Questions for
Cognizant (CTS)
In above query the inner query is executed 1st then the value of inner-query is assigned to
the outer query. Interview Questions for Infosys | SQL
Interview Questions for Infosys | Interview
Step 1:Executed inner query :(Select max(Salary) from Employee); Questions Asked in Infosys | Infosys
Interview Questions
consider Output is:50000
Oracle 18c Advantages and disadvantages |
Step 2:  Oracle 18c Releases

Oracle 18c Impact on DBAs | Cons of Oracle


Select Employee_No,Employee_Name from Employee
18c | Oracle 18c Disadvantages

where Salary=50000; BI Interview Question | Business Intelligence


Interview Questions
2.Multi Row Subqueries with Real life Scenario:
Oracle 18c | Oracle 18c Features | Oracle
18c Advantages

http://www.complexsql.com/subqueries-correlated-subquery/ 3/11
11/30/2017 Subqueries in SQL | Correlated Subquery | Difference Between SubQuery and Correlated Subquery

If the output of Inner query count is more than 1 then these subqueries are called as multi SQL Joins | Postgres Joins | MSSQL Joins |
row subqueries.We need to use ANY,IN,EXIST Operator in outer query of multi row Full Outer Join | Cartesian Join
subqueries because output of outer query is not a single value.
RPD Development | Consistency Check in
Real Life Scenario: RPD Development | Errors in RPD

OBIEE Repository Development Basics |


Fetch the list of Employees which is assigned to ‘OBIEE’ and ‘Oracle’ Department.
Presentation Layer in RPD | Presentation
Layer | Presentation Layer in OBIEE
Select Employee_No,Employee_Name from Employee
OBIEE Repository Development Basics |

where Department_Name in Business Model Mapping Layer development


| BMM Layer | Logical layer in OBIEE

(Select Department_Name from Employee where Department_name in (‘OBIEE’,Oracle’));


NEWSLETTER
The Query is executed in following Steps:

SUBSCRIBE
Step 1:
Email address: Your email address
Inner Query Execution: SIGN UP

Select Department_Name from Employee where Department_name in (‘OBIEE’,Oracle’);

Consider the output is ‘OBIEE’ and ‘ORACLE’

Step 2:

Outer Query Execution:

Select Employee_No,Employee_Name from Employee

where Department_Name in (‘OBIEE’,ORACLE’);

CLICK HERE TO GET 20 MOST IMPORTANT INTERVIEW QUESTIONS FOR IBM

http://www.complexsql.com/subqueries-correlated-subquery/ 4/11
11/30/2017 Subqueries in SQL | Correlated Subquery | Difference Between SubQuery and Correlated Subquery

3.Correlated Subquery :

Correlated Query is nothing but the subquery whose output is depending on the inner query
used in that query.Correlated query is the query which is executed after the outer query is
executed.The outer query is always dependent on inner query.The approach of the correlated
subquery is bit di erent than normal subqueries.In normal subqueries the inner queries are
executed rst and then the outer query is executed but in Correlated Subquery outer query is
always dependent on inner query so rst outer query is executed then inner query is
executed.Correlated Subqueries always uses operator like Exist,Not Exist,IN,Not IN.

“Correlated Queries are also called as Synchronized queries…”

Execution Steps of Correlated Subqueries:

1.Executes the outer Query

2.For Each row of outer query inner subquery is executed once

3.The result of correlated subquery determines whether the fetched row should be the part
of our output results

4.The Process is Repeated for all Rows

“It is not recommended to use Correlated Subqueries as it slows down the performance”

Real Life Example:

Fetch the Employees who have not assigned a single department.

Select * from Employee E where Not exist

(Select Department_no From Department D where E.Employee_id=D.Employee_ID);

http://www.complexsql.com/subqueries-correlated-subquery/ 5/11
11/30/2017 Subqueries in SQL | Correlated Subquery | Difference Between SubQuery and Correlated Subquery

Execution of query:

Step 1:

Select * from Employee E ;

It will fetch the all employees

Step 2:

The First Record of the Employee second query is executed and output is given to rst query.

(Select Department_no From Department D where E.Employee_id=D.Employee_ID);

Step 3:

Step 2 is repeated until and unless all output is been fetched.

Nested Subqueries:

The Subqueries are called as nested subqueries when another subquery is used in where or
having condition of the Outer Query.The Execution of Nested suubquery always follows
bottom up approach.

Real Life Example:

Select * from Employee

where Employee_No Exist

(Select * from Employee

where Department_Name=

http://www.complexsql.com/subqueries-correlated-subquery/ 6/11
11/30/2017 Subqueries in SQL | Correlated Subquery | Difference Between SubQuery and Correlated Subquery

(Select Department_Name from Employee where Department_Name=’OBIEE’));

Execution of Query:

Step 1:

Executed Bottom query:

Select Department_Name from Employee where Department_Name=’OBIEE’);

Step 2:

Executed The Second Query which is above bottom query:

Select * from Employee

where Department_Name=’OBIEE’;

Step 3:

Excecuted the Top Query

Select * from Employee

where Employee_No Exist

(Select * from Employee

where Department_Name=’OBIEE’);

Hope you will get idea about the subqueries in SQL.If you have any issues related to this or
any information needed kindly comment.If you want this article in PDF format kindly
comment here…

http://www.complexsql.com/subqueries-correlated-subquery/ 7/11
11/30/2017 Subqueries in SQL | Correlated Subquery | Difference Between SubQuery and Correlated Subquery

HOME

Like 66  

Deal of the day, India's Biggest De…


Your search for great deals and coupon savings ends here. Fin…

Share this:

   66

Related

Interview Questions For Interview Questions for SQL Interview Questions


SQL | SQL interview Infosys | SQL Interview for Tech Mahindra |
Questions for MNCs Questions for Infosys | Important SQL Interview
March 12, 2017 Interview Questions Asked Questions for Tech
In "SQL Interview in Infosys | Infosys Mahindra | Tech Mahindra
Questions" Interview Questions Interview Questions and
October 24, 2017 answers
In "SQL Interview February 26, 2017
Questions" In "SQL Interview
Questions"

http://www.complexsql.com/subqueries-correlated-subquery/ 8/11
11/30/2017 Subqueries in SQL | Correlated Subquery | Difference Between SubQuery and Correlated Subquery

 PREVIOUS NEXT 
20 SQL Queries for interview | SQL Interview Questions for Tech
Complex SQL Queries for Mahindra | Important SQL
Interview Interview Questions for Tech
Mahindra | Tech Mahindra
Interview Questions and answers

6 COMMENTS

Sajid
FEBRUARY 26, 2017 AT 6:38 AM

Thank you very much for this wonderful article

Just a small suggestion why don’t you create new subsection under Oracle->SQL Tutorials->
Topics..which will help users who are visiting this site rst time

admin 
FEBRUARY 26, 2017 AT 8:45 AM

Sure Sajid…I will create one section named SQL tutorials for userfriendlyness..Thanks for
valuable suggestions…:)

surya
MARCH 1, 2017 AT 8:05 PM

It is the excellent and clear way of explaining about it.Even standard books also don’t clarify
this separation among these sub queries clearly .Your way of explaining is really helping a lot
http://www.complexsql.com/subqueries-correlated-subquery/ 9/11
11/30/2017 Subqueries in SQL | Correlated Subquery | Difference Between SubQuery and Correlated Subquery

at the time of learning the topic ‘ nd the n Th highest salary of an employee using correlated
query’.

Thank you so much sir….we are waiting for your valuable explanation……………..

admin 
MARCH 1, 2017 AT 8:19 PM

Sure Surya…I will write article on nding Nth highest salary using correlated query till this
weekend…keep visiting site

swati
MARCH 2, 2017 AT 2:19 PM

Please tell me good SQL book

admin 
MARCH 2, 2017 AT 5:12 PM

You will refer complete Reference SQL.That book is really perfect for learning purpose

10 TRACKBACKS / PINGBACKS

 SQL Interview Questions For Tech Mahindra | Important SQL Interview Questions

 SQL Interview Questions for IBM | Important SQL Interview Questions

http://www.complexsql.com/subqueries-correlated-subquery/ 10/11
11/30/2017 Subqueries in SQL | Correlated Subquery | Difference Between SubQuery and Correlated Subquery

 Interview Questions For Oracle | SQL interview Questions

 Pivot in SQL with Example | Pivot Clause | Pivot and unpivot in SQL | Pivot in Oracle |
SQL Pivot with Real Life Example

 Di erence between Truncate and Delete, Drop in SQL | Truncate Delete and Drop
Commands in SQL | SQL Di erence between Truncate,Delete and Drop Commands | Real
Life Examples of Truncate Delete and Drop Command

 Process Commands in Unix | Unix Filter commands | Touch command with example | ps
command with examples | Kill command with examples

 Table partition | Table Partitioning in Oracle | Partitioning in SQL | How Partitioning


helps improving Performance | Table Partitioning with Examples | Partitioning on existing
table

 Database Normalization Examples | Normalization in Database with Examples |


Database Normalization with Real Life Examples | Designing Normalized Database

 Operators in SQL | SQL Operators | Arithmetic Operators | Logical Operators |


Comparison Operators | Negation Operators | Operators in SQL with Examples

 Interview Questions for TCS | SQL Interview Questions for TCS | Interview Questions
Asked in TCS | TCS Interview Questions

Comments are closed.

Copyright © 2017 | www.complexsql.com | Amit S

http://www.complexsql.com/subqueries-correlated-subquery/ 11/11

Potrebbero piacerti anche