Click here to Skip to main content
15,895,794 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a question regarding the execution of this query:
SQL
SELECT
    student.id,
    student.name,
    student.class,
    teachers.name
FROM student 
INNER JOIN admin
    ON admin.id = student.id
WHERE
    student.id <> 0


I think it first executes the below statement:
SQL
SELECT
    student.id,
    student.name,
    student.class,
    teachers.name
FROM student
WHERE
    student.id <> 0


I think we will get a table, and then the table which we got from above query will compare with the inner join statement:
SQL
INNER JOIN admin
    ON admin.id = student.id


Am I correct in my above thinking?
Posted
Updated 21-Dec-10 7:25am
v2
Comments
AspDotNetDev 21-Dec-10 13:27pm    
FYI, I reformatted your question. It was hard to read otherwise. Also, where did the "teachers" table come from? That query shouldn't even run.

No, you are not correct.

There are different algorithms, depending on the DB server, indexes and data order (clustered PK), whether calculated values are joined or not etc.

As, suggested here[^], have a look at a query plan, which most SQL systems can create for a query, it should give you an idea what it does.


For more read this[^].
 
Share this answer
 
Comments
Manfred Rudolf Bihy 21-Dec-10 13:28pm    
Nice links, thanks! 5+
This is the execution order of sql server select command:
1. FROM clause
2. WHERE clause
3. GROUP BY clause
4. HAVING clause
5. SELECT clause
6. ORDER BY clause

If you look at the order again you can conclude that FROM is logical to be on first place, because executing it first sql server can dramatically narrow down possible record set sizes. Then it goes WHERE and so on...

Further more there is a several logical joins statements available in sql server, but sql implements these logical operators with "three" different physical operators:
1. Nested Loops Join
2. Merge Join
3. Hash Join
Which one of them is used is a "mistery" to me also ;), because sql have internal logic when and what join type to use. We could write about This is very wide topic, so if you want more information about this google or find some good e-book about it. ;)
My choice is "SQL Server MVP Deep Dives", it is a contributions from many SQL Server MVPs, and you have explained everything.

I hope that I helped at least a little. ;)
 
Share this answer
 
Comments
AspDotNetDev 21-Dec-10 13:42pm    
I wouldn't really say it "executes" in that order (maybe evaluates logically in that order). If it were to "execute" the FROM clause first, it would load into memory every single record in that table, which could take forever. Also, you don't mention JOINs anywhere on your "execution" list... that is what the OP is asking about.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900