1

What is the simplest way to find the largest value of a field when there are 2 or more results?

I've been playing with the W3Schools site using the orders table. I've tried to display all the rows with the largest employee number (9). Here is the link - http://www.w3schools.com/sql/trysql.asp?filename=trysql_func_avg

I can only figure out how to get it to print out one row. How can I get it to print all of the results?

I tried the following:

select max(EmployeeID)
from orders;

and just get the result of 9. I'm having trouble getting my head around this.

1
  • max(EmployeeID) returns the greatest value of EmployeeID in the table. There can only be one maximum number, so 9 is the whole result of that query. Just as there can be only one result for average, as in the sample you linked to. Commented Jun 25, 2013 at 10:07

1 Answer 1

3

MAX() is an aggregate function, and all aggregate functions return only one row.

To get all the rows of the order table which belong to the employee with the highest id use a sub-query this

select *
from orders
where EmployeeID  = (select max(EmployeeID) from orders);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.