user1107888
user1107888

Reputation: 1525

Selecting Customer with maximum orders

I have the following simple table:

CustomerID  CustomerName   NumOfOrders
1           Joe            15
2           Jane           20
7           Clara          1

I want to find the customer with maximum number of orders. Seems trivial enough but I can,t seem to find a solution.

Upvotes: 1

Views: 119

Answers (6)

Sathish D
Sathish D

Reputation: 5034

You can use ORDER BY NumOfOrders DESC LIMIT 1

Upvotes: 0

davek
davek

Reputation: 22915

select customername, sum(numOfOrders) as cnoo
from myTable
group by customername 
order by cnoo 
limit 1

and if customerName is unique (i.e. only one row per customer):

select customername
from myTable
order by numOfOrders desc
limit 1

Upvotes: 1

Renuka
Renuka

Reputation: 206

Try this....

select top 1 customerName from customer_Order order by numOfOrders desc

Upvotes: 0

Stephan
Stephan

Reputation: 8090

Try this:

SELECT
    *
FROM
    my_table
ORDER BY
    NumOfOrders DESC
LIMIT 1

Upvotes: 4

PSR
PSR

Reputation: 40328

SELECT CustomerID, NumOfOrders FROM myTable
where  NumOfOrders  =(select MAX(NumOfOrders) FROM myTable)

Upvotes: 1

Fahim Parkar
Fahim Parkar

Reputation: 31647

How about this?

SELECT CustomerID, CustomerName, MAX(NumOfOrders) FROM myTable

Demo

Upvotes: 2

Related Questions