SELECT
specify column names from a table, or * for wildcard
SELECT column, another_column
FROM mytable
SELECT DISTINCT
to filter out duplicates
SELECT DISTINCT column another_column, …
FROM mytable
WHERE condition(s);
WHERE
to filter
SELECT column, another_column, …
FROM mytable
WHERE condition
AND/OR another_condition
AND/OR …;
ORDER BY
to sort
SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC;
LIMIT
to reduce results
SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC
LIMIT num_limit OFFSET num_offset;
INSERT
to add data
INSERT INTO mytable
VALUES (value_or_expr, another_value_or_expr, …),
(value_or_expr_2, another_value_or_expr_2, …),
…;
INSERT INTO boxoffice
(movie_id, rating, sales_in_millions)
VALUES (1, 9.9, 283742034 / 1000000);
UPDATE
to update data
UPDATE mytable
SET column = value_or_expr,
other_column = another_value_or_expr,
…
WHERE condition;
DELETE
to delete data
DELETE FROM mytable
WHERE condition;
CREATE
to create a table
CREATE TABLE IF NOT EXISTS mytable (
column DataType TableConstraint DEFAULT default_value,
another_column DataType TableConstraint DEFAULT default_value,
…
);
CREATE TABLE movies (
id INTEGER PRIMARY KEY,
title TEXT,
director TEXT,
year INTEGER,
length_minutes INTEGER
);
ALTER
to modify table columns
ALTER TABLE mytable
ADD column DataType OptionalTableConstraint
DEFAULT default_value;
ALTER TABLE mytable
DROP column_to_be_deleted;
ALTER TABLE mytable
RENAME TO new_table_name;
DROP
to remove table
DROP TABLE IF EXISTS mytable;
W3Schools – Practice
SQL (Structured Query Language) in one page – Cheatsheet