Wednesday, July 8, 2015

SELECT statement in the Transact-SQL and order

Select queries used to Retrieves  rows from the database  and enables the selection of one or many rows or columns. We can use multiple table for retrieving the data , this can be achieved in complex query statement.

Generic syntax for select statement.

select <column names> from <table name> 

The full syntax of the SELECT statement is complex, but the main clauses can be summarized as:
[ WITH { [ XMLNAMESPACES ,] [ <common_table_expression> ] } ]
SELECT select_list [ INTO new_table ]
[ FROM table_source ] [ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC | DESC ] ]


The UNION, EXCEPT and INTERSECT operators can be used between queries to combine or compare their results into one result set.

when query execute the the logical sequence of execution will be 

The following steps show the logical processing order, or binding order, for a SELECT statement. This order determines when the objects defined in one step are made available to the clauses in subsequent steps. For example, if the query processor can bind to (access) the tables or views defined in the FROM clause, these objects and their columns are made available to all subsequent steps. Conversely, because the SELECT clause is step 8, any column aliases or derived columns defined in that clause cannot be referenced by preceding clauses. However, they can be referenced by subsequent clauses such as the ORDER BY clause. Note that the actual physical execution of the statement is determined by the query processor and the order may vary from this list.
  1. FROM
  2. ON
  3. JOIN
  4. WHERE
  5. GROUP BY
  6. WITH CUBE or WITH ROLLUP
  7. HAVING
  8. SELECT
  9. DISTINCT
  10. ORDER BY
  11. TOP

No comments:

Post a Comment