Skip to content

MySQL Tutorial MySQL Functions

MySQL Functions Overview

Lesson 114 of 308 2 min read By Deepak Paswan Updated Oct 5, 2026

A function takes zero or more values (arguments) and returns one value. MySQL has several hundred built in. You have already used a few: CONCAT, ROUND, NOW, COUNT.

query.sql
SQL
SELECT UPPER('mysql') AS shouting, LENGTH('hello') AS bytes, ROUND(3.14159, 2) AS pi_short, CONCAT('Hello', ', ', 'world') AS greeting;
Output
OUTPUT
+----------+-------+----------+--------------+
| shouting | bytes | pi_short | greeting     |
+----------+-------+----------+--------------+
| MYSQL    |     5 |     3.14 | Hello, world |
+----------+-------+----------+--------------+
1 row in set

Write the name, then the arguments in brackets, separated by commas. Even a function with no arguments needs the brackets: NOW().

Two kinds

KindWorks onReturnsExamples
Scalar functionsOne row at a timeOne value per rowUPPER(), ROUND(), DATE_FORMAT()
Aggregate functionsA group of rowsOne value per groupCOUNT(), SUM(), AVG(), MAX()

Aggregate functions are the subject of the next chapters. A scalar function runs for every row of the result:

query.sql
SQL
CREATE TABLE people (id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(20), last_name VARCHAR(20));
INSERT INTO people (first_name, last_name) VALUES ('asha', 'RAO'), ('ben', 'carter');

SELECT id, CONCAT(UPPER(LEFT(first_name, 1)), LOWER(SUBSTRING(first_name, 2)), ' ', UPPER(last_name)) AS display_name FROM people;
Output
OUTPUT
+----+--------------+
| id | display_name |
+----+--------------+
|  1 | Asha RAO     |
|  2 | Ben CARTER   |
+----+--------------+
2 rows in set

Nesting

The result of one function can be the argument of another. MySQL works from the inside out: LEFT(first_name, 1) first, then UPPER(...) on its result.

Categories you will meet

CategoryExamplesLesson
StringCONCAT, SUBSTRING, TRIM, REPLACEString Functions
NumericROUND, FLOOR, MOD, POWERNumeric Functions
Date and timeNOW, DATE_ADD, DATEDIFF, DATE_FORMATDate Functions
ConditionalIF, IFNULL, COALESCE, NULLIFConditional Functions
Hash and encryptionSHA2, AES_ENCRYPTHash Functions
SystemVERSION, DATABASE, LAST_INSERT_IDSystem Functions
JSONJSON_EXTRACT, JSON_OBJECTJSON Functions
WindowROW_NUMBER, RANK, LAGWindow Functions

NULL and functions

Most functions return NULL when an argument is NULL. Notable exceptions are CONCAT_WS, COALESCE, IFNULL, IF and the aggregate functions (which skip NULL).

query.sql
SQL
SELECT CONCAT('a', NULL, 'b') AS concat_null, CONCAT_WS('-', 'a', NULL, 'b') AS concat_ws_skips, UPPER(NULL) AS upper_null;
Output
OUTPUT
+-------------+-----------------+------------+
| concat_null | concat_ws_skips | upper_null |
+-------------+-----------------+------------+
| NULL        | a-b             | NULL       |
+-------------+-----------------+------------+
1 row in set

Deterministic and non-deterministic

A function is deterministic if the same input always gives the same output (UPPER('a')). NOW(), RAND() and UUID() are non-deterministic. This matters in generated columns (which need deterministic functions), in replication (statements with non-deterministic functions need care) and in query caching.

Functions and indexes

A function applied to a column in a WHERE clause usually prevents the use of an index on that column (Sargable Conditions). Apply functions to the values, not to the column.

The full list

The MySQL manual has a page called “Built-In Function and Operator Reference” that lists every function, with the version in which it was added. Use it whenever you wonder, “Is there a function for this?”. There often is.

lightbulb
Check in the shell
SELECT function(args); is the fastest way to learn what a function does. Try values with edge cases: NULL, an empty string, a negative number, a very long text.