0% found this document useful (0 votes)
12 views6 pages

Dbms Record4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Dbms Record4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment-4

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 –

Query – select min(Emp_sal) , Emp_address from Employees group by Emp_address order by


count(Emp_sal) desc;
Output –

Query – select avg(Emp_sal) , Emp_address from Employees group by Emp_address order by


avg(Emp_sal) desc;
Output –

Query – select count(Emp_id) , Emp_address from Employees group by Emp_address having


count(Emp_id) order by count(Emp_id) desc;
Output –

Query – select min(Emp_sal) , Emp_address from Employees group by Emp_address having


min(Emp_sal) > 50000 order by min(Emp_sal) desc;
Output –
Aim – 4.2) Queries on Controlling Data: Commit, Rollback, and Save point.
Description –
In SQL , Commit ,Rollback, Save Point comes under the Transaction Control Language which helps
the user manage the transactions that take place in a database .
COMMIT command in SQL is used to save all the transaction- related changes permanently to the
disk . Whenever DML Commands such as INSERT , UPDATE and DELETE are used , the changes
made by these commands are permanent only after closing the session.
Syntax – COMMIT ;
SAVPOINT allows us to divide the databse operations into parts . Using the SAVEPOINT command
in SQL , we can save these different parts of the same transaction using different names .
Syntax – SAVEPOINT savepoint_name ;
ROLLBACK can be used to get the data or restore the data to the last savepoint or last committed
state , If due to some reasons thedata inserted , deleted or updated is not correct , you can rollback the
data to a particular savepoint or if savepoint is not done , then to the last committed state ,
Syntax – ROLLBACK;
(or)
ROLLBACK to savepoint_name;

We cannot perform rollback after commiting any dml statement in database.


Query – insert into Employees (Emp_id,Emp_name) values(112,’Ravi Kishore G’);

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 –

Query – select Emp_id , Emp_name , Emp_sal from Employees where Emp_Designation =


‘Architect’;
Output –

Query – update Employees set Emp_sal = Emp_sal + 5000 where Emp_id=105;

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 –

You might also like