MAX function in SQL Server What is it usually used for? Join Quantrimang.com to learn everything you need to know about MAX SQL Server function okay!
IT is currently one of the most popular industries today. This is the “cradle” that produces many useful products, applications, and websites for the community, helping people's lives become more comfortable and interesting. The treasure of knowledge about IT is extremely rich and diverse. Once you decide to build a career in this field, you need to learn about programming languages and database administration.
Speaking of database administration, you definitely need to learn how to use SQL Server. This is a Microsoft product. It is used to help programmers easily store data according to RDBMS standards.
Learning SQL Server is not difficult. Similar to a programming language, SQL Server also has functions with different functions to make the coding and database building process easier.
Basically, SQL Server is a procedural language so it is not difficult to use. You just need to spend time learning and practicing to be able to use SQL Server fluently in a short time.
The MAX() function is one of the popular functions that you need to know when using SQL Server. Below is basic information about SQL Server's MAX() function.
Describe
MAX function in SQL Server is a type of aggregate function. It is used to get the maximum or largest value in a group of values of a specific column or expression. It is the most commonly used built-in function in SQL Server. This function accepts a single parameter, which can be a column or valid expression, and returns a result summarizing the input data set. The MAX statement in SQL Server can also work with WHERE, GROUP BY, ORDER BY, and HAVING clauses when you want to get filtered results.
Syntax
To use the MAX function in SQL Server, we use the following syntax:
SELECT MAX(cot)
FROM bang
[WHERE dieukien];
Parameter:
- cot: column or calculated value, the expression for which you want to find the maximum value
- state: table used to retrieve records. There must be at least 1 table in the FROM clause.
- dieukien: option. Conditions that a record must meet to be selected.
Note:
- The MAX function can be used in the following versions of SQL Server: SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005.
For example
Let's see and explore some examples of the MAX function in SQL Server.
Suppose, we have a data table as follows:
Example 1: Find the highest number of cards in the table
SELECT MAX(Sobai)
FROM Quantrimang
WHERE Sobai
Result: 94
In this example, we have the maximum number of articles in a category in the Quantrimang table that is less than 100.
Example 2: Using GROUP BY
The following example finds the largest value in all records related to a large category, you will do the following:
SELECT Chuyenmuclon, MAX(Sobai) AS "So bai nhieu nhat"
FROM Quantrimang
GROUP BY Chuyenmuclon;
Example 3: Using MAX() with Having clause
The Max() function can be used with the Having clause as shown in the example below. This is the GROUP By clause that groups employees by department, takes the highest salary of each department, then uses the Having clause to filter the results by only returning departments with salaries higher than 50000.
Sample code:
SELECT DepartmentId, Max(Salary) DeptMaxSal FROM Employee
GROUP BY DepartmentID
HAVING MAX (Salary) > 50000;
Previous article: FLOOR function in SQL Server
Next article: MIN function in SQL Server