SQL Query

MySQL: How to Count Rows in SQL in SQL

MySQL guide: Count the total number of rows in a table using COUNT() or COUNT(column).

This guide is specifically for MySQL syntax.

How to Count Rows in SQL in SQL

Count the total number of rows in a table using COUNT(*) or COUNT(column).

Quick Answer

SELECT COUNT(*) FROM table_name;

Explanation

COUNT(*) counts all rows including NULLs, while COUNT(column) only counts non-NULL values in that column. COUNT(DISTINCT column) counts unique non-NULL values.

Query Variants

Count All

SELECT COUNT(*) FROM employees;

Count Column

SELECT COUNT(department) FROM employees;

Count Distinct

SELECT COUNT(DISTINCT department) FROM employees;

Count With Where

SELECT COUNT(*) FROM employees WHERE salary > 50000;

Pro Tips

  • Use COUNT(*) for total row count
  • Use COUNT(column) to exclude NULL values
  • COUNT(1) is equivalent to COUNT(*)

Related SQL Queries

Continue learning with more SQL query examples:


MySQL-Specific Notes

This page covers MySQL syntax. Other databases may have different syntax for similar operations.

Related Content

Try it yourself

Practice this query in our interactive SQL sandbox.