How do I get distinct values from the table along with the count of how many rows there are containing that value

Viewed 49

IN SQL (and if possible how to write it in Laravel Eloquent), if there are 10 MySQL rows having value IN', 4 rows having value 'US', 7 rows having value 'FR'; I want the query result to be three rows:

  1. IN | 10
  2. US | 4
  3. FR | 7
2 Answers
SELECT column_name, count(*) as c FROM table_name GROUP BY column_name ORDER BY c DESC
Model::query()->groupBy('country_name')->select('counrty_name',DB::raw('COUNT(1)'))->get()
Related