An Introduction to SQL Functions [Slides]
An Introduction to SQL Functions [Slides]
|
SQL functions are built-in operations that can be used to perform various calculations,
manipulations, or transformations on data within an SQL database. Advantages of using
these functions include:
01.
01. Efficiency 04.
01. Extensive functionality
They take advantage of internal algorithms and data Database management systems provide a wide range
structures, which results in faster query execution times of built-in functions that allow for complex calculations,
and improved overall performance. string manipulations, and date and time operations.
02.
01. Reliability and compatibility 05.
01. Portability
They adhere to the SQL standards and are implemented SQL queries use standard functions; therefore, they can
consistently across different database platforms. be easily migrated or executed on different database
systems without significant modifications.
03.
01. Documentation and built-in support 06. Ease of use
They come with extensive documentation provided by They are readily available for use without any additional
the database management system vendors, making configuration. This saves you from reinventing the wheel
them easier for developers to understand and use by implementing common operations from scratch.
effectively.
2
Introduction to SQL functions
| The output of a function in SQL is treated as a derived column and appears in the SELECT
clause of a query. The general syntax of an SQL function is:
3
Introduction to SQL functions
Function types
123 Numeric
These functions perform calculations on a set of values in a
abc column and return a numerical value.
SUM(), AVG(), COUNT(), MAX(), MIN(), POWER(),
SQRT(), ROUND()
String
These functions operate on string values and perform
operations such as concatenation, manipulation, and text
formatting.
CONCAT(), LENGTH(), SUBSTRING(), UPPER(), LOWER()
4
Introduction to SQL functions
Function types
Datetime
These are functions used to handle date and time values and
perform operations like formatting, extraction, and
manipulation.
123 CURRENT_DATE(), DATEADD(), FORMAT()
IF()
Miscellaneous
This category represents a variety of functions that do a
Many functions do not fit this classification! For
example, MIN() works with strings, numbers, and dates. variety of things such as converting data types and dealing
Focus on understanding how each function works, rather with NULL values.
than classifying them. CONVERT(), CAST(), NULLIF(), IFNULL()
5
Introduction to SQL functions
| SQL functions vary in behaviour. Aggregate functions summarise data at a column level.
Scalar functions manipulate data at the row level.
Patient_id Born FLOOR(Born) Scalar functions take a row and return a single value for
each row, e.g. UPPER(), LOWER(), CONVERT(), CAST(),
1 1969 1960
IF(), FORMAT() and arithmetic operators () +, -, *, /
3 1972 1970
Example:
7 1954 1950
FLOOR(Born) rounds the values in each row down to the
8 2004 2000 nearest integer. Each row has a new decade column with the
output of FLOOR().
Example data
To find the total amount of income expenditure in the Free State province in South Africa, we are going to
use the South African Household Income and Expenditure Survey dataset (SAHIES).
7
Introduction to SQL functions
| SUM() is an aggregate function that returns the total sum of a numeric column.
Free_state
24799
SELECT
SUM(Free_state) AS Total_free_state SUM 1255 = 38037
Query
` FROM
Sahies.Income_expenditure_2020; 11983
8
Introduction to SQL functions
| We can aggregate multiple columns or the same column using a different function.
Suppose we want to calculate the total income spent for the Western and Free State provinces and the average spent for the
Free State province.
9
Introduction to SQL functions
| Since aggregate functions produce a single row, we cannot use them with the columns in
our main table.
SELECT
Free_state has 3 rows,
Free_state, and AVG(Free_state)
AVG(Free_state) has 1, so running this
Query FROM query results in an
Sahies.Income_expenditure_2020; error.
Output Error!
10
Introduction to SQL functions
| Suppose we want to calculate the difference in income expenditure between the Free
State and the Northern Cape province.
Expenditure_group,
24799
- 20000
(Free_state
Query -
- Northern_cape) AS Diff_fs_and_nc 1255 989
FROM -
Sahies.Income_expenditure_2020; 11983 15406
Expenditure_group Diff_fs_and_nc
Note how this name is slightly harder to
Housing 4799 understand because we used abbreviations.
11
Introduction to SQL functions
|
We can use functions within functions, known as nesting. SQL evaluates the innermost function
first and then works its way outwards. The result of each inner function is used as the input for the
outer function.
12
Introduction to SQL functions
| Some SQL built-in functions do not require any arguments. They are used to perform
calculations or retrieve information.
CURRENT_DATE()
Returns the current date. Output:
CURRENT_DATE()
SELECT
Query: 2023-06-20
CURRENT_DATE();
Output:
RAND()
Generates a random number between 0 and 1. Limpopo Random_number
23599 0.81247…
SELECT
Query: Free_state, 912 0.01824…
13