MySQL Functions - Cheatsheet
MySQL Functions - Cheatsheet
Get unlimited access to the best of Medium for less than $1/week. Become a member
Source: cleanpng.com
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 1/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
There are a ton of functions provided by MySQL and most of them are used
extensively. I will be providing the most commonly used functions with a short
description. The intension of the article is to provide one spot for all MySQL
functions so that one can quickly go through it before your interview or an
examination. I’m assuming you already have basic knowledge of SQL. Without
wasting your time let me directly jump into the functions.
Before that, I would like you to know that I have used MySQL Workbench to execute
the queries and employee database. Let’s quickly have a look at the employee dataset
description.
1. EMPNO: Employee ID
3. JOB: Designation
4. MGR: Manager ID
Also, upcoming queries would be more clear if you know the values in the employee
table.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 2/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
LENGTH( )
CHAR_LENGTH, CHARACTER_LENGTH, and LENGTH, all three functions give the
same result which is the number of characters in the given string. All three take one
parameter which is a string value.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 3/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
CONCAT( )
The concatenation of string is a very commonly used technique in all programming
languages. SQL provides it too. CONCAT is used to join two or more values into a
single string value, it can join any type of data.
FORMAT( )
It formats the floating-point number to specified decimal places and returns the
value as a string.
Parameters
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 4/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
INSERT( )
Used to insert a string in place of another string starting from a specified position
until a certain number of characters.
In this example, we are replacing all JOB values to ‘company employee’ starting from
position 1 of JOB value taking all the characters of it(length).
Parameters
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 5/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
INSTR( )
Returns position of the 1st occurrence of the string in another string. Here, ‘g’ 1st
occurs at position 19 in the string ‘ Medium is best blogging platform’.
LOCATE( )
Locate is the improved version of INSTR which addresses the drawback of INSTR.
What if we want the position of the third occurrence of the string? LOCATE gives us
the flexibility to specify from what position to start the search from. Below, we start
searching from position 21 of the string ‘Medium is best blogging platform’ to get
the position of the third occurrence of ‘g’.
UCASE( ), LCASE( )
Very straight forward, UCASE to convert string to uppercase and LCASE to convert a
string into lowercase.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 6/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
LEFT( ), RIGHT( )
Left: Extract the specified number of characters from the beginning of the string.
Right: Extract the specified number of characters from the end of the string.
Below, we are extracting one character from the beginning and end of each string.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 7/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
REPLACE( )
Replaces all the occurrences of the specified string with another specified string.
We are replacing all the uppercase ‘A’ with lowercase ‘a’ in each job value. I have
replaced a single character with another single character but the same can be done
with string. Go ahead and experiment, change ‘man’ with ‘women’.
SUBSTR( )
To extract a substring from a string, we have to specify starting positions and the
number of characters needed from the start point. Here, we are extracting the first
three characters of each job value. That is, 3 characters starting from position 1 of
the string.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 8/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
Aggregate Functions
Aggregate functions provided by MySQL are max, min, avg, and count. I have
demonstrated each by finding maximum, minimum, mean/average, and the total
count of salaries.
FLOOR( ), CEIL( )
Irrespective of the decimal value, the floor returns the nearest integer less than or
equal to the float number, and ceil returns the nearest integer greater than or equal
to the float number.
Open in app
Search
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 9/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
Source: mathsisfun.com
POWER( )
Returns the value of the number raised to another specified number. In this case, it
returns the square of all the salaries.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 10/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
ROUND( )
Rounds the number to a specified number of decimal places. It takes two
parameters, the number to be rounded and the required decimal places.
Source: mathsisfun.com
TRUNCATE( )
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 11/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
Returns the value truncated to a specified number of decimal values. If the second
argument is 0 then the decimal point is removed, if positive then the specified
number of values in the decimal part is truncated, if negative then the specified
number of values in the integer part is truncated.
Source: slideshare.net
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 12/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
Round, rounds the value to the nearest integer while truncate just drops the extra value.
ADDDATE( )
Used to add a time/date interval to date and then return the date. The adding unit
can be of type day, month, year, quarter, etc. The list is as below.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 13/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
DATEDIFF( )
Suppose if we want to display the number of experiences in years an employee has
in the company, we need to subtract the current date with the date of hire. This is
where DATEDIFF() comes handy, it returns the number of days between two dates.
to get the difference in years we need to do some math explicitly: divide by 365, and
round the resultant value.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 14/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
The DAYOFMONTH returns the number of days since the beginning of the year
given a date value.
The DAYOFWEEK basically returns an integer representing the day of the week
starting from Sunday as 0 given the date value. Look at DAYNAME in the below
table, Wednesday is the 4th(DAYOFWEEK) day of the week, Friday is the
6th(DAYOFWEEK) day of the week, and so on.
The DAYOFYEAR returns an integer representing the day count since the beginning
of the year(January 1st). Below, 17th December 1980 is the 352nd day of the year
1980 from January 1.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 15/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
EXTRACT( )
Used to extract the specified part of the given date.
We can extract any of the given below part of the information from date.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 16/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
QUARTER( )
Returns the quarter of the year in which the given date falls in.
IF( )
Returns value if the given condition is true else another value.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 17/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
CASE( )
Suppose we would like to categorize employees based on their salary. Salary less
than 1000 as Underpaid, between 1000 and 3000 as Fairly paid, and more than 3000
as Overpaid. We have to use the nested if function as below.
This is fine if there are only a few conditions, what if we have several conditions?
then we need to use the CASE function as below.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 18/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
COALESCE( )
COALESCE takes a list of arguments and returns the first non-null value. In the
below example, if the value of comm is null then it returns zero.
DATABASE( )
It returns the name of the current database you are working in.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 19/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
SELECT DATABASE();
ISNULL( )
Returns 0 if the given value of a non-null else returns 1.
NULLIF( )
It takes two arguments and returns null if both the values are the same else the first
argument passed. Arguments can be of any type.
Below we are comparing if salary and commission of each employee are the same.
We can see no employee has the same salary and commission hence returns salary
since it is the first argument passed.
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 20/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 21/30
22/10/2023, 07:33 MySQL Functions: Cheatsheet with examples | by Sujan Shirol | Analytics Vidhya | Medium
Follow
https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074 22/30