SQL Count

HTML
CSS
C#
SQL

SQL Select Count

The 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.

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.

We have a table called Employee with four columns:

called Bike

Suppose, you want to count the total number of bike colors from the Bike Table. For this operation, you have to write the following SQL statement:

SELECT COUNT (Bike_Cost) AS TotalBikeColor FROM Bikes ; 

This query will show the following output:

 

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.

We have an Employee_details table with four columns:

Suppose, you want to count the total number of bike colors from the Bike Table. For this operation, you have to write the following SQL statement:

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.

We have an Employee_details table with four columns:

Suppose, you want to count the total values of the Emp_City column of the above Employee_details table. For this query, you have to write the following statement in Structured Query Language:

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.

NOTE: Practice below practice questions on MSSQL SERVER, it will not execute on site editor.

1.Write sql query to get all the record count from the Sales.Customer table using ADVENTURE2019 database

2.Write a query to get all the records from the person id column in Sales.Customer table using ADVENTURE2019 database

Hint:  Null values in the column are not counted

3.Write a query to get all the record of card type from sales.CreditCard table, no value should be repeated

Hint: Use distinct with count

4.Write sql query to get all the record count of the title column from Person.Person table.

5.write sql query to get all the record count from middle name column of Person.Person table

Note: All queries are executed in the SQL Server using the ADVENTURE2019 database.