SQL functions are built-in functions provided by SQL databases to perform operations on data.
These
functions can be categorized into several types, including aggregate functions, scalar functions, and
window functions. Here's a detailed look at these SQL functions:
### 1. Aggregate Functions
Aggregate functions perform a calculation on a set of values and return a single value. They are
commonly used with the `GROUP BY` clause.
- **AVG()**: Returns the average value of a numeric column.
```sql
SELECT AVG(salary) FROM employees;
```
- **COUNT()**: Returns the number of rows that match a specified condition.
```sql
SELECT COUNT(*) FROM employees;
```
- **MAX()**: Returns the maximum value in a set of values.
```sql
SELECT MAX(salary) FROM employees;
```
- **MIN()**: Returns the minimum value in a set of values.
```sql
SELECT MIN(salary) FROM employees;
```
- **SUM()**: Returns the sum of a numeric column.
```sql
SELECT SUM(salary) FROM employees;
```
### 2. Scalar Functions
Scalar functions operate on individual values and return a single value.
- **UCASE()/UPPER()**: Converts a string to upper-case.
```sql
SELECT UCASE(name) FROM employees;
```
- **LCASE()/LOWER()**: Converts a string to lower-case.
```sql
SELECT LCASE(name) FROM employees;
```
- **LENGTH()**: Returns the length of a string.
```sql
SELECT LENGTH(name) FROM employees;
```
- **SUBSTRING()/SUBSTR()**: Extracts a substring from a string.
```sql
SELECT SUBSTRING(name, 1, 3) FROM employees;
```
- **REPLACE()**: Replaces occurrences of a specified substring.
```sql
SELECT REPLACE(name, 'John', 'Jon') FROM employees;
```
- **ROUND()**: Rounds a numeric value to the nearest integer or to a specified number of decimal
places.
```sql
SELECT ROUND(salary, 2) FROM employees;
```
- **NOW()**: Returns the current date and time.
```sql
SELECT NOW();
```
- **DATEDIFF()**: Returns the difference in days between two dates.
```sql
SELECT DATEDIFF('2024-06-15', '2024-06-10') AS DiffDate;
```
- **CONCAT()**: Concatenates two or more strings.
```sql
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
```
### 3. Window Functions
Window functions perform calculations across a set of table rows related to the current row. They
are used with the `OVER` clause.
- **ROW_NUMBER()**: Assigns a unique sequential integer to rows within a partition.
```sql
SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num FROM
employees;
```
- **RANK()**: Assigns a rank to rows within a partition, with gaps in rank values when there are ties.
```sql
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS rank FROM employees;
```
- **DENSE_RANK()**: Assigns a rank to rows within a partition, without gaps in rank values when
there are ties.
```sql
SELECT name, salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_rank FROM
employees;
```
- **NTILE()**: Distributes rows of an ordered partition into a specified number of groups.
```sql
SELECT name, salary, NTILE(4) OVER (ORDER BY salary DESC) AS ntile FROM employees;
```
- **LEAD()**: Provides access to a row at a specified physical offset following the current row.
```sql
SELECT name, salary, LEAD(salary, 1) OVER (ORDER BY salary DESC) AS next_salary FROM
employees;
```
- **LAG()**: Provides access to a row at a specified physical offset preceding the current row.
```sql
SELECT name, salary, LAG(salary, 1) OVER (ORDER BY salary DESC) AS previous_salary FROM
employees;
```
- **FIRST_VALUE()**: Returns the first value in an ordered set of values.
```sql
SELECT name, salary, FIRST_VALUE(salary) OVER (ORDER BY salary DESC) AS highest_salary FROM
employees;
```
- **LAST_VALUE()**: Returns the last value in an ordered set of values.
```sql
SELECT name, salary, LAST_VALUE(salary) OVER (ORDER BY salary DESC) AS lowest_salary FROM
employees;
```
### Examples
**Using Aggregate Functions:**
```sql
SELECT department, COUNT(*) AS num_employees, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
```
**Using Scalar Functions:**
```sql
SELECT name, UPPER(name) AS uppercase_name, LENGTH(name) AS name_length
FROM employees;
```
**Using Window Functions:**
```sql
SELECT name, department, salary,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS department_rank
FROM employees;
```
These functions enhance the capabilities of SQL by allowing you to perform complex calculations and
data transformations directly within your queries. Understanding and using these functions
effectively can significantly improve your ability to manipulate and analyze data in SQL.