Basic SQL syntax
SELECT, FROM
# Provide the columns from the table where the columns exist
SELECT COLUMN FROM TABLE; # for one col
SELECT COL1, COL2, COL3 FROM TABLE; # for more cols
LIMIT
# Limits based number of rows returned
SELECT COL FROM TABLE LIMIT number;
ORDER BY
# Orders table based on the column. Used with DESC.
SELECT COL FROM TABLE ORDER BY COL; # from low to high
SELECT COL FROM TABLE ORDER BY COL DESC; # or
SELECT COL FROM TABLE ORDER BY -COL; # from high to low
WHERE
# A conditional statement to filter your results with specified string
SELECT id, col1, col2 FROM TABLE WHERE col1 = 'Sample String';
Operator
# add a new col calculate by col1, col2
SELECT id, col1, col2, col1/col2 AS col3 FROM TABLE LIMIT 10;
LIKE
# Fuzzy match, only pulls rows where column has 'me' within the text
SELECT col FROM TABLE WHERE col LIKE 'C%'; # also can %s%, %s
IN
# A filter for only rows with column of 'Y' or 'N'
SELECT * FROM TABLE WHERE col IN ('stringA', 'stringB');
NOT
# NOT is frequently used with LIKE and IN
# results will not contain the specified string
SELECT * FROM TABLE WHERE col NOT IN ('stringA', 'stringB');
SELECT * FROM TABLE WHERE col NOT LIKE 'C%'; # also can %s%, %s
AND & BETWEEN
# AND: Filter rows where two or more conditions must be true
# BETWEEN: Often easier syntax than using an AND
SELECT * FROM TABLE WHERE col1 > 1000 AND col2 = 0 AND col3 = 0;
SELECT * FROM TABLE WHERE col IN 'string' AND col2 BETWEEN '2016-01-01' AND '2017-01-01'
ORDER BY occurred_at DESC;
OR
# Filter rows where at least one condition must be true
SELECT col1 FROM TABLE WHERE col2 > 10 OR col3 > 10;
JOIN (Next...)
网友评论