In SQL, a function is a reusable piece of SQL code that performs a specific task or operation and returns a value. SQL functions can be categorized into two main types: scalar functions and aggregate functions.
Scalar Functions:
Scalar functions operate on a single value and return a single value as a result. They are used for various purposes, including data manipulation, string manipulation, date calculations, and mathematical operations.
Scalar functions can be used within SQL queries to transform, manipulate, or calculate values on a per-row basis.
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;
Aggregate Functions:
Aggregate functions perform calculations on sets of values and return a single value as a result. They are used to summarize data or provide statistical information about a group of rows.
Aggregate functions are often used in conjunction with the GROUP BY clause to perform calculations on groups of rows, treating the group as a single entity.
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
User-Defined Functions (UDFs):
In addition to built-in SQL functions, some database systems allow you to create custom user-defined functions (UDFs). UDFs are functions that you define yourself and can encapsulate custom logic to perform specific tasks. UDFs can be scalar or table-valued.
CREATE FUNCTION CalculateAge(@birthdate DATE)
RETURNS INT
AS
BEGIN
DECLARE @age INT;
SET @age = DATEDIFF(YEAR, @birthdate, GETDATE());
RETURN @age;
END;
SELECT employee_id, CalculateAge(birthdate) AS age
FROM employees;
In database queries, you can use functions in various parts of SQL statements, including the SELECT clause to generate derived columns, the WHERE clause to filter data based on specific criteria, the JOIN conditions to manipulate join keys, and more. Functions provide a way to perform calculations, transformations, and data manipulations within SQL queries, making them a powerful tool for working with databases and generating meaningful results from your data.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In SQL, a function is a reusable piece of SQL code that performs a specific task or operation and returns a value. SQL functions can be categorized into two main types: scalar functions and aggregate functions.
Scalar Functions:
Aggregate Functions:
User-Defined Functions (UDFs):
In database queries, you can use functions in various parts of SQL statements, including the SELECT clause to generate derived columns, the WHERE clause to filter data based on specific criteria, the JOIN conditions to manipulate join keys, and more. Functions provide a way to perform calculations, transformations, and data manipulations within SQL queries, making them a powerful tool for working with databases and generating meaningful results from your data.