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

SQL_Function_Types_with_Examples

The document outlines various types of SQL functions, categorized into aggregate, scalar, date, conversion, window, system, and user-defined functions. Examples for each function type are provided, demonstrating their usage in SQL queries. Key functions include COUNT, SUM, UPPER, ABS, NOW, CAST, ROW_NUMBER, USER, and a custom user-defined function for adding numbers.

Uploaded by

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

SQL_Function_Types_with_Examples

The document outlines various types of SQL functions, categorized into aggregate, scalar, date, conversion, window, system, and user-defined functions. Examples for each function type are provided, demonstrating their usage in SQL queries. Key functions include COUNT, SUM, UPPER, ABS, NOW, CAST, ROW_NUMBER, USER, and a custom user-defined function for adding numbers.

Uploaded by

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

Types of SQL Functions with Examples

1. Aggregate Functions

- COUNT(): SELECT COUNT(*) FROM employees;

- SUM(): SELECT SUM(salary) FROM employees;

- AVG(): SELECT AVG(salary) FROM employees;

- MIN(): SELECT MIN(salary) FROM employees;

- MAX(): SELECT MAX(salary) FROM employees;

2. Scalar Functions

a. String Functions

- UPPER(): SELECT UPPER(name) FROM employees;

- LOWER(): SELECT LOWER(name) FROM employees;

- CONCAT(): SELECT CONCAT(first_name, ' ', last_name) FROM employees;

- SUBSTRING(): SELECT SUBSTRING(name, 1, 3) FROM employees;

- LENGTH(): SELECT LENGTH(name) FROM employees;

b. Numeric Functions

- ABS(): SELECT ABS(-15);

- ROUND(): SELECT ROUND(salary, 2) FROM employees;

- CEIL(): SELECT CEIL(4.3);

- FLOOR(): SELECT FLOOR(4.7);

- MOD(): SELECT MOD(10, 3);

c. Date/Time Functions

- NOW(): SELECT NOW();

- DATEADD(): SELECT DATEADD(day, 7, hire_date);

- DATEDIFF(): SELECT DATEDIFF(day, hire_date, GETDATE());

- EXTRACT(): SELECT EXTRACT(YEAR FROM hire_date);


Types of SQL Functions with Examples

3. Date Functions

- CURRENT_DATE: SELECT CURRENT_DATE;

- CURRENT_TIME: SELECT CURRENT_TIME;

- MONTH(): SELECT MONTH(hire_date);

- YEAR(): SELECT YEAR(hire_date);

- DAY(): SELECT DAY(hire_date);

4. Conversion Functions

- CAST(): SELECT CAST(salary AS VARCHAR);

- CONVERT(): SELECT CONVERT(VARCHAR, salary);

5. Window (Analytic) Functions

- ROW_NUMBER(): SELECT ROW_NUMBER() OVER (ORDER BY salary DESC) FROM employees;

- RANK(): SELECT RANK() OVER (ORDER BY salary DESC) FROM employees;

- LEAD(): SELECT LEAD(salary, 1) OVER (ORDER BY hire_date) FROM employees;

- LAG(): SELECT LAG(salary, 1) OVER (ORDER BY hire_date) FROM employees;

6. System Functions

- USER(): SELECT USER();

- SESSION_USER(): SELECT SESSION_USER();

- DATABASE(): SELECT DATABASE();

- VERSION(): SELECT VERSION();

7. User-Defined Functions (UDFs)


Types of SQL Functions with Examples

Custom functions created by users using SQL or procedural extensions like PL/pgSQL or T-SQL.

Example (in PostgreSQL):

CREATE FUNCTION add_numbers(a INT, b INT) RETURNS INT AS $$

BEGIN

RETURN a + b;

END;

$$ LANGUAGE plpgsql;

You might also like