Analytics Vidhya
Published in

Analytics Vidhya

MySQL Functions: Cheatsheet with examples

All commonly used MySQL functions in one place with examples and a short explanation.

MySQL functions
Source: cleanpng.com
MySQL functions
  1. EMPNO: Employee ID
  2. ENAME: Employee name
  3. JOB: Designation
  4. MGR: Manager ID
  5. HIREDATE: Date when the employee was hired
  6. SAL: Salary per month
  7. COMM: Commission earned
  8. DEPTNO: Department number the employee belongs to
SELECT * FROM emps;
MySQL functions

LENGTH( )

SELECT ename, CHAR_LENGTH(ename), CHARACTER_LENGTH(ename), LENGTH(ename) FROM emps;
MySQL functions

CONCAT( )

SELECT CONCAT(ename, ' works as ',job) FROM emps;
MySQL functions

FORMAT( )

Parameters

number: Required. The number to be formatted

decimal_places: Required. The number of decimal places for number. If this parameter is 0, this function returns a string with no decimal places

SELECT comm, FORMAT(comm, 1) FROM emps;
MySQL functions

INSERT( )

Parameters

string: Required. The string that will be modified

position: Required. The position where to insert string2

number: Required. The number of characters to replace

string2: Required. The string to insert into the string

SELECT ename, job, INSERT(JOB,1, length(job), 'company employee') FROM emps;
MySQL functions

INSTR( )

SELECT INSTR('Medium is best blogging platform', "g") AS MatchPosition;OUTPUT: 19

LOCATE( )

SELECT LOCATE("g", "Medium is best blogging platform", 21) AS MatchPosition;OUTPUT: 23

UCASE( ), LCASE( )

SELECT job, LCASE(job), UCASE(job) FROM emps;
MySQL functions

LEFT( ), RIGHT( )

SELECT job, LEFT(job, 1), RIGHT(job, 1) FROM emps;
MySQL functions

REPLACE( )

SELECT job, REPLACE(job, 'A', 'a') from emps;
MySQL functions

SUBSTR( )

SELECT job, SUBSTR(job, 1, 3) AS Abbrevation FROM emps;
MySQL functions

Aggregate Functions

SELECT MAX(sal), MIN(sal), AVG(sal), COUNT(sal), SUM(SAL) FROM emps;
MySQL functions

FLOOR( ), CEIL( )

MySQL functions
Source: mathsisfun.com
SELECT comm, FLOOR(comm), CEIL(COMM) FROM emps;
MySQL functions

POWER( )

SELECT ename, sal, pow(sal,2) FROM emps;
MySQL functions

ROUND( )

MySQL functions
Source: mathsisfun.com
SELECT comm, ROUND(comm,1), ROUND(comm,2), ROUND(comm,3) FROM emps;
MySQL functions

TRUNCATE( )

MySQL functions
Source: slideshare.net
SELECT comm, TRUNCATE(comm,1), TRUNCATE(comm,-1) FROM emps;
MySQL functions

ADDDATE( )

MySQL functions
SELECT hiredate, ADDDATE(hiredate, INTERVAL 10 DAY), ADDDATE(hiredate, INTERVAL 2 MONTH), ADDDATE(hiredate, INTERVAL 2 YEAR) FROM emps;
MySQL functions

CURDATE( ), CURTIME( ), CURRENT_TIMESTAMP( )

SELECT curdate(), CURTIME(), current_timestamp();
MySQL functions

DATEDIFF( )

SELECT ename, hiredate, DATEDIFF(CURDATE(), hiredate) as 'experience in days' FROM emps;
MySQL functions
SELECT ename, hiredate, ROUND(DATEDIFF(CURDATE(), hiredate)/365) as 'experience in years' FROM emps;
MySQL functions

DAYNAME( ), DAYOFMONTH( ), DAYOFWEEK( ), DAYOFYEAR( )

SELECT DAYNAME(hiredate), DAYOFMONTH(hiredate), DAYOFWEEK(hiredate), DAYOFYEAR(hiredate) FROM emps;
MySQL functions

EXTRACT( )

SELECT EXTRACT(MONTH FROM hiredate), EXTRACT(YEAR FROM hiredate), EXTRACT(DAY FROM hiredate) FROM emps;
MySQL functions
MySQL functions

QUARTER( )

  • January-March falls in the 1st quarter.
  • April-June falls in the 2nd quarter.
  • July-September falls in the 3rd quarter.
  • October-December falls in the 4th quarter.
SELECT hiredate, QUARTER(hiredate) FROM emps;
MySQL functions

IF( )

IF(condition, value_if_true, value_if_false)

SELECT IF(SAL<1000, "YES", "NO") FROM EMPS;
MySQL functions

CASE( )

SELECT ename, sal, IF(sal<1000, “Underpaid”, IF(sal>1000 AND sal>3000,’Fairly paid’,’Overpaid’)) FROM EMPS;
SELECT ename, sal,
CASE
WHEN sal<1000 THEN 'Underpaid'
WHEN sal>1000 AND sal<3000 THEN 'Fairly paid'
ELSE 'Overpaid'
END AS 'salary status'
FROM emps;
MySQL functions

COALESCE( )

SELECT ename, comm, COALESCE(comm, 0) FROM emps;
MySQL functions

DATABASE( )

SELECT DATABASE();

ISNULL( )

SELECT comm, ISNULL(comm) FROM emps;
MySQL functions

NULLIF( )

SELECT NULLIF(25, 25);
MySQL functions
SELECT sal, comm, NULLIF(sal, comm) FROM emps;
MySQL functions
Photo by Pete Pedroza on Unsplash

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store