Query Flow in SQL
Query Flow in a SQL statement is in a specific fixed order:
SELECT [] FROM [] WHERE [] GROUP BY [] HAVING [] ORDER BY []
Query is optimized by SQL engine, and is executed depending on the data and available indexes.
What is the function the SELECT is to tell SQL server to retrieve data, particular columns and rows and how to sort it.
Syntax of SELECT statement in SQL
SELECT [DISTINCT][TOP (n)] *, columns, or expressions [FROM data source(s)]
[JOIN data source ON condition]
[WHERE conditions] (filters rows based upon condition)
[GROUP BY columns] (groups data into smaller sets of data)
[HAVING conditions] (restricts aggregate functions)
[ORDER BY Columns]; (decides sort order of data)
SELECT statement in details
SELECT statement begins with:
- the name of column or list of columns
- Other expressions
In a basic form SELECT statement:
Without FROM clause, SELECT produces a simple row with value or variable.
FROM clause comes next following the SELECT and what to select as column. After FROM, the table name is provided. For example:
SELECT employeeColumn FROM EmployeeTable
FROM also has ability to gather data from multiple tables or from multiple data sources. FROM is essential part of query. Because, to get an output from a column, sort it, indicate a WHERE clause for retrieving a particular row and related tasks, FROM shows the table where column is. Without the table, WHERE, ORDER BY, GROUP BY and other clauses would not have a data to work with their conditional statements.