Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
ClausesStatementsKeywordsExpressionsFunctions

Functions

MySQL functions perform specific operations on data, making queries more efficient and flexible. They are categorized into aggregate, string, date, and mathematical functions.

Common Uses of MySQL Functions

  1. Aggregate Functions: Summarize data (e.g., SUM()).
  2. String Functions: Modify text (e.g., CONCAT()).
  3. Date Functions: Manipulate dates (e.g., NOW()).
  4. Mathematical Functions: Perform calculations (e.g., ROUND()).
  5. JSON Functions: Handle JSON data (e.g., JSON_OBJECT()).

Examples

  1. Aggregate Function
     
    SELECT AVG(salary) AS average_salary FROM employees;

    Explanation: Calculates the average salary of employees.

  2. String Function
     
    SELECT UPPER(name) AS uppercase_name FROM customers;

    Explanation: Converts customer names to uppercase.

  3. Date Function
     
    SELECT CURDATE() AS today;

    Explanation: Retrieves the current date.

  4. Mathematical Function
     
    SELECT ROUND(price, 2) AS rounded_price FROM products;

    Explanation: Rounds the price to two decimal places.

  5. JSON Function
     
    SELECT JSON_OBJECT('id', id, 'name', name) AS json_data FROM users;

    Explanation: Converts id and name columns into a JSON object.