📜  sql count where - SQL (1)

📅  最后修改于: 2023-12-03 15:20:14.224000             🧑  作者: Mango

SQL Count Where

SQL Count Where is a query that allows you to count the number of rows in a table based on a specified condition, using the WHERE clause.

The basic syntax for COUNT() function is as follows:

SELECT COUNT(*) FROM table_name WHERE condition;

This will return the total number of rows in table_name that meet the specified condition.

Here is an example:

SELECT COUNT(*) FROM customers WHERE country = 'USA';

This will return the total number of customers from the USA in the customers table.

You can also use COUNT(expression) to count the number of rows where the specified expression is not NULL.

SELECT COUNT(city) FROM customers WHERE country = 'USA';

This will return the total number of customers from the USA who have a city specified in the customers table.

COUNT(DISTINCT expression) can be used to count the number of unique values of the specified expression.

SELECT COUNT(DISTINCT city) FROM customers WHERE country = 'USA';

This will return the total number of unique cities for customers from the USA in the customers table.

In conclusion, SQL Count Where is a powerful tool that allows you to count the number of rows in a table based on a specific condition. With the addition of COUNT(expression) and COUNT(DISTINCT expression), you can count the number of rows with non-NULL values and count the number of unique values, respectively.