Skip to main content
Web Development4 min read

MySQL Tips & Tricks : Functions

By Bhavik Rathod

MySQL Tips & Tricks : Functions

In our previous article on MySQL, we covered Triggers. In this article we are covering another important feature of MySQL “Functions“

What is Functions?

A Function in MySql is a set of SQL Statements for Performing tasks. you can pass parameters into function and they return a value. Functions used for code reusability so if you have to write large SQL Statements to perform the same task again and again, in that case you can create a function for that repeating task. Instead of writing the same code again and again, you can simply call that function.

MySql Supports Built in Functions and User Defined Functions.

MySQL Built in Functions :

  1. String Functions : String Functions allow users to manipulate data strings or query information about a string returned by the SELECT query. For more information about string functions read 12.8 String Functions and Operators
  1. Numeric Functions :Numeric Functions are used primarily for numeric manipulations and/or mathematical calculations. For more information about numeric functions read 12.6 Numeric Functions and Operators
  1. Date and Time Functions : These functions perform date arithmetic.Functions that expect date values usually accept datetime values and ignore the time part. Functions that expect time values usually accept datetime values and ignore the date part. For more information about date functions read 12.7 Date and Time Functions

    You can Find lists of MySql Built in Functions from Article 12.1 Built-In Function and Operator Reference.

How to create a User defined Function?

Creating a Function in MySQL is Similar to Creating a function in Other Languages.

Syntax of Create Function in MySQL:

DELIMITER // 
CREATE FUNCTION function_name (
parameter_1 datatype,
parameter_2 datatype,
parameter_n datatype
)
RETURNS return_datatype  
BEGIN  
Declaration_section  
Executable_section  
END // 
DELIMITER;

function_name : name of the user defined function.

parameter: number of parameter you want to pass in the function. It can be one or more than one.

return_datatype: return value datatype of the function

declaration_section: in this section all variables are declared.executable_section: code for the function is written here.

How to Call Function?

SELECT function_name(parameter);

How to Drop Function?

Once a function is created, you need to remove it from the database. Once you have created your function in MySQL, you might find that you need to remove it from the database.

DROP FUNCTION [IF EXISTS] function_name;

Example

Step-1 Let’s create Table and Insert Data into Table.

mysql> CREATE TABLE Students (StudentId INT PRIMARY KEY, Name VARCHAR(50), BirthDate Date);
Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO Students (StudentId, Name, BirthDate ) VALUES ( 1, 'Rahul', '2007-03-18');
Query OK, 1 rows inserted (0.03 sec)

mysql> INSERT INTO Students (StudentId, Name, BirthDate ) VALUES ( 7, 'Tina', '2006-05-21');
Query OK, 1 rows inserted (0.03 sec)

mysql> INSERT INTO Students (StudentId, Name, BirthDate ) VALUES ( 18, 'Suresh', '2005-01-08');
Query OK, 1 rows inserted (0.03 sec)

`Note: We’ve used BirthDate in YYYY-MM-DD Format.`

Step-2 Let’s create a function in MySQL using phpMyAdmin:

Creating Function from Routines Section, In Our Case We don’t have any Routines Yet, So by Clicking on Add Routine we Can Create New Routine.

Step-3 Syntax of Creating CalcAge function in MySQL.

DELIMITER //

CREATE FUNCTION CalcAge ( age DATE )
RETURNS INT DETERMINISTIC
BEGIN

   DECLARE age INT;

   SELECT YEAR(CURRENT_DATE()) - YEAR(birth_date) INTO age;   

   RETURN age;

END; //

DELIMITER ;

By Clicking on Add Routine You Can Find this Screen where you can Add your function.

Step-4 The syntax to call a function in MySQL is:

mysql> SELECT StudentId, Name, BirthDate, CalcAge(BirthDate) AS age 
FROM Students
WHERE CalcAge(BirthDate) > 16;

The syntax to a drop a function in MySQL is:

DROP FUNCTION CalcAge;

Differences between MySQL Functions and Procedures:

  • Procedure and Function both store SQL Statements but Procedure doesn’t return any value and Function returns the value.
  • Procedure invoked with CALL Statement when Function invoked with SELECT Statement.
  • Procedure parameters can be defined as Input Only parameters, Output Only parameters and Input-Output parameters and Function have only Input parameters. Function and Procedure both have parameters but declaration syntax is different for both.

We hope this short brief will help you in understanding Functions and using it more effectively. You can contact us to Share your thoughts or reviews.

Blogs

Field notes on shipping software, design, and everything in between.

AI SDK Laravel - library (1)
Web DevelopmentJul 3, 2026

Laravel AI SDK : A Complete Overview of Building AI Features in PHP

Laravel AI is Laravel’s official, first-party package for building AI features into PHP applications. Instead of pulling in…

Playwright MCP
Web DevelopmentJun 2, 2026

How I Audited and Fixed My Next.js Mobile UI in Minutes Using Playwright MCP

Every developer knows the tedious dance of mobile UI auditing. You deploy your site, open Chrome DevTools, toggle…

agile team management thumbnail
StoriesJul 19, 2023

Agile Team Management Unleashed: Insights from Top 5 Management Books for Small IT Agencies

Welcome to our blog, where we embark on a journey to unlock the secrets of maintaining a small…

Ways to create Effective UI/UX Design Portfolio
UI/UXDec 15, 2022

Ways to create Effective UI/UX Design Portfolio

In an industry where education and experience are not necessarily requirements for a dream position, a UX design…

5 Prototyping Tools for UI/UX Designers
UI/UXSep 12, 2022

5 Prototyping Tools for UI/UX Designers

It may sound cliche, but it’s also true that prototyping plays an important role in communicating how the…

Dark UI Design : Best Practices
UI/UXAug 25, 2022

Dark UI Design : Best Practices

Dark theme was introduced with IOS 13 in September 2019 followed by Android 10(Q) in May 2020 after…

What is viewBinding in Android and How we can use it?
Android App DevelopmentAug 24, 2022

What is viewBinding in Android and How we can use it?

Till now when we want to connect an XML element to a java or kotlin file we always…

5 Figma Plugins that can be used to create Good User Interface
UI/UXAug 24, 2022

5 Figma Plugins that can be used to create Good User Interface

After the launch in 2016, Figma has grown to become one of the leading UI design tools on…

MySQL Tips & Tricks : Functions
Web DevelopmentAug 24, 2022

MySQL Tips & Tricks : Functions

In our previous article on MySQL, we covered Triggers. In this article we are covering another important feature…

Free consultation to scope your
creative projects

Book demo

FAQ

Ask AI to summarize QuriousClick
Have a project in mind?Tell us what you're planning and we'll help you shape the scope, timeline and next steps.
Book a discovery call
  • QuriousClick is a design and development studio. Since 2014 our in-house team has designed, built and supported websites, web and mobile apps, brands and AI products for 100+ customers worldwide.