Functions
Learn how to create user-defined stored functions that return a single value and can be used directly inside SELECT statements and expressions.
Introduction
Earlier lessons covered MySQL's built-in String, Numeric, and Date functions — ready-made tools like UPPER(), ROUND(), and NOW() that ship with MySQL itself. This lesson introduces something different: writing your own custom functions.
A user-defined stored function lets you package your own calculation or logic under a name, store it inside the database, and then call it just like any built-in function inside a SELECT statement.
- Why this lesson is about user-defined functions, not built-in ones.
- What a stored function is and how to create one.
- How to use a custom function directly inside a SELECT statement.
- The key difference between a stored function and a stored procedure.
- How to remove a function with DROP FUNCTION.
Not the Built-in Functions
This lesson is NOT about the built-in String, Numeric, and Date functions covered earlier in this course (like UPPER(), ROUND(), or DATEDIFF()). Those ship pre-built with MySQL. This lesson is about user-defined stored functions — custom functions that you write yourself and save inside the database.
Both kinds are called the same way, with parentheses after a name, but a user-defined function is something you author, using CREATE FUNCTION, to encapsulate your own logic — logic MySQL has no built-in equivalent for.
What is a Stored Function?
A stored function is a saved, reusable block of SQL logic, similar to a stored procedure, but with one defining rule: a function must return exactly one value, and because of that, it can be used directly inside a SELECT statement or any other expression.
- A stored function is saved inside the database, like a stored procedure.
- It always returns exactly one value.
- It can be used inline, anywhere an expression or built-in function could be used.
Creating a Function
A function is created with CREATE FUNCTION, declaring its parameters, the type of value it RETURNS, and a body that ends by returning that value with RETURN. Just like a stored procedure, DELIMITER is needed to define a function containing semicolons.
Consider a "students" table where each student has a numeric score. A function can calculate a letter grade from that score.
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
score INT
);
INSERT INTO students (name, score) VALUES
('Alex', 78),
('Sam', 45),
('Priya', 92);
DELIMITER $$
CREATE FUNCTION letter_grade(score INT)
RETURNS CHAR(1)
DETERMINISTIC
BEGIN
DECLARE grade CHAR(1);
IF score >= 90 THEN
SET grade = 'A';
ELSEIF score >= 75 THEN
SET grade = 'B';
ELSEIF score >= 60 THEN
SET grade = 'C';
ELSE
SET grade = 'F';
END IF;
RETURN grade;
END$$
DELIMITER ;The "letter_grade" function takes a numeric score and always returns a single CHAR(1) value — the corresponding letter grade. DETERMINISTIC tells MySQL that the same input will always produce the same output, which MySQL requires functions to declare.
Using a Function in a SELECT
Because a stored function always returns a single value, it can be called directly inside a SELECT statement, exactly like a built-in function such as ROUND() or UPPER().
SELECT name, score, letter_grade(score) AS grade
FROM students;+-------+-------+-------+
| name | score | grade |
+-------+-------+-------+
| Alex | 78 | B |
| Sam | 45 | F |
| Priya | 92 | A |
+-------+-------+-------+The function runs once per row, transforming each numeric score into its letter grade directly inside the query results.
Function vs Procedure
Stored functions and stored procedures look similar, but they serve different purposes.
| Aspect | Stored Procedure | Stored Function |
|---|---|---|
| Return value | Not required to return a value; may return none, or a result set | Must return exactly one value |
| Called with | CALL procedure_name(...) | Used directly inside expressions, e.g. SELECT function_name(...) |
| Use inside SELECT | Not allowed | Allowed |
| Typical use | Multi-step actions like inserting/updating data | Calculating or transforming a single value |
If you need to compute and return a single value to use inside a query, reach for a function. If you need to perform an action — like inserting a row or running several steps — reach for a procedure.
Dropping a Function
A function that is no longer needed can be removed with DROP FUNCTION.
DROP FUNCTION letter_grade;Common Mistakes
- Confusing a user-defined stored function with MySQL's built-in String/Numeric/Date functions — they are entirely different things.
- Trying to CALL a function the way you CALL a procedure — functions are used inline, not called with CALL.
- Forgetting the RETURN statement, or forgetting to declare the correct RETURNS type.
- Forgetting DELIMITER when defining a function whose body contains semicolons.
Best Practices
- Use a function when you need to compute a single value that will be used inside a query.
- Use a procedure instead when the logic performs actions like inserting or updating rows.
- Give functions clear, descriptive names, like letter_grade or calculate_discount.
- Mark functions DETERMINISTIC when the same input always produces the same output.
Frequently Asked Questions
Can a stored function modify table data, like INSERT or UPDATE?
By default, MySQL restricts functions from performing data-modifying statements when certain settings are enabled, since functions are meant for computing and returning values, not performing actions. Use a stored procedure for that instead.
Can a function have more than one parameter?
Yes. A function can accept multiple input parameters, separated by commas, just like a stored procedure's IN parameters — but it still returns only a single value.
Is a user-defined function the same as a built-in function like ROUND()?
They are used the same way, inline inside expressions, but a built-in function ships with MySQL itself, while a user-defined function is written and saved by you inside your own database.
Why does MySQL require RETURNS and DETERMINISTIC?
RETURNS declares the exact data type the function promises to return, and DETERMINISTIC tells MySQL whether the same inputs always produce the same output, which affects how MySQL is allowed to optimize or replicate the function.
Key Takeaways
- This lesson covers user-defined stored functions, distinct from MySQL's built-in String/Numeric/Date functions.
- CREATE FUNCTION name(...) RETURNS type BEGIN ... RETURN ...; END defines one.
- A function must return exactly one value.
- Unlike a procedure, a function can be used directly inside a SELECT statement or expression.
- DROP FUNCTION removes a function that is no longer needed.
Summary
User-defined stored functions let you package your own single-value calculations inside the database, so they can be reused directly inside SELECT statements and other expressions, just like MySQL's built-in functions.
In this lesson, you learned the difference between built-in functions and user-defined stored functions, how to create one with CREATE FUNCTION, and how it differs from a stored procedure. Next, you will learn about triggers — logic that runs automatically in response to table changes.
- You understand what a user-defined stored function is.
- You can create one with CREATE FUNCTION.
- You know how a function differs from a stored procedure.
- You are ready to learn about triggers.