0% found this document useful (0 votes)
100 views

SQL Practical

The document describes creating tables to store employee and department data, populating the tables with sample data, and performing queries on the data: - It creates an employee table with fields for employee ID, department ID, name, and salary, and inserts 10 rows - It creates a department table with fields for department ID, name, and supervisor, and inserts 4 rows - It queries the data, such as finding employees for a department, minimum/maximum/average salaries, and increasing salaries - It adds a bonus field to employee and calculates bonuses as 5% of salary

Uploaded by

Shreyas Damle
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

SQL Practical

The document describes creating tables to store employee and department data, populating the tables with sample data, and performing queries on the data: - It creates an employee table with fields for employee ID, department ID, name, and salary, and inserts 10 rows - It creates a department table with fields for department ID, name, and supervisor, and inserts 4 rows - It queries the data, such as finding employees for a department, minimum/maximum/average salaries, and increasing salaries - It adds a bonus field to employee and calculates bonuses as 5% of salary

Uploaded by

Shreyas Damle
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1. Create a table for house hold Electricity bill with the following fields.

Fieldname Type
RR_number varchar2(10)
Consumer_name varchar2(25)
Date_billing date
Units number (4)

Ans: Create table Ebill11 (rr_number varchar2 (10), consumer_name varchar2 (25),
date_billing date, units number (4));

a) Insert 5 records into the table.


Ans: insert into Ebill9 values ('a101','kumar','02-mar-18', 98);
insert into Ebill9 values ('a102','naveen','02-feb-18', 88);
insert into Ebill9 values (‘a103’,’suma’,’20-sep-20’, 108);
insert into Ebill9 values (‘a104’,’sheetal’,’15-aug-21’, 158);

b) Check the structure of table and note your observation.


Ans : Desc ebill9;

c) Add 2 new fields to the table. bill_amt number (6, 2)


duedate date
Ans: Alter table Ebill9 add (bill_amt number(6, 2), duedate date );
d) Compute the bill amount for each customer as per the following rules.
Minimum amount Rs.50
First 100 units Rs.4.50/unit
>100 units Rs.5.50/unit
Ans: update Ebill9 set bill_amt=50;

update Ebill9 set bill_amt = bill_amt + units*4.50 where units<=100;

update Ebill9 set bill_amt=bill_amt +100*4.50+(units-100)*5.50 where


units>100;

e) Compute due date as billing date +15days.


Ans: update Ebill9 set duedate=date_billing+15;

f) List all the bills generated.


Ans: select * from Ebill9;

2. Create a student database with the following fields .


FieldName Type
Stud_id number(4)
Stud_name varchar2(25)
Sub1 number(2)
Sub2 number(2)
Sub3 number(2)
Ans: create table student12(sid number(4), sname varchar2(25), sub1 number(2),
sub2 number(2), sub3 number(2));

a) Add 3 records into the table


Ans: insert into student12 values (1001, 'Anuthara', 67, 82, 86);
insert into student12 values (1002, 'arjun', 32, 66, 78);
insert into student12 values (1003, 'karthik', 37, 65, 68);

b) Display the description of the fields in the table


Ans: desc student;

c) Alter the table to add total number(3) and perc number(3)


Ans: alter table student12 add(total number(3),perc number(3));

d) Calculate the total marks & percentage for each student


Ans: update student12 set total= sub1 + sub2 +sub3;

update student12 set perc= total/3;

e) Compute the result as “pass” or fail by checking if the student has scored more
than 35 marks in each subject.
Ans: alter table student12 add(result varchar2(10));

update student set result=’pass’ where sub1>=35 and sub2>=35 and sub3>=35;

update student set result=’fail’ where sub1<35 or sub2<35 or sub3<35;

f) List the content of the table


Ans: select * from student;

g) Retrieve only student id and student name of all the students


Ans: select sid,sname from student;

h) List the students who have result as “pass”


Ans: select * from student where result=’pass’;

i) List the students who have result as “fail”


Ans: select * from student where result=’fail’;

j) Count the number of students who have passed


Ans: select count(*) from student where result=’pass’;

k) Count the number of students who have failed


Ans: select count(*) from student where result=’fail’;

l) Count the number of students who have percentage more than 60.
Ans: select count(*) from student where per>=60;

m) Sort the table according to the order of stud_id.


Ans: select * from student order by sid;

EXPERIMENT NO:3
Generate the employee details and compute the salary based on the department
Create the following table employee
Field name Data type Description
Empid number(4) employee’s id number
Deptid number(2) department’s id
number
Empname varchar2(25) employee name
Empsalary number(5) salary of the employee
 Enter 10 rows of data for table employee and 4 rows of data for department
table

EMPLOYEE TABLE

SQL> create table employee(empid number(4),deptid number(2),empname


varchar2(25), empsalary number(5));
Table created.
SQL> insert into employee values(1001,01,'arun',25000);
1 row created.
SQL> insert into employee values(1002,02,'priya',27500);
1 row created.
SQL> insert into employee values(1003,02,'vamshi',23700);
1 row created.
SQL> insert into employee values(1004,03,'anup',36000);
1 row created.
SQL> insert into employee values(1005,04,'hari',29000);
1 row created.
SQL> insert into employee values(1006,03,'anil',19350);
1 row created.
SQL> insert into employee values(1007,02,'harsha',52000);
1 row created.
SQL> insert into employee values(1008,03,'soorya',14500);
1 row created.
SQL> insert into employee values(1009,04,'keerthi',37900);
1 row created.
SQL> insert into employee values(1010,02,'ragav',9000);
1 row created.
SQL> select * from employee;
EMPID DEPTID EMPNAME EMPSALARY
------------------------------------------------------------------------------------------
1001 1 arun 25000
1002 2 priya 27500
1003 2 vamshi 23700
1004 3 anup 36000
1005 4 hari 29000
1006 3 anil 19350
1007 2 harsha 52000
1008 3 soorya 14500
1009 4 keerthi 37900
1010 2 ragav 9000

10 rows selected
Create another table department with the following structure
Field name Data type Description
Deptid number(2) department’s id
number
Deptname varchar2(15), department name
Supervisor varchar2(15), department head’s
name
Assume the department names as purchase(id-01),accounts(id-02),sales(id-
03),apprentice(id-04)
DEPARTMENT TABLE
SQL> create table department(deptid number(2),deptname
varchar2(15),supervisor varchar2(15));
Table created.

SQL> insert into department values(01,'purchase','ameen');


1 row created.
SQL> insert into department values(02,'accounts','krishna');
1 row created.
SQL> insert into department values(03,'sales','tanveer');
1 row created.
SQL> insert into department values(04,'apprentice','ashish');
1 row created.
SQL> select * from department;
DEPTID DEPTNAME SUPERVISOR
------------ ----------------- ----------------
1 purchase ameen
2 accounts krishna
3 sales tanveer
4 apprentice ashish
1. Find the names of all employees who work for the accounts department.
SQL> select empname from employee where deptid=(select deptid from
department where deptname='accounts');
EMPNAME
--------------------
priya
vamshi
harsha
ragav
2. How many employees work for accounts department.
SQL> select count(*) from employee where deptid=(select deptidfromdepartment
where deptname='accounts');

COUNT(*)
---------------
4

3. What is the minimum, maximum and average salary of employees working


for accounts department?
SQL> select min(empsalary) from employee where deptid=(select deptid from
department where deptname='accounts');
MIN(EMPSALARY)
-------------------------
9000

SQL> select max(empsalary) from employee where deptid=(select deptid from


department where deptname='accounts');
MAX(EMPSALARY)
----------------------------
52000

SQL> select avg(empsalary) from employee where deptid=(select deptid from


department where deptname='accounts');
AVG(EMPSALARY)
---------------------------
28050

4. List the employees working for a particular supervisor.


SQL> select * from employee where deptid=(select deptid from department
where supervisor='tanveer');
EMPID DEPTID EMPNAME EMPSALARY
------------------------------------------------------------------
1004 3 anup 36000
1006 3 anil 19350
1008 3 soorya 14500

5. Retrieve the department names for each department where only one
employee works.
SQL> select deptname from department where deptid=(select deptid from
employee group by deptid having count(*)=1);
DEPTNAME
----------------
Purchase

6. Increase the salary of all employees in the sales department by 15%


SQL> update employee set empsalary=empsalary+empsalary*0.15 where
deptid=(select deptid from department where deptname='sales');
3 rows updated.
SQL> select * from employee where deptid=(select deptid from department where
deptname='sales');

EMPID DEPTID EMPNAME EMPSALARY


-------------------------------------------------------------------
1004 3 anup 41400
1006 3 anil 22253
1008 3 soorya 16675
7. Add a new column to the table employee, called (bonus number(5)) and
compute 5% of the salary to the said field.
SQL> alter table employee (add bonus number(5));
Table altered.
SQL> update employee set bonus=empsalary*0.05;
10 rows updated.
SQL> select * from employee;
EMPID DEPTID EMPNAME EMPSALARY BONUS
-----------------------------------------------------------------------------
1001 1 arun 25000 1250
1002 2 priya 27500 1375
1003 2 vamshi 23700 1185
1004 3 anup 41400 2070
1005 4 hari 29000 1450
1006 3 anil 22253 1113
1007 2 harsha 52000 2600
1008 3 soorya 16675 834
1009 4 keerthi 37900 1895
1010 2 ragav 9000 450

10 rows selected.

8. Delete all the rows for employee in the apprentice department

SQL> delete from employee where deptid=(select deptid from department


where deptname='apprentice');

2 rows deleted.

EXPERIMENT NO: 4
Create database for the bank transaction.
Create the following table customer
Field name Data type Description
Cno varchar2(4) customer number
Cname varchar2(25) customer name
caddress varchar2(25) customer address
Cphone varchar2(12) customer phone
number

Create another table bank with the following structure


Field name Data type Description
accno varchar2(4) customer’s account
number
tramt number(8,2) transaction amount
trdate date transaction date
trtype varchar2(10) transaction type
Cno varchar2(4) customer number

1. Create a table for customer.


SQL> create table customer (cno varchar2(4),cname varchar2(25),caddress
varchar2(25), cphone varchar2(12));
Table created.

2. Create a table for bank.


SQL> create table bank (accno varchar2(4),tramt
number(8,2),trdatedate,trtype varchar2(10),cno varchar2(4));
Table created.
3. Insert data values into customer table.

SQL> insert into customer values('100','anush','bangalore',NULL);


1 row created.

SQL> insert into customer values ('101','ganesh','mangalore','9876024561');


1 row created.

SQL> insert into customer values('102','manisha','mysore','9535879022');


1 row created.
4. Insert data values into bank table.

SQL> insert into bank values('1310',70000,'26-mar-2014','credit','100');


1 row created.

SQL> insert into bank values('1320',23961.75,'01-mar-2014','debit','100');


1 row created.

SQL> insert into bank values('1330',2400,'21-jan-2014','credit','102');


1 row created.

SQL> insert into bank values('1340',59681.25,'14-feb-2014','debit','103');


1 row created.

SQL> insert into bank values('1350',25091.25,'11-mar-2014','debit','103');


1 row created.

5. To display all records from customer table.

SQL> select * from customer;

CNO CNAME CADDRESS CPHONE


------ --------------- ------------------------- ------------
100 anushbangalore
101 ganeshmangalore 9876024561
102 manishamysore 9535879022

6. To display all records from bank table.

SQL> select * from bank;

ACCN TRAMT TRDATE TRTYPE CNO


--------- ------------- ------------------- ---------- ---------------
1310 70000 26-MAR-14 credit 100
1320 23961.75 01-MAR-14 debit 100
1330 2400 21-JAN-14 credit 102
1340 59681.25 14-FEB-14 debit 103
1350 25091.25 11-MAR-14 debit 103

7. To display all records from bank whose transaction date is greater than or
equal to 01-mar-2014.
SQL> select * from bank where trdate>='01-mar-2014';
ACCN TRAMT TRDATE TRTYPE CNO
--------- ------------- ------------------- -------------- -----------
1310 70000 26-MAR-14 credit 100
1320 23961.75 01-MAR-14 debit 100
1350 25091.25 11-MAR-14 debit 103
8. To join two tables.

SQL> select accno,customer.cno,cname from customer,bank where


customer.cno=bank.cno;
ACCN CNO CNAME
--------- -------- -------------
1310 100 anush
1320 100 anush
1330 102 manisha

9. Display count of all records and their corresponding transaction group by


transaction type

SQL> select trtype,count(*) from bank group by trtype;


TRTYPE COUNT (*)
------------- ----------------
debit 3
credit 2

10. Display all records from customer, order by customer name in


descending order.
SQL> select * from customer order by cnamedesc;

CNO CNAME CADDRESS CPHONE


------- ------------------ ------------------ --------------------
102 manishamysore 9535879022
101 ganeshmangalore 9876024561
100 anushbangalore

11. Change the transaction amount to 70000 whose account number is


1330.

SQL> update bank set tramt=70000 where accno='1330';


1 row updated.

SQL> select * from bank;


ACCN TRAMT TRDATE TRTYPE CNO
---------- ---------- --------------- ---------------- -----------
1310 70000 26-MAR-14 credit 100
1320 23961.75 01-MAR-14 debit 100
1330 70000 21-JAN-14 credit 102
1340 59681.25 14-FEB-14 debit 103
1310 25091.25 11-MAR-14 debit 103

12. Alter table customer to change the size of customer address

SQL> alter table customer modify caddress varchar2(20);


Table altered.
SQL>desc customer;
Name Null? Type
----------------------------------------- -------- ---------------------------

CNO VARCHAR2(4)
CNAME VARCHAR2(25)
CADDRESS VARCHAR2(20)
CPHONE VARCHAR2(12)
13. Delete records from bank having an account number 1340

SQL> delete from bank where accno='1340';

1 row deleted.

SQL> select * from bank;

ACCN TRAMT TRDATE TRTYPE CNO


---------- ----------- ------------------- ----------- --------------
1310 70000 26-MAR-14 credit 100
1320 23961.75 01-MAR-14 debit 100
1330 70000 21-JAN-14 credit 102
1310 25091.25 11-MAR-14 debit 103

14. Create a table containing customer number ranging from 100 to 102
SQL> create table tempcustomeras(select * from customer where cno between
100 and 102);

Table created.

SQL> select * from tempcustomer;

CNO CNAME CADDRESS CPHONE


------- ------------ ------------------- ----------------------
100 anushbangalore
101 ganeshmangalore 9876024561
102 manishamysore 9535879022

15. Display records from customer whose name starts with ‘g’

SQL> select * from customer where cname like 'g%';

CNO CNAME CADDRESS CPHONE


---- ------------------------- -------------------- ------------
101 ganeshmangalore 9876024561

16. Display today’s date

SQL> select sysdate from dual;

SYSDATE
---------
28-AUG-18

17. Display total transaction amount from bank table

SQL> select sum(tramt) from bank;

SUM(TRAMT)
----------
189053

18. Create a view on customer showing customer number and customer


phone number

SQL> create view viewcustomer as select cno,cphone from customer;

View created.

SQL> select * from viewcustomer;

CNO CPHONE
---- ------------
100
101 9876024561
102 9535879022

19. Display distinct customer number from bank

SQL> select distinct(cno) from bank;

CNO
----
100
102
103

20. Display account number from bank who have more than one
transaction

SQL> select accno,count(*) from bank group by accno having count(*)>1;

ACCN COUNT(*)
---- ----------
1310 2

21. Display all records from customer whose phone number is NULL

SQL> select * from customer where cphone is NULL;

CNO CNAME CADDRESS CPHONE


---- ------------------------- -------------------- ------------------------
100 anushbangalore

22. Delete all records from customers table

SQL> delete from customer;

3 rows deleted.
23. Drop table bank.

SQL> drop table bank;

Table dropped.

24. Drop table customer

SQL> drop table customer;

Table dropped.

You might also like