SQL Order of Operations

The SQL order of operations refers to the sequence in which different clauses of a SQL query are executed. Each clause has a specific purpose and performs a specific task, and the order in which these clauses are executed can affect the final result of the query.

From

The FROM clause is used to specify the table or tables from which to retrieve data in a SELECT statement.

The basic syntax of the FROM clause is:

SELECT * FROM orders;

It's also possible to use multiple tables and subqueries in the FROM clause to retrieve data from a complex join of tables and perform calculations on the data.

It's also common practice to include the keyword "AS" in the table names, to make them more readable and understandable.

Where

The WHERE clause is used to filter rows based on a specified condition.

The basic syntax of the WHERE clause is:

SELECT * FROM orders

WHERE status ='shipped';

It's worth noting that the WHERE clause is optional, and if it is not used in a statement, then all rows from the table(s) will be retrieved.

Group By

The GROUP BY clause is used in a SELECT statement to group rows that have the same values into summary rows.

The GROUP BY clause is typically used in conjunction with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to perform a calculation on each group and return a single value for each group.

For example, the following query groups customers by country and returns the number of customers in each country:

SELECT country, COUNT(*) as num_customers FROM customers

GROUP BY country;

You can use multiple columns in the GROUP BY clause, in that case the query will group the results by the combination of values in those columns.

Having

The HAVING clause is used in a SELECT statement in conjunction with the GROUP BY clause to filter groups based on a specified condition. The HAVING clause is used to filter the results of a query after they have been grouped.

The syntax for the HAVING clause is as follows:

SELECT column1, aggregate_function(column2) FROM table_name

GROUP BY column1

HAVING aggregate_function(column2) condition;


You cannot use the HAVING clause without a GROUP BY clause and cannot use it to filter individual rows.

Select

n SQL, the SELECT clause is used to specify the columns that should be retrieved in a SELECT statement.

The basic syntax of the SELECT clause is:

SELECT column1, column2, ...

The SELECT clause can also be used in combination with other clauses to retrieve and filter data from multiple tables or perform calculations on the data.

It's important to note that the SELECT clause is mandatory in a SELECT statement, and at least one column name must be specified in it. The SELECT clause usually goes first in a SELECT statement.

Order By

The ORDER BY clause is used in a SELECT statement to sort the result set based on one or more columns in ascending or descending order.

The syntax for the ORDER BY clause is as follows:

SELECT column1, column2, ... FROM table_name

ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;

You can also order by multiple columns, when you order by multiple columns, it will first sort by the first column then by the next and so on, it will only sort by next columns when the values are equal in the previous column.

Limit

The LIMIT clause is used to specify the maximum number of rows that a SELECT statement should return. It is typically used in combination with the ORDER BY clause to retrieve a specific number of rows from a table.

The syntax for the LIMIT clause is as follows:

SELECT column1, column2, ... FROM table_name

ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...

LIMIT number_of_rows;

Also the LIMIT clause is not supported by all SQL databases, but is supported by most of them like MySQL, PostgreSQL, SQLite and Redshift, etc.

Author:
Eamonn Woodham
Powered by The Information Lab
1st Floor, 25 Watling Street, London, EC4M 9BR
Subscribe
to our Newsletter
Get the lastest news about The Data School and application tips
Subscribe now
© 2024 The Information Lab