Functions
Functions
A function in SQL is a block of code or a stored program that performs a specific task and
returns a value. Functions are used to simplify and modularize code, making it reusable and
easier to manage.
Predefined functions in SQL are built-in functions provided by the SQL language to
perform specific operations on data.
These functions are pre-written and readily available for use in SQL queries to simplify
common tasks like calculations, string manipulation, date handling, and aggregation.
1. String Function –
In SQL, string functions are used to manipulate and process string data. Here are some
commonly used string functions with examples:
2. LENGTH
Returns the length of a string.
Syntax:
LEN(string)
Ex
3. LOWER
Converts a string to lowercase.
Syntax:
LOWER(string)
Ex
5. SUBSTRING
Extracts a substring from a string.
Syntax:
SUBSTRING(string, start_position, length)
EX
6. TRIM
Removes leading and trailing spaces from a string.
Syntax:
TRIM(string)
EX
8. REPLACE
Replaces occurrences of a substring with another substring.
Syntax:
REPLACE(string, old_substring, new_substring)
Ex
9. REVERSE
Reverses a string.
Syntax:
REVERSE(string)
Ex
10. FORMAT
So this is the Employees table, we will use for executing aggregate functions
2.Aggregate Function
Aggregate functions in SQL are used to perform calculations on a set of values, returning a
single summarizing value. These functions are commonly used in conjunction with the
GROUP BY clause.
Here are the most used aggregate functions with examples:
1. COUNT
Counts the number of rows or non-NULL values in a column.
Syntax:
COUNT(column_name)
Ex
3. AVG
Calculates the average value of a numeric column.
Syntax:
AVG(column_name)
Ex
4. MAX
5. MIN
Returns the minimum value in a column.
Syntax:
MIN(column_name)
Ex
Notes:
Aggregate functions ignore NULL values unless explicitly handled.
You can combine multiple aggregate functions in a single query.
2. Numeric/Mathematical Functions
2. CEILING (number)
Description: Rounds a number up to the nearest integer.
Example:
3. FLOOR (number)
Description: Rounds a number down to the nearest integer.
Example:
6. SQRT (number)
Description: Returns the square root of a number
Example
7. LOG (number)
Description: Returns the natural logarithm (base e) of a number.
Example:
Windows Function
10 | P a g e Learning Document w w w. s i m d a a . co m
Windows functions in SQL are used for performing calculations across a set of rows that are
related to the current row. Common examples of window functions include ranking,
cumulative sums, running totals, and averages.
1. ROW_NUMBER()
Purpose: Assigns a unique number to each row within a partition, ordered by specific
criteria.
Example: Assign a row number to each employee in their department, ordered by salary
(highest first).
2. RANK()
11 | P a g e Learning Document w w w. s i m d a a . co m
Purpose: Assigns a rank to each row within a partition, with ties receiving the same rank and
skipping numbers.
Example: Rank employees in each department based on their salary.
3. DENSE_RANK()
Purpose: Similar to RANK() but does not skip ranks if there are ties.
Example: Rank employees in each department by salary without skipping ranks.
6.FIRST_VALUE() / LAST_VALUE()
12 | P a g e Learning Document w w w. s i m d a a . co m
Purpose: Fetches the first or last value in a partition based on ordering.
Example: Get the lowest and highest salaries in each department.
13 | P a g e Learning Document w w w. s i m d a a . co m