What Is Functions in MySQL? Definition & Use

Formulas & functions · Updated November 23, 2026

In MySQL, a function is a stored program that accepts arguments, performs a calculation or operation, and returns a single value. Functions are built into the server (like NOW() or CONCAT()) or can be user-defined (UDFs) via CREATE FUNCTION. They are used inside SQL statements wherever an expression is allowed, such as in SELECT, WHERE, or ORDER BY clauses. Unlike stored procedures, functions must return a value and cannot be used to perform transactions or return result sets.

Built-in Functions vs. User-Defined Functions

MySQL ships with hundreds of built-in functions grouped by purpose: string functions, numeric functions, date/time functions, aggregate functions, and more. For example, UPPER('hello') returns 'HELLO', and ROUND(3.14159, 2) returns 3.14. These are deterministic or non-deterministic depending on the function; NOW() is non-deterministic because it changes each call.

User-defined functions (UDFs) are written in SQL or in C/C++ (for high-performance needs). SQL UDFs are created with CREATE FUNCTION and follow the same rules as built-ins. Example:

CREATE FUNCTION add_one(x INT) RETURNS INT
RETURN x + 1;

Then you can call SELECT add_one(5); which returns 6. UDFs cannot modify the database (no INSERT/UPDATE/DELETE) because they are read-only by design.

How Functions Work in SQL Statements

Functions are evaluated during query execution. When you write SELECT CONCAT(first_name, ' ', last_name) FROM users;, MySQL calls CONCAT for each row, passing the column values as arguments. The result is a single value per row. In WHERE clauses, functions filter rows: WHERE YEAR(order_date) = 2023 returns rows where the year part of the date is 2023. However, using functions on columns can prevent index usage, which is a common performance pitfall.

Functions can be nested: UPPER(CONCAT(first_name, ' ', last_name)). The inner function runs first, then the outer.

Types of Built-in Functions

MySQL groups built-in functions into these main categories:

  • String functions: CONCAT(), SUBSTRING(), LENGTH(), REPLACE().
  • Numeric functions: ABS(), CEIL(), FLOOR(), MOD().
  • Date and time functions: NOW(), CURDATE(), DATE_ADD(), DATEDIFF().
  • Aggregate functions: COUNT(), SUM(), AVG(), MAX(), MIN() – these operate on a set of rows and return a single summary value.
  • Control flow functions: IF(), CASE (though CASE is an expression, not a function).
  • JSON functions: JSON_EXTRACT(), JSON_ARRAY().

Each function has a specific syntax and return type. Always check the MySQL reference manual for exact behavior because some functions have subtle differences (e.g., LENGTH() returns bytes, not characters, in multibyte strings).

How to Create a User-Defined Function

To create a SQL UDF, you need the CREATE ROUTINE privilege. The syntax is:

DELIMITER //
CREATE FUNCTION get_full_name(fname VARCHAR(50), lname VARCHAR(50))
RETURNS VARCHAR(101)
DETERMINISTIC
BEGIN
    RETURN CONCAT(fname, ' ', lname);
END //
DELIMITER ;

Key points:

  • The DELIMITER command is needed in the mysql client so the semicolon inside the body doesn’t end the statement prematurely.
  • DETERMINISTIC means the function always returns the same result for the same input. If you omit it, MySQL may still work, but it’s required for binary logging in some cases.
  • You can specify READS SQL DATA or NO SQL to declare data access.
  • Drop with DROP FUNCTION get_full_name;

UDFs are stored in the server and can be used in queries just like built-ins. They are not compiled; they are interpreted each time they run, which can be slower than built-ins.

Common Problems and Misconceptions

One common mistake is confusing functions with stored procedures. A function returns a single value and cannot use SELECT to return result sets; a procedure can return multiple result sets and is called with CALL. Another issue is using functions in WHERE clauses on indexed columns, which disables index usage. For example, WHERE DATE(created_at) = '2023-01-01' is slower than WHERE created_at >= '2023-01-01' AND created_at < '2023-01-02'. Also, non-deterministic functions like RAND() are not allowed in some contexts, such as in generated columns or indexes.

Another misconception: aggregate functions like COUNT() are often called “functions,” but they are special because they operate on groups. They are still functions in the SQL standard, but they behave differently in that they can be used with GROUP BY.

Choosing Between a Function and a Stored Procedure

Use a function when you need to compute a value that can be used in expressions, such as in a SELECT list or a WHERE clause. Use a stored procedure when you need to perform multiple statements, modify data, or return multiple result sets. Functions cannot perform DML (INSERT/UPDATE/DELETE) except in limited cases (e.g., they can call a procedure that does DML, but that's discouraged). Procedures can have input, output, and inout parameters; functions only have input parameters and one return value.

Performance and Safety Considerations

Functions can impact performance if used excessively in large queries. Each call has overhead. For UDFs written in SQL, the overhead is higher than built-ins. For heavy computations, consider a C-based UDF or moving logic to the application layer. Also, be careful with non-deterministic functions in replication: if you use NOW() in a statement, it may replicate differently. To avoid issues, MySQL uses statement-based or row-based replication; row-based is safer for non-deterministic functions.

Security: UDFs run with the privileges of the definer, not the invoker, unless you specify SQL SECURITY INVOKER. This can be a security risk if a malicious function is created. Always restrict who can create functions and review their code.

Frequently Asked Questions

Can I use a function in an index? Yes, MySQL supports functional indexes (also called expression indexes) since version 8.0.13. For example, CREATE INDEX idx_upper ON users ((UPPER(last_name))); allows fast lookups on the uppercase value.

What is the difference between a function and a procedure? A function returns a single value and can be used in SQL expressions; a procedure is called with CALL and can return multiple result sets or modify data. Functions cannot perform DML (except in rare cases), procedures can.

Why does my function return NULL? Functions return NULL if any argument is NULL, unless the function is specifically designed to handle NULLs (like IFNULL()). Check your inputs and use COALESCE() if needed.

Can I call a stored procedure from a function? Yes, you can call a procedure from a function using CALL, but the procedure must not return a result set or use transactions. This is generally discouraged because it can lead to unexpected behavior.

Are MySQL functions case-sensitive? Function names are case-insensitive, but string comparisons depend on the collation. For example, UPPER('a') works regardless of case, but STRCMP() may be case-sensitive depending on collation.

How do I see all available functions? Run SHOW FUNCTION STATUS; to list user-defined functions, and consult the MySQL manual for built-in functions. You can also query the information_schema.ROUTINES table.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.