Is there any specific order of execution in SQL query?











up vote
0
down vote

favorite












I am confused about the execution order of SQL queries.



For example, (Inner join in MySQL in the code below), between WHERE clause and SELECT * FROM clause, which one gets to be interpreted and executed first?



That is to say, does the query below bring *(all) of the tables data first then find the cases that match with WHERE condition? or Do they just find the list of data that match with WHERE condition and then SELECT * FROM from the WHERE result?



SELECT * FROM customers, orders
WHERE customers.id = orders.customer_id;


As above case, I am wondering how the SQL queries are executed in general.










share|improve this question


















  • 5




    periscopedata.com/blog/sql-query-order-of-operations
    – scaisEdge
    Nov 4 at 17:45






  • 1




    The whole point of languages like SQL is for you to describe (logically) what you want, and to leave it up to the optimizer about how best to produce that result. That's why e.g. execution order is far more loosely defined with SQL than with many procedural languages.
    – Damien_The_Unbeliever
    Nov 4 at 18:32










  • Possible duplicate of What's the execute order of the different parts of a SQL select statement?
    – philipxy
    Nov 4 at 19:03










  • See How to Ask. Please research.
    – philipxy
    Nov 4 at 19:04















up vote
0
down vote

favorite












I am confused about the execution order of SQL queries.



For example, (Inner join in MySQL in the code below), between WHERE clause and SELECT * FROM clause, which one gets to be interpreted and executed first?



That is to say, does the query below bring *(all) of the tables data first then find the cases that match with WHERE condition? or Do they just find the list of data that match with WHERE condition and then SELECT * FROM from the WHERE result?



SELECT * FROM customers, orders
WHERE customers.id = orders.customer_id;


As above case, I am wondering how the SQL queries are executed in general.










share|improve this question


















  • 5




    periscopedata.com/blog/sql-query-order-of-operations
    – scaisEdge
    Nov 4 at 17:45






  • 1




    The whole point of languages like SQL is for you to describe (logically) what you want, and to leave it up to the optimizer about how best to produce that result. That's why e.g. execution order is far more loosely defined with SQL than with many procedural languages.
    – Damien_The_Unbeliever
    Nov 4 at 18:32










  • Possible duplicate of What's the execute order of the different parts of a SQL select statement?
    – philipxy
    Nov 4 at 19:03










  • See How to Ask. Please research.
    – philipxy
    Nov 4 at 19:04













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am confused about the execution order of SQL queries.



For example, (Inner join in MySQL in the code below), between WHERE clause and SELECT * FROM clause, which one gets to be interpreted and executed first?



That is to say, does the query below bring *(all) of the tables data first then find the cases that match with WHERE condition? or Do they just find the list of data that match with WHERE condition and then SELECT * FROM from the WHERE result?



SELECT * FROM customers, orders
WHERE customers.id = orders.customer_id;


As above case, I am wondering how the SQL queries are executed in general.










share|improve this question













I am confused about the execution order of SQL queries.



For example, (Inner join in MySQL in the code below), between WHERE clause and SELECT * FROM clause, which one gets to be interpreted and executed first?



That is to say, does the query below bring *(all) of the tables data first then find the cases that match with WHERE condition? or Do they just find the list of data that match with WHERE condition and then SELECT * FROM from the WHERE result?



SELECT * FROM customers, orders
WHERE customers.id = orders.customer_id;


As above case, I am wondering how the SQL queries are executed in general.







mysql sql inner-join






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 4 at 17:42









Poream3387

369112




369112








  • 5




    periscopedata.com/blog/sql-query-order-of-operations
    – scaisEdge
    Nov 4 at 17:45






  • 1




    The whole point of languages like SQL is for you to describe (logically) what you want, and to leave it up to the optimizer about how best to produce that result. That's why e.g. execution order is far more loosely defined with SQL than with many procedural languages.
    – Damien_The_Unbeliever
    Nov 4 at 18:32










  • Possible duplicate of What's the execute order of the different parts of a SQL select statement?
    – philipxy
    Nov 4 at 19:03










  • See How to Ask. Please research.
    – philipxy
    Nov 4 at 19:04














  • 5




    periscopedata.com/blog/sql-query-order-of-operations
    – scaisEdge
    Nov 4 at 17:45






  • 1




    The whole point of languages like SQL is for you to describe (logically) what you want, and to leave it up to the optimizer about how best to produce that result. That's why e.g. execution order is far more loosely defined with SQL than with many procedural languages.
    – Damien_The_Unbeliever
    Nov 4 at 18:32










  • Possible duplicate of What's the execute order of the different parts of a SQL select statement?
    – philipxy
    Nov 4 at 19:03










  • See How to Ask. Please research.
    – philipxy
    Nov 4 at 19:04








5




5




periscopedata.com/blog/sql-query-order-of-operations
– scaisEdge
Nov 4 at 17:45




periscopedata.com/blog/sql-query-order-of-operations
– scaisEdge
Nov 4 at 17:45




1




1




The whole point of languages like SQL is for you to describe (logically) what you want, and to leave it up to the optimizer about how best to produce that result. That's why e.g. execution order is far more loosely defined with SQL than with many procedural languages.
– Damien_The_Unbeliever
Nov 4 at 18:32




The whole point of languages like SQL is for you to describe (logically) what you want, and to leave it up to the optimizer about how best to produce that result. That's why e.g. execution order is far more loosely defined with SQL than with many procedural languages.
– Damien_The_Unbeliever
Nov 4 at 18:32












Possible duplicate of What's the execute order of the different parts of a SQL select statement?
– philipxy
Nov 4 at 19:03




Possible duplicate of What's the execute order of the different parts of a SQL select statement?
– philipxy
Nov 4 at 19:03












See How to Ask. Please research.
– philipxy
Nov 4 at 19:04




See How to Ask. Please research.
– philipxy
Nov 4 at 19:04












1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










There is a logical order to evaluation of the query text, but the database engine can choose what order execute the query components based upon what is most optimal. The logical text parsing ordering is listed below. That is, for example, why you can't use an alias from SELECT clause in a WHERE clause. As far as the query parsing process is concerned, the alias doesn't exist yet.




  1. FROM


  2. ON


  3. OUTER


  4. WHERE


  5. GROUP BY


  6. CUBE | ROLLUP (these are not present in MySQL but are in some other SQL dialects)


  7. HAVING


  8. SELECT


  9. DISTINCT


  10. ORDER BY


  11. LIMIT (or, in MSSQL, TOP)



See the Microsoft documentation (see "Logical Processing Order of the SELECT statement") for more information on this.






share|improve this answer























  • nice answer ... it would be better with a reference
    – Dan Farrell
    Nov 4 at 17:48






  • 1




    @DanFarrell : done.
    – rsjaffe
    Nov 4 at 17:50






  • 1




    Question is tagged MySQL. There is no TOP in MySQL; instead the keword is LIMIT
    – Madhur Bhaiya
    Nov 4 at 18:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53143613%2fis-there-any-specific-order-of-execution-in-sql-query%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










There is a logical order to evaluation of the query text, but the database engine can choose what order execute the query components based upon what is most optimal. The logical text parsing ordering is listed below. That is, for example, why you can't use an alias from SELECT clause in a WHERE clause. As far as the query parsing process is concerned, the alias doesn't exist yet.




  1. FROM


  2. ON


  3. OUTER


  4. WHERE


  5. GROUP BY


  6. CUBE | ROLLUP (these are not present in MySQL but are in some other SQL dialects)


  7. HAVING


  8. SELECT


  9. DISTINCT


  10. ORDER BY


  11. LIMIT (or, in MSSQL, TOP)



See the Microsoft documentation (see "Logical Processing Order of the SELECT statement") for more information on this.






share|improve this answer























  • nice answer ... it would be better with a reference
    – Dan Farrell
    Nov 4 at 17:48






  • 1




    @DanFarrell : done.
    – rsjaffe
    Nov 4 at 17:50






  • 1




    Question is tagged MySQL. There is no TOP in MySQL; instead the keword is LIMIT
    – Madhur Bhaiya
    Nov 4 at 18:12















up vote
4
down vote



accepted










There is a logical order to evaluation of the query text, but the database engine can choose what order execute the query components based upon what is most optimal. The logical text parsing ordering is listed below. That is, for example, why you can't use an alias from SELECT clause in a WHERE clause. As far as the query parsing process is concerned, the alias doesn't exist yet.




  1. FROM


  2. ON


  3. OUTER


  4. WHERE


  5. GROUP BY


  6. CUBE | ROLLUP (these are not present in MySQL but are in some other SQL dialects)


  7. HAVING


  8. SELECT


  9. DISTINCT


  10. ORDER BY


  11. LIMIT (or, in MSSQL, TOP)



See the Microsoft documentation (see "Logical Processing Order of the SELECT statement") for more information on this.






share|improve this answer























  • nice answer ... it would be better with a reference
    – Dan Farrell
    Nov 4 at 17:48






  • 1




    @DanFarrell : done.
    – rsjaffe
    Nov 4 at 17:50






  • 1




    Question is tagged MySQL. There is no TOP in MySQL; instead the keword is LIMIT
    – Madhur Bhaiya
    Nov 4 at 18:12













up vote
4
down vote



accepted







up vote
4
down vote



accepted






There is a logical order to evaluation of the query text, but the database engine can choose what order execute the query components based upon what is most optimal. The logical text parsing ordering is listed below. That is, for example, why you can't use an alias from SELECT clause in a WHERE clause. As far as the query parsing process is concerned, the alias doesn't exist yet.




  1. FROM


  2. ON


  3. OUTER


  4. WHERE


  5. GROUP BY


  6. CUBE | ROLLUP (these are not present in MySQL but are in some other SQL dialects)


  7. HAVING


  8. SELECT


  9. DISTINCT


  10. ORDER BY


  11. LIMIT (or, in MSSQL, TOP)



See the Microsoft documentation (see "Logical Processing Order of the SELECT statement") for more information on this.






share|improve this answer














There is a logical order to evaluation of the query text, but the database engine can choose what order execute the query components based upon what is most optimal. The logical text parsing ordering is listed below. That is, for example, why you can't use an alias from SELECT clause in a WHERE clause. As far as the query parsing process is concerned, the alias doesn't exist yet.




  1. FROM


  2. ON


  3. OUTER


  4. WHERE


  5. GROUP BY


  6. CUBE | ROLLUP (these are not present in MySQL but are in some other SQL dialects)


  7. HAVING


  8. SELECT


  9. DISTINCT


  10. ORDER BY


  11. LIMIT (or, in MSSQL, TOP)



See the Microsoft documentation (see "Logical Processing Order of the SELECT statement") for more information on this.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 5 at 3:53

























answered Nov 4 at 17:46









rsjaffe

3,36871431




3,36871431












  • nice answer ... it would be better with a reference
    – Dan Farrell
    Nov 4 at 17:48






  • 1




    @DanFarrell : done.
    – rsjaffe
    Nov 4 at 17:50






  • 1




    Question is tagged MySQL. There is no TOP in MySQL; instead the keword is LIMIT
    – Madhur Bhaiya
    Nov 4 at 18:12


















  • nice answer ... it would be better with a reference
    – Dan Farrell
    Nov 4 at 17:48






  • 1




    @DanFarrell : done.
    – rsjaffe
    Nov 4 at 17:50






  • 1




    Question is tagged MySQL. There is no TOP in MySQL; instead the keword is LIMIT
    – Madhur Bhaiya
    Nov 4 at 18:12
















nice answer ... it would be better with a reference
– Dan Farrell
Nov 4 at 17:48




nice answer ... it would be better with a reference
– Dan Farrell
Nov 4 at 17:48




1




1




@DanFarrell : done.
– rsjaffe
Nov 4 at 17:50




@DanFarrell : done.
– rsjaffe
Nov 4 at 17:50




1




1




Question is tagged MySQL. There is no TOP in MySQL; instead the keword is LIMIT
– Madhur Bhaiya
Nov 4 at 18:12




Question is tagged MySQL. There is no TOP in MySQL; instead the keword is LIMIT
– Madhur Bhaiya
Nov 4 at 18:12


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53143613%2fis-there-any-specific-order-of-execution-in-sql-query%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings