What are SQL Functions

 

SQL Functions are built-in pieces of code that perform specific operations on your data. Instead of writing complex logic manually, you use these functions to transform data, perform calculations, or format output directly within your query.

They are generally categorized into two main groups based on how they process rows:

 

1. Aggregate Functions (Multiple Rows → One Value)
These functions take a group of values from multiple rows and return a single summary result. They are almost always used with the GROUP BY clause.
  • SUM(): Adds up all values in a column.
  • AVG(): Calculates the numerical average.
  • COUNT(): Counts the number of rows or non-null values.
  • MAX() / MIN(): Finds the highest or lowest value in a set.

 

2. Scalar Functions (One Row → One Value)
These functions operate on an individual value in a single row and return a modified result for every row in the result set.
  • String Functions: UPPER() (converts to uppercase), CONCAT() (joins strings), LENGTH() (gets character count).
  • Numeric Functions: ROUND() (rounds decimals), ABS() (absolute value).
  • Date Functions: NOW() (current date/time), DATEDIFF() (difference between two dates).