MAXVALUE

MAXVALUE in SQL is part of the sequence generator functionality. It sets the upper limit of possible numbers that a sequence can generate. A sequence stops producing new numbers when this limit has been reached, unless the sequence cycles.

Example

CREATE TABLE employees (
id INT AUTO_INCREMENT,
name VARCHAR(255),
birth_date DATE,
PRIMARY KEY (id)
) AUTO_INCREMENT = 1 MAXVALUE 99999;

Output

MySQL doesn’t provide direct output for MAXVALUE used in the above way. The MAXVALUE 99999 here just limits the id field to not exceed it.

Explanation

The MAXVALUE keyword in SQL specifies the maximum value a sequence of numbers can have. When we specify MAXVALUE for AUTO_INCREMENT in a MySQL CREATE TABLE statement, MySQL will stop generating new numbers for the id column once it reaches the specified MAXVALUE.

Example

CREATE TABLE example_table (
id SERIAL PRIMARY KEY,
value INTEGER
);
INSERT INTO example_table (value)
VALUES (10),
(20),
(15),
(30),
(25);
SELECT MAX(value) AS max_value FROM example_table;

Output

max_value
-----------
30

Explanation

In the given SQL code, we created a table example_table with id and value as columns. The id column is a serial type, meaning a unique number gets generated for each new record. We then inserted 5 rows in this table with different values for the value column. Finally, we used the MAX() function to retrieve the maximum value from the value column. The function returned 30 which is the maximum value among all values in the value column.

Example

SELECT MAX(salary) as max_salary
FROM employees;

Output

+-------------+
| max_salary |
+-------------+
| 12500 |
+-------------+

Explanation

MAX(salary) is an aggregate function that finds the maximum salary among all employees in the employees table. The column is aliased as max_salary for a clearer output display.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.