SQL Group By Clause, SQL Tutorial, Global Guide Line Technology.
Bookmark Bookmark: SQL Group By Clause, SQL Tutorial Forum | Blog |


Basic SQL Guide.           
SQL Introduction.               
 What is Table?                  
 SQL Create Table.            
 SQL Insert Statement.      
 SQL Select Keyword.        
 SQL Order by clause.        
 SQL Count Statement.      
 SQL Group By Clause.
 SQL Having Clause.          
 SQL Alias Tables.             
 SQL Join Tables.              
 SQL Outer Join.               
 SQL Update Statement.   
 SQL Delete Statement.    
 SQL Distinct Keyword.      
 SQL Where Keyword.       
 SQL And Or Keyword.      
 SQL In Keyword.              
 SQL Between Keyword.   
 SQL Like Keyword.          
 SQL Functions.                
 SQL Concatenate.           
 SQL Substrings.               
 SQL Trim Function.         
 SQL Constraints.              
 SQL Primary Key.            
 SQL Foreign Key.            
 SQL Create Views.           
 SQL Create Index.           
 SQL Alter Table.             
 SQL Drop Table.             
 SQL Truncate Table.      
 SQL Summary.                

SQL Tutorial >> SQL Group By Clause.

     Back             Next     

Now here in SQL Tutorial we return to the aggregate functions of SQL Group by clause. Remember we used the SUM keyword to calculate the total number of the employees records in our table of employees. Well, we need to do two things: First, we need to make sure we select the employees name as well as total salary. Second, we need to make sure that all the salary figures are grouped by month. The corresponding SQL syntax for SQL group by keyword is,

Syntax for SQL Group By Clause

SELECT "column_name1", SUM("column_name2")
FROM "table_name"
GROUP BY "column_name1"

Let's illustrate using the following table,

Table Employees_Salary
FirstName Month Salary
Austin January 11000$
Creston January 11000$
Kate January 12000$
Angela January 10900$
Bobbi January 12000$
Cute January 13000$
Austin February 11100$
Creston February 11100$
Kate February 12100$
Angela February 11000$
Bobbi February 12200$
Cute February 13300$

Example of SQL Group By Clause

SELECT Month, SUM(Salary)
FROM Employees_Salary


Month SUM(Salary)
January 69900$
February 70800$

The SQL GROUP BY keyword is used when we are selecting multiple columns from a table (or tables) and at least one arithmetic operator appears in the SELECT statement. When that happens, we need to GROUP BY all the other selected columns, i.e., all columns except the one(s) operated on by the arithmetic operator. Hope this SQL Tutorial section is enough to explain SQL Group By Clause for beginners.

     Back             Next     

[ About ] [ Contact ] [ Home ]
[ Links ] [ Site Map ] [ Services ] [ Privacy ]

Copyright 2005 -  2018 www.globalguideline.com All rights reserved. (Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher) 134 visitors are online now