...
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 SQL is about tables. |
Tables have columns and columns have a name and a data type: INT, STRING, DATE, BOOLEAN, etc.
...
The execution order of a SQL is not the as the read query:
FROM → JOIN ON → WHERE → SELECT → DISCTINCT → GROUP BY → HAVING → ORDER BY → OFFSET → LIMIT
...
Everything starts with a FROM table. which
The FROM table (1) is JOIN with other tables ON some constraints to produce a new table.
Then the table (12) above is pruned horizontally with WHERE by dropping all the records no matching the conditions
The new table (23) is pruned vertically then, to match the SELECT columns only.
DISTINCT removes all the duplicate records in (34)
GROUP BY will create a new table from (45) grouping by the provide columns.
HAVING will prune the previous (56) table by removing all the records no matching the conditions. HAVE acts similarly to the WHERE clause for the GROUP BY.
The filtered grouped table (67) will be ORDER BY some columns
OFFSET N will skip N first records in (78)
and LIMIT M will take the output table from (89) and remove all the records after the M first ones.
...