MySQL Cheatsheet
MySQL Cheatsheet
MySQL Cheatsheet
Haris Ali Khan
Database
It is defined as a collection of interrelated data stored together to serve multiple
applications.
MySQL Elements
MySQL has certain elements that play an important role in querying a database.
Literals
Literals refer to a fixed data value
Data Types
Data types are means to identify the type of data.
#Numeric
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 1/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
MEDIUMINT
BIGINT
#String/Text
NULL Values
If a column has no value, then it is said to be NULL
Comments
A comment is a text that is not executed.
/* This is a multi-line
comment in MySQL */
# It is a single-line commend
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 2/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
You can perform simple calculations in MySQL, just by using the Select
command, there's no need to select any particular database to perform these
commands.
Addition
It will add two numbers
Select 5+8;
Subtraction
It will subtract the second number from first
Select 15-5;
Multiplication
It will give the product of supplied numbers
Select 5*5;
Division
It will divide the number
Select 24/4;
Accessing Database
These commands allow one to check all the databases and tables
Show command
It will show all the databases in the system
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 3/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
Show databases;
show tables;
Use command
It will start using the specified database i.e. now you can create tables in the
selected database
use database_name;
Creating tables
These commands allow you to create the table in MySQL
Insert command
It will add data into the selected table
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 4/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
Inserting Dates
It will add the following data into the selected column of the table
Select Command
A select query is used to fetch the data from the database
DISTINCT Keyword
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 5/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
It will retrieve only distinct data i.e. duplicate data rows will get eliminated
ALL Keyword
It will retrieve all the data of the selected column
Column Aliases
It is used to give a temporary name to a table or a column in a table for the
purpose of a particular query
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 6/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
Select <col1>,<col2>
From <table_name>
Where <column> LIKE 'Ha%';
Select <col1>,<col2>
From <table_name>
Where <column> LIKE 'Ha__y%';
Searching NULL
It returns data that contains a NULL value in them
SQL Constraints
SQL constraints are the rules or checks enforced on the data columns of a table
NOT NULL
It will create a table with NOT NULL constraint to its first column
DEFAULT
DEFAULT constraint provides a default value to a column
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 7/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
<col2> <data_type>,
<col3> <data_type>);
UNIQUE
UNIQUE constraint ensures that all values in the column are different
CHECK
CHECK constraint ensures that all values in a column satisfy certain conditions
Primary Key
Primary key is used to uniquely identify each row in a table
Foreign Key
Desc <table_name>;
Modifying Data
Update Command
It will update the values of selected columns
Update <table_name>
SET <col1> = <new_value>, <col2> = <new_value>
Where <condition>;
Deleting Data
Delete Command
It will delete the entire row that will satisfy the condition
Ordering Records
Order by clause is used to sort the data in ascending or descending order of
specified column
order by clause
It will return records in the ascending order of the specified column name's data
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 9/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
It will return records in the descending order of the specified column name's
data
Grouping Result
It is used to arrange identical data into groups so that aggregate functions can
work on them
Group by clause
It allows you to group two or more columns and then you can perform
aggregate function on them
Having clause
Having clause is used to put conditions on groups
Altering Table
These commands allow you to change the structure of the table
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 10/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
Dropping Table
DROP command
It will delete the complete table from the database
MySQL Functions:
There are many functions in MySQL that perform some task or operation and
return a single value
Text/String Functions
Text function work on strings
Char Function
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 11/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
Select Char(72,97,114,114,121);
Concat Function
It concatenates two strings
Select Concat("Harry","Bhai");
Lower/Lcase
It converts a string into lowercase
Upper/Ucase
It converts a string into uppercase
Select Upper("CodeWithHarry");
Substr
It extracts a substring from a given string
Select Substr(string,m,n);
Trim
It removes leading and trailing spaces from a given string
Instr
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 12/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
It searches for given second string into the given first string
Select Instr(String1,String2);
Length
It returns the length of given string in bytes
Select Length(String)
Numeric Functions
Numeric function works on numerical data and returns a single output
MOD
It returns modulus of two numbers
Select MOD(11,4);
Power
It returns the number m raised to the nth power
Select Power(m,n);
Round
It returns a number rounded off number
Select Round(15.193,1);
Sqrt
It returns the square root of a given number
Select Sqrt(144);
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 13/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
Truncate
It returns a number with some digits truncated
Select Truncate(15.75,1);
Date/Time Functions
These are used to fetch the current date and time and allow you to perform
several operations on them
Curdate Function
It returns the current date
Select Curdate();
Date Function
It extracts the date part of the expression
Month Function
It returns the month from the date passed
Select Month(date);
Day Function
It returns the day part of a date
Select Day(date);
Year Function
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 14/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
Select Year(date);
Now Function
It returns the current date and time
Select now();
Sysdate Function
It returns the time at which function executes
Select sysdate();
Aggregate Functions
Aggregate functions or multiple row functions work on multiple data and returns
a single result
AVG Function
It calculates the average of given data
COUNT Function
It counts the number of rows in a given column
MAX Function
It returns the maximum value from a given column
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 15/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
MIN Function
It returns the minimum value from a given column
SUM Function
It returns the sum of values in given column
MySQL Joins
Join clause is used to combine or merge rows from two or more tables based on
a related attribute
INNER JOIN
It returns all rows from multiple tables where the join condition is satisfied. It is
the most common type of join.
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 16/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
It returns all rows from the RIGHT-hand table specified in the ON condition and
only those rows from the other table where the join condition is satisfied
FULL JOIN
It combines the results of both left and right outer joins
SELF JOIN
In this join, table is joined with itself
Post Comment
Comments (34)
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 17/18
14/07/2024, 06:20 MySQL Cheatsheet | CodeWithHarry
saugade7681_gm 2024-07-05
ALL is good cheatsheet bhai but the long querys are not fully print when
we click on download the cheatsheet Ex:- JOIN QUERYS ...IN A LAST .
REPLY
waleedkhansafimd983 2024-06-26
REPLY
sdas696291_gm 2024-06-03
REPLY
audu7797 2024-05-10
REPLY
prernarana247_gm 2024-02-18
REPLY
https://www.codewithharry.com/blogpost/mysql-cheatsheet/ 18/18