SQL SELECT COUNT

The SQL SELECT COUNT statement is used to return the number of rows that match a specified condition. It can be used to count the number of rows in a table or the number of rows that meet certain criteria.

Syntax

The basic syntax for SELECT COUNT is:

SELECT COUNT(column_name)
FROM table_name;
            

Example

Consider the Orders table. We will count how many products have been ordered.

Code Example


-- Count the number of orders
SELECT COUNT(Product) FROM Orders;

-- Count the number of orders for each customer
SELECT CustomerID, COUNT(Product) FROM Orders GROUP BY CustomerID;
                

Output

3 2

Explanation

- The first query returns the total number of products ordered.
- The second query groups the results by CustomerID and returns the number of orders each customer has placed.