SQL - MAX Function
SQL - MAX Function
Description
The SQL MAX function is used to return the maximum value of an expression in a SELECT
statement.
Syntax
The syntax for the MAX function in SQL is:
SELECT MAX(aggregate_expression)
FROM tables
[WHERE conditions];
OR the syntax for the MAX function when grouping the results by one or more columns is:
MAX(aggregate_expression)
FROM tables
[WHERE conditions]
Parameters or Arguments
expression1, expression2, ... expression_n
Expressions that are not encapsulated within the MAX function and must be included in the
GROUP BY clause at the end of the SQL statement.
aggregate_expression
This is the column or expression from which the maximum value will be returned.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the
FROM clause.
WHERE conditions
Optional. These are conditions that must be met for the records to be selected.
https://www.techonthenet.com/sql/max.php 1/5
6/19/2021 SQL: MAX Function
For example, you might wish to know the maximum salary of all employees.
FROM employees;
In this SQL MAX function example, we've aliased the MAX(salary) field as "Highest salary". As a
result, "Highest salary" will display as the field name when the result set is returned.
For example, you could also use the SQL MAX function to return the name of each department
and the maximum salary in the department.
FROM employees
GROUP BY department;
Because you have listed one column in your SQL SELECT statement that is not encapsulated in
the MAX function, you must use the SQL GROUP BY clause. The department field must,
therefore, be listed in the GROUP BY section.
Each time a report is run in Oracle, a record is written to this table noting the above info. What I
am trying to do is pull from this table when the last time each distinct report was run and who ran it
last.
My initial query:
FROM report_history
GROUP BY report_name
runs fine. However, it does not provide the name of the user who ran the report.
https://www.techonthenet.com/sql/max.php 2/5
6/19/2021 SQL: MAX Function
Adding user_name to both the select list and to the group by clause returns multiple lines for each
report; the results show the last time each person ran each report in question. (i.e. User1 ran
Report 1 on 01-JUL-03, User2 ran Report1 on 01-AUG-03). I don't want that....I just want to know
who ran a particular report the last time it was run.
Any suggestions?
Answer: This is where things get a bit complicated. The SQL SELECT statement below will
return the results that you want:
FROM report_history
First, we've aliased the first instance of the report_history table as rh.
Second, we've included two components in our FROM clause. The first is the table called
report_history (aliased as rh). The second is a select statement:
FROM report_history
We've aliased the max(report_run_date) as maxdate and we've aliased the entire result set as
maxresults.
Now, that we've created this select statement within our FROM clause, Oracle will let us join these
results against our original report_history table. So we've joined the report_name and
report_run_date fields between the tables called rh and maxresults. This allows us to retrieve the
report_name, max(report_run_date) as well as the user_name.
Question: I need help with a SQL query. I have a table in Oracle called orders which has the
following fields: order_no, customer, and amount.
I need a query that will return the customer who has ordered the highest total amount.
Answer: The following SQL should return the customer with the highest total amount in the
orders table.
https://www.techonthenet.com/sql/max.php 3/5
6/19/2021 SQL: MAX Function
SELECT query1.*
FROM orders
FROM orders
This SQL SELECT statement will summarize the total orders for each customer and then return
the customer with the highest total orders. This syntax is optimized for Oracle and may not work
for other database technologies.
Question: I'm trying to retrieve some info from an Oracle database. I've got a table named
Scoring with two fields - Name and Score. What I want to get is the highest score from the table
and the name of the player.
FROM Scoring
Question: I need help in a SQL query. I have a table in Oracle called cust_order which has the
following fields: OrderNo, Customer_id, Order_Date, and Amount.
I would like to find the customer_id, who has Highest order count.
SELECT MAX(COUNT(*))
FROM CUST_ORDER
GROUP BY CUSTOMER_ID;
This gives me the max Count, But, I can't get the CUSTOMER_ID. Can you help me please?
Answer: The following SQL SELECT statement should return the customer with the highest order
count in the cust_order table.
https://www.techonthenet.com/sql/max.php 4/5
6/19/2021 SQL: MAX Function
SELECT query1.*
FROM cust_order
FROM cust_order
This SQL SELECT statement will summarize the total orders for each customer and then return
the customer with the highest order count. This syntax is optimized for Oracle and may not work
for other database technologies.
Question: I'm trying to get the employee with the maximum salary from department 30, but I
need to display the employee's full information. I've tried the following query, but it returns the
result from both department 30 and 80:
SELECT *
FROM employees
FROM employees
WHERE department_id=30);
Answer: The SQL SELECT statement that you have written will first determine the maximum
salary for department 30, but then you select all employees that have this salary. In your case, you
must have 2 employees (one in department 30 and another in department 80) that have this same
salary. You need to make sure that you are refining your query results to only return employees
from department 30.
SELECT *
FROM employees
WHERE department_id=30
FROM employees
WHERE department_id=30);
This will return the employee information for only the employee in department 30 that has the
highest salary.
Copyright TechOnTheNet.com
https://www.techonthenet.com/sql/max.php 5/5