SQL Groupby Use groupby with aggregate column select count(id), country from Customers group by country order by country desc Extract month and year PostgreSQL select extract(month from starttime) , extract(year from starttime) from table