count-rows

Count Rows

To count rows you can use the COUNT() function.

SELECT COUNT(*) FROM <table>;

To count unique entries use the DISTINCT keyword too:

SELECT COUNT(DISTINCT <column>) FROM <table>;

To count aggregated rows with common values use the GROUP BY keywords:

SELECT COUNT(<column>) FROM <table> GROUP BY <column with common value>;

or

SELECT <column> COUNT(*) as <alias> FROM <table> GROUP BY <column with common value>;

Example:

SELECT genre, COUNT(*) AS genre_count FROM books GROUP BY genre;

Obtaining Totals

To total up numeric columns use the SUM() function.

SELECT SUM(<numeric column) FROM <table>;
SELECT SUM(<numeric column) AS <alias> FROM <table>
                                       GROUP BY <another column>
                                       HAVING <alias> <operator> <value>;

Last updated

Was this helpful?