goofyui
goofyui

Reputation: 3492

What will be the sequence of SQL Join query execution?

When we have multiple Join queries , Derived table join query and finally where conditions, What will be the sequence of SQL Join query execution?

Which block of code will get executed first (or) whether outermost block or inner most block of code in SQL ?

select 
  TablePerson.PersonName, TablePet.PetName, TablePet.PetType
from 
  TablePerson
left outer join 
(
  select TablePet.ownerID, TablePet.PetName, TableTypes.PetType
  from TablePet 
  inner join TableTypes on TablePet.PetTypeID = TableTypes.TypeID
) 
  TablePet
on
  TablePet.OwnerID = TablePerson.PersonID

Upvotes: 2

Views: 1771

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269643

SQL is a declarative language, not a procedural language. A SQL query represents the result set, not the specific actions to generate it.

SQL basically has three steps in processing a query:

  • Parsing the query
  • Optimizing the query to generate the best execution plan for the data
  • Executing the query

The exact same query can have different execution plans at different times, depending on the data and the environment.

This is very important to understand. You cannot specify what the execution plan is. The optimizer determines that -- regardless of CTEs and subqueries. You can influence the execution plan by providing hints in the query.

Upvotes: 2

Related Questions