SQL Count

SQL Count

The SQL COUNT function returns the number of the rows in a select query.

COUNT syntax

SELECT COUNT(*) FROM table_name;

SELECT COUNT(column_name) FROM table_name;

COUNT example

Coder books table

ID Title Price Description
1 Learn SQL 20 Learn SQL language
2 Learn MySQL 22 Learn MySQL language
3 HTML book 17 Learn HTML
4 Learn PHP 20 Introduction to PHP
5 Learn PHP 20 PHP course
SELECT COUNT(*) 
FROM coder_books;

Result

5

SELECT COUNT(price) 
FROM coder_books 
WHERE price >= 20;

Result

4

SELECT price, COUNT(price) no_count
FROM coder_books
GROUP BY price
HAVING COUNT(*) > 1;

Result

Price no_count
20 3