0% found this document useful (0 votes)
33 views17 pages

SQL Cheat Sheet

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)
33 views17 pages

SQL Cheat Sheet

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/ 17

10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

SQL Cheat Sheet with Commands & Description


[Sep 2022]
By Richard Peterson Updated July 26, 2022

In this SQL Query cheat sheet you will


learn

Create Database
SQL Data Types Cheat Sheet
MySQL SELECT statement command
MySQL WHERE clause commands
MySQL Command INSERT INTO Table
MySQL DELETE command
MySQL Update Command
ORDER BY in MySQL: DESC & ASC
command
MySQL GROUP BY and HAVING Clause command
MySQL Wildcards commands
MYSQL Regular Expressions (REGEXP)
Regular expression Metacharacters
SQL Functions commands
MySQL Aggregate function commands
MySQL IS NULL & IS NOT NULL commands
MySQL AUTO_INCREMENT commands
MYSQL – ALTER, DROP, RENAME, MODIFY
MySQL LIMIT & OFFSET
MySQL SubQuery commands
MySQL JOINS commands
MySQL UNION commands
MySQL in Views commands
MySQL Index commands

Create Database and table commands

Command Description

https://www.guru99.com/sql-cheat-sheet.html#2 1/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

Command Description

CREATE DATABASE DATABASE; Create database

IF NOT EXISTS let you to instruct MySQL


CREATE DATABASE IF NOT EXISTS server to check the existence of a database
database1; with a similar name prior to creating
database.

CREATE DATABASE IF NOT EXISTS the Latin1 character set uses the
database1 CHARACTER SET latin1 latin1_swedish_ci collation which is the
COLLATE latin1_swedish_ci Swedish case insensitive order.

You can see list of existing databases by


SHOW DATABASES
running following SQL command.

CREATE TABLE [IF NOT EXISTS]


TableName (fieldname dataType
Create table syntax
[optional parameters]) ENGINE =
storage Engine;

SQL Data Types Cheat Sheet

Numeric Data types

Command Description

TINYINT( ) -128 to 127 normal 0 to 255 UNSIGNED.

-32768 to 32767 normal

SMALLINT( )
0 to 65535 UNSIGNED.

MEDIUMINT( -8388608 to 8388607 normal

) 0 to 16777215 UNSIGNED.

-2147483648 to 2147483647 normal

INT( )
0 to 4294967295 UNSIGNED.

https://www.guru99.com/sql-cheat-sheet.html#2 2/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

Command Description

-9223372036854775808 to 9223372036854775807 normal

BIGINT( )
0 to 18446744073709551615 UNSIGNED.

FLOAT A small approximate number with a floating decimal point.

DOUBLE( , ) A large number with a floating decimal point.

A DOUBLE stored as a string , allowing for a fixed decimal point.


DECIMAL( , )
Choice for storing currency values.

Text Data Types

Command Description

CHAR( ) A fixed section from 0 to 255 characters long.

VARCHAR( ) A variable section from 0 to 255 characters long.

TINYTEXT A string with a maximum length of 255 characters.

TEXT A string with a maximum length of 65535 characters.

BLOB A string with a maximum length of 65535 characters.

MEDIUMTEXT A string with a maximum length of 16777215 characters.

MEDIUMBLOB A string with a maximum length of 16777215 characters.

LONGTEXT A string with a maximum length of 4294967295 characters.

LONGBLOB A string with a maximum length of 4294967295 characters.

Date / Time data types

Command Description

DATE YYYY-MM-DD

https://www.guru99.com/sql-cheat-sheet.html#2 3/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

Command Description

DATETIME YYYY-MM-DD HH:MM:SS

TIMESTAMP YYYYMMDDHHMMSS

TIME HH:MM:SS

Other data types

Command Description

ENUM To store text value chosen from a list of predefined text values.

This is also used for storing text values chosen from a list of predefined
SET
text values. It can have multiple values.

BOOL Synonym for TINYINT(1), used to store Boolean values

BINARY Similar to CHAR, difference is texts are stored in binary format.

VARBINARY Similar to VARCHAR, difference is texts are stored in binary format.

MySQL SELECT statement command

Command Description

SELECT [DISTINCT|ALL ] { * | [fieldExpression [AS newName]}


SQL SELECT
FROM tableName [alias] [WHERE condition][GROUP BY
statement syntax
fieldName(s)] [HAVING condition] ORDER BY fieldName(s)

SELECT * FROM table1; select the table

we are only interested


in getting only the t1,
SELECT t1,t2,t3, t4 FROM table1;
t2, t3 and t4 fields
only.

https://www.guru99.com/sql-cheat-sheet.html#2 4/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

Command Description

SELECT Concat(t1, (, t3, )) , t4 FROM table2; Getting table2 listing

Alias field names


SELECT column_name|value|expression [AS] alias_name;
syntax

MySQL WHERE clause with AND, OR, IN, NOT IN commands

Command Description

SELECT * FROM tableName


WHERE clause Syntax
WHERE condition;

SELECT * FROM table1 WHERE t1 WHERE clause combined with – AND LOGICAL
= 2 AND t2 = 2008; Operator

SELECT * FROM table1 WHERE t1 WHERE clause combined with – OR LOGICAL


= 1 OR t1 = 2; Operator

SELECT * FROM table2 WHERE t1


WHERE clause combined with – IN Keyword
IN (1,2,3);

SELECT * FROM table2 WHERE t1


WHERE clause combined with – NOT IN Keyword
NOT IN (1,2,3);

SELECT * FROM table2 WHERE t3 WHERE clause combined with Equal(=) to


= Female; COMPARISON OPERATORS

SELECT * FROM table3 WHERE t3 WHERE clause combined with greater than(>) to
> 2000; COMPARISON OPERATORS

SELECT * FROM table1 WHERE WHERE clause combined with Not Equal to
t1<> 1; (<>)COMPARISON OPERATORS

MySQL Command INSERT INTO Table

https://www.guru99.com/sql-cheat-sheet.html#2 5/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

Command Description

INSERT INTO table_name(column_1,column_2,…) basic syntax of the SQL


VALUES (value_1,value_2,…); INSERT command

INSERT INTO table1 (t1,t2,t3,t4) VALUES (X1,X2,X3,X4); INSERT data into table

Inserting into a Table from


INSERT INTO table_1 SELECT * FROM table_2;
another Table

MySQL DELETE command

Command Description

DELETE FROM table_name [WHERE condition]; Delete a row in MySQL

Example :-

DELETE FROM table1 WHERE table1_id = 18;

(delete entry of 18 number id form table1.)

DELETE FROM table1 WHERE table1_id IN (20,21);

(delete entry of 20 and 21 number id form table1)

MySQL Update Command

Command Description

UPDATE table_name SET column_name = new_value [WHERE update command


condition]; syntax

Example :-

SELECT * FROM table1 WHERE t1 = 1;

(retrieve the record for t1 =1)

UPDATE table1 SET t4 = X1 WHERE t1 = 1;

(update the t4 value in table)

https://www.guru99.com/sql-cheat-sheet.html#2 6/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

ORDER BY in MySQL: DESC & ASC command

Command Description

Order by
SELECT statement… [WHERE condition | GROUP BY field_name(s)
clause basic
HAVING condition] ORDER BY field_name(s) [ASC | DESC];
syntax

SELECT {fieldName(s) | *} FROM tableName(s) [WHERE condition] DESC and ASC


ORDER BY fieldname(s) ASC /DESC [LIMIT N] syntax

Example :-

For DESC (descending)

SELECT * FROM table1 ORDER BY t3 DESC;

For ASC (ascending)

SELECT * FROM table1 ORDER BY t3 ASC;

MySQL GROUP BY and HAVING Clause command

Group by

Command Description

SELECT statements… GROUP BY column_name1[,column_name2,…] GROUP BY


[HAVING condition]; Syntax

Example for grouping a single column :-

SELECT t4 FROM table1 ;

SELECT t4 FROM table1 GROUP BY t4;( suppose we want to get the unique values for t4.)

Example for grouping a multiple columns :-

SELECT t1_id,t4 FROM table2 ;

SELECT t1_id,t4 FROM table2 GROUP BY t1_id,t4;(using group by method )

Grouping and aggregate functions

https://www.guru99.com/sql-cheat-sheet.html#2 7/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

Command Description

SELECT t2,COUNT(t1) FROM Suppose we want the total number of t2 column


table1 GROUP BY t2; values in our database.

HAVING clause

Command Description

SELECT * FROM table2 GROUP BY all the t4 for table2 t1 id x1. We would use the
t1_id,t4 HAVING t1_id = x1; following script to achieve our results.

MySQL Wildcards commands for Like, NOT Like, Escape, ( %


), ( _ )

% the percentage wildcards commmand in MySQL

Command Description

SELECT statements… WHERE fieldname LIKE basic syntax for % percentage


xxx%; wildcard

Example :- we would use the percentage wildcard to perform a pattern match on both
sides of the word “X1” as part t2 of table1

SELECT * FROM table1 WHERE t2 LIKE %X1%;

1.00 EXPLORE MORE

Learn Java
Programming with
Beginners Tutorial
08:32

Linux Tutorial for


00:00 35:04 Beginners: Introduction
to Linux Operating...
01:35

What is Integration
Testing Software Testing
https://www.guru99.com/sql-cheat-sheet.html#2 8/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

SELECT * FROM table1 WHERE t2 LIKE %X1;

(the percentage wildcard at the beginning of the search criteria only)

SELECT * FROM table1 WHERE t2 LIKE X1%;

(the percentage wildcard to the end of the specified pattern to be matched.)

_ underscore wildcard command

Command Description

SELECT * FROM table1 WHERE t3 LIKE all the table1 that were t3 in the year
x2_; “x2”

NOT Like wildcard command

Command Description

SELECT * FROM table1 WHERE t3 Suppose we want to get table1 that were not t3
NOT LIKE X2_; in the year X2_

Escape keyword wildcard command

Command Description

LIKE 67#%% ESCAPE #; we want to check for the string “67%”

MYSQL Regular Expressions (REGEXP)

Command Description

SELECT statements… WHERE fieldname REGEXP basic syntax of Regular


pattern; Expression

Example :- all the table1 t1 that have the word X1 in them. It does not matter whether
the “X1” is at the beginning, middle or end of the title.

SELECT * FROM table1 WHERE t1 REGEXP X1;

https://www.guru99.com/sql-cheat-sheet.html#2 9/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

Regular expression Metacharacters

Command Description

The asterisk (*) metacharacter is used to match zero (0) or more


*
instances of the strings preceding it

The plus (+) metacharacter is used to match one or more instances


+
of strings preceding it.

The question(?) metacharacter is used to match zero (0) or one


?
instances of the strings preceding it.

The dot (.) metacharacter is used to match any single character in


.
exception of a new line.

[abc] The charlist [abc] is used to match any of the enclosed characters.

The charlist [^abc] is used to match any characters excluding the


[^abc]
ones enclosed.

[A-Z] The [A-Z] is used to match any upper case letter

[a-z] The [a-z] is used to match any lower case letter

[0-9] The [0-9] is used to match any digit from 0 through to 9.

^ The caret (^) is used to start the match at beginning.

| The vertical bar (|) is used to isolate alternatives.

[[:<:]] The[[:<:]] matches the beginning of words.

[[:>:]] The [[:>:]] matches the end of words.

The [:class:] matches a character class i.e. [:alpha:] to match letters,


[:class:] [:space:] to match white space, [:punct:] is match punctuations and
[:upper:] for upper class letters.

SQL Functions commands


https://www.guru99.com/sql-cheat-sheet.html#2 10/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

String functions

Command Description

SELECT t1_id,t2,
the “UCASE” function to do that. It takes a string as a
UCASE(t2) FROM
parameter and converts all the letters to upper case.
table1;

Numeric functions

Command Description Example

DIV Integer division SELECT 23 DIV 6;

/ Division SELECT 23 / 6 ;

- Subtraction SELECT 23 – 6 ;

+ Addition SELECT 23 + 6 ;

SELECT 23 * 6 AS
* Multiplication
multiplication_result;

% or SELECT 23 % 6 ; or
Modulus
MOD SELECT 23 MOD 6;

this function removes decimals places from a


SELECT FLOOR(23 /
Floor number and rounds it to the nearest lowest
6) AS floor_result;
number.

this function rounds a number with decimal SELECT ROUND(23 /


Round
places to the nearest whole number. 6) AS round_result;

Stored functions

Command Description

https://www.guru99.com/sql-cheat-sheet.html#2 11/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

Command Description

CREATE FUNCTION
sf_name
([parameter(s)])

basic syntax for creating a stored function


RETURNS data type

DETERMINISTIC

STATEMENTS

CREATE FUNCTION Mandatory and tells MySQL server to create a function named
sf_name `sf_name’ with optional parameters defined in the
([parameter(s)]) parenthesis.

Mandatory and specifies the data type that the function


RETURNS data type
should return.

The function will return the same values if the same


DETERMINISTIC
arguments are supplied to it.

STATEMENTS The procedural code that the function executes.

MySQL Aggregate function commands

Command Description

SELECT COUNT(t1_id) FROM table1 WHERE t1_id = 2; COUNT Function

SELECT MIN(t3) FROM table2; MIN function

SELECT MAX(t3) FROM table2; MAX function

SELECT SUM(t4) FROM table3; SUM function

SELECT AVG(t4) FROM table3; AVG function

MySQL IS NULL & IS NOT NULL commands

https://www.guru99.com/sql-cheat-sheet.html#2 12/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

Command Description

SELECT COUNT(t3) FROM table1;

Null as a Value
( if t3 have null value present that not count)

CREATE TABLE table2(

t1_number int NOT NULL,

t2_names varchar(255) ,
NOT NULL Values
t3 varchar(6)

);

comlumn_name IS NULL
NULL Keywords Basic
comlumn_name NOT NULL syntax

SELECT * FROM table1 WHERE t2_number IS NULL; Example of IS NULL

SELECT * FROM table1 WHERE t2_number IS NOT


Example of IS NOT NULL
NULL;

MySQL AUTO_INCREMENT commands

Command Description

CREATE TABLE table1 (

t1_id int(11) AUTO_INCREMENT,

t2_name varchar(150) DEFAULT NULL,

Auto increment syntax


t3 varchar(500) DEFAULT NULL,

PRIMARY KEY (t1_id)

);

MYSQL – ALTER, DROP, RENAME, MODIFY

Command Description

ALTER TABLE table_name ADD COLUMN column_name


Alter- syntax
data_type;

https://www.guru99.com/sql-cheat-sheet.html#2 13/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

Command Description

DROP TABLE sample_table; DROP TABLE syntax

RENAME COMMAND
RENAME TABLE current_table_name TO new_table_name;
syntax

ALTER TABLE table1 CHANGE COLUMN t1_names t1name


CHANGE KEYWORD
char(250) NOT NULL;

ALTER TABLE table1MODIFY t1name char(50) NOT NULL; MODIFY KEYWORD

ALTER TABLE table1 ADD t4 date NULL AFTER t3; AFTER KEYWORD

MySQL LIMIT & OFFSET

Command Description

SELECT {fieldname(s) | *} FROM tableName(s) [WHERE


LIMIT keyword syntax
condition] LIMIT N;

OFF SET in the LIMIT


SELECT * FROM table1 LIMIT 1, 2;
query

MySQL SubQuery commands :

Command Description

SELECT t1_name FROM table1 WHERE category_id =( SELECT


sub queries
MIN(t1_id) from table2);

MySQL JOINS commands

Command Description

SELECT * FROM table1 CROSS JOIN table2 Cross JOIN

https://www.guru99.com/sql-cheat-sheet.html#2 14/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

Command Description

SELECT table1.t1 , table1.t2 , table2.t1

FROM table1 ,table2


INNER JOIN
WHERE table2.id = table1.table2_id

SELECT A.t1 , B.t2 , B.t3

FROM table2 AS A

LEFT JOIN
LEFT JOIN table1 AS B

ON B.table2_id = A.id

SELECT A.t1 , A.t2, B.t3

FROM table1 AS A

RIGHT JOIN
RIGHT JOIN table2 AS B

ON B.id = A.table2_id

SELECT A.t1 , B.t2 , B.t3

FROM table2 AS A

“ON” and “USING” clauses


LEFT JOIN table1 AS B

USING ( table2_id )

MySQL UNION commands

Command Description

SELECT column1, column2 FROM table1 UNION syntax

SELECT column1,column2 FROM table2; UNION DISTINCT

MySQL in Views commands

Command Description

CREATE VIEW view_name AS SELECT statement; Views syntax

DROP VIEW general_v_movie_rentals ; Dropping views

https://www.guru99.com/sql-cheat-sheet.html#2 15/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]

MySQL Index commands

Command Description

CREATE INDEX id_index ON table_name(column_name); Add index basic syntax

DROP INDEX index_id ON table_name; Drop index basic syntax

You Might Like:

MySQL Index Tutorial – Create, Add & Drop


MySQL Functions: String, Numeric, User-Defined, Stored
Database Design in DBMS Tutorial: Learn Data Modeling
SQL vs MySQL – Difference Between Them
MariaDB vs MySQL – Difference Between Them

Prev Report a Bug Next

About

About Us

Advertise with Us

Write For Us

Contact Us

Career Suggestion
SAP Career Suggestion Tool

Software Testing as a Career

Interesting

eBook

Blog
Quiz

https://www.guru99.com/sql-cheat-sheet.html#2 16/17
10/1/22, 3:33 PM SQL Cheat Sheet with Commands & Description [Sep 2022]
Q
SAP eBook

Execute online

Execute Java Online

Execute Javascript

Execute HTML

Execute Python

© Copyright - Guru99 2022         Privacy Policy  |  Affiliate


Disclaimer  |  ToS

https://www.guru99.com/sql-cheat-sheet.html#2 17/17

You might also like