Here's one way to get the specified result:
SELECT v.vote_date
, COUNT(1) AS votes_cast
FROM votes v
GROUP
BY v.vote_date
ORDER
BY votes_cast DESC
, v.vote_date DESC
LIMIT 1
The GROUP BY
clause allows groups of rows to be aggregated into a single row. In this example, we are "grouping" rows that have the same value of vote_date
. The COUNT
function is an aggregate function, which operates on all rows within a group, and returns a single value for the group. (COUNT starts at zero, and for each row that has a non-null value for the expression, one is added to the count.) You could also use SUM(1)
in place of COUNT(1)
in this query.
The ORDER BY
returns the rows in the specified sequence. In this example, we want the row that has the highest value for COUNT. So we sort the rows on this expression in descending order. In the case of two or more vote_date with the same "highest" count, we'll return the latest vote_date first.
The LIMIT n
clause restricts the number of rows returned, so that only n
rows are returned.
(This is not the only SQL statement that will return the specified result, it's just one example.)