Learn about the most important MySQL functions. On this page, you will find an overview of all key functions, their syntax, and practical examples of use. Functions are divided into different categories based on their application.
The query displays the first names and last names of the first 5 employees from the table. The LIMIT 5 clause limits the results to only 5 records.
SELECT first_name, last_name FROM employees LIMIT 5;
The query lists the first name, last name, and salary of all employees working in the "Production" department. The WHERE clause filters the results to display only the employees from this specific department.
SELECT first_name, last_name, salary FROM employees WHERE department = 'Production';
The query counts the total number of all employees in the table. The COUNT(*) function returns the number of all rows in the table.
SELECT COUNT(*) FROM employees;
The query counts the number of distinct departments in the company. Using COUNT(DISTINCT department) ensures that each department is counted only once, regardless of how many employees work in an individual department.
SELECT COUNT(DISTINCT department) FROM employees;
The query lists the first names, last names, and salaries of all employees, sorted by salary from highest to lowest. The ORDER BY salary DESC clause sorts the results so that the highest-paid employees are displayed first.
SELECT first_name, last_name, salary FROM employees ORDER BY salary DESC;
The query displays data analysis by department. For each department, it calculates the number of employees (using COUNT) and their average salary (using AVG). The results are grouped by department using the GROUP BY clause.
SELECT department, COUNT(*) as employee_count, AVG(salary) as average_salary FROM employees GROUP BY department;
Functions in MySQL allow you to perform mathematical calculations, manipulate text strings, manage dates, and execute advanced conditional data logic directly inside your query. The table below provides a comprehensive overview of the most useful functions with examples for your simulator.
| Function | Description | Syntax (HTML) | Simulator Example |
|---|---|---|---|
| COUNT() | Counts the number of rows that match a specified condition. | COUNT(column) |
SELECT COUNT(*) FROM employees WHERE department = 'Management'; |
| SUM() | Calculates the total sum of numeric values in a column. | SUM(column) |
SELECT SUM(salary) FROM employees WHERE department = 'Production'; |
| AVG() | Calculates the average value of a numeric column. | AVG(column) |
SELECT AVG(salary) FROM sorting WHERE department = 'Store'; |
| MIN() | Returns the lowest (minimum) value within a column. | MIN(column) |
SELECT MIN(salary) FROM employees; |
| MAX() | Returns the highest (maximum) value within a column. | MAX(column) |
SELECT MAX(age) FROM first_table; |
| GROUP_CONCAT() | Concatenates values from multiple rows into a single comma-separated string. | GROUP_CONCAT(column) |
SELECT GROUP_CONCAT(first_name) FROM employees GROUP BY department; |
| CONCAT() | Combines two or more text strings into one single string. | CONCAT(s1, s2, ...) |
SELECT CONCAT(first_name, ' ', last_name) FROM employees; |
| SUBSTRING() | Extracts a segment of text from a string (starting from a specific index position). | SUBSTRING(text, start, length) |
SELECT SUBSTRING(first_name, 1, 3) FROM first_table; |
| TRIM() | Strips leading and trailing whitespace spaces from a text string. | TRIM(text) |
SELECT TRIM(first_name) FROM employees; |
| UPPER() | Converts all characters in a text string to UPPERCASE. | UPPER(text) |
SELECT UPPER(last_name) FROM employees; |
| LOWER() | Converts all characters in a text string to lowercase. | LOWER(text) |
SELECT LOWER(first_name) FROM first_table; |
| LENGTH() | Returns the total length of a text string measured in bytes/characters. | LENGTH(text) |
SELECT LENGTH(first_name) FROM employees; |
| REPLACE() | Replaces all occurrences of a specified substring with new text. | REPLACE(t, old, new) |
SELECT REPLACE(department, 'Sales', 'Marketing') FROM sorting; |
| NOW() | Returns the current date and time (exact current timestamp). | NOW() |
SELECT NOW(), entry_date FROM dates; |
| CURDATE() | Returns only the current date (excluding hours and minutes). | CURDATE() |
SELECT CURDATE(), entry_date FROM dates; |
| CURTIME() | Returns only the current time (hours, minutes, seconds). | CURTIME() |
SELECT CURTIME() FROM dates; |
| DATE_FORMAT() | Formats a date value output according to a specified layout. | DATE_FORMAT(date, format) |
SELECT DATE_FORMAT(entry_date, '%d.%m.%Y') FROM dates; |
| DATE_ADD() | Adds a specified time interval (days, months, years) to a date value. | DATE_ADD(d, INTERVAL x unit) |
SELECT entry_date, DATE_ADD(entry_date, INTERVAL 1 YEAR) FROM dates; |
| DATE_SUB() | Subtracts a specified time interval from a date value. | DATE_SUB(d, INTERVAL x unit) |
SELECT entry_date, DATE_SUB(entry_date, INTERVAL 1 MONTH) FROM dates; |
| DATEDIFF() | Calculates the total number of days between two separate date values. | DATEDIFF(d1, d2) |
SELECT DATEDIFF(CURDATE(), entry_date) FROM dates; |
| DAYNAME() | Returns the specific name of the weekday for a given date (e.g., Monday). | DAYNAME(date) |
SELECT entry_date, DAYNAME(entry_date) FROM dates; |
| ROUND() | Rounds a numeric value to a specified number of decimal positions. | ROUND(number, decimals) |
SELECT ROUND(AVG(salary), 2) FROM employees; |
| CEIL() | Rounds a numeric value upward to the nearest whole integer. | CEIL(number) |
SELECT CEIL(15.15); |
| FLOOR() | Rounds a numeric value downward to the nearest whole integer. | FLOOR(number) |
SELECT FLOOR(15.85); |
| ABS() | Returns the absolute (always positive) value of a number. | ABS(number) |
SELECT ABS(-250); |
| POWER() | Raises the first number to the power of the second number (base to exponent). | POWER(base, exponent) |
SELECT POWER(2, 3); |
| SQRT() | Calculates the square root of a specific numeric value. | SQRT(number) |
SELECT SQRT(81); |
| IF() | Returns one value if a condition evaluates to true, and another if it evaluates to false. | IF(condition, true_val, false_val) |
SELECT first_name, IF(department = 'Management', 'Leader', 'Worker') FROM employees; |
| IFNULL() | Returns an alternative fallback value if the targeted column contains a NULL value. | IFNULL(column, fallback) |
SELECT first_name, IFNULL(value, 0) FROM null_table; |
| NULLIF() | Returns NULL if both given values are exactly equal; otherwise, returns the first value. | NULLIF(v1, v2) |
SELECT NULLIF(department, 'Store') FROM sorting; |
| COALESCE() | Returns the first non-NULL value found within the provided list of parameters. | COALESCE(v1, v2, ...) |
SELECT COALESCE(value, note, 'No data') FROM null_table; |