Skip to content

Instantly share code, notes, and snippets.

@dsaiztc
Last active May 19, 2016 10:04
Show Gist options
  • Select an option

  • Save dsaiztc/0d596c8467fd2fcc0c0a to your computer and use it in GitHub Desktop.

Select an option

Save dsaiztc/0d596c8467fd2fcc0c0a to your computer and use it in GitHub Desktop.
SQL basics.
DELETE FROM table_name
WHERE some_column=some_value;
INSERT INTO table
(column1, column2, ... )
VALUES
(expression1, expression2, ... );
INSERT INTO table
(column1, column2, ... )
SELECT expression1, expression2, ...
FROM source_tables
WHERE conditions;
SELECT *
FROM table1
SELECT column1, column2, ...
FROM table1
SELECT column1, column2, ...
FROM table1
WHERE condition(s)
SELECT DISTINCT column1, column2, ...
FROM table1
SELECT column1, column2, ...
FROM table1
ORDER BY column1, column2, ... DESC
SELECT column1 AS alias1, column2 AS alias2, ...
FROM table1
SELECT column1, column2, COUNT(*)
FROM table1
GROUP BY column1, column2
SELECT column1, SUM(column2)
FROM table1
GROUP BY column1
SELECT column1, column2, AVG(column3)
FROM table1
GROUP BY column1, column2 # Equivalent (and less error-prone): GROUP BY 1, 2
SELECT column1, column2, ...
FROM table1
GROUP BY 1, 2, ... # We need to group by each column we select and don't aggregate
HAVING condition1, condition2, ... # We can't call WHERE in aggregate functions (to test the result of aggregation)
SELECT column1, column2, ...
FROM table1, table2
WHERE condition1, condition2, ...
SELECT table1.column1, table1.column2, table2.column1, ...
FROM table1
JOIN table2
ON table1.t1_id = table2.t1_id
WHERE contidion1, condition2, ...
SELECT table1.column1, table1.column2, table2.column1, table3.column2
FROM table1
JOIN table2
ON table1.t1_id = table2.t1_id
JOIN table3
ON table2.t2_id = table3.t2_id
WHERE contidion1, condition2, ...
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment