Learn how to use SQL SELECT COUNT statement with the tutorial and Examples. Find out how to get total of a selected column in a table with SQL SELECT COUNT statement.
You can also count all the total data of a table using the (*). Retrieve only the required data using the DISTINCT clause and count the total using COUNT clause.
How to Use SQL SELECT COUNT Statement to Count Rows
There are many types whose syntax given below to count the total data of a table. You can count all the data or you can count only the selected column data.
If there are duplicated data in the table, you can retrieve only the unique data and get the total of the data in a table.
Syntax1: Select and Count All The Record in Table.
1 2 |
SELECT COUNT(*) FROM tablename; |
The above syntax finds the total of all the column of a table. You have to specify only the table name from where you want to retrieve the data.
Syntax2: Count Total of Selected Column in Table.
1 2 |
SELECT COUNT(column_name) FROM tablename; |
The above syntax counts the total of only the selected columns. You have to specify the column name and the table name from where you want to fetch the data.
Syntax3: Count Distinct Rows of Selected Column in Table.
1 2 |
SELECT COUNT(DISTINCT column_name) FROM tablename; |
The above-given syntax fetches only the distinct values from the table and counts only the total of the distinct column.
Syntax4: Count Total of Selected Column in Table With WHERE Condition.
1 2 3 |
SELECT COUNT(column_name) FROM tablename WHERE condition; |
In addition to all the above-given syntax, you can use the conditional statement to fetch the required data and count.
Parameter Descriptions
Sr.No | Parameter Name | Description |
---|---|---|
1 | column_name | Enter required column name you want to find the total of the selected column from the table. You can also use the symbol (*) to find the total for all the columns on a table. |
2 | tablename | Specify the name of the table from where you want to fetch and select the data and find the total records. |
3 | condition | Specify the condition of the data by which you select and count the required data only. |
1 2 |
SELECT COUNT(Name) FROM Customers; |
1 2 |
SELECT COUNT(*) FROM Customers; |
1 2 |
SELECT COUNT(DISTINCT Product_Price) FROM Customers; |
1 2 3 |
SELECT COUNT(Email) FROM Customers; WHERE Product_Price>5000; |
You must also learn.
Reference