SQL PHP HTML ASP JavaScript articles and free scripts to download
SQL

SQL COUNT Command: Number of records

We can count the number of records in a table with different combinations. Let us first count the total number of records in the table with this count command.
SELECT count( * ) as  total_record FROM student
Output of above query is here.
total_record
35
This will display total records under the name total_record in the table student. Now we can add some condition to this SQL to count the records with different conditions. Let us find out the number of students in class Four in out table.

We are using alias here to display the value under hading total_record

SELECT count( * ) as total_record FROM `student` WHERE class = 'four'  
Output is here
total_record
9
This will return the count of students in class four only. Here since we are displaying only one count for a fixed class so we can use this way. We may require to display total of each class in a table so here we have to use GROUP BY clause to display totals of each class.

We can count the records in different combinations like finding out the number of students of class four who has got more than 60 mark.
SELECT count(*) FROM `student` WHERE class='Four' and mark >60
Ouput of above query is here.
count(*)
5

Using BETWEEN

SELECT count(*) FROM `student` WHERE class='Four' and mark between 50 and 60
Oupput is here
count(*)
4
Download sql dump of this student table

Number of User Comments : 11


Google+

Nagappan06-11-2009
IT'S VERY NICE
nayan02-04-2010
hi thanks for giving information thanks again...
sumit27-04-2010
please tell me the query if i want to count the no. employees from a table emp_detail (and display it) and also need to see the different entries of a different table course_detail ? I need to see them on single web page together..
Rajan Arora25-06-2010
Really simple and nice way to explain... Gr8 gng... Keep it Up ...
ragavan11-07-2010
u r done a very good job.
i need the answer for how to get the totals of the three columns in the table.
vikas10-10-2010
how to get the totals of between two given date.
Bruno17-12-2010
Pls I need a syntax for count with this scenario: patients who visited their gp in the last three months
Siddharth23-12-2011
how to add a new row of total at the last of all the integer record..

plz help me
sathya23-12-2011
Pls i need a syntax to increase the count by 1. (e.g): In a shop bill generation, the bill no should increase automatically by 1 at each bill
dhanasekaran04-02-2012
hi..plz help me out. i like to get count of records in the table ( included deleted recors ). in other words.. the number of records from the table creation.
Alvin56709-08-2012
Hi there,

is it possible to do this?

Select count(enabled = 1) from user
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer