Home » SQL » SQL Functions
Many built-in functions are provided by SQL to perform operations on data. These built-in functions are handy when performing mathematical calculations, string concatenations, sub-strings, and so on. There are two categories of SQL functions
The aggregate functions return a single value after performing calculations on a group of values. Below are some of the frequently used Aggregrate functions.
The Average function returns an average value after calculating it from values in a numeric column.
This is how its syntax looks:
SELECT AVG(column_name) FROM table_nameLet’s consider the following Emp table
| eid | name | age | salary |
|---|---|---|---|
| 401 | Anu | 22 | 9000 |
| 402 | Shane | 29 | 8000 |
| 403 | Rohan | 34 | 6000 |
| 404 | Scott | 44 | 10000 |
| 405 | Tiger | 35 | 8000 |
Also, the SQL query to find the average salary will be,
SELECT avg(salary) from Emp;This table shows the result of the above query:
| avg(salary) |
|---|
| 8200 |
In this case, the count function returns the number of rows present in the table either based on some condition or without condition.
This is how its syntax looks:
SELECT COUNT(column_name) FROM table-nameLet’s consider the Emp table below:
| eid | name | age | salary |
|---|---|---|---|
| 401 | Anu | 22 | 9000 |
| 402 | Shane | 29 | 8000 |
| 403 | Rohan | 34 | 6000 |
| 404 | Scott | 44 | 10000 |
| 405 | Tiger | 35 | 8000 |
Also, the SQL query to count employees, that will satisfy the specified condition is given as:
SELECT COUNT(name) FROM Emp WHERE salary = 8000;This table shows the result of the above query:
| count(name) |
|---|
| 2 |
Let’s consider the Emp table below:
| eid | name | age | salary |
|---|---|---|---|
| 401 | Anu | 22 | 9000 |
| 402 | Shane | 29 | 8000 |
| 403 | Rohan | 34 | 6000 |
| 404 | Scott | 44 | 10000 |
| 405 | Tiger | 35 | 8000 |
SQL query is,
SELECT COUNT(DISTINCT salary) FROM emp;
This table shows the result of the above query:
| count(distinct salary) |
|---|
| 4 |
This function is typically used to return the first value of a selected column
This is how its syntax looks:
SELECT FIRST(column_name) FROM table-name;
Let’s consider the Emp table below:
| eid | name | age | salary |
|---|---|---|---|
| 401 | Anu | 22 | 9000 |
| 402 | Shane | 29 | 8000 |
| 403 | Rohan | 34 | 6000 |
| 404 | Scott | 44 | 10000 |
| 405 | Tiger | 35 | 8000 |
SQL query will be,
SELECT FIRST(salary) FROM Emp;
This table shows the result of the above query:
| first(salary) |
|---|
| 9000 |
The LAST function is used to return the return last value of the selected column.
This is how its syntax looks:
SELECT LAST(column_name) FROM table-name;
Let’s consider the Emp table below:
| eid | name | age | salary |
|---|---|---|---|
| 401 | Anu | 22 | 9000 |
| 402 | Shane | 29 | 8000 |
| 403 | Rohan | 34 | 6000 |
| 404 | Scott | 44 | 10000 |
| 405 | Tiger | 35 | 8000 |
SQL query will be,
SELECT LAST(salary) FROM emp;
Result of the above query will be,
| last(salary) |
|---|
| 8000 |
The MAX function is used to return the maximum value from the selected column of the table.
This is how its syntax looks:
SELECT MAX(column_name) from table-name;
Consider the following Emp table
| eid | name | age | salary |
|---|---|---|---|
| 401 | Anu | 22 | 9000 |
| 402 | Shane | 29 | 8000 |
| 403 | Rohan | 34 | 6000 |
| 404 | Scott | 44 | 10000 |
| 405 | Tiger | 35 | 8000 |
SQL query to find the Maximum salary will be,
SELECT MAX(salary) FROM emp;
Result of the above query will be,
| MAX(salary) |
|---|
| 10000 |
The MIN function when used returns the minimum value from a selected column of the table.
This is how its syntax looks:
SELECT MIN(column_name) from table-name;
Consider the following Emp table,
| eid | name | age | salary |
|---|---|---|---|
| 401 | Anu | 22 | 9000 |
| 402 | Shane | 29 | 8000 |
| 403 | Rohan | 34 | 6000 |
| 404 | Scott | 44 | 10000 |
| 405 | Tiger | 35 | 8000 |
SQL query to find minimum salary is,
SELECT MIN(salary) FROM emp;
Result will be,
| MIN(salary) |
|---|
| 6000 |
The SUM function is typically used to return the total sum of selected columns' numeric values.
This is how its syntax looks:
SELECT SUM(column_name) from table-name;
Consider the following Emp table
| eid | name | age | salary |
|---|---|---|---|
| 401 | Anu | 22 | 9000 |
| 402 | Shane | 29 | 8000 |
| 403 | Rohan | 34 | 6000 |
| 404 | Scott | 44 | 10000 |
| 405 | Tiger | 35 | 8000 |
SQL query to find sum of salaries will be,
SELECT SUM(salary) FROM emp;
Result of above query is,
| SUM(salary) |
|---|
| 41000 |
The Scalar functions will return a single value from an input value. Below are some frequently used SQL Scalar Functions.
It is used to convert the value of a string column to Uppercase characters.
This is how its syntax looks:
SELECT UCASE(column_name) from table-name;
Consider the following Emp table
| eid | name | age | salary |
|---|---|---|---|
| 401 | anu | 22 | 9000 |
| 402 | shane | 29 | 8000 |
| 403 | rohan | 34 | 6000 |
| 404 | scott | 44 | 10000 |
| 405 | Tiger | 35 | 8000 |
SQL query for using UCASE is,
SELECT UCASE(name) FROM emp;
Result is,
| UCASE(name) |
|---|
| ANU |
| SHANE |
| ROHAN |
| SCOTT |
| TIGER |
The LCASE function can be used to convert the value of string columns to Lowercase characters.
This is how its syntax looks:
SELECT LCASE(column_name) FROM table-name;
Consider the following Emp table
| eid | name | age | salary |
|---|---|---|---|
| 401 | Anu | 22 | 9000 |
| 402 | Shane | 29 | 8000 |
| 403 | Rohan | 34 | 6000 |
| 404 | SCOTT | 44 | 10000 |
| 405 | Tiger | 35 | 8000 |
SQL query for converting string value to Lower case is,
SELECT LCASE(name) FROM emp;
Result will be,
| LCASE(name) |
|---|
| anu |
| shane |
| rohan |
| scott |
| tiger |
The MID function is typically used to extract substrings from column values of string type in a table.
This is how its syntax looks:
SELECT MID(column_name, start, length) from table-name;
Consider the following Emp table
| eid | name | age | salary |
|---|---|---|---|
| 401 | anu | 22 | 9000 |
| 402 | shane | 29 | 8000 |
| 403 | rohan | 34 | 6000 |
| 404 | scott | 44 | 10000 |
| 405 | Tiger | 35 | 8000 |
SQL query will be,
SELECT MID(name,2,2) FROM emp;
Result will come out to be,
| MID(name,2,2) |
|---|
| nu |
| ha |
| oh |
| co |
| ig |
The ROUND function is typically used to round a numeric field to a number of nearest integers and is used on decimal point values.
This is how its syntax looks:
SELECT ROUND(column_name, decimals) from table-name;
Consider the following Emp table
| eid | name | age | salary |
|---|---|---|---|
| 401 | anu | 22 | 9000.67 |
| 402 | shane | 29 | 8000.98 |
| 403 | rohan | 34 | 6000.45 |
| 404 | scott | 44 | 10000 |
| 405 | Tiger | 35 | 8000.01 |
SQL query is,
SELECT ROUND(salary) from emp;
Result will be,
| ROUND(salary) |
|---|
| 9001 |
| 8001 |
| 6000 |
| 10000 |
| 8000 |
