Here are the rules and steps to use SELECT statement in SQL:
Step-1: If SELECT statement is UNION of SELECT statements, follow Step-2 to Step-7 for each statement.
Step-2: Create product table from FROM clause. If there’s just one table, then product will be that particular table.
Step-3: For a WHERE clause, apply search condition for each row of product table. Retain rows for which conditions are TRUE and discard rows for which it is FALSE or NULL. If HAVING clause has a subquery, perform subquery for each row.
Step-4: For a GROUP BY clause, remaining rows of product table should be arranged in row groups such that rows in each group have identical values in all of grouping columns.
Step-5: For a HAVING clause, apply search condition to each row group. Retain rows for which conditions are TRUE and discard rows for which it is FALSE or NULL. If HAVING clause has a subquery, perform subquery for each row group.
Step-6: For each remaining row group calculate value of every item in select list to create a single row of query results. For a simple column reference, value of column in current row group should be used. For a column function, current row group as its argument if GROUP BY is specified should be used. Otherwise, entire set of rows should be used.
Step-7: If DISTINCT keyword is used, remove any duplicate rows of query results.
Step-8: If SELECT statement is a UNION of SELECT statement, merge query results for individual statements in to a single table of query results. IF UNION ALL keyword is used, retain duplicate rows .
Step-9: For a ORDER BY clause, sort query results as per ORDER BY.
Step-10: Execution of SELECT is complete.