SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 

SQL GROUP BY Command


You can see the Count command before using GROUP BY command here. GROUP BY command will create groups in the field name specified and will count the number of records in the groups. This is very useful command.

We can also use WHERE command along with GROUP BY command in Mysql tables.

SELECT count(*) as total_records, class FROM `student` group by class


This will display the total records in each class. Like this


total_records class
1 Eight
3 Five
9 Four
2 Nine
10 Seven
7 Six
3 Three
Let us try to get the total number of girls records in each class by using GROUP BY query. Here we want to filter our query for only girls so we have to use one WHERE clause to restrict the records using the sex field. Here is the query.

Related Tutorial
Copy data to new table
SQL Left Join
Number of Affected rows
SQL Rename table
PHP MySQL functions


SELECT count( * ) AS total_records, class FROM `student` WHERE sex='female' GROUP BY class


The output is here
total_records class
5 Four
1 Nine
5 Seven
5 Six
1 Three


PART II: We will try to apply group by command more than one fields. For this we will create a new table and discuss in detail. Next Part II

Down load the SQL DUMP of this student table






sampat26-02-2009
i want to display the all record from table,
in which if column contain duplicate value then it will show duplicate value at once
smo27-02-2009
You have to use distinct SQL command. Read here on how to use Distinct to get unique records.
alicia22-08-2009
very nice tutorial it really helps me a lot :)
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
Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
SQL Tutorial List
SQL Commands
SQL Sections
Date & Time
Join Table
String
Math
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.