0% found this document useful (0 votes)
3 views

Functions

SQL functions are blocks of code that perform specific tasks and return values, categorized into predefined and user-defined functions. Predefined functions include string, aggregate, and mathematical functions, while user-defined functions allow for custom logic. Aggregate functions summarize data and are often used with GROUP BY and HAVING clauses for filtering results.

Uploaded by

vivek4chougule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Functions

SQL functions are blocks of code that perform specific tasks and return values, categorized into predefined and user-defined functions. Predefined functions include string, aggregate, and mathematical functions, while user-defined functions allow for custom logic. Aggregate functions summarize data and are often used with GROUP BY and HAVING clauses for filtering results.

Uploaded by

vivek4chougule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Functions in SQL

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.

There are two types of function-


1. Predefined function
2. User defined function.

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.

A User-Defined Function (UDF) in SQL is a function created by a user to perform a specific


task. Unlike system-defined functions, UDFs allow you to define custom logic and
computations tailored to your requirements.

Types of Predefined functions


1. String Function
2. Aggregate Function
3. Windows Function
4. Rank Function
5. Mathematical Function

1. String Function –
In SQL, string functions are used to manipulate and process string data. Here are some
commonly used string functions with examples:

1|Page Learning Document w w w. s i m d a a . co m


1. CONCAT
Combines two or more strings into one.
Syntax:
CONCAT(string1, string2, ...)
Ex-

2. LENGTH
Returns the length of a string.
Syntax:
LEN(string)
Ex

3. LOWER
Converts a string to lowercase.
Syntax:
LOWER(string)
Ex

2|Page Learning Document w w w. s i m d a a . co m


4. UPPER
Converts a string to uppercase.
Syntax:
UPPER(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

3|Page Learning Document w w w. s i m d a a . co m


7. CHARINDEX
Finds the position of a substring in a string.
Syntax:
CHARINDEX(substring, 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

4|Page Learning Document w w w. s i m d a a . co m


Formats a string or number (specific to some databases like SQL Server)
Syntax:
FORMAT(value, format)
Ex

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

5|Page Learning Document w w w. s i m d a a . co m


2. SUM
Calculates the total sum of a numeric column.
Syntax:
SUM(column_name)
Ex

3. AVG
Calculates the average value of a numeric column.
Syntax:
AVG(column_name)
Ex

4. MAX

6|Page Learning Document w w w. s i m d a a . co m


Returns the maximum value in a column.
Syntax:
MAX(column_name)
Ex

5. MIN
Returns the minimum value in a column.
Syntax:
MIN(column_name)
Ex

6. GROUP BY with Aggregate Functions


Aggregate functions are often used with the GROUP BY clause to perform calculations for
specific groups.

7. HAVING Clause with Aggregate Functions

7|Page Learning Document w w w. s i m d a a . co m


The HAVING clause is used to filter results based on aggregate function conditions.
Example:

Notes:
 Aggregate functions ignore NULL values unless explicitly handled.
 You can combine multiple aggregate functions in a single query.

2. Numeric/Mathematical Functions

8|Page Learning Document w w w. s i m d a a . co m


Used to perform mathematical operations.
1. ABS (number)
 Description: Returns the absolute value of a number (removes the negative sign, if
any).
 Example:

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:

4. ROUND (number, decimals)


 Description: Rounds a number to a specified number of decimal places.
 Example:

5. POWER (base, exponent)

9|Page Learning Document w w w. s i m d a a . co m


 Description: Raises a base number to the power of an exponent.
 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:

8. MOD (number1, number2)


 Description: Returns the remainder of a division.
 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

You might also like