MAX (TSQL Function)

Returns the maximum value of a column.

Syntax

MAX ( expression )

Arguments

expression
is an expression of any type.

Return Types

Returns the type of expression.

Remarks

NULL values are ignored.

Examples

DECLARE @t TABLE (
    id         INT NOT NULL PRIMARY KEY,
    name       VARCHAR(30),
    birthdate  DATE
);

INSERT INTO @t VALUES (10, 'John',  '19700301'),
                      (20, 'Fred',  '20021123'),
                      (40, 'Alex',  '19950712'),
                      (50, 'Maria', '19700322'),
                      (30, 'Alice', '19950101'),
                      (15,  NULL,   '19950101');

SELECT YEAR(birthdate), MAX(id), MAX(name), MAX(birthdate), COUNT(*)
FROM @t
GROUP BY YEAR(birthdate)
ORDER BY YEAR(birthdate);

The result is:

$g$0       |$max$1     |$max$2                        |$max$3    |$count$4   |
-----------+-----------+------------------------------+----------+-----------+
       1970|         50|Maria                         |1970-03-22|          2|
       1995|         40|Alice                         |1995-07-12|          3|
       2002|         20|Fred                          |2002-11-23|          1|

(3 row(s) affected)
RSQL, a simple alternative to Microsoft SQL Server