How to use SQL max function with date and number

The max function

  • The max() function of SQL returns the maximum value in a given column.
  • You can use the Max function in the simple select statement.
  • You can also use the SQL Max aggregate function with the group by clause to return maximum values in consolidated data.
An example of Sum function

Syntax of using the max() function

The general syntax of using the Max SQL function is:

Select max(column_name) from table_name

Where col_name = value

Examples of using max function

Following examples show how to use the Max function. For the max() function, we will use our created table tbl_emp_salary_paid that contains the salaries of employees paid every month.

You can see it by clicking the demo link given under each example.

Example of using Max in simple select statement

This example returns the highest salary paid to any employee in the tbl_emp_salary_paid table. That means, the highest value from the emp_sal_paid column will be returned.

The SQL select with max statement:


See this example online with graphic

As you can see, the query returned maximum salary paid, which is a numeric column. Similarly, you can run the max function on the date column to return the max date.

A Group by clause example with Max function

This query will return maximum salary paid to each employee. This is accomplished by using the group by clause in the select statement with the SQL sum function.

The max with group by query:


See this example online with graphic

You can see two queries by clicking above link. One is the simple select statement to show all table data. The other query is using the max function of SQL with the group by clause.

 

Also see – The count function | The Where clause | Sum function

Was this article helpful?

Related Articles

Leave A Comment?