Basics
Basic SQL Structures & Command
Order of basic clauses:
WITH
SELECT list-cols [INTO new-table][AS col-alias]
FROM table-source [WHERE search-condition]
GROUP BY groupby-expression
WINDOW windowfunc-expression
HAVING search-condition (within groupby clause)
ORDER BY order-expression [ASC ascending | DESC descending]
LIMIT num-results
UNION, EXCEPT, INTERSECT, AND, OR, AS, RANGE, IN, BETWEEN, etc. can be used between queries to combine or compare their results. However, the order of execution is different.
Order of execution in SQL commands:
FROM / JOIN / ON
WHERE
GROUP BY
WITH CUBE | ROLLUP
HAVING
SELECT (Window Functions SUM, COUNT, AVG, etc. are executed here!)
DISTINCT
ORDER BY
TOP
LIMIT
Command | Description | Example |
---|---|---|
SELECT | Selects given columns from a given table |
|
FROM | Table source to display |
|
WHERE | Condition to filter table |
|
(I)LIKE | Matches similar values |
|
IN (BETWEEN) | Including (within) values |
|
AND (OR) | Logic and (or) operators |
|
IS (NOT) NULL | Is data missing (or not)? |
|
GROUP BY | Groups data by a condition |
|
HAVING | Filters grouped datasets with given conditions |
|
ORDER BY | Sorts data asc or desc order |
|
LIMIT | Limits the number of results |
|
SQL Aggregate Functions:
Aggregate | Description | Example |
---|---|---|
COUNT | Counts num rows in a given column | |
SUM | Adds values in a given column | |
MIN (MAX) | Returns minimum (maximum) value in a column | |
AVG | Calculates the average of group of selected values |
Last updated