When is a subquery executed?

LAST READ: APR 14 2024 | Q. BY: SAJAL RAJWAR

V

Vikram Chawhan

SubQuery | Outer Query


When a query executes with a subquery; in this situation, the SubQuery is executed first after that the Outer Query will be executed.

The query optimizer knows how many rows are in each table, which tables have indexed and on what fields. It uses that information to decide what part to execute first.


How To Execute A SubQuery Execution 

Use two tables, 1st table name is Students and 2nd table is Marks with multiple columns names like [student_id, name from Students table] and [total_marks from Marks table] 


1st query, table_name 'Marks' 


SELECT * FROM 'Marks' 

WHERE student_id = '2';


2nd query, table_name 'Students' 


SELECT a.student_id, a.name, b.total_marks 

FROM Students a, Marks b

WHERE a.student_id = b.student_id

AND b.total_marks >80;


Subquery execution with both upper queries 


SELECT a.student_id, a.name, b.total_marks 

FROM Students a, Marks b

WHERE a.student_id= b.student_id AND b.total_marks >

              (SELECT total_marks

                FROM Marks

                WHERE student_id = '2');

1 votes

Your Answer

Email Us: advertise@gdatamart.com

Donate Us: Support to GDATAMART

LifeStyle & Fun

© 2024 GDATAMART.COM (All Rights Reserved)