user972616
user972616

Reputation: 1404

MySQL - Using COUNT(*) outside the SELECT

I have this data and I want to return the names of customers in descending order of their count

Customer | Count
abc      | 2
efg      | 3
hij      | 7
klm      | 3

So for the table above the result should be

Customer
hij
efg
klm
abc

I have this query

Select Customer, Count(*) AS CustomerCount
From 'customers'
group by Customer
order by CustomerCount DESC

which gives me

Customer | Count
hij      | 7
efg      | 3
klm      | 3
abc      | 2

But I don't want to display the count and I don't know how to do that. Can anybody help me?

Upvotes: 1

Views: 1447

Answers (3)

user1053839
user1053839

Reputation: 390

this is the solution for you problem:

Select Customer, Count(*)
From 'customers'
order by count(*) DESC
group by Customer

Upvotes: 0

AndreKR
AndreKR

Reputation: 33727

You can just write ORDER BY COUNT(*) without having COUNT(*) in the select expression.

Upvotes: 2

Ike Walker
Ike Walker

Reputation: 65587

MySQL lets you order by an aggregate function without including it in the select clause, so this should work for you:

Select Customer
From customers
group by Customer
order by COUNT(*) DESC

Upvotes: 3

Related Questions