Skip to main content

Difference between correlated and non correlated subquery in oracle sql plsql interview question.

 "It is the accepted question in oracle SQL and plsql interview question."


INTRODUCTION:

Subqueries can be classified into the two parts i.e. Correlated and non-correlated subquery. In both mention subquery, there is the combination of outer and inner query. Same select statement are used for both the sub-queries. Execution of the sub query can include from and where clause, group by, having and order by clause. It contains the select statement inside the another select statement. It is used with the update and delete statements.

Correlated subquery is also called as the simple subquery. Not frequently, both subqueries type is providing the same results. Subquery is also called as the nested query. SQL query is divided into two parts i.e. inner subquery and outer subquery. Outer query is called as the parent query. Inner query is called as the child query.

Subqueries can be defined as the query which is nested in the other query. Subquery can be used to update the database by using insert, update and delete  sql statements. Subquery which is used for selecting the records as per where or having clause is called as inner query. Subquery inside another subquery is called as the nested subqueries. 

In order to get or retrieve the data from one table and condition based on another table, we can use join or subquery but in order to get or retrieve the data from more than one tables then we have to use join instead of subquery.

In the where clause, we can use upto 255 subqueries. In the from clause and select clause, there has been no limit specified.

CORRELATED SUBQUERY:

When Subquery refers to the main query for execution, it is called as correlated subquery. It refers the column from the outer query. It is repeatedly executing the subquery for each row of the outer query using the current row value to provide an output. Subquery result is used for evaluation purpose of the outer query. It is processed row by row. It is evaluated by the parent statement i.e. insert, update and delete. ANY and ALL operator is used in the correlated subquery. 
Process:
From outer query, candidate get is obtained.
The inner query is executed with the help of candidate key value.
Inner query value is used to qualify or disqualify the candidate key of the outer query.

It is also called as synchronized query. The word correlate means that mutual relationship or connection between the two things which is dependent on one another.

EXISTS:

It is used to check the presence or existence of any record in the subquery and will return true if the subquery returns one or more row records. It is generally used in the correlated subqueries, and it is used with the WHERE clause to filter the rows on the specific condition. You can conditionally retrieve the data based on the existence of related records in the same or another table.

SYNTAX:

select column_name from table_name where column_name EXISTS (Subquery);

IN:

It is used for small range of values upto 1000.IN operator is less preferred than exists operator in the large and complex subquery result set.

SYNTAX:

select column_name from table_name where column_name IN (Subquery);

NOT EXISTS:

It is just opposite of exist operator.
SYNTAX:

select column_name from table_name where column_name NOT EXISTS (Subquery);

NON- CORRELATED SUBQUERY:
When subquery is executed once and its result is used to extract the data from the main query, then it is called as non-correlated subquery. It is called as the simple subquery and it is classified into single row subquery,multi row subquery and multi column subquery.

Scalar subquery is defined as subquery which returns the single value as output and it is mostly used with aggregate functions i.e count,min,max,sum and avg.

The below provided comparison operator is used in the subquery, then it is called as single row or scalar subquery.

=

equal

> 

Greater than

< 

Less than

>=

Greater than equal to

<=

Less than equal to

<> 

Not equal

!=

Not equal

Multi-row subquery: It is the type of subquery in SQL which returns the multiple rows of the data.

The below provided operators is used in the subquery, then it is called as multi row subquery.

IN

Equal to any value present in the list.

NOT IN

Not equal to any value in the list.

ANY

It will return any row that will match with value in the list. It is used with the WHERE or HAVING clause to filter the rows on the specific condition and will return true when at least one value after comparison.SYNTAX:select column_name from table_name where column_name operator ANY(Subquery); Operator should be any one i.e. <,>,<>,!=,=,>=and<=

ALL

It will return all row that will match all the values in the list. It will return true when all value in the set will match after comparison. It is used with the WHERE or HAVING clause to filter the rows on the specific condition.SYNTAX:select column_name from table_name where column_name operator ALL(Subquery); Operator should be any one i.e. <,>,<>,!=,=,>=and<=


DIFFERENCE:


CORRELATED SUBQUERY

NONCORRELATED SUBQUERY

It is dependent on the outer query.

It is independent on the outer query.

It executes the outer query before the inner query.

It executes the inner query before the outer query.

Less efficient in terms of performance for large datasets.

More efficient.

It is not used with IN and not in clause.

It is used with IN and not in clause.

It is slower queries.

It is faster queries.

It depends on the data of the outer query.

It does not depend on the data of the outer query.

It is more flexible than non-correlated subqueries.

It is less flexible than non-correlated subqueries.

Due to complex to understand, it is used in specific  

Due to easier to understand, it is more used.

It is evaluated in top to bottom manner.

It is evaluated in bottom to up manner.

Inner query is executed for each row in outer query.

Outer query is executed for each row in inner query.


Comments

Popular posts from this blog

Difference between union and union all in oracle sql plsql

"This is one of the most important questions that interviewer might be asking in an interview.Infyosis,Linked group,Kyndryl,Vodafone Idea,IBM and Mphasis etc" INTRODUCTION: It is said to be set operators which is used to combine, add or concatenate two or more result sets. It is used in the multiple select statements where to combine to produce the final desired results.In other words,multiple select statements of similar column name in same order and having same data type to produce the unified result.In MIS datalake,when you are adding the modules of GST reconciliation which is to be used in micros in Excel or VB script, union and union all set operator is preferred for eliminating duplicate records or keeping duplicate records as per business requirement. Column must be of the same datatype for these operations of one select statement with another select statements. Columns must be in the same order in this set operators in the multiple select statements. Same number of co

Difference between decode and case oracle sql plsql interview question and answer

" This is the most accepted question for PLSQL developer role.It has been asked in many interviews like Mphasis and Kyndryl. "  INTRODUCTION: Case was came after oracle database version 8.1.6 whereas decode was came before oracle database version 8.1.6 .Decode and case statements in an oracle work or acts like if then elsif conditional statements. if sal='2000000' then 'General Manager' elsif sal='3000000' then 'Senior Manager' elsif sal='1200000' then 'Manager' else 'Employee' end if; create table student_details(Student_RollNo Number,Student_Name Varchar2(20),student_marks Number,student_result varchar2(1)); insert into student_details values (100,'Rahul',90,'P'); insert into student_details values (101,'Rakesh',30,'F'); insert into student_details values (102,'Ram',60,'P'); insert into student_details values (103,'Rohan',10,'F'); insert into student_detai

Difference view and Materialized view in Oracle SQL PLSQL Interview Question and Answer.

 " This is excepted question in oracle interview. It is asked in oracle technical support,PLSQL developer, SQL Analyst and SQL developer. " INTRODUCTION: View and Materialized view are called as the database object or the named query. View is the virtual table which stores the select statement from the base table by execution of the sql query at the runtime. View Query is running for the view definition. In the case of the materialized view, the data from the base table is loaded in the temporary or physical memory location by which the materialized view gets refreshed and all the dependent tables on the materialized view are also get updated after refresh operation. Materialized views are used in lakhs lines of code. In the data lake, tables are dependent on the master table to get updated where more than lakhs line of codes, today every reporting is based on this, in that case materialized view are used and preferred on the master tables. An activity of materialized view wi