0% found this document useful (0 votes)
24 views

Nimakarimian Sql-Cheatsheet - BW

This cheat sheet provides a summary of SQL commands and functions in 3 sentences or less: The cheat sheet covers common SQL statements like SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, ALTER TABLE, DROP TABLE, UNIQUE, NOT NULL, CHECK, DEFAULT, PRIMARY KEY and VIEW. It also includes clauses for SELECT like WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, TOP. Stored procedures and user defined functions are demonstrated with an example of creating and executing a stored procedure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Nimakarimian Sql-Cheatsheet - BW

This cheat sheet provides a summary of SQL commands and functions in 3 sentences or less: The cheat sheet covers common SQL statements like SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, ALTER TABLE, DROP TABLE, UNIQUE, NOT NULL, CHECK, DEFAULT, PRIMARY KEY and VIEW. It also includes clauses for SELECT like WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, TOP. Stored procedures and user defined functions are demonstrated with an example of creating and executing a stored procedure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL CheatSheet Cheat Sheet

by Nima (nimakarimian) via cheatography.com/113429/cs/33110/

SELECT SELECT TOP (cont) VIEW

Specific Select Col1,C​ol2​,...,Col n MYSQL SELECT column​_na​me(s) FROMCREATE


from tablena table_nameCREATE
WHERE VIEW
condition
view_name
LIMITASnumber
SELE
Columns me ORACLE SELECT column​_na​me(s) FROMVIEW
table_name ORDER BY column​_na​me(s) FE
Select Select Distinct * from tablename NLY; UPDATING CREATE OR REPLACE VIEW view_n
Distinct VIEW
Not all database systems support the SELE
you can show the results in your desired DROPPING DROP VIEW view_name;
CT TOP clause. MySQL supports the LIMI
name by using 'AS' clause like : VIEW
T clause to select a limited number of
select Count(*) as Countr​yCount from ( records, while Oracle uses FETCH FIRST
select Country from Customers) CKECK
n ROWS ONLY and ROWNUM.
MYSQL - CREATE TABLE Persons ( ID i
GROUP BY CREATE
BETWEEN
SELECT column_name(s) TABLE -
SELECT column_name(s)
FROM table_name Others - CREATE TABLE Persons ( ID i
FROM table_name
WHERE condition CREATE
WHERE column​_name BETWEEN
GROUP BY column​_na​me(s) TABLE-
value1 AND value2;
ORDER BY column​_na​me(s);
// you can also use NOT with
The GROUP BY statement groups rows that BETWEEN
have the same values into summary rows, SELECT * FROM Products
like "find the number of customers in each WHERE Produc​tName NOT BETWEEN
countr​y". value1 AND value2

The GROUP BY statement is often used LIKE


with aggregate functions (COUNT(), MAX(),
SELECT column1, column2, ...
MIN(), SUM(), AVG()) to group the result-
FROM table_name
set by one or more columns.
WHERE columnN LIKE pattern;

INSERT INTO WHERE Custom​erName LIKE


'a%==Finds any values that start
INSERT INTO table_name (column1,
with "​a"
column2, column3, ...)
VALUES (value1, value2, value3, The percent sign (%) represents zero, one,

...); or multiple characters


The underscore sign (_) represents one,
single character
SELECT TOP
You can also combine any number of
SQL SELECT TOP number​|pe​rcent column​_na​me(s) FROM table_name WHERE condit
conditions using AND or OR operators.
SERVER ion;

By Nima (nimakarimian) Published 18th July, 2022. Sponsored by CrosswordCheats.com


Last updated 18th July, 2022. Learn to solve cryptic crosswords!
Page 1 of 7. http://crosswordcheats.com

cheatography.com/nimakarimian/
www.nimakarimian.ir
SQL CheatSheet Cheat Sheet
by Nima (nimakarimian) via cheatography.com/113429/cs/33110/

CKECK (cont) UNIQUE (cont) Stored procedures (User defined functions)

Multiple CREATE TABLE Persons ( ID> int


​ ​ ​ AgeNOT
int NULL, LastName varcha​r(255)
CREATENOT
PROCEDURE
NULL, procedure_name
FirstName varcha​r(2
Columns ); ); AS
and -- MYSQL -- sql_st​atement
Naming CREATE TABLE Persons ( GO;
CHECK ALTER TABLE Persons ADD CHECK​ ​ ​ (Age>=18);
ID int NOT NULL, ------​---​---​---​-------
-ALTER​‐ ​ ​ ​ ​Las​tName varcha​r(255) NOT NULL, EXEC proced​ure​_name;
TABLE- ​ ​ ​ ​Fir​stName varcha​r(255),
------​---​---​---​-------
​ ​ ​ Age int,
Multiple ALTER TABLE Persons ADD CONSTRAINT CHK_Pe​rsonAge CHECK (Age>=18 AND EXAMPLE ====>
City='​San​dnes');
​ ​ ​ ​UNIQUE (ID)
Columns CREATE PROCEDURE Select​All​‐
);
and Cus​tomers @City nvarch​ar(30),
-- Define multiple Unique keys --
naming - @Posta​lCode nvarch​ar(10)
CREATE TABLE Persons (
ALTER​‐ AS
​ ​ ​ ID int NOT NULL,
TABLE- SELECT * FROM Customers WHERE
​ ​ ​ ​Las​tName varcha​r(255) NOT NULL,
DROP ALTER TABLE Persons DROP CONSTRAINT CHK_Pe​rso​nAge; City = @City AND PostalCode =
​ ​ ​ ​Fir​stName varcha​r(255),
CHECK @Posta​lCode
​ ​ ​ Age int,
- GO;
​ ​ ​ ​CON​STRAINT UC_Person UNIQUE
MYSQL-
(ID,La​stName) ------​---​---​---​-------
DROP ); CHK_Pe​rso​nAge;
ALTER TABLE Persons DROP CHECK EXEC Select​All​Cus​tomers @City
CHECK = 'London', @Posta​lCode = 'WA1
The UNIQUE constraint ensures that all
-Others- 1DP';
values in a column are different.
END EXAMPLE <====
UNIQUE
Both the UNIQUE and PRIMARY KEY
-- SQL SERVER -- constr​aints provide a guarantee for ISNULL
CREATE TABLE Persons ( uniqueness for a column or set of columns. MYSQL SELECT Produc​tName, UnitPrice
​ ​ ​ ID int NOT NULL UNIQUE, ucts;
​ ​ ​ ​Las​tName varcha​r(255) A PRIMARY KEY constraint automa​tically
SQL SELECT Produc​tName, UnitPrice
NOT NULL, has a UNIQUE constr​aint.
SERVER ucts;
​ ​ ​ ​Fir​stName varcha​‐
However, you can have many UNIQUE ORACLE SELECT Produc​tName, UnitPrice
r(255),
constr​aints per table, but only one ;
PRIMARY KEY constraint per table.

By Nima (nimakarimian) Published 18th July, 2022. Sponsored by CrosswordCheats.com


Last updated 18th July, 2022. Learn to solve cryptic crosswords!
Page 2 of 7. http://crosswordcheats.com

cheatography.com/nimakarimian/
www.nimakarimian.ir
SQL CheatSheet Cheat Sheet
by Nima (nimakarimian) via cheatography.com/113429/cs/33110/

ISNULL (cont) UNION WILDCARDS (cont)

Altern​ [] Represents
SELECT Produc​tName, UnitPrice * (Units​InStock + COALES​CE(​Uni​tsO​nOrder, 0))any singleProdh[oa]t finds
FROM
ative ucts; SELECT column​_na​me(s) FROM character within the hot and hat,
table1 brackets but not hit
COALESCE() Function Works with MYSQL
SQLSERVER and ORACLE UNION ^ Represents any h[^oa]t finds
SELECT column​_na​me(s) FROM character not in the hit, but not
ANY & ALL table2; brackets hot and hat
The UNION operator selects only - Represents any single c[a-b]t finds
ANY SELECT column​_na​me(s) FROM table_name WHERE column​_name operator ANY (SELECT column​_name FROM table
distinct values by default. character within the cat and cbt
on);
To allow duplicate values, use specified range
ALL SELECT column​_na​me(s) FROM table_name WHERE column​_name operator ALL (SELECT column​_name FROM table
UNION ALL:
on); MIN() & MAX()
SELECT column​_na​me(s) FROM
SOME SELECT * FROM Products WHERE Price > SOME (SELECT Price FROM Products WHERE Price > 20);
table1 MIN() SELECT MIN(co​lum​n_name) FROM ta
returns a boolean value as a result UNION n;
The operator must be a standard SELECT column​_na​me(s) FROM
MAX() SELECT MAX(co​lum​n_name) FROM ta
comparison operator (=, <>, !=, >, >=, <, or table2;
n;
<=).
The UNION operator is used to combine the
**you can use MIN() and MAX() anywhere
result-set of two or more SELECT statem​‐
WHERE keyword in any Query you like
ents.
the examples above are just to show
SELECT Col1,Col2 FROM tablename
implem​ent​ation**
WHERE condition Every SELECT statement within UNION
must have the same number of columns
WHERE is for when you want to declare a DELETE
The columns must also have similar data
specific condition for the query. in the other DELETE DELETE FROM table_​name;
types
hand, you want to filter the records EVERYTHING
The columns in every SELECT statement
TIP1 WHERE can be used in UPDATE,
must also be in the same order DELETE DELETE FROM table_name WHER
SELECT, DELETE, ETC...
WITH
Aliases CONDITION

Column SELECT column​_name AS alias_name FROM table_​name;


Aliases
Table SELECT column​_na​me(s) FROM table_name AS alias_​na
Aliases me;

WILDCARDS

% Represents zero bl% finds bl,


or more black, blue, and
characters blob
_ Represents a h_t finds hot, hat,
single character and hit

By Nima (nimakarimian) Published 18th July, 2022. Sponsored by CrosswordCheats.com


Last updated 18th July, 2022. Learn to solve cryptic crosswords!
Page 3 of 7. http://crosswordcheats.com

cheatography.com/nimakarimian/
www.nimakarimian.ir
SQL CheatSheet Cheat Sheet
by Nima (nimakarimian) via cheatography.com/113429/cs/33110/

NOTNULL DEFAULT Database (cont)

CREATE TABLE Persons ( DEFAULT - CREATE TABLE Orders ( BACKUP


ID int BACKUP
NOT NULL,
DATABASE
OrderN​umber
databa​sename
intT
​ ​ ​ ID int NOT NULL, CREATE Differ​‐ BACKUP DATABASE databa​sename T
​ ​ ​ ​Las​tName varcha​r(255) TABLE- ential L;
NOT NULL, ALTERTABLE BACKUP
ALTER TABLE Persons ALTER City SET DEFAULT 'Sandnes';
​ ​ ​ ​Fir​stName varcha​r(255) -MYSQL-
NOT NULL, ALTERTABLE ALTER TABLE Persons ADD ALTER table df_City DEFAULT 'Sandnes' FOR
CONSTRAINT
​ ​ ​ Age int -SQLSE​‐ ADD Column ALTER TABLE table_name ADD c
); RVER-
DROP ALTER TABLE table_name DROP
------​---​---​---​---​---​-- ALTERT​ABL​‐ ALTER TABLE Persons MODIFY City DEFAULT 'Sandnes';
Column
-​------- E-O​racle-
ALTER/​‐ ALTER TABLE table_name ALTER
ALTER TABLE Persons
DROPTA​‐ ALTER TABLE Persons ALTER City DROP DEFAULT;
MODIFY
MODIFY Age int NOT NULL; BLE​-MYSQL- COLUMN
To create a NOT NULL constraint on the "​‐ DROPTABLE SQLSERVER
ALTER TABLE Persons ALTER COLUMN City DROP DEFAULT;
Age​" column when the "​Per​son​s" table is -Others- ALTER/​‐ ALTER TABLE table_name MODIF
already created, use the above Code
MODIFY ;
CASE COLUMN
DATE
CASE MYSQL
DATE - format YYYY-MM-DD ​ ​ ​ WHEN condition1 THEN ALTER/​‐ ALTER TABLE table_name MODIF
DATETIME - format: YYYY-MM-DD result1 MODIFY
HH:MI:SS ​ ​ ​ WHEN condition2 THEN COLUMN
TIMESTAMP - format: YYYY-MM-DD result2 ORACLE
HH:MI:SS ​ ​ ​ WHEN conditionN THEN
FOR MYSQL=​==> resultN PRIMARY KEY
YEAR - format YYYY or YY ​ ​ ​ ELSE result --MYSQL--
<=== END; CREATE TABLE Persons (
FOR SQLSER​VER​===> ​ ​ ​ ID int NOT NULL,
TIMESTAMP - format: a unique Database ​ ​ ​ ​Las​tName varcha​r(255)
number NOT NULL,
CREATE CREATE DATABASE testDB;
<=== ​ ​ ​ ​Fir​stName varcha​‐
SHOW SHOW DATABASES
r(255),
SELECT INTO (copy) DROP DROP DATABASE databa​sename
​ ​ ​ Age int,
;
SELECT column1, column2, ​ ​ ​ ​PRIMARY KEY (ID)
column3, ...
INTO newtable [IN extern​aldb]
FROM oldtable
WHERE condition;

SELECT * INTO Custom​ers​Bac​kup


2017 IN 'Backu​p.mdb'
FROM Customers;

By Nima (nimakarimian) Published 18th July, 2022. Sponsored by CrosswordCheats.com


Last updated 18th July, 2022. Learn to solve cryptic crosswords!
Page 4 of 7. http://crosswordcheats.com

cheatography.com/nimakarimian/
www.nimakarimian.ir
SQL CheatSheet Cheat Sheet
by Nima (nimakarimian) via cheatography.com/113429/cs/33110/

PRIMARY KEY (cont) IN JOINS (cont)

> ); SELECT column_name(s) SELF SELECT column​_na​me(s) FROM table


--SQLS​ERVER ORACLE-- FROM table_name JOIN n;
CREATE TABLE Persons ( WHERE column​_name IN (value1,
​ ​ ​ ID int NOT NULL PRIMARY KEY, value2, ...); ORDER BY
​ ​ ​ ​Las​tName varcha​r(255) NOT NULL, OR SELECT column1, column2, ...
​ ​ ​ ​Fir​stName varcha​r(255), SELECT column​_na​me(s) FROM table_name
​ ​ ​ Age int
FROM table_name ORDER BY column1, column2, ...
);
WHERE column​_name IN (SELECT ASC|DESC;
--Defining multiple Columns as PK--
STATEM​ENT);
CREATE TABLE Persons (
The IN operator allows you to specify Constr​aints
​ ​ ​ ID int NOT NULL,
​ ​ ​ ​Las​tName varcha​r(255) NOT NULL, multiple values in a WHERE clause. CREATE TABLE table_name (
​ ​ ​ ​Fir​stName varcha​r(255), ​ ​ ​ ​column1 datatype
​ ​ ​ Age int, The IN operator is a shorthand for multiple constr​aint,
​ ​ ​ ​CON​STRAINT PK_Person PRIMARY OR condit​ions.
​ ​ ​ ​column2 datatype
KEY (ID,La​stName) constr​aint,
); UPDATE
​ ​ ​ ​column3 datatype
In the example above there is only ONE UPDATE UPDATE table_name SET column1 = value1, column2 = value2, ... WHE
constr​aint,
PRIMARY KEY (PK_Pe​rson). WITH ​ ​ ​ ....
However, the VALUE of the primary key is CONDITION );
made up of
Be careful when updating records. If you
TWO COLUMNS (ID + LastName). Constr​aints can be specified when the table
omit the WHERE clause, ALL records will
is created with the CREATE TABLE
be updated!
SUM() & AVG() & COUNT () statement, or after the table is created with
the ALTER TABLE statement.
AVG() SELECT AVG(co​lum​n_name) FROM table_name WHERE condition;
JOINS
SUM() SELECT SUM(co​lum​n_name) FROM table_name
INNER WHERE INNER
FROM table1 condition;
JOIN table2 NOT NULL
ON table1.co​lum​n_name = table2.co​lum​
COUNT() SELECT COUNT(​col​umn​_name)JOIN
FROM table_name WHERE conditi UNIQUE
on; LEFT FROM table1 LEFT JOIN table2 PRIMARY KEY
ON table1.Column = table2.Column
JOIN FOREIGN KEY
CHECK
RIGHT FROM table1 RIGHT JOIN table2 ON table1.co​lum​n_name = table2.co​lum​
DEFAULT
JOIN
CREATE INDEX
FULL FROM table1 FULL OUTER JOIN table2 ON table1.co​lum​n_name = table2.co
JOIN ion; PRIMARY KEY on ALTER TABLE

ALTERTABLE ALTER TABLE Persons ADD PRIM


SIMPLE
ALTERTABLE ALTER TABLE Persons ADD CONS
multiple or );
naming
DROP PK - ALTER TABLE Persons DROP PRI
MYSQL-
DROP PK - ALTER TABLE Persons DROP CON
Others-

By Nima (nimakarimian) Published 18th July, 2022. Sponsored by CrosswordCheats.com


Last updated 18th July, 2022. Learn to solve cryptic crosswords!
Page 5 of 7. http://crosswordcheats.com

cheatography.com/nimakarimian/
www.nimakarimian.ir
SQL CheatSheet Cheat Sheet
by Nima (nimakarimian) via cheatography.com/113429/cs/33110/

FOREIGN KEY FOREIGN KEY (cont) TABLE (cont)

MYSQL - CREATE TABLE Orders ( FOREIGN


OrderID int ALTER
NOT NULL,
TABLE Orders
OrderN​umber TRUNCATE
ADD FOREIGN
int KEY
NOT (PersonID)
NULL,
TRUNCATEPersonID
REFERENCES
TABLE table_​name
int,
Person​s(P
PRIM
CREATE KEY- ;
TABLE - ALTER
The TRUNCATE TABLE statement is used
Others - CREATE TABLE Orders ( TABLE -int NOT NULL PRIMARY KEY,
OrderID OrderN​umber
to delete the dataint NOT
inside NULL,
a table, PersonID i
but not the
CREATE Multiple ALTER TABLE Orders ADD CONSTRAINT FK_Per​son​Order
table itself. FOREIGN KEY (Per
TABLE - Rows or ID);
Multiple CREATE TABLE Orders ( FK with int NOT NULL,
OrderID OrderN​umber INSET INTO
int NOT NULL, PersonID int, PRIM
Rows or ); Name -
INSERT INTO table2
FK with ALTER
SELECT * FROM table1
Name TABLE-
WHERE condition;
DROP FK ALTER TABLE Orders DROP FOREIGN KEY FK_Per​son​Order;
-MYSQL- The INSERT INTO SELECT statement
copies data from one table and inserts it
DROP FK ALTER TABLE Orders DROP CONSTRAINT FK_Per​son​Order;
into another table.
-Others-

The INSERT INTO SELECT statement


TABLE
requires that the data types in source and
CREATE CREATE TABLE table_name ( column1
target datatype,
tables match. column2 datatype

DROP DROP TABLE table_​name;

CREATE INDEX
CREATE TABLE new_ta​ble​_name AS SELECT column1, column​2,...
from ....; CREATE CREATE INDEX index_name ON tabl
another
CREATE CREATE UNIQUE INDEX index_name
Table
UNIQUE
DROP - DROP INDEX table_​nam​e.i​nde​x
SQLSE​‐
RVER-
DROP - ALTER TABLE table_name DROP IND
MYSQL-
DROP - DROP INDEX index_​name;
ORACLE-

By Nima (nimakarimian) Published 18th July, 2022. Sponsored by CrosswordCheats.com


Last updated 18th July, 2022. Learn to solve cryptic crosswords!
Page 6 of 7. http://crosswordcheats.com

cheatography.com/nimakarimian/
www.nimakarimian.ir
SQL CheatSheet Cheat Sheet
by Nima (nimakarimian) via cheatography.com/113429/cs/33110/

AUTO INCREMENT EXISTS

CREATE CREATE TABLE Persons ( SELECT


Personid
column_name(s)
int NOT NULL AUTO_I​NCR​EMENT, LastName varcha​r(255) NOT NUL
TABLE - ; FROM table_name
MYSQL- WHERE EXISTS
ALTERTABLE (SELECT column​_name FROM
ALTER TABLE Persons AUTO_I​NCR​EME​NT=100;
-MYSQL- table_name WHERE condit​ion);
CREATE CREATE TABLE Persons ( ThePersonid int IDENTI​TY(1,1)
EXISTS operator PRIMARY KEY,
is used to test for the LastName varcha​r(255) NOT NU
TABLE - existence of any record in a subquery.
SQLSE​‐
RVER- The EXISTS operator returns TRUE if the
CREATE subquery
CREATE SEQUENCE seq_person returns1 one
MINVALUE or more
START WITHrecords.
1 INCREMENT BY 1 CACHE 10;
SEQUENCE -
ORACLE- HAVING

use sequence INSERT INTO Persons (Perso​nid​,Fi​rst​Nam​e,L​ast​Name)


SELECT column_name(s) VALUES (seq_p​ers​on.n​ex​tva​l,'​Lar​s',​'M
in oracle FROM table_name
WHERE condition
GROUP BY column​_na​me(s)
HAVING condition
ORDER BY column​_na​me(s);

The HAVING clause was added to SQL


because the WHERE keyword cannot be
used with aggregate functions.

By Nima (nimakarimian) Published 18th July, 2022. Sponsored by CrosswordCheats.com


Last updated 18th July, 2022. Learn to solve cryptic crosswords!
Page 7 of 7. http://crosswordcheats.com

cheatography.com/nimakarimian/
www.nimakarimian.ir

You might also like