MySQL provides built-in functions across five core categories: String, Numeric, Date and Time, Control Flow, and Aggregate functions. Knowing when to transform data in SQL versus application code keeps queries concise while avoiding accidental full-table scans.
| Category | Essential Functions | Common Use Cases |
|---|---|---|
| String | CONCAT_WS(), LOWER(), TRIM(), SUBSTRING(), REPLACE() |
Formatting full names, cleaning whitespace, masking text |
| Numeric | ROUND(), FLOOR(), CEIL(), ABS(), MOD() |
Rounding currency, calculating pagination counts |
| Date / Time | NOW(), CURDATE(), DATE_ADD(), DATEDIFF(), DATE_FORMAT() |
Expiration dates, shipping durations, monthly grouping |
| Control Flow | COALESCE(), IFNULL(), NULLIF(), IF(), CASE |
Safe NULL fallbacks, avoiding divide-by-zero |
| Aggregate | COUNT(), SUM(), AVG(), MIN(), MAX() |
Totals, averages, dashboards with GROUP BY |
This single reporting query combines string formatting, safe NULL handling, rounding, and date calculations on demo_customers and demo_orders:
SELECT
c.id,
CONCAT_WS(' ', c.first_name, c.last_name) AS full_name,
LOWER(TRIM(c.email)) AS clean_email,
COALESCE(COUNT(o.id), 0) AS order_count,
ROUND(COALESCE(SUM(o.total_amount), 0.00), 2) AS total_spent,
DATEDIFF(NOW(), MAX(o.created_at)) AS days_since_last_order
FROM demo_customers AS c
LEFT JOIN demo_orders AS o
ON o.customer_id = c.id
GROUP BY c.id, c.first_name, c.last_name, c.email;
WHERE Columns: Calling LOWER(email) or YEAR(created_at) on the left side of a WHERE comparison prevents MySQL from using regular B-tree indexes and forces a full scan. Normalize strings when inserting, or add an indexed generated column.NOW() and RAND() change output between calls; pass fixed timestamps from your backend when caching query results or writing deterministic unit tests.SUBSTRING(str, pos, len) in MySQL is 1-based rather than 0-based like JavaScript or Python slices.CONCAT() and CONCAT_WS()?CONCAT() returns NULL if any argument is NULL, whereas CONCAT_WS(' ', a, b) skips NULL arguments cleanly.
LENGTH() count characters or bytes in MySQL?LENGTH() counts bytes (so an emoji takes 4 bytes in utf8mb4), while CHAR_LENGTH() counts actual characters.
Yes, MySQL supports user-defined stored functions via CREATE FUNCTION, though built-in C-level functions execute much faster.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.