Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info

SQL is a language intended for table transformation. The input is one or more tables, the output is also a table and every instruction applied in the middle gets and outputs a table. Everything in

...

SELECT clause is about tables.

Tables have columns and columns have a name and a data type: INT, STRING, DATE, BOOLEAN, etc.

Table rows are data records and the intersection with a column is a value (which can be NULL).

...

The execution order of a SQL query is not the same as the used to write it:

FROM → JOIN ON → WHERE → SELECT → DISCTINCT → GROUP BY → HAVING → ORDER BY → OFFSET → LIMIT

  1. Everything starts with a FROM table.

...

  1. The FROM table (1) is JOIN with other tables ON some constraints to produce a new table.

  2. Then the table (

...

  1. 2) above is pruned horizontally with WHERE by dropping all the records

...

  1. not matching the conditions

  2. The new table (

...

  1. 3) is pruned vertically then, to match the SELECT columns only.

  2. DISTINCT removes all the duplicate records in (

...

  1. 4)

  2. GROUP BY will create a new table from (

...

  1. 5) grouping by the

...

  1. provided columns.

  2. HAVING will prune the previous (

...

  1. 6) table by removing all the records

...

  1. that don't match the conditions. HAVE acts similarly to the WHERE clause for the GROUP BY.

  2. The filtered grouped table (

...

  1. 7) will be ORDER BY some columns

  2. OFFSET N will skip N first records in (

...

  1. 8)

  2. and LIMIT M will take the output table from (

...

  1. 9) and remove all the records after the M first ones.

The result of all the above operations (many optional) will output another temporary table that will be destroyed once it has been read.

...