Transact SQL
What’s T-SQL?
• T-SQL is a query lsnguage specific to SQL server
• Mostly performs operations like retrieving data from a single row, insert new row and
retrieving multiple rows
• T-SQL is a procedural language used by Microsoft in SQL Server. It adds declared
variables, transaction control, error and exception handling, and row processing to SQL.
Features of T-SQL
• T-SQL has a BULK INSERT statement that allows users to import a file into the
database table or view in a user-specified format.
• T-SQL provides various support functions for string processing and data processing
Types of functions in T-SQL
• Aggregate Functions: It operates on a collection of values but returns a single value.
• Ranking Function: It returns a ranking value for each partitioning row.
• Scalar Functions: It operates on a single value and returns a single value.
Aggregate Function
• This function returns one value after calculating multiple values of a columns
There are 5 types of SQL aggregate functions:
• Count()
• Sum()
• Avg()
• Min()
• Max()
Why use Aggregate functions?
• They allow us to perform calculations on large data sets quickly and efficiently.
• These functions generate statistical reports, perform financial analysis, and manage
inventory levels.
• In some cases, using aggregate functions can lead to more efficient query execution
compared to retrieving all individual rows and performing calculations in application
code.
Examples (AVG function)
Rank function
• The RANK() function allocates a rank, that is, an integer number to each row within a
group of data sets. The RANK() function is also known as the window function
• Three questions to look at before ranking
• Rank what?
• Within what group?
• Rank by what?
Example
Scalar Function
• Scalar functions are the built-in functions in SQL, and whatever be the input provided to
the scalar functions, the output returned by these functions will always be a single value.
• commonly used scalar functions in SQL
1. UCASE()
2. LCASE()
3. MID()
4. LENGTH()
5. ROUND()
6. NOW()
7. FORMAT()
Scalar Function Description
Example