π§ MySQL Built-in Functions β String, Math, Date, JSON, Conditional
π§² Introduction β Why MySQL Functions Matter
MySQL functions are predefined operations you can use to transform, calculate, format, and analyze data directly in your SQL queries. From text formatting and number crunching to date manipulation and flow control, MySQL functions let you write powerful, efficient queries with minimal code.
These functions are the foundation of query logic for dashboards, reports, ETL pipelines, stored procedures, and real-time apps.
π― In this guide, youβll learn:
- The most important MySQL built-in function categories
- Syntax and examples for string, math, date, and control functions
- Use cases that improve performance and reduce app-side processing
- How to use aggregate and JSON functions in MySQL
π 1. MySQL Function Categories
Category | Examples | Purpose |
---|---|---|
π€ String | CONCAT() , SUBSTRING() , REPLACE() | Format and extract string values |
π’ Math | ROUND() , ABS() , CEIL() | Perform calculations and rounding |
ποΈ Date/Time | NOW() , CURDATE() , DATEDIFF() | Handle dates and timestamps |
π Aggregate | SUM() , AVG() , COUNT() | Summarize grouped data |
β Conditional | IF() , CASE , IFNULL() | Control logic and handle NULLs |
π¦ JSON | JSON_EXTRACT() , JSON_ARRAY() | Parse and manipulate JSON data |
π§ Information | VERSION() , DATABASE() , USER() | Get metadata about the current session |
π€ 2. String Functions in MySQL
β Examples
-- Concatenate first and last name
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;
-- Replace characters
SELECT REPLACE(phone, '-', '') FROM contacts;
-- Extract substring
SELECT SUBSTRING(email, 1, 5) FROM employees;
π§ Tip: Always TRIM()
data before comparisons or display.
π’ 3. Math Functions in MySQL
β Examples
SELECT ROUND(price, 2) FROM products;
SELECT ABS(-42);
SELECT POWER(2, 4); -- β 16
π Use math functions in reports, discounts, billing logic, and validations.
ποΈ 4. Date & Time Functions
β Examples
SELECT CURDATE(); -- Current date
SELECT NOW(); -- Current timestamp
SELECT DATEDIFF(CURDATE(), birth_date) / 365 AS age FROM users;
π‘ Combine with INTERVAL
and DATE_ADD()
for flexible scheduling queries.
π 5. Aggregate Functions (GROUP BY)
β Examples
SELECT department, COUNT(*) FROM employees GROUP BY department;
SELECT AVG(salary) FROM employees WHERE department = 'Sales';
π§ These are key to building analytical dashboards and summaries.
β 6. Conditional Functions
β Examples
SELECT IF(score >= 60, 'Pass', 'Fail') FROM students;
SELECT CASE
WHEN rating >= 4 THEN 'Excellent'
WHEN rating >= 3 THEN 'Good'
ELSE 'Needs Improvement'
END AS feedback FROM reviews;
Use IFNULL()
or COALESCE()
to handle null values gracefully.
π¦ 7. JSON Functions in MySQL 5.7+
β Examples
SELECT JSON_EXTRACT(data, '$.address.city') FROM users;
SELECT JSON_ARRAY('red', 'green', 'blue');
SELECT JSON_OBJECT('name', name, 'email', email) FROM customers;
π Powerful for apps storing structured data or interacting with APIs.
π§ 8. Metadata & Utility Functions
SELECT VERSION(); -- MySQL version
SELECT DATABASE(); -- Current database
SELECT USER(); -- Logged-in user
Useful in admin tools, logs, or multi-database apps.
βοΈ 9. Real-World Use Cases
Scenario | Function(s) Used |
---|---|
Normalize names | UPPER() , LOWER() , TRIM() |
Calculate age from birth date | DATEDIFF() , CURDATE() |
Dynamic discount logic | IF() , CASE , ROUND() |
Create JSON response | JSON_OBJECT() , JSON_ARRAY() |
Null-safe summaries | COALESCE() , IFNULL() , SUM() |
π Summary β Recap & Best Practices
MySQL functions are your go-to tools for query-side logic, formatting, calculations, and transformations. Using functions efficiently improves query performance, reduces app-side processing, and ensures better data consistency.
π Key Takeaways:
- Learn which function types match your use case (text, math, JSON)
- Use conditional and date logic to simplify business rules
- JSON functions are powerful for modern, API-driven databases
βοΈ Real-World Relevance:
Used in real-time web apps, data pipelines, dashboards, and ERP systems to deliver dynamic, smart SQL solutions.
β FAQ β MySQL Functions
β Are MySQL functions case-sensitive?
β No, function names are case-insensitive. But string comparisons may be, depending on collation.
β Can I create custom functions?
β
Yes, use CREATE FUNCTION
to build your own stored functions.
β Whatβs the difference between IF()
and CASE
?
β
IF()
is simpler for binary logic. CASE
is more flexible for multi-branch conditions.
β Are JSON functions available in all MySQL versions?
β No. JSON support starts in MySQL 5.7+.
Share Now :