SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Group by query in MSSQL Table

In our student database we have seen how count query works. Now we will discuss how to display total number of records based on GROUP By SQL command. You can read more on GROUP BY command here.


Let us try to display total number of students in each class. Here we will display class name and number of students in the class.


You can read the basic SELECT QUERY here , we will only change the sql part here.


rs1.open " select COUNT(class) as no,class from student group by class " , conn


Within the Do While loop we will keep this code to display the data.

Response.Write rs1("class") & rs1("no") & "<br>"


GROUP BY command with SUM
We can find out total mark of students in each class by using SUM and GROUP BY sql commands. Here is the query and display part

rs1.open " select SUM(mark) as no,class FROM student GROUP BY class " , conn


GROUP BY command with AVG
Same way we can get teh average mark in each class by using AVG command with GROUP BY
rs1.open " select AVG(mark) as no,class FROM student GROUP BY class " , conn


GROUP BY command with MAX and MIN
Same way we can find out the highest mark in each class by using MAX command and the minimun mark by using MIN query
rs1.open " select MAX(mark) as no,class FROM student GROUP BY class " , conn
rs1.open " select MIN(mark) as no,class FROM student GROUP BY class " , conn
Further readings
SELECT query in collecting records from MSSQL table
TOP with SELECT query to collect number of records
SELECT query with AND OR NOT for MSSQL table
SELECT query with LIKE using wildcards % and _
Count: Counting total records in MSSQL table
Group By: SQL command in MSSQL table
Different Date & Time formats by using Convert in SELECT query
 
Scripts
PHP
JavaScript
All ASP Tutorials
SELECT Query
SELECT
AND OR NOT
count
Date formats
Group By
LIKE & _
TOP
ASP Sections
Date and time
Declaring array
Form in ASP
Server.MapPath
Date Time & MSSQL
Select Query
File System Object
String Functions
Popular Tutorials
Managing two drop downs
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.