Chapter 7: SQL Functions
Mathematical functions
SQL has many mathematical functions that you can use in your queries. Here are a few:
ABS()returns the absolute value of a number.ROUND()rounds a number to a specified number of decimal places.CEIL()returns the smallest integer greater than or equal to a number.FLOOR()returns the largest integer less than or equal to a number.POWER()returns a number raised to a specified power.
Date and time functions
SQL also has several functions for working with dates and times, including:
NOW()returns the current date and time.DATE()extracts the date part of a datetime value.TIME()extracts the time part of a datetime value.YEAR()extracts the year from a date value.MONTH()extracts the month from a date value.DAY()extracts the day of the month from a date value.
String functions
For working with strings, SQL provides functions such as these:
LENGTH()returns the length of a string.SUBSTRING()returns a portion of a string.CONCAT()concatenates two or more strings.UPPER()converts a string to uppercase.LOWER()converts a string to lowercase.
Aggregate functions
Aggregate functions perform calculations on groups of rows. For example:
COUNT()counts the number of rows in a group.SUM()calculates the sum of a group of values.AVG()calculates the average of a group of values.MAX()returns the maximum value in a group.MIN()returns the minimum value in a group.