Link to home
Start Free TrialLog in
Avatar of ankykele
ankykele

asked on

SQL query

Hello,
I am trying to write a SQL query that will return the item in a column which occurs the most.  My data looks like this:

ID          Country
1               US
2               UK
3               Canada
4               US

I would like my query to return the highest frequency item in the country column: US

Thanks in advance!
ASKER CERTIFIED SOLUTION
Avatar of Thomasian
Thomasian
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
with cte as
(
select country,count(ID) as cnt from tblcounty group by country )

select * from cte where cnt=(Select max(cnt) from cte);

Open in new window

the query posted by Thomasian will show only one country in case of two or more countries having the highest frequency.
Avatar of ankykele
ankykele

ASKER

Thanks alot for the solution!!!
Avatar of Pratima

return the highest frequency item in the country column: US
Meabns required only on record

SELECT Top 1 Country
FROM tablename
GROUP BY Country
ORDER BY COUNT(ID) DESC