SQL COUNT WHERE

The SQL Count() function can also be used with the WHERE clause in the SELECT statement.

The Count() with WHERE clause returns only those rows from the table which match with the condition specified in the WHERE clause.

Syntax of SQL Count() Function With WHERE clause

Examples of SQL Count Function with WHERE clause

Example 1: First create a table and then run the count function.

The following CREATE TABLE statement creates the Cars_Details table with five fields:

The following INSERT query inserts the record of cars into the Cars_Details table:

The following SELECT query shows the data of the Cars_Details table:


Car_NumberCar_NameCar_AmountCar_Price
2578Creta3900000
9258Audi21100000
8233Venue6900000
8990Nexon7700000
7085Mercedes68000000
1258Thar21500000
2564Jaguar46000000
9578Scorpio81800000

The following query shows the total value of those cars whose Car_Number is greater than and equal to 7000:

Output:

SQL COUNT WHERE

Example 2: The following query creates the College_Student_Details table using the CREATE TABLE statement:

The following SQL queries insert the record of students into the above table using INSERT INTO statement:

Let's see the record of the above table using the following SELECT statement:


Student_IDStudent_NameStudent_CourseStudent_AgeStudent_Marks
101AnujB.tech2088
102RamanMCA2498
104ShyamBBA1992
107VikashB.tech2078
111MonuMBA2165
114JonesB.tech1893
121ParulBCA2097
123DivyaB.tech2189
128HemantMBA2390
130NidhiBBA2088
132PriyaMBA2299
138MohitMCA2192

The following Count query displays the total number of those students whose Student_Marks is greater than 90:

Output:

SQL COUNT WHERE
Next TopicSQL SELECT MIN




Latest Courses