MySQL Cheatsheet CodeWithHarry
MySQL Cheatsheet CodeWithHarry
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
Data Types
#Numeric
TINYINT
SMALLINT
MEDIUMINT
BIGINT
1/15
CodeWithHarry
#String/Text
BLOB or TEXT
NULL Values
Comments
/* This is a multi-line
comment in MySQL */
# It is a single-line commend
Addition
Select 5+8;
Subtraction
Select 15-5;
Multiplication
2/15
CodeWithHarry
Select 5*5;
Division
Select 24/4;
Accessing Database
These commands allow one to check all the databases and tables
Show command
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
3/15
CodeWithHarry
(<column_name> <data_type>,
<column_name> <data_type>,
<column_name> <data_type>);
Insert command
Values (<value1>,<value2>...);
This query will add NULL value in the col3 of the selected table
Values (val1,val2,NULL);
Inserting Dates
It will add the following data into the selected column of the table
Values ('2021-12-10');
Select Command
A select query is used to fetch the data from the database
It will retrieve all the data of the row that will satisfy the condition
4/15
CodeWithHarry
Where <condition_to_satisfy>;
It will retrieve data of selected columns that will satisfy the condition
Where <condition_to_satisfy>;
DISTINCT Keyword
It will retrieve only distinct data i.e. duplicate data rows will get eliminated
ALL Keyword
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
From <table_name>;
It will only retrieve data of those columns whose values will fall between value1 and value2 (both
inclusive)
From <table_name>
5/15
CodeWithHarry
Select <col1>,<col2>
From <table_name>
Select <col1>,<col2>
From <table_name>
Searching NULL
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
<col2> <data_type>,
<col3> <data_type>);
DEFAULT
6/15
CodeWithHarry
<col2> <data_type>,
<col3> <data_type>);
UNIQUE
UNIQUE constraint ensures that all values in the column are different
<col2> <data_type>,
<col3> <data_type>);
CHECK
CHECK constraint ensures that all values in a column satisfy certain conditions
<col2> <data_type>,
<col3> <data_type>);
Primary Key
<col2> <data_type>,
<col3> <data_type>);
Foreign Key
PersonID int,
7/15
CodeWithHarry
);
Desc <table_name>;
Modifying Data
Update Command
Update <table_name>
Where <condition>;
Deleting Data
Delete Command
It will delete the entire row that will satisfy the condition
Where <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
8/15
CodeWithHarry
It will return records in the descending order of the specified column name's data
It will return records in the ascending order of column1 and descending order of column2
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
Altering Table
These commands allow you to change the structure of the table
Add <new_column>;
Dropping Table
DROP command
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
Select Char(72,97,114,114,121);
Concat Function
Select Concat("Harry","Bhai");
10/15
CodeWithHarry
Lower/Lcase
Upper/Ucase
Select Upper("CodeWithHarry");
Substr
Select Substr(string,m,n);
Trim
Instr
It searches for given second string into the given first string
Select Instr(String1,String2);
Length
Select Length(String)
Numeric Functions
Numeric function works on numerical data and returns a single output
11/15
CodeWithHarry
MOD
Select MOD(11,4);
Power
Select Power(m,n);
Round
Select Round(15.193,1);
Sqrt
Select Sqrt(144);
Truncate
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
Select Curdate();
12/15
CodeWithHarry
Date Function
Month Function
Select Month(date);
Day Function
Select Day(date);
Year Function
Select Year(date);
Now Function
Select now();
Sysdate Function
Select sysdate();
Aggregate Functions
Aggregate functions or multiple row functions work on multiple data and returns a single result
13/15
CodeWithHarry
AVG Function
COUNT Function
MAX Function
MIN Function
SUM Function
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.
It returns all rows from the left-hand table specified in the ON condition and only those rows
from the other table where the join condition is fulfilled.
SELECT columns FROM table1 LEFT [OUTER] JOIN table2 ON table1.column = table
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
SELECT columns FROM table1 RIGHT [OUTER] JOIN table2 ON table1.column = tabl
FULL JOIN
SELF JOIN
15/15