MySQL Functions with examples

MySQL functions are built-in tools in MySQL that help you perform tasks on your data, such as calculations, string manipulation, or date management.

They make queries easier by processing data directly in the database.

Screenshot By Author

What is a Function in MySQL?

  • A function is a predefined set of SQL operations that returns a value.
  • Used to perform calculations, manipulate strings, handle dates, etc.
  • Functions return values; they can be used in SELECT, WHERE, and ORDER BY clauses.

Types of MySQL Functions

  1. String Functions
  2. Numeric Functions
  3. Date and Time Functions
  4. Aggregate Functions
  5. Control Flow Functions

Not a member yet? CLICK HERE to read the full article for free!!

  1. String Functions: These handle text or string data
Screenshot By Author

CONCAT(): Combine two or more strings

SELECT CONCAT('Hi', ' ', 'There');
String Functions

2. Numeric Functions: These work with numbers.

Screenshot By Author

ROUND(): Rounds a number to a specified number of decimal places.

SELECT ROUND(15.789, 2);
Numeric Functions

3. Date and Time Functions: These work with dates and times.

Screenshot By Author

NOW(): Returns the current date and time

SELECT NOW();
Date and Time functions

4. Aggregate functions: These perform calculations on a group of rows.

Screenshot By Author

COUNT(): Counts the number of rows.

SELECT COUNT(*) Employees;
Aggregate functions

5. Control Flow Functions: Control flow functions help you make decisions in your SQL queries, similar to if-else statements in programming.

CASE 
WHEN score >= 90 THEN 'A'
WHEN score >= 80 THEN 'B'
ELSE 'C'
END
Control Flow functions

Functions are used in SQL queries, often in the SELECT statement, to process or transform data. They save time and make data handling easier.

MySQL functions are built-in operations that can be used to manipulate data, perform calculations, or return specific results.

Here are some more stories you might like

If you like my content, give me a few claps and follow for more!

Thanks for your time!


MySQL Functions with examples was originally published in Javarevisited on Medium, where people are continuing the conversation by highlighting and responding to this story.

This post first appeared on Read More