How to implement Group By Clause concepts in SQL Statement

Group By clause:-
When we run aggregated function in select query, we get the output on the basis of all the records but if we want that aggregated function should give the output on the basis of group and column then we can use Group By clause.
  •  Before grouping the the records if we want to keep some conditions then we can specify WHERE Clause.  
  • After Grouping the records if we want to some conditions then we can use 'having ' Clause.
There are some steps to implement this concepts which are as given below:-
Step 1 :- First create a ST_Records table and insert some values in this table as given below:-
Create table ST_Records(sid int,sname varchar(20),Depart_Name varchar(20),Salary money )
insert into ST_Records values(107,'Ramesh','CA',1000)

Output:- SELECT*FROM ST_Records 


Step 2:- If you want to count the values of  sname column then you have to use Group By  clause in sql command as given below:-
select sname,count(*)from ST_Records 
group by sname
group by

Step 3:- Now you can use conditions(WHERE CLAUSE)  also before Group By Clause as given below:-
select Depart_Name,sname,count(*)from ST_Records where Salary>=10000
group by Depart_Name,sname

Step 4:- Now you can use more than one conditions in Group By Clause by WHERE Clause and having clause as given below:-
select sname,count(*)from ST_Records 
group by sname
having COUNT(*)>1

You  can easily fetch the relevant data by using Group By ,where and Having clause in sql statement.
For More:-
  1. All command of sql server
  2. How to implement Integrity Concepts in sql server
  3. How to implement Check Constraints in sql server
  4. How to implement Default constraint in sql server
  5. What is different between delete and Truncate statement
  6. How to implement stored procedures in sql server
  7. What is the different between stored procedure and function
  8. How to implement transaction concepts in sql server
  9. How to implement join concepts in sql server
  10. How to implement view concepts in sql server
  11. How to implement Trigger concepts in sql server


Post a Comment

Powered by Blogger.