SQL Query

How to Count Rows in SQL in SQL

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

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:

Related Content

From Our Blog

Try it yourself

Practice this query in our interactive SQL sandbox.