Dbms Record4
Dbms Record4
Aim – 4.1) Queries using Group By, Order By, and Having Clauses.
Description –
Before Knowing about the Group By , Order By and Having clauses of sql , We need to be familiar
with Aggregate Functions .
Count() – Count returns the total number of records of specified Attribute
Syntax – count ( attribute ) ;
Sum() – Sums up all the non null values of specified Attribute.
Syntax – sum ( attribute );
Average() – The Average is nothing but sum() / count() of the Specified attribute
Syntax – average ( attribute );
Min() – Min returns the minimum value of the specified Attribute.
Syntax – min ( attribute );
Max() – Max returns the maximum value of the specified attribute .
Syntax – max ( attribute );
The GROUP BY statement in SQL is used to arrange identical data into groups with the help of
aggregate functions.
Syntax – select column1 , aggregate_function ( column 2 )
from table_name
where condition
group by column1 , column2 ;
The HAVING clause is used to arrange identical groups that are arranged by GROUP BY statement
based on certain condition .
Syntax – select column1 , aggregate_function ( column 2 )
from table_name
where condition
group by column1 , column2
having condition ;
The ORDER BY clause is used to arrange identical groups that are arranged by GROUP BY
statement are re-arranged in ascending order by default using the aggregate function . In Order to
arrange then in descending order we need to use desc keyword .
Syntax – select column1 , aggregate_function ( column 2 )
from table_name
where condition
group by column1 , column2
having condition
order by aggregate_function ( column2 );
Query – select count(Emp_id) from Employees ;
Output –
Query – select max(Emp_sal) from Employees;
Output –
Query – select min(Emp_sal) from Employees;
Output –
Query – select sum(Emp_sal) from Employees ;
Output –
Query – select avg(Emp_sal) from Employees;
Output –
Query – select count ( Emp_id ) , Emp_address from Employees group by Emp_address order by
count(Emp_id) desc;
Output –
Output –
Query – savepoint a;
Output –
Query – insert into Employees (Emp_id,Emp_name) values(113,’Sumanasa Ravi’);
Output –
Query – savepoint b;
Output –
Query – insert into Employes (Emp_id,Emp_name) values(114,’Karunya Prasad’);
Output –
Query – savepoint c;
Output –
Query – select Emp_id , Emp_name from Employees;
Output –
Query – rollback to b;
Output –
Query - select Emp_id , Emp_name from Employees ;
Output –
Output –
Query – commit;
Output –
Query – update Employees set Emp_sal = Emp_sal + 5000 where Emp_id=101;
Output –
Query – rollback;
Output –
Query - select Emp_id , Emp_name , Emp_sal from Employees where Emp_Designation =
‘Architect’;
Output –