REFERENCE : http://www.ifadey.com/2010/11/best-way-to-write-sql-query/
Clause Order
Thinking and writing SQL query clauses in particular order (in which database engine executes the query) matters a lot because problem (getting required data) gets much simplified by creating query in such an order. Actually the problem is automatically divided and simplified when writing query in execution order. Let’s take a look how the database engine executes the query.1. The Source of Data
The first clause which gets executed is FROM clause. It means the first step when you write a query is to know FROM which table(s) you want to get the data. If the required data is in multiple tables, then think about the JOINs in first step. When FROM clause gets executed, all rows are fetched from the tables specified. Let’s call them Individual Rows.Click here to read more about SQL JOINs.
2. Filter Individual Rows
Second step is to think about WHERE clause which is used to filter Individual Rows on particular condition(s).3. Grouping of Individual Rows
GROUP BY is the third clause which gets executed. So think about it in the third step when writing a query. This clause group Individual Rows (fetched using FROM and filtered using WHERE) on the basis and order of columns specified.Click here to read more about SQL GROUP BY.
No comments:
Post a Comment