SQL Cheat Sheet
This is our SQL cheat sheet. It is a too long/detailed to be a cheat sheet but too short to be a tutorial. We focus on MySQL but mention other DBs. We include more than what I've seen in other SQL cheat sheets.
Purpose - This is meant to be used as a reference. It is not meant to be a complete guide. It should help if you just want to remember the syntax of a statement or if you know enough to kind of figure out how it works based on an example.
|
Operators |
|
Common MySQL types |
| Comments |
Queries / Select
| Entire table |
| Only two columns |
| Anything matching search term |
| Only distinct records |
| Count of distinct records for one col |
| Order Ascending |
| Order Descending |
| Both |
Insert / Update / Delete
| Insert |
| Update |
| Update - NO!!!! (sets for all rows) |
| Delete |
| Delete - NO!!!! (deletes everything) |
| Delete - NO!!!! (deletes everything) |
Funtions and Limits
| smallest value in col |
| largest value in col |
| number in col |
| average value |
| sum of values |
| first 5 recors ( MSSQL ) |
| first 5 recors ( MySQL ) |
| first 5 recors ( Oracle ) |
Pattern Matching
| Match a pattern |
| zero, one, or multiple |
| single char, mysql, mssql/access use ? instead |
| Query Set Membership |
| Query Set Membership |
| Query Set Membership |
| Query by Ranges |
| Query by Ranges |
| Query by Ranges |
| Query by Ranges |
| AND |
| Multi Table Query |
| Multi Table Query |
| Check for NULL |
| Check for NOT NULL |
Joins and Unions
Join Types:
Join Types:
| matched from both tables |
| all from left, matched from right |
| matched from the left, all from the right |
| matched in either table |
| Inner Join |
| Inner Join |
| LEFT Join |
| RIGHT Join |
| FULL OUTER Join |
| Self Join ( all customers from same city ) |
| UNION ( same cols, types, order ) |
| UNION include dups ( same cols, types, order ) |
Misc
| GROUP BY |
| GROUP BY |
| Select records referenced in other table |
| Select matching records referenced in other table |
| Copy from one table to another (good for backups) |
| Copy from one table to another (good for backups) |
| Copy: using INSERT |
| Copy: using INSERT |
| NULL Functions MySQL only ( Oracle, etc. are different ) |
| NULL Functions MySQL only ( Oracle, etc. are different ) |
Create / Delete Database
| Create DB |
| Delete DB |
Create Table
|
Create Table |
|
Create Table from Existing Table (cols and values) |
|
Create Table with non-null unique ID |
|
Create Table - non-null ID and Last Name. This uses a multi-column constraint. ( all DBs ) |
|
Create Table - non-null Primary Key |
|
Create Table - non-null, Named, Multi Col, Primary Key |
|
Create Table - Primary Key and Foreign Key (MySQL only) |
|
Create Table - Primary Key and Foreign Key (all DBs) |
|
Create Table - With a Constraint (MySQL only) |
|
Create Table - With Named Constraint (all DBs) |
|
Create Table - With a Default Value |
|
Create Table - Date for Default Value |
|
Create Table - Auto Increment Primary Key |
Table Removal
|
Drop Table |
|
Truncate Table |
Alter Table
|
ALTER Table - Change Columns |
|
ALTER Table - add/remove multi column constraint |
|
ALTER Table - non-null, Named, Multi Col, Primary Key |
|
Drop Primary Key |
|
ALTER Table - add / remove foreign keys |
|
ALTER Table - Checks and Constraints |
|
ALTER Table - Add / Remove Default Values |
|
Remove Index |
Create Index
|
Create Index |
|
Create Unique Index |
Dates - MySQL Only
DATE | YYYY-MM-DD |
DATETIME | YYYY-MM-DD HH:MI:SS |
TIMESTAMP | YYYY-MM-DD HH:MI:SS |
YEAR | YYYY or YY |
|
Query by date |
Views
|
Create a View |
|
Query a View |
|
Create or Replace a View |
|
Drop a View |