Cover Image for MySQL Functions
98 views

MySQL Functions

MySQL provides a wide range of built-in functions that you can use in SQL queries to perform various tasks, including data manipulation, calculations, string operations, date and time operations, and more. These functions can simplify your SQL queries and enable you to work with data in a more efficient and organized manner. Here are some of the commonly used categories of MySQL functions:

  1. Numeric Functions:
  • ABS(): Returns the absolute value of a number.
  • ROUND(): Rounds a number to a specified number of decimal places.
  • CEIL() (or CEILING()): Returns the smallest integer value greater than or equal to a number.
  • FLOOR(): Returns the largest integer value less than or equal to a number.
  • MOD(): Returns the remainder of a division operation.
  • POWER(): Returns the result of raising a number to a specified power.
  1. String Functions:
  • CONCAT(): Combines two or more strings.
  • LENGTH() (or CHAR_LENGTH()): Returns the length of a string in characters.
  • UPPER(): Converts a string to uppercase.
  • LOWER(): Converts a string to lowercase.
  • SUBSTRING() (or SUBSTR()): Returns a substring from a string.
  • TRIM(): Removes leading and trailing spaces from a string.
  1. Date and Time Functions:
  • NOW(): Returns the current date and time.
  • DATE(): Extracts the date part from a datetime value.
  • TIME(): Extracts the time part from a datetime value.
  • DATEDIFF(): Calculates the difference between two dates.
  • DATE_ADD(): Adds a specified time interval to a date.
  • DATE_FORMAT(): Formats a date according to a specified pattern.
  1. Aggregate Functions:
  • SUM(): Calculates the sum of a set of values.
  • AVG(): Calculates the average of a set of values.
  • COUNT(): Counts the number of rows in a result set.
  • MIN(): Returns the minimum value in a set of values.
  • MAX(): Returns the maximum value in a set of values.
  1. Conditional Functions:
  • IF(): Returns one value if a condition is true and another value if it is false.
  • CASE: Allows you to perform conditional logic within SQL queries.
  1. Math Functions:
  • SQRT(): Calculates the square root of a number.
  • LOG(): Calculates the natural logarithm of a number.
  • RAND(): Generates a random number.
  • PI(): Returns the mathematical constant π (pi).
  1. Control Flow Functions:
  • IFNULL(): Returns the first non-null value in a list.
  • COALESCE(): Returns the first non-null value in a list of expressions.
  • NULLIF(): Returns null if two expressions are equal; otherwise, it returns the first expression.

These are just a few examples of the many functions available in MySQL. You can use these functions to perform a wide range of operations in your SQL queries and customize the retrieval and manipulation of data to suit your specific needs. Depending on your use case, you may need to explore more specialized or advanced functions provided by MySQL.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS