SQL function usage encyclopedia

SQL function usage encyclopedia

In database management, SQL functions are a powerful tool. They can help us process data, perform complex calculations, and even change the way data is displayed. This article will introduce two main SQL functions: Aggregate function and Scalar function, as well as their usage methods and examples.

Introduction to SQL functions

SQL functions are predefined calculation tools used in SQL statements that operate on a column of data and return a single result. SQL functions are mainly divided into two categories: Aggregate functions and Scalar functions.

The aggregate function is a function that operates on a set of values ​​and returns a single value, such as sum, average, maximum, minimum, etc.

The Scalar function is a function that operates on a single value and returns a single value, such as converting data types, finding the length of a string, extracting the date part, etc.

SQL Aggregate function introduction

How to use AVG()

The AVG() function returns the average of a set of values. For example, if we want to query the average SAT score of students, we can use the following SQL statement:

SELECT AVG(sat_score) AS avg_score FROM students;

The query results may be as follows:

+-----------+
| avg_score |
+-----------+
| 1400      |
+-----------+

COUNT() usage

The COUNT() function returns the number of values ​​in a set. For example, if we want to query the number of students, we can use the following SQL statement:

SELECT COUNT(*) AS student_count FROM students;

The query results may be as follows:

+---------------+
| student_count |
+---------------+
| 9             |
+---------------+

How to use FIRST()

The FIRST() function returns the first value in a set of values. For example, if we want to query the name of the first student entered into the system, we can use the following SQL statement:

SELECT FIRST(FullName) AS first_student FROM students;

The query results may be as follows:

+---------------+
| first_student |
+---------------+
| Monique Davis |
+---------------+

How to use LAST()

The LAST() function returns the last value in a set of values. For example, if we want to query the name of the last student entered into the system, we can use the following SQL statement:

SELECT LAST(FullName) AS last_student FROM students;

The query results may be as follows:

+---------------+
| last_student  |
+---------------+
| Raymond F. Boyce |
+---------------+

How to use MAX()

The MAX() function returns the maximum value in a set of values. For example, if we want to query a student's highest SAT score, we can use the following SQL statement:

SELECT MAX(sat_score) AS max_score FROM students;

The query results may be as follows:

+-----------+
| max_score |
+-----------+
| 2400      |
+-----------+

How to use MIN()

The MIN() function returns the minimum value in a set of values. For example, if we want to query a student's minimum SAT score, we can use the following SQL statement:

SELECT MIN(sat_score) AS min_score FROM students;

The query results may be as follows:

+-----------+
| min_score |
+-----------+
| 400       |
+-----------+

How to use SUM()

The SUM() function returns the sum of a set of values. For example, if we want to query the total SAT scores of students, we can use the following SQL statement:

SELECT SUM(sat_score) AS total_score FROM students;

The query results may be as follows:

+-------------+
| total_score |
+-------------+
| 12600       |
+-------------+

Introduction to SQL Scalar functions

How to use UCASE()

The UCASE() function converts a string to uppercase. For example, if we want to convert a student's name to uppercase, we can use the following SQL statement:

SELECT UCASE(FullName) AS upper_name FROM students;

The query results may be as follows:

+------------------------+
| upper_name             |
+------------------------+
| MONIQUE DAVIS          |
| TERI GUTIERREZ         |
| SPENCER PAUTIER        |
| LOUIS RAMSEY           |
| ALVIN GREENE           |
| SOPHIE FREEMAN         |
| EDGAR FRANK "TED" CODD |
| DONALD D. CHAMBERLIN   |
| RAYMOND F. BOYCE       |
+------------------------+

How to use LCASE()

The LCASE() function converts a string to lowercase. For example, if we want to convert a student's name to lowercase, we can use the following SQL statement:

SELECT LCASE(FullName) AS lower_name FROM students;

The query results may be as follows:

+------------------------+
| lower_name             |
+------------------------+
| monique davis          |
| teri gutierrez         |
| spencer pautier        |
| louis ramsey           |
| alvin greene           |
| sophie freeman         |
| edgar frank "ted" codd |
| donald d. chamberlin   |
| raymond f. boyce       |
+------------------------+

How to use MID()

The MID() function is used to extract substrings from strings. For example, if we want to extract the first three characters of a student's name, we can use the following SQL statement:

SELECT MID(FullName, 1, 3) AS name_start FROM students;

The query results may be as follows:

+------------+
| name_start |
+------------+
| Mon        |
| Ter        |
| Spe        |
| Lou        |
| Alv        |
| Sop        |
| Edg        |
| Don        |
| Ray        |
+------------+

How to use SubString()

The SubString() function is also used to extract substrings from strings. For example, if we want to extract the second to fourth characters of a student's name, we can use the following SQL statement:

SELECT SubString(FullName, 2, 3) AS name_part FROM students;

The query results may be as follows:

+-----------+
| name_part |
+-----------+
| oni       |
| eri       |
| pen       |
| oui       |
| lvi       |
| oph       |
| dga       |
| ona       |
| aym       |
+-----------+

How to use LEN()

The LEN() function returns the length of the string. For example, if we want to query the length of a student's name, we can use the following SQL statement:

SELECT LEN(FullName) AS name_length FROM students;

The query results may be as follows:

+-------------+
| name_length |
+-------------+
| 13          |
| 14          |
| 15          |
| 12          |
| 12          |
| 14          |
| 24          |
| 20          |
| 17          |
+-------------+

How to use ROUND()

The ROUND() function is used to round numerical values. For example, if we wanted to round a student's SAT score to the nearest hundred, we could use the following SQL statement:

SELECT ROUND(sat_score, -2) AS rounded_score FROM students;

The query results may be as follows:

+---------------+
| rounded_score |
+---------------+
| 400           |
| 800           |
| 1000          |
| 1200          |
| 1200          |
| 1200          |
| 2400          |
| 2400          |
| 2400          |
+---------------+

How to use NOW()

The NOW() function returns the current date and time. For example, if we want to query the current date and time, we can use the following SQL statement:

SELECT NOW() AS current_time;

The query results may be as follows:

+---------------------+
| current_time        |
+---------------------+
| 2023-11-27 12:34:56 |
+---------------------+

FORMAT() usage

The FORMAT() function is used to format numeric values ​​or dates. For example, if we want to format a student's SAT score as a numerical value with two decimal places, we can use the following SQL statement:

SELECT FORMAT(sat_score, 2) AS formatted_score FROM students;

The query results may be as follows:

+-----------------+
| formatted_score |
+-----------------+
| 400.00          |
| 800.00          |
| 1,000.00        |
| 1,200.00        |
| 1,200.00        |
| 1,200.00        |
| 2,400.00        |
| 2,400.00        |
| 2,400.00        |
+-----------------+

Summarize

This article introduces the two main functions of SQL: Aggregate function and Scalar function, as well as their usage and examples. These functions are very useful in database management and can help us process data, perform complex calculations, and even change the way data is displayed. I hope that through this article, you can better understand and use these SQL functions.

Guess you like

Origin blog.csdn.net/heihaozi/article/details/134643877