In SQL, you can use the COUNT()
function to count the number of rows that match a specific condition. Here's the basic syntax:
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
You can replace column_name
with the name of the column you want to count the values of, and table_name
with the name of the table you want to count the values from. The WHERE
clause is optional and can be used to specify a condition that the rows must meet in order to be counted.
Here's an example:
SELECT COUNT(*)
FROM orders
WHERE status = 'completed';
This query will count the number of rows in the orders
table where the status
column is equal to 'completed'
. The *
inside the COUNT()
function means that it will count all rows that match the condition, regardless of the values in the columns.
You can also use COUNT(DISTINCT column_name)
to count the number of unique values in a column:
SELECT COUNT(DISTINCT customer_id)
FROM orders;
This query will count the number of unique values in the customer_id
column of the orders
table.