Last active
May 19, 2016 10:04
-
-
Save dsaiztc/0d596c8467fd2fcc0c0a to your computer and use it in GitHub Desktop.
SQL basics.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DELETE FROM table_name | |
| WHERE some_column=some_value; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| INSERT INTO table | |
| (column1, column2, ... ) | |
| VALUES | |
| (expression1, expression2, ... ); | |
| INSERT INTO table | |
| (column1, column2, ... ) | |
| SELECT expression1, expression2, ... | |
| FROM source_tables | |
| WHERE conditions; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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