The SELECT COUNT
function in SQL is used to return the number of rows that match a specified condition. It is often used to count rows in a table or the number of non-null values in a particular column.
The basic syntax of COUNT()
is as follows:
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
Consider the following Orders
table:
OrderID | CustomerName | OrderDate | Status |
---|---|---|---|
1 | Sayyed Faraz UL Haq | 2024-01-10 | Completed |
2 | Khizar Mirza | 2024-02-05 | Pending |
3 | Laeeque Deshmuq | 2024-02-20 | Completed |
4 | Muntajeeb Ahmed Moosa | 2024-03-02 | Pending |
5 | Tausif Khan | 2024-03-05 | Completed |
To count the total number of rows in the Orders
table, you can use the following SQL query:
SELECT COUNT(OrderID)
FROM Orders;
This query will return the total number of orders in the table.
If you want to count the number of orders with a status of "Completed," you can add a WHERE
clause like this:
SELECT COUNT(OrderID)
FROM Orders
WHERE Status = 'Completed';
This query will return the number of orders where the status is "Completed."
The result of the query to count orders with a "Completed" status would be:
COUNT(OrderID) |
---|
3 |
In this case, there are 3 completed orders in the Orders
table.