C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
SQL SELECT COUNTThe SQL COUNT() is a function that returns the number of records of the table in the output. This function is used with the SQL SELECT statement. Let's take a simple example: If you have a record of the voters in the selected area and want to count the number of voters, then it is very difficult to do it manually, but you can do it easily by using SQL SELECT COUNT query. Syntax of Select Count Function in SQLSELECT COUNT(column_name) FROM table_name; In the syntax, we have to specify the column's name after the COUNT keyword and the name of the table on which the Count function is to be executed. Examples of Select Count Function in SQLIn this article, we have taken the following two SQL examples that will help you to run the Count function in the query: Example 1: In this example, we have a table called Bike with three columns:
SELECT COUNT (Bike_Color) AS TotalBikeColor FROM Bikes ; This query will show the following output on the screen:
The output of this query is six because the Bike_Color column does not contain any NULL value.
SELECT COUNT (Bike_Cost) AS TotalBikeCost FROM Bikes ; This query will show the following output on the screen:
The output of this query is four because two values of the Bike_Cost column are NULL and, these two NULL values are excluded from the count function. That's why this query shows four instead of 6 in the output. Example 2: In this example, we have an Employee_details table with four columns:
SELECT COUNT (Emp_City) AS TotalCity FROM Employee_details ; This query will show the following output on the screen:
The output of this query is two because the three values of the Emp_City column are NULL. And, these three NULL values are excluded from the count function. That's why this query shows two instead of 5 in the output. Select Count(*) Function in SQLThe count(*) function in SQL shows all the Null and Non-Null records present in the table. Syntax of Count (*) Function in SQLSELECT COUNT(*) FROM table_name; Example of Count (*) Function in SQLIn this example, we have the following Bike table with three columns:
SELECT COUNT (*) FROM Bikes ; This query will show the following output on the screen:
SQL Count() Function With WHERE ClauseWe can also use the Count() function with the WHERE clause. The Count Function with WHERE clause in the SELECT statement shows those records that matched the specified criteria. Syntax of Count() Function With WHERE clause in SQLSELECT COUNT(column_name) FROM table_name WHERE [condition]; Examples of Count Function With WHERE clause in SQLThe following two examples will help you to run the Count function with the WHERE clause in the SQL query: Example 1: In this example, we have the following Bike table with three columns:
SELECT COUNT (Bike_Name) AS TotalBikeBlackColor FROM Bikes WHERE Bike_Color = 'Black'; This query will show the following output on the screen:
Example 2: In this example, we have an Employee_details table with four columns:
SELECT COUNT (Emp_Name) AS TotalEmpCity FROM Employee_details WHERE Emp_City = 'Delhi'; This query will show the following output on the screen:
SQL Count Function With DISTINCT keywordThe DISTINCT keyword with the COUNT function shows only the numbers of unique rows of a column. Syntax of Count Function With DISTINCT keyword in SQLSELECT COUNT(DISTINCT column_name) FROM table_name WHERE [condition]; Examples of Count Function With DISTINCT keyword in SQLThe following two examples will help you how to run the Count function with the DISTINCT keyword in the SQL query: Example 1: In this example, we have taken the following Cars table with three columns:
SELECT COUNT (DISTINCT Car_Color) AS Unique_Car_Color FROM Cars ; This query will show the following output on the screen:
The output of this query is three because there are three unique values of the car. Example 2: In this example, we have taken an Employee table with four columns:
SELECT COUNT (DISTINCT Emp_Salary) AS Unique_Salary FROM Employee ; This query will show the following output on the screen:
Next TopicSQL SELECT TOP
|