How to Count the Number of Rows with the Same Value
Suppose we have a table in our SQL database.
We want the number of occurrences of all values in a particular column.
SELECT val, COUNT(*) AS count FROM table_name GROUP BY dog;
The notable section here is the
GROUP BY clause. It subdivides the dataset into chunks based on unique combinations of the specified fields. We can specify multiple fields if we want unique combinations of those fields.
If we want to filter by some specific count number, we can use the
HAVING is for group-level criteria while
WHERE is for row-level criteria.
Let’s get only the dogs that have a
count >= 10.
SELECT val, COUNT(*) AS count FROM table_name GROUP BY dog HAVING count >= 10;
More SQL Articles
- How to Modify Table to Use Composite Primary Key in MySQL
- How to Format a Number with Two Decimal Places in SQLite
- How to Order By Multiple Columns in SQL
- How to Order a SQL String Column By Its Numeric Value
- How to Check if a Column is Null in MySQL
- How to Combine Columns Values Into a New Column in MySQL
- How to Drop Multiple Columns with ALTER TABLE in SQL
- What Stripe Data Do I Store in My Database?
- How To Query JSON Object with Unknown Keys in PostgreSQL
- How To Convert an Escaped JSON String in PostgreSQL to a JSON Object