Lesson 40 of 60 – MySQL Functions
67%

MySQL Functions

MySQL provides many built-in functions that help you perform calculations, manipulate text, work with dates, and analyze data.

Note: A function accepts input values and returns a result. MySQL has functions for strings, numbers, dates, aggregate calculations, and many other tasks.

1. What is a MySQL Function?

A MySQL function is a built-in operation that performs a specific task and returns a value.

SELECT UPPER('hello');

Result:

HELLO

The UPPER() function converts text to uppercase.

2. Why Use Functions?

Functions make SQL queries more powerful and useful.

  • Perform calculations
  • Modify text
  • Work with dates and times
  • Calculate totals
  • Count records
  • Find maximum and minimum values
  • Format data

3. Basic Function Syntax

The common syntax is:

FUNCTION_NAME(value);

Example:

SELECT LENGTH('MySQL');

Result:

5

4. Function with a Table Column

Functions can be applied directly to table columns.

SELECT UPPER(name)
FROM students;

This converts every student's name to uppercase in the query result.

5. UPPER() Function

The UPPER() function converts text to uppercase.

SELECT UPPER('python');

Result:

PYTHON

Example with a table:

SELECT UPPER(name)
FROM students;

6. LOWER() Function

The LOWER() function converts text to lowercase.

SELECT LOWER('MYSQL');

Result:

mysql

Example:

SELECT LOWER(email)
FROM students;

7. LENGTH() Function

The LENGTH() function returns the length of a string in bytes.

SELECT LENGTH('MySQL');

Result:

5

For ASCII characters, the byte length is normally the same as the character count.

8. CHAR_LENGTH() Function

CHAR_LENGTH() returns the number of characters in a string.

SELECT CHAR_LENGTH('Hello');

Result:

5

CHAR_LENGTH() is especially useful when working with multibyte character sets because it counts characters rather than bytes.

9. CONCAT() Function

The CONCAT() function joins multiple strings together.

SELECT CONCAT('Hello', ' ', 'World');

Result:

Hello World

Example:

SELECT CONCAT(first_name, ' ', last_name)
FROM students;

10. ROUND() Function

The ROUND() function rounds a number to a specified number of decimal places.

SELECT ROUND(125.678, 2);

Result:

125.68

Example:

SELECT ROUND(fee, 2)
FROM students;

11. CEIL() Function

The CEIL() function returns the smallest integer greater than or equal to a number.

SELECT CEIL(12.3);

Result:

13

12. FLOOR() Function

The FLOOR() function returns the largest integer less than or equal to a number.

SELECT FLOOR(12.9);

Result:

12

13. ABS() Function

The ABS() function returns the absolute value of a number.

SELECT ABS(-50);

Result:

50

Example:

SELECT ABS(-125.50);

14. MOD() Function

The MOD() function returns the remainder after division.

SELECT MOD(10, 3);

Result:

1

Because 10 divided by 3 leaves a remainder of 1.

15. NOW() Function

The NOW() function returns the current date and time.

SELECT NOW();

The exact result depends on the MySQL server's current date and time.

16. CURDATE() Function

The CURDATE() function returns the current date.

SELECT CURDATE();

Example result:

2026-09-21

The actual result changes according to the current date.

17. CURTIME() Function

The CURTIME() function returns the current time.

SELECT CURTIME();

The result depends on the current MySQL server time.

18. YEAR() Function

The YEAR() function extracts the year from a date.

SELECT YEAR('2026-09-21');

Result:

2026

Example:

SELECT YEAR(admission_date)
FROM students;

19. MONTH() Function

The MONTH() function extracts the month number from a date.

SELECT MONTH('2026-09-21');

Result:

9

20. DAY() Function

The DAY() function returns the day of the month from a date.

SELECT DAY('2026-09-21');

Result:

21

21. COUNT() Function

The COUNT() function counts rows or non-NULL values depending on how it is used.

SELECT COUNT(*)
FROM students;

This counts the number of rows in the students table.

You can also count non-NULL values in a particular column:

SELECT COUNT(email)
FROM students;

22. SUM() Function

The SUM() function calculates the total of numeric values.

SELECT SUM(fee)
FROM students;

This returns the total fee from the selected rows.

23. AVG() Function

The AVG() function calculates the average of numeric values.

SELECT AVG(fee)
FROM students;

This returns the average fee from the selected rows.

24. MAX() Function

The MAX() function returns the largest value.

SELECT MAX(fee)
FROM students;

This returns the highest fee in the selected data.

25. MIN() Function

The MIN() function returns the smallest value.

SELECT MIN(fee)
FROM students;

This returns the lowest fee in the selected data.

26. Combining Functions

You can use multiple functions in the same query.

SELECT
    COUNT(*) AS total_students,
    SUM(fee) AS total_fee,
    AVG(fee) AS average_fee,
    MAX(fee) AS highest_fee,
    MIN(fee) AS lowest_fee
FROM students;

This query produces several useful statistics at once.

27. Functions with WHERE

Functions can be used together with WHERE to calculate results for selected records.

SELECT AVG(fee)
FROM students
WHERE course = 'Python';

This calculates the average fee only for Python students.

28. Functions with Aliases

Aliases make function results easier to understand.

SELECT
    COUNT(*) AS total_students,
    SUM(fee) AS total_fee
FROM students;

Here, total_students and total_fee are column aliases for the calculated results.

29. Common MySQL Function Categories

Category Examples
String Functions UPPER(), LOWER(), LENGTH(), CONCAT()
Numeric Functions ROUND(), CEIL(), FLOOR(), ABS(), MOD()
Date & Time Functions NOW(), CURDATE(), CURTIME(), YEAR(), MONTH(), DAY()
Aggregate Functions COUNT(), SUM(), AVG(), MAX(), MIN()

These categories will be covered in more detail in the upcoming MySQL lessons.

30. Complete MySQL Functions Example

CREATE TABLE students (
    student_id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    course VARCHAR(100) NOT NULL,
    fee DECIMAL(10,2) NOT NULL
);

INSERT INTO students
(name, course, fee)
VALUES
('Amit Kumar', 'Python', 15000.00),
('Priya Singh', 'Java', 18000.00),
('Rahul Kumar', 'Python', 12000.00),
('Neha Sharma', 'PHP', 10000.00);

-- String function
SELECT UPPER(name) AS student_name
FROM students;

-- Numeric function
SELECT ROUND(fee, 0) AS rounded_fee
FROM students;

-- Aggregate functions
SELECT
    COUNT(*) AS total_students,
    SUM(fee) AS total_fee,
    AVG(fee) AS average_fee,
    MAX(fee) AS highest_fee,
    MIN(fee) AS lowest_fee
FROM students;

-- Function with WHERE
SELECT AVG(fee) AS python_average_fee
FROM students
WHERE course = 'Python';

This example demonstrates string, numeric, and aggregate functions in practical queries.

📌 Key Points

  • MySQL functions perform specific operations and return values.
  • Functions can work with values, expressions, and table columns.
  • String functions work with text data.
  • Numeric functions perform mathematical operations.
  • Date and time functions work with dates and times.
  • Aggregate functions calculate results from multiple rows.
  • UPPER() converts text to uppercase.
  • LOWER() converts text to lowercase.
  • CONCAT() joins strings.
  • ROUND(), CEIL(), FLOOR(), ABS(), and MOD() are numeric functions.
  • NOW(), CURDATE(), CURTIME(), YEAR(), MONTH(), and DAY() work with date/time values.
  • COUNT(), SUM(), AVG(), MAX(), and MIN() are important aggregate functions.

🧠 Quick Quiz

Question: Which MySQL function is used to calculate the total of numeric values?