- DDL (Data Definition Language)
- DML (Data Manipulation Language)
- DCL (Data Control Language)
- TCL (Transaction Control Language)
- https://github.com/enochtangg/quick-SQL-cheatsheet
- http://www.dusun-think.net/dosya/czyayin/mysqlcheatsheet.pdf
- https://www.cheatography.com/guslong/cheat-sheets/essential-mysql/
- https://data36.com/wp-content/uploads/2018/12/sql-cheat-sheet-for-data-scientists-by-tomi-mester.pdf
- https://www.kdnuggets.com/2018/07/sql-cheat-sheet.html
- https://devhints.io/mysql
- http://www.sqltutorial.org/sql-cheat-sheet/
Definition: Deals with descriptions of the database schema and is used to create and modify the structure of database objects
Examples of DDL commands:
CREATE– is used to create the database or its objects (like table, index, function, views, store procedure and triggers).DROP– is used to delete objects from the database.ALTER- is used to alter the structure of the database.TRUNCATE– is used to remove all records from a table, including all spaces allocated for the records are removed.COMMENT– is used to add comments to the data dictionary.RENAME– is used to rename an object existing in the database.
CREATE DATABASEdatabase_nameCREATE TABLE(col def,…,PRIMARY KEY(col),FOREIGN KEY (col) REFERENCES table(col2))CREATE PROCEDUREprocedure_nameASsql_statementGO;CREATE INDEXidx_cityONtable_customers(field_city);DROP INDEXidx_cityONtable_customers;CREATE VIEWview_nameAS SELECTcolumn1, column2FROMtable_nameWHEREcondition;CREATE TRIGGER[TRIGGER_NAME] [before | after] {insert | update | delete}on[table_name] [FOR/AFTER/INSTEAD OF] [INSERT/UPDATE/DELETE]AS[trigger_body]
ALTER TABLEtable_nameADDcolumn_name column_definition;
ALTER TABLEtable_nameMODIFYcolumn_name column_type;
ALTER TABLEtable_nameDROP COLUMNcolumn_name;
DROPdatabase_nameDROP VIEWview_name;
TRUNCATE TABLEtable_name;
RENAME TABLEtable_name TO new_table_name;RENAME DATABASEdatabase_name TO new_database_name;
EXEC procedure_name;
Definition: Deals with the manipulation of data present in database
Examples of DML:
SELECT– is used to retrieve data from the a database.INSERT– is used to insert data into a table.UPDATE– is used to update existing data within a table.DELETE– is used to delete records from a database table.
SELECT*FROMtable_name;SELECT*FROMview_name;
DISTINCT: returns distinct values only (filters away duplicate values and returns rows of specified column)
SELECT DISTINCTcolumn_name;
SELECTcolumn1, column2FROMtable_nameWHEREcondition;SELECT*FROMtable_nameWHEREcondition1ANDcondition2;SELECT*FROMtable_nameWHEREcondition1ORcondition2;SELECT*FROMtable_nameWHERE NOTcondition;SELECT*FROMtable_nameWHEREcondition1AND(condition2ORcondition3);SELECT*FROMtable_nameWHERE EXISTS(SELECTcolumn_nameFROMtable_nameWHEREcondition);
WHERE exp AND|OR exp AND|OR exp…
where exp can be one of the following:
- column
=value - column
>value - column
>=value - column
<value - column
<=value - column
BETWEENvalue1ANDvalue2 - column
IN(value1,value2,…) - column
NOT IN(value1,value2,…) - column
LIKEvalue - column
NOT LIKEvalue
SELECT*FROMtable_nameORDER BYcolumn;SELECT*FROMtable_nameORDER BYcolumnDESC;SELECT*FROMtable_nameORDER BYcolumn1ASC, column2DESC;
SELECT TOPnumber columns_namesFROMtable_nameWHEREcondition;SELECT TOPpercent columns_namesFROMtable_nameWHEREcondition;- Not all database systems support
SELECT TOP. The MySQL equivalent is theLIMITclause SELECTcolumn_namesFROMtable_nameLIMIToffset, count;
- % (percent sign) is a wildcard character that represents zero or more characters
- _ (underscore) is a wildcard character that represents a single character (_ is exactly one character in the LIKE statement)
SELECTcolumn_namesFROMtable_nameWHEREcolumn_nameLIKEpattern;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”LIKE'%[^0-9]%' (match all strings that don't have a digit)
- essentially the IN operator is shorthand for multiple OR conditions
SELECTcolumn_namesFROMtable_nameWHEREcolumn_nameIN(value1, value2, …);SELECTcolumn_namesFROMtable_nameWHEREcolumn_nameIN(SELECT STATEMENT);
SELECTcolumn_namesFROMtable_nameWHEREcolumn_nameBETWEENvalue1ANDvalue2;SELECT*FROMProductsWHERE(column_nameBETWEENvalue1ANDvalue2)AND NOTcolumn_name2IN(value3, value4);SELECT*FROMProductsWHEREcolumn_nameBETWEEN#01/07/1999# AND #03/12/1999#;
SELECT*FROMtable_nameWHEREcolumn_nameIS NULL;SELECT*FROMtable_nameWHEREcolumn_nameIS NOT NULL;
SELECTcolumn_nameASalias_nameFROMtable_name;SELECTcolumn_nameFROMtable_nameASalias_name;SELECTcolumn_nameASalias_name1, column_name2ASalias_name2;SELECTcolumn_name1, column_name2 + ‘, ‘ + column_name3ASalias_name;
- 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
SELECTcolumns_namesFROMtable1UNION SELECTcolumn_nameFROMtable2;UNIONoperator only selects distinct values,UNION ALLwill allow duplicates
SELECTcolumn_namesFROMtable1INNER JOINtable2ONtable1.column_name=table2.column_name;SELECTtable1.column_name1, table2.column_name2, table3.column_name3FROM((table1INNER JOINtable2ONrelationship)INNER JOINtable3ONrelationship);
- LEFT (OUTER) JOIN: returns all records from the left table (table1), and the matched records from the right table (table2)
SELECTcolumn_namesFROMtable1LEFT JOINtable2ONtable1.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)
SELECTcolumn_namesFROMtable1RIGHT JOINtable2ONtable1.column_name=table2.column_name;
SELECTcolumn_namesFROMtable1FULL OUTER JOINtable2ONtable1.column_name=table2.column_name;
SELECTcolumn_namesFROMtable1 T1, table1 T2WHEREcondition;
- Each row from 1st table joins with all the rows of 2nd table.
Example:
| table_a | table_b |
|---|---|
| 1 | a |
| 2 | b |
| 3 | c |
SELECT a.num, b.word
FROM table_a a
CROSS JOIN table_b b;
Result:
| num | word |
|---|---|
| 1 | a |
| 2 | a |
| 3 | a |
| 1 | b |
| 2 | b |
| 3 | b |
| 1 | c |
| 2 | c |
| 3 | c |
INTERSECT: set operator which is used to return the records that two SELECT statements have in common
- Generally used the same way as UNION above
SELECTcolumns_namesFROMtable1INTERSECT SELECTcolumn_nameFROMtable2;
EXCEPT: set operator used to return all the records in the first SELECT statement that are not found in the second SELECT statement
- Generally used the same way as UNION above
SELECTcolumns_namesFROMtable1EXCEPT SELECTcolumn_nameFROMtable2;
- The
ANYoperator returns true if any subquery values meet the condition - The
ALLoperator returns true if all subquery values meet the condition SELECTcolumns_namesFROMtable1WHEREcolumn_name operator (ANY|ALL) (SELECTcolumn_nameFROMtable_nameWHEREcondition);
GROUP BY: statement often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns
SELECTcolumn_name1, COUNT(column_name2)FROMtable_nameWHEREconditionGROUP BYcolumn_name1ORDER BYCOUNT(column_name2) DESC;
HAVING: this clause was added to SQL because the WHERE keyword could not be used with aggregate functions
SELECTCOUNT(column_name1), column_name2FROMtableGROUP BYcolumn_name2HAVINGCOUNT(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"
WITH RECURSIVEcteAS(
SELECTc0.*FROMcategoriesASc0WHEREid = 1# Starting point
UNION ALL
SELECTc1.*FROMcategoriesASc1JOINcteONc1.parent_category_id = cte.id
)
SELECT*
FROMcte
SELECT COUNT (DISTINCTcolumn_name);
SELECT MIN (column_names) FROMtable_nameWHEREcondition;SELECT MAX (column_names) FROMtable_nameWHEREcondition;
SELECT AVG (column_name) FROMtable_nameWHEREcondition;
SELECT SUM (column_name) FROMtable_nameWHEREcondition;
Used to insert new records/rows in a table
INSERT INTOtable_name (column1, column2)VALUES(value1, value2);INSERT INTOtable_nameVALUES(value1, value2 …);
Used to modify the existing records in a table
UPDATEtable_nameSETcolumn1 = value1, column2 = value2WHEREcondition;UPDATEtable_nameSETcolumn_name = value;
Used to delete existing records/rows in a table
DELETE FROMtable_nameWHEREcondition;DELETE*FROMtable_name;
Definition: Deals with the rights, permissions and other controls of the database system
Examples of DCL commands:
GRANT-gives user’s access privileges to database.REVOKE-withdraw user’s access privileges given by using the GRANT command.
GRANTprivilege_nameONobject_name {Database_name|Table_name|View_name|Dashboard_name}TO{user_name |PUBLIC |role_name}- [WITH GRANT OPTION];
privilege_name[ALL|EXECUTE|SELECT|INSERT|UPDATE|DELETE|CREATE|ALTER|DROP]
REVOKEprivilege_nameONobject_name {Database_name|Table_name|View_name|Dashboard_name}FROM{user_name |PUBLIC |role_name}
Example: Revoke ALL ANSI-92 permissions (ie: SELECT, INSERT, UPDATE, DELETE, and REFERENCES) on a table for a user named anderson
REVOKE ALL ONemployeesFROManderson;
ACCESSALLCREATE TABLECREATE VIEWCREATE DASHBOARDDROPDROP VIEWDELETE DASHBOARDSELECT,INSERT,TRUNCATE,UPDATE,DELETESELECT VIEWEDIT DASHBOARDVIEW DASHBOARDVIEW SQL EDITOR
SELECT,INSERT,TRUNCATE,UPDATE,DELETEDROPTRIGGERREFERENCESEXECUTE
SELECTDROP
VIEWEDITDELETE
Definition: Deals with the transaction within the database
Examples of TCL commands:
COMMIT– commits a Transaction.ROLLBACK– rollbacks a transaction in case of any error occurs.SAVEPOINT–sets a savepoint within a transaction.SET TRANSACTION–specify characteristics for the transaction.
SELECTcol1FROMtblORDER BYRAND()LIMIT10;//returns 10 rows random
LOAD DATA INFILE″filename″INTO TABLEtable
SHOW DATABASES|TABLESSHOW COLUMNS FROMtable