notebook/IT/SQL/Cheasheet.md

243 lines
9.4 KiB
Markdown
Raw Permalink Normal View History

2021-04-20 21:13:14 +00:00
# SQL Cheatsheet
2020-11-26 17:38:25 +00:00
2021-04-20 21:13:14 +00:00
## Quick SQL Cheatsheet
2020-11-26 17:38:25 +00:00
2021-04-20 21:13:14 +00:00
A quick reminder of all relevant SQL queries and examples on how to use them.
This repository is constantly being updated and added to by the community.
2020-11-26 17:38:25 +00:00
Pull requests are welcome. Enjoy!
2021-04-20 21:13:14 +00:00
## Table of Contents
1. [Finding Data Queries.](#find)
2. [Data Modification Queries.](#modify)
3. [Reporting Queries.](#report)
4. [Join Queries.](#joins)
5. [View Queries.](#view)
6. [Altering Table Queries.](#alter)
7. [Creating Table Query.](#create)
2020-11-26 17:38:25 +00:00
<a name="find"></a>
2021-04-20 21:13:14 +00:00
## 1. Finding Data Queries
2020-11-26 17:38:25 +00:00
### **SELECT**: used to select data from a database
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT` * `FROM` table_name;
### **DISTINCT**: filters away duplicate values and returns rows of specified column
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT DISTINCT` column_name;
### **WHERE**: used to filter records/rows
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT` column1, column2 `FROM` table_name `WHERE` condition;
* `SELECT` * `FROM` table_name `WHERE` condition1 `AND` condition2;
* `SELECT` * `FROM` table_name `WHERE` condition1 `OR` condition2;
* `SELECT` * `FROM` table_name `WHERE NOT` condition;
* `SELECT` * `FROM` table_name `WHERE` condition1 `AND` (condition2 `OR` condition3);
* `SELECT` * `FROM` table_name `WHERE EXISTS` (`SELECT` column_name `FROM` table_name `WHERE` condition);
### **ORDER BY**: used to sort the result-set in ascending or descending order
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT` * `FROM` table_name `ORDER BY` column;
* `SELECT` * `FROM` table_name `ORDER BY` column `DESC`;
* `SELECT` * `FROM` table_name `ORDER BY` column1 `ASC`, column2 `DESC`;
### **SELECT TOP**: used to specify the number of records to return from top of table
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT TOP` number columns_names `FROM` table_name `WHERE` condition;
* `SELECT TOP` percent columns_names `FROM` table_name `WHERE` condition;
* Not all database systems support `SELECT TOP`. The MySQL equivalent is the `LIMIT` clause
* `SELECT` column_names `FROM` table_name `LIMIT` offset, count;
### **LIKE**: operator used in a WHERE clause to search for a specific pattern in a column
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* % (percent sign) is a wildcard character that represents zero, one, or multiple characters
* _ (underscore) is a wildcard character that represents a single character
* `SELECT` column_names `FROM` table_name `WHERE` column_name `LIKE` pattern;
* `LIKE` a% (find any values that start with “a”)
* `LIKE` %a (find any values that end with “a”)
* `LIKE` %or% (find any values that have “or” in any position)
* `LIKE` _r% (find any values that have “r” in the second position)
* `LIKE` a_%_% (find any values that start with “a” and are at least 3 characters in length)
* `LIKE` [a-c]% (find any values starting with “a”, “b”, or “c”
### **IN**: operator that allows you to specify multiple values in a WHERE clause
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* essentially the IN operator is shorthand for multiple OR conditions
* `SELECT` column_names `FROM` table_name `WHERE` column_name `IN` (value1, value2, …);
* `SELECT` column_names `FROM` table_name `WHERE` column_name `IN` (`SELECT STATEMENT`);
### **BETWEEN**: operator selects values within a given range inclusive
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT` column_names `FROM` table_name `WHERE` column_name `BETWEEN` value1 `AND` value2;
* `SELECT` * `FROM` Products `WHERE` (column_name `BETWEEN` value1 `AND` value2) `AND NOT` column_name2 `IN` (value3, value4);
* `SELECT` * `FROM` Products `WHERE` column_name `BETWEEN` #01/07/1999# AND #03/12/1999#;
### **NULL**: values in a field with no value
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT` * `FROM` table_name `WHERE` column_name `IS NULL`;
* `SELECT` * `FROM` table_name `WHERE` column_name `IS NOT NULL`;
### **AS**: aliases are used to assign a temporary name to a table or column
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT` column_name `AS` alias_name `FROM` table_name;
* `SELECT` column_name `FROM` table_name `AS` alias_name;
* `SELECT` column_name `AS` alias_name1, column_name2 `AS` alias_name2;
* `SELECT` column_name1, column_name2 + , + column_name3 `AS` alias_name;
### **UNION**: set operator used to combine the result-set of two or more SELECT statements
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* Each SELECT statement within UNION must have the same number of columns
* The columns must have similar data types
* The columns in each SELECT statement must also be in the same order
* `SELECT` columns_names `FROM` table1 `UNION SELECT` column_name `FROM` table2;
* `UNION` operator only selects distinct values, `UNION ALL` will allow duplicates
### **INTERSECT**: set operator which is used to return the records that two SELECT statements have in common
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* Generally used the same way as **UNION** above
* `SELECT` columns_names `FROM` table1 `INTERSECT SELECT` column_name `FROM` table2;
### **EXCEPT**: set operator used to return all the records in the first SELECT statement that are not found in the second SELECT statement
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* Generally used the same way as **UNION** above
* `SELECT` columns_names `FROM` table1 `EXCEPT SELECT` column_name `FROM` table2;
### **ANY|ALL**: operator used to check subquery conditions used within a WHERE or HAVING clauses
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* The `ANY` operator returns true if any subquery values meet the condition
* The `ALL` operator returns true if all subquery values meet the condition
* `SELECT` columns_names `FROM` table1 `WHERE` column_name operator (`ANY`|`ALL`) (`SELECT` column_name `FROM` table_name `WHERE` condition);
### **GROUP BY**: statement often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT` column_name1, COUNT(column_name2) `FROM` table_name `WHERE` condition `GROUP BY` column_name1 `ORDER BY` COUNT(column_name2) DESC;
### **HAVING**: this clause was added to SQL because the WHERE keyword could not be used with aggregate functions
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT` `COUNT`(column_name1), column_name2 `FROM` table `GROUP BY` column_name2 `HAVING` `COUNT(`column_name1`)` > 5;
### **WITH**: often used for retrieving hierarchical data or re-using temp result set several times in a query. Also referred to as "Common Table Expression"
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `WITH RECURSIVE` cte `AS` (<br/>
2021-04-20 21:13:14 +00:00
&nbsp;&nbsp;`SELECT` c0.\* `FROM` categories `AS` c0 `WHERE` id = 1 `# Starting point`<br/>
2020-11-26 17:38:25 +00:00
&nbsp;&nbsp;`UNION ALL`<br/>
2021-04-20 21:13:14 +00:00
&nbsp;&nbsp;`SELECT` c1.\* `FROM` categories `AS` c1 `JOIN` cte `ON` c1.parent_category_id = cte.id<br/>
2020-11-26 17:38:25 +00:00
)<br/>
`SELECT` *<br/>
`FROM` cte
<a name="modify"></a>
2021-04-20 21:13:14 +00:00
## 2. Data Modification Queries
2020-11-26 17:38:25 +00:00
### **INSERT INTO**: used to insert new records/rows in a table
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `INSERT INTO` table_name (column1, column2) `VALUES` (value1, value2);
* `INSERT INTO` table_name `VALUES` (value1, value2 …);
### **UPDATE**: used to modify the existing records in a table
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `UPDATE` table_name `SET` column1 = value1, column2 = value2 `WHERE` condition;
* `UPDATE` table_name `SET` column_name = value;
### **DELETE**: used to delete existing records/rows in a table
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `DELETE FROM` table_name `WHERE` condition;
* `DELETE` * `FROM` table_name;
<a name="report"></a>
2021-04-20 21:13:14 +00:00
## 3. Reporting Queries
2020-11-26 17:38:25 +00:00
### **COUNT**: returns the # of occurrences
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT COUNT (DISTINCT` column_name`)`;
### **MIN() and MAX()**: returns the smallest/largest value of the selected column
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT MIN (`column_names`) FROM` table_name `WHERE` condition;
* `SELECT MAX (`column_names`) FROM` table_name `WHERE` condition;
### **AVG()**: returns the average value of a numeric column
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT AVG (`column_name`) FROM` table_name `WHERE` condition;
### **SUM()**: returns the total sum of a numeric column
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT SUM (`column_name`) FROM` table_name `WHERE` condition;
<a name="joins"></a>
2021-04-20 21:13:14 +00:00
## 4. Join Queries
### **INNER JOIN**: returns records that have matching value in both tables
2020-11-26 17:38:25 +00:00
* `SELECT` column_names `FROM` table1 `INNER JOIN` table2 `ON` table1.column_name=table2.column_name;
* `SELECT` table1.column_name1, table2.column_name2, table3.column_name3 `FROM` ((table1 `INNER JOIN` table2 `ON` relationship) `INNER JOIN` table3 `ON` relationship);
### **LEFT (OUTER) JOIN**: returns all records from the left table (table1), and the matched records from the right table (table2)
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT` column_names `FROM` table1 `LEFT JOIN` table2 `ON` table1.column_name=table2.column_name;
### **RIGHT (OUTER) JOIN**: returns all records from the right table (table2), and the matched records from the left table (table1)
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT` column_names `FROM` table1 `RIGHT JOIN` table2 `ON` table1.column_name=table2.column_name;
### **FULL (OUTER) JOIN**: returns all records when there is a match in either left or right table
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT` column_names `FROM` table1 ``FULL OUTER JOIN`` table2 `ON` table1.column_name=table2.column_name;
### **Self JOIN**: a regular join, but the table is joined with itself
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT` column_names `FROM` table1 T1, table1 T2 `WHERE` condition;
<a name="view"></a>
2021-04-20 21:13:14 +00:00
## 5. View Queries
2020-11-26 17:38:25 +00:00
### **CREATE**: create a view
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `CREATE VIEW` view_name `AS SELECT` column1, column2 `FROM` table_name `WHERE` condition;
### **SELECT**: retrieve a view
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `SELECT` * `FROM` view_name;
### **DROP**: drop a view
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `DROP VIEW` view_name;
<a name="alter"></a>
2021-04-20 21:13:14 +00:00
## 6. Altering Table Queries
2020-11-26 17:38:25 +00:00
### **ADD**: add a column
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `ALTER TABLE` table_name `ADD` column_name column_definition;
### **MODIFY**: change data type of column
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `ALTER TABLE` table_name `MODIFY` column_name column_type;
### **DROP**: delete a column
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `ALTER TABLE` table_name `DROP COLUMN` column_name;
<a name="create"></a>
2021-04-20 21:13:14 +00:00
## 7. Creating Table Query
2020-11-26 17:38:25 +00:00
### **CREATE**: create a table
2021-04-20 21:13:14 +00:00
2020-11-26 17:38:25 +00:00
* `CREATE TABLE` table_name `(` <br />
`column1` `datatype`, <br />
`column2` `datatype`, <br />
`column3` `datatype`, <br />
`column4` `datatype`, <br />
`);`
2021-04-20 21:13:14 +00:00
:q
:q