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

SQL Functions

The document provides an overview of SQL functions, which are built-in operations in SQL that perform specific tasks on data. It categorizes SQL functions into ten types, including Aggregate, Scalar, Date and Time, Mathematical, String, Conversion, Window, System, Control Flow, and JSON functions, each with explanations and examples. The document serves as a comprehensive guide for understanding and utilizing various SQL functions effectively.

Uploaded by

paulozalonso
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL Functions

The document provides an overview of SQL functions, which are built-in operations in SQL that perform specific tasks on data. It categorizes SQL functions into ten types, including Aggregate, Scalar, Date and Time, Mathematical, String, Conversion, Window, System, Control Flow, and JSON functions, each with explanations and examples. The document serves as a comprehensive guide for understanding and utilizing various SQL functions effectively.

Uploaded by

paulozalonso
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

SQL Functions

1.What is SQL Functions?


2. Types of SQL Functions
1. Aggregate Functions
2. Scalar Functions
3. Date and Time Functions
4. Mathematical Functions
5. String Functions
6. Conversion Functions
7. Window Functions
8. System Functions
9. Control Flow Functions
10. JSON Functions

3. SQL Function Types Explanation with Examples

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


SQL Functions
SQL functions are built-in operations in SQL (Structured
Query Language) that perform a specific task or
calculation on data.

They can be categorized into several types, based on their


usage and the kind of results they produce.

Types of SQL Functions:


1. Aggregate Functions

2. Scalar Functions

3. Date and Time Functions

4. Mathematical Functions

5. String Functions

6. Conversion Functions

7. Window Functions

8. System Functions

9. Control Flow Functions

10. JSON Functions

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


1. Aggregate Functions
These functions perform operations on a set of values and
return a single value, often used with the GROUP BY clause.

COUNT()
SUM()
AVG()
MAX()
MIN()

1.COUNT( ): Counting Rows Output:

2. Sum( ): Summing Values Output:

3. AVG( ): Calculating Average


Output:

4. MAX( ): Finding Maximum Value Output:

5. MIN( ): Finding Minimum Value


Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


2. Scalar Functions
These functions return a single value based on the input
values. They can be used to manipulate individual values like
strings, numbers, or dates.

String Functions: UPPER(), LOWER(), CONCAT(), LENGTH()


Numeric Functions: ABS(), ROUND(), CEIL(), FLOOR()
Date and Time Functions: NOW(), CURDATE(), YEAR(),
DATEDIFF()

1. UPPER(): Convert to Uppercase Output:

2. LOWER(): Convert to Lowercase Output:

3. LENGTH(): Calculate String Length Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


4. ROUND(): Round a Numeric Value Output:

5. DATE(): Extract Date Output:

6. CONCAT(): Concatenate Strings Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


3. Date and Time Functions
Functions that are used to manipulate or retrieve date and
time values.

NOW()
CURDATE()
YEAR()
DATE()
DATEDIFF()

1. NOW(): Current Date and Time Output:

Output:
2. CURDATE(): Current Date

3. YEAR(): Extract Year


Output:

4. DATE(): Extract Date from DateTime Output:

5. DATEDIFF(): Difference Between Two Dates Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


4. Mathematical Functions
Functions that perform mathematical calculations on numeric
values.

ROUND()
CEIL()
FLOOR()
ABS()
POWER()
SQRT()
1. ROUND(): Round to Nearest Integer or Decimal Place Output:

2. CEIL(): Round Up to the Nearest Integer Output:

3. FLOOR(): Round Down to the Nearest Integer Output:

4. ABS(): Absolute Value Output:

5. POWER(): Raise a Value to a Power Output:

6. SQRT(): Square Root Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


5. String Functions
Functions used for manipulating string values.

CONCAT()
UPPER()
LOWER()
SUBSTRING()
REPLACE()
LENGTH()
TRIM()

1. CONCAT(): Concatenate Strings Output:

2. UPPER(): Convert to Uppercase Output:

3. LOWER(): Convert to Lowercase Output:

4. SUBSTRING(): Extract Part of a String Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


5. REPLACE(): Replace a Substring
Output:

6. LENGTH(): Get the Length of a String


Output:

7. TRIM(): Remove Leading and Trailing Spaces Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


6. Conversion Functions
Functions that convert a value from one data type to another.

CAST()
CONVERT()

1. CAST(): Convert One Data Type to Another Output:

2. CONVERT(): Convert Data Type (SQL Server


Output:
Equivalent of CAST())

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


7. Window Functions
Functions that perform calculations across a set of rows
related to the current row, without collapsing the result set.

ROW_NUMBER()
RANK()
DENSE_RANK()
LEAD()
LAG()

1. ROW_NUMBER(): Assigns a unique row number to each row within a partition.

Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


2. RANK(): Provides a rank for each row within a partition, with gaps in the
ranking when there are ties.

Output:

3. DENSE_RANK(): Similar to RANK(), but without gaps in the ranking.

Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


4. LEAD(): Accesses data from the next row in the result set.

Output:

5. LAG(): Accesses data from the previous row in the result set.

Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


8. System Functions
These functions return information about the database or the
environment.

USER()
DATABASE()
VERSION()
Scope _IDENTITY( )

1. USER(): Returns the name of the current user. Output:

2. DB_NAME(): Returns the name of the current database. Output:

3. VERSION: Returns the current version of SQL Server. Output:

4. SCOPE_IDENTITY(): Returns the last identity value


generated within the current scope. Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


9. Control Flow Functions
These functions are used to evaluate conditions and control
the flow of SQL queries.

IF()
CASE
COALESCE()
NULLIF()

1. IF: Returns one value if a condition is true and another value if it's false
(primarily in MySQL).

Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


2. CASE: Evaluates conditions and returns a value based on the first condition
that is true.

Output:

3. COALESCE: Returns the first non-NULL value in a list of expressions.

Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


4. NULLIF: Returns NULL if the two expressions are equal; otherwise, it returns
the first expression.

Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


10. JSON Functions (available in certain databases like
MySQL and PostgreSQL)
Functions used for handling JSON data.

JSON_OBJECT()
JSON_ARRAY()
JSON_EXTRACT()
JSON_VALUE()

1. JSON_OBJECT() (MySQL/PostgreSQL): Generates a JSON object from key-value


pairs.

Output:

2. JSON_ARRAY() (MySQL/PostgreSQL): Creates a JSON array from a list of


values.

Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


3. JSON_EXTRACT() (MySQL/PostgreSQL): Extracts data from a JSON
document.

Output:

4. JSON_VALUE(): Extracts a scalar value from a JSON string.

Output:

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K


Thank You for Reading and
Engaging with our Content

If You Found this Post Helpful !!!

FOLLOW US FOR MORE UPDATES AND INSIGHTS

Group Link : https://www.linkedin.com/groups/14386110

Let’s Grow and Learn Together

www.hopelearning.net Single Tutor | Learn without Confusions Ramisha Rani K

You might also like