DBMS Lab
DBMS Lab
CUSTOMER
Cust_ID
11
22
33
44
55
Name
John
Peter
Smith
Petrick
Scott
State
Caribou
Denver
Phoenix
Macow
Berley
Country
Germany
Australia
Australia
Germany
Switzerland
Amount
2345
2222
4523
8977
5688
Phone
98456
34256
34558
12945
88457
Queries:-
Solution:
SQL> create table Customer
2 (
3 Cust_Id number(10),
4 Name varchar(20),
5 State varchar(20),
6 Country varchar(20),
7 Amount number(10),
8 Phone number(10)
9 );
Table created.
SQL> insert into Customer values(11,'John','Caribou','Germany',2345,98456);
1 row created.
SQL> insert into Customer values(22,'Peter','Denver','Australia',2222,34256);
1 row created.
SQL> insert into Customer values(33,'Smith','Phoenix','Australia',4523,34558);
1 row created.
SQL> insert into Customer values(44,'Petrick','Macow','Germany',8977,12945);
1 row created.
SQL> insert into Customer values(55,'Scott','Berley','Switzerland',5688,88457);
1 row created.
SQL> select * from Customer;
CUST_ID NAME
STATE
COUNTRY
AMOUNT
---------- ---------- ---------- ---------- ---------- ---------11
22
33
44
55
John
Peter
Smith
Petrick
Scott
Caribou
Denver
Phoenix
Macow
Berley
Germany
2345
98456
Australia
2222
34256
Australia
4523
34558
Germany
8977
12945
Switzerland 5688
88457
PHONE
Q1. Write a query to select customer Id and name of those customers belonging to Germany.
SQL> select Cust_Id,Name from Customer where Country='Germany';
CUST_ID NAME
---------- -------------------11 John
44 Petrick
Q2. Write a query to display complete information of customer whose Amount>5000.
SQL> select * from Customer where Amount > 5000;
CUST_ID NAME
STATE
COUNTRY
AMOUNT
PHONE
---------- ---------- ---------- ---------- ---------- ---------44
Petrick Macow
Germany
8977
12945
55
Scott
Berley
Switzerland 5688
88457
Q3. Write a query to select customer Id and country of customer whose name containing a substring as
et.
SQL> select Cust_Id,Country from Customer where Name like '%et%';
CUST_ID COUNTRY
---------- -------------------22 Australia
44 Germany
Q4. Write a query to display the average of amount of all customer.
SQL> select avg(Amount) as Average from Customer;
AVERAGE
---------4751
Q5. Write a query to display the complete information of Peter.
SQL> select * from Customer where Name = 'Peter';
CUST_ID NAME
STATE
COUNTRY
AMOUNT
PHONE
---------- ---------- ---------- ---------- ---------- ---------22
Peter
Denver
Australia
2222
34256
Q6. Write a query to display the information of customer whose Amount>5000 and Amount<7000.
SQL> select * from Customer where Amount > 5000 and Amount < 7000;
CUST_ID NAME
STATE
COUNTRY
AMOUNT
PHONE
---------- ---------- ---------- ---------- ---------- ---------55
Scott
Berley
Switzerland 5688
88457
Q7. Write a query to select customer Id and state of those customers whose name contains h as a third
character.
SQL> select Cust_Id,State from Customer where Name like '__h%';
CUST_ID STATE
---------- -------------------11 Caribou
Q8. Write a query to display the maximum amount.
SQL> select max(Amount) as Maximum from Customer;
MAXIMUM
---------8977
Q9. Write a query to display the complete information of customer belonging to Australia.
SQL> select * from Customer where Country = 'Australia';
CUST_ID NAME
STATE
COUNTRY
AMOUNT
PHONE
---------- ---------- ---------- ---------- ---------- ---------22
Peter
Denver
Australia
2222
34256
33
Smith
Phoenix
Australia
4523
34558
Q10. Write a query to display name of customer whose Amount>2000 and Amount<5000.
SQL> select Name from Customer where Amount > 2000 and Amount < 5000;
NAME
-------------------John
Peter
Smith
Q11. Write a query to select customer Id and phone of customer whose name starts with Pe.
SQL> select Cust_Id,Phone from Customer where Name like 'Pe%';
CUST_ID
PHONE
---------- ----------
22
44
34256
12945
Question 2: Create the following table and solve the given queries:
SURBHI_BANK
ID
10
20
Name
ICICI
HDFC
Branch
Delhi
Agra
Account
34
56
Interest
2
5
Amount
56000
43255
30
40
50
IDBI
HDFC
YES
Delhi
Jaipur
Nagpur
77
89
20
3
7
5
67345
87623
45213
Queries:-
Q3. Write a query to select name whose branch has pur as a substring.
SQL> select Name from Surbhi_bank where Branch like '%pur%';
NAME
---------HDFC
YES
Q4. Write a query to display maximum of amount as MAXIMUM.
SQL> select max(Amount) as Maximum from Surbhi_bank;
MAXIMUM
---------87623
Q5. Write a query to display name and branch whose number of account>50.
SQL> select Name,Branch from Surbhi_bank where Account > 50;
NAME
BRANCH
---------- ---------HDFC
Agra
IDBI
Delhi
HDFC
Jaipur
Q6. Write a query to display average of amount as AVERAGE for Delhi Branch.
SQL> select avg(Amount) as Average from Surbhi_bank where Branch = 'Delhi';
AVERAGE
---------61672.5
Q7. Write a query to select name whose branch name has g as a substring.
SQL> select Name from Surbhi_bank where Branch like '%g%';
NAME
---------HDFC
YES
Q8. Write a query to select minimum amount as MINIMUM among all banks.
SQL> select min(Amount) as Minimum from Surbhi_bank;
MINIMUM
---------43255
Q9. Write a query to display Id and name whose interest>5 and interest<8.
SQL> select ID,Name from Surbhi_bank where Interest > 5 and Interest < 8;
ID NAME
---------- ---------40 HDFC
Q10. WAQ to display branch name whose amount > 20000 and < 55000.
SQL> select name from Surbhi_bank where Amount>20000 AND Amount<55000;
NAME
-------------------HDFC
YES
Q11. Write a query to count Id of HDFC bank.
SQL> select count(ID) from Surbhi_bank where Name = 'HDFC';
COUNT(ID)
---------2
Q12. Write a query to display the sum of amount as SUM for Delhi branch.
SQL> select sum(Amount) as Sum from Surbhi_bank where Branch = 'Delhi';
SUM
---------123345
Q13. Write a query to update Delhi branch by Bangalore where amount>60000.
SQL> update Surbhi_bank set Branch = 'Bangalore' where Amount > 60000 and Branch
= 'Delhi';
1 row updated.
1.
2.
3.
4.
50 YES
Nagpur
10 ICICI
Delhi
30 IDBI
Delhi
40 HDFC
Jaipur
20
34
77
5 45213
2 56000
3 67345
89
7 87623
30 IDBI
Delhi
40 HDFC
Jaipur
50 YES
Nagpur
77
89
3 67345
7 87623
20
5 45213
Q10. WAQ to modify the data type of no_user column from char to int.
SQL> alter table Surbhi_bank modify no_user number(10);
Table altered.
ID NAME
BRANCH
ACCOUNT INTEREST AMOUNT NO_USER
---------- ---------- ---------- ---------- ---------- ------- -------10 ICICI
Delhi
34
2 56000
20 HDFC
Agra
56
5 43255
30 IDBI
Delhi
77
3 67345
40 HDFC
Jaipur
89
7 87623
50 YES
Nagpur
20
5 45213
Q11. WAQ to update the value no_user = 5 for ICICI and HDFC bank.
SQL> update Surbhi_bank set no_user=5 where name='ICICI' OR name = 'HDFC';
3 rows updated.
SQL> select * from Surbhi_bank;
ID NAME
BRANCH
ACCOUNT INTEREST AMOUNT NO_USER
---------- ---------- ---------- ---------- ---------- ------- -------10 ICICI
Delhi
34
2 56000
5
20 HDFC
Agra
56
5 43255
5
30 IDBI
Delhi
77
3 67345
40 HDFC
Jaipur
89
7 87623
5
50 YES
Nagpur
20
5 45213
Q12. WAQ to list the details of bank whose no of user column contains null
value.
SQL> select * from Surbhi_bank where no_user is NULL;
ID NAME
BRANCH
ACCOUNT INTEREST AMOUNT NO_USER
---------- ---------- ---------- ---------- ---------- ------- -------30 IDBI
Delhi
77
3 67345
50 YES
Nagpur
20
5 45213
Question 4. Consider the following student relation and write SQL commands / output for
the following queriesNo.
1
2
3
Name
Pankaj
Shalini
Sanjay
Age
24
21
22
Department
Computer
History
Hindi
Dateofadm
10/01/97
24/03/98
12/12/96
Fee
120
200
300
Sex
M
F
M
4
5
6
7
8
Sudha
Rakesh
Shakeel
Surya
Shikha
25
22
30
34
23
History
Hindi
History
Computer
Hindi
01/07/99
05/09/97
27/06/98
25/02/97
31/07/97
400
250
300
210
200
F
M
M
M
F
1 row created.
SQL> insert into student values(5,'Rakesh',22,'Hindi','05-Sep-1997',250,'M');
1 row created.
SQL> insert into student values(6,'Shakeel',30,'History','27-Jun-1998',300,'M'
1 row created.
SQL> insert into student values(7,'Surya',34,'Computer','25-Feb-1997',210,'M')
1 row created.
SQL> insert into student values(8,'Shikha',23,'Hindi','31-Jul-1997',200,'F');
1 row created.
SQL> select * from student;
NO NAME
AGE DEPARTMENT DATEOFADM
FEE SEX
---------- ---------- ---------- ---------- --------- ---------- ----1 Pankaj
24 Computer 10-JAN-97
120 M
2 Shalini
21 History 24-MAR-98
200 F
3 Sanjay
22 Hindi
12-DEC-96
300 M
4 Sudha
25 History 01-JUL-99
400 F
5 Rakesh
22 Hindi
05-SEP-97
250 M
6 Shakeel
30 History 27-JUN-98
300 M
7 Surya
34 Computer 25-FEB-97
210 M
8 Shikha
23 Hindi
31-JUL-97
200 F
8 rows selected.
Q1. WAQ to show all information about the students of history department.
SQL> select * from student where Department = 'History';
NO NAME
AGE DEPARTMENT DATEOFADM
---------- ---------- ---------- ---------- --------- ---------- ----2 Shalini
21 History 24-MAR-98
200 F
4 Sudha
25 History 01-JUL-99
400 F
6 Shakeel
30 History 27-JUN-98
300 M
FEE SEX
Q2. WAQ to list the names of female students who are in Hindi department.
SQL> select Name from student where Sex = 'F' AND Department = 'History';
NAME
---------Shalini
Sudha
Q3. WAQ to list names of all students with their date of admission in ascending order.
SQL> select Name,Dateofadm from student order by name;
NAME
DATEOFADM
---------- --------Pankaj
10-JAN-97
Rakesh
05-SEP-97
Sanjay
12-DEC-96
Shakeel 27-JUN-98
Shalini 24-MAR-98
Shikha
31-JUL-97
Sudha
01-JUL-99
Surya
25-FEB-97
8 rows selected.
Q4. WAQ to display students Name, Fee, Age for male students only.
SQL> select Name,Fee,Age from student where Sex = 'M';
---------- ---------- ---------Pankaj
120
Sanjay
300
Rakesh
250
Shakeel
300
Surya
210
24
22
22
30
34
Q5. WAQ to count the number of student with age < 23.
SQL> select count(no) from student where Age > 23;
COUNT(NO)
---------4
Q6. WAQ to insert a new row in the student table with the following data: 9,Zaheer,36,Computer,
{12/03/97}, 230, M.
SQL> insert into student values(9,'Zaheer',36,'Computer','12-Mar-1997',230,'M');
1 row created.
SQL> select * from student;
NO NAME
FEE SEX
rows selected.
Q10. Select sum (fee) from student where dateofadm < {01/01/98}.
SQL> select sum (fee) from student where Dateofadm < '01-Jan-1998';
SUM(FEE)
---------1310
Book_ID
C0001
F0001
T0001
T0002
F0002
Book_Name
Fast Cook
The Tears
My first c++
C++ Brainworks
Thunderbolts
Author_Name
Lata Kapoor
William Hopkins
Brian & Brooke
A.W. Rossaine
Anna Roberts
Book_id
T0001
C0001
F0001
Publishers
EPB
First Publ.
EPB
TDH
First publ.
Price
355
650
350
350
750
Type
Cookery
Fiction
Text
Text
Fiction
Qty
5
20
10
15
50
Quantity_Issued
4
5
2
Write SQL queries for:1. To show book name, author name and price of book of First Publ.
publishers.
2. To list the names from books of text type.
3. To display the names and price from books in ascending order of their price.
4. To increase the price of all books of EPB publishers by 50.
5. To display the Book_Id, Book_Name and Quantity_Issued for all books
which have been issued.
6. To insert a new row in the table issued during the following data: F0003,1
7. select count(*) from book.
8. select max(Price) from books where quantity >= 15.
9. select book_Name, Author_Name from book where Publishers = EPB.
10.select count (Distinct Publishers) from books where price > = 400;
8 Type varchar(10),
9 Qty number(5)
10 );
Table created.
SQL> insert into Library values('C0001','Fast Cook','Lata Kapoor','EPB',355,'Cookery',5);
1 row created.
SQL> insert into Library values('F0001','The Tears','William Hopkins','First
Publ.',650,'Fiction',20);
1 row created.
SQL> insert into Library values('T0001','My First C++','Brian &
Brooke','EPB',350,'Text',10);
1 row created.
SQL> insert into Library values('T0002','C++ Brainworks','A.W.
Rossaine','TDH',350,'Text',15);
1 row created.
SQL> insert into Library values('F0002','Thunderbolts','Anna Roberts','First
Publ.',750,'Fiction',50);
1 row created.
SQL> select * from library;
BOOK_ID BOOK_NAME
AUTHOR_NAME
PUBLISHERS
PRICE TYPE
-------- --------------- -------------------- --------------- ---------- ---------- ---------C0001 Fast Cook
Lata Kapoor
EPB
355 Cookery
5
F0001 The Tears
William Hopkins
First Publ.
650 Fiction
20
T0001 My First C++ Brian & Brooke
EPB
350 Text
10
T0002 C++ Brainworks A.W. Rossaine
TDH
350 Text
15
F0002 Thunderbolts Anna Roberts
First Publ.
750 Fiction
50
QTY
PRICE
650
750
Q3. To display the names and price from books in ascending order of their
price.
SQL> select Book_Name,Price from Library order by Price;
BOOK_NAME
PRICE
--------------- ---------My First C++
350
C++ Brainworks
350
Fast Cook
355
The Tears
650
Thunderbolts
750
QTY
Q5. To display the Book_Id, Book_Name and Quantity_Issued for all books
which have been issued.
SQL> select Library.Book_ID, Library.Book_Name,Books.Quantity_Issued from
Library,Books where Library.Book_ID = Books.Book_ID;
BOOK_ID
-----------------------------C0001
F0001
T0001
BOOK_NAME
QUANTITY_ISSUED
--------------- --------------Fast Cook
5
The Tears
2
My First C++
4
Q6. To insert a new row in the table issued during the following data:
F0003,1
SQL> insert into Books values('F0003',1);
1 row created.
SQL> select * from Books;
BOOK_ID
QUANTITY_ISSUED
------------------------------ --------------T0001
4
C0001
5
F0001
2
F0003
1
Q7. select count(*) from book.
Question 6: Create the following tables such that Empno. and Sno. Are not null and
unique. Salary is between 4000and 10000.
Solve the given queries:
SURBHI_EMP
EmpNo.
123
127
124
125
128
129
Name
Amit
Manoj
Abhay
Vinod
Abhi
Ramesh
DOB
23-Jan-1965
12-Dec-1976
11-Aug-1975
04-Apr-1977
10-Mar-1974
28-Oct-1981
N_Place
Delhi
Mumbai
Allahabad
Delhi
Mumbai
Pune
Hobbies
Music
Writing
Music
Sports
Gardening
Sports
STAR
SNo.
123
127
124
125
128
Area
Agra
Mathura
Agra
Delhi
Pune
Appointment
25-Jan-2006
22-Dec-2006
19-Aug-2007
14-Apr-2004
13-Mar-2008
Salary
5000
6000
5500
8500
7500
Retirement
25-Jan-2026
22-Dec-2026
19-Aug-2016
14-Apr-2018
13-Mar-2028
Dept
Marketing
Finance
Marketing
Sales
Sales
Queries:-
1.
2.
3.
4.
Show empno, name and salary of those who have sports as hobby.
Show number of employee area wise.
Show sno, name, hobby and salary in descending order of salary.
Show the appointment date and native place of those whose name starts with
A or ends in d.
DOB
N_PLACE
HOBBIES
123 Amit
23-JAN-65 Delhi
Music
127 Manoj
12-DEC-76 Mumbai
124 Abhay
125 Vinod
04-APR-77 Delhi
128 Abhi
10-MAR-74 Mumbai
129 Ramesh
Writing
Sports
28-OCT-81 Pune
Gardening
Sports
APPOINTMENT
25-JAN-06
22-DEC-06
124 Agra
19-AUG-07
125 Delhi
14-APR-04
128
Pune
13-MAR-08
Q1. Write a query to show empno, name and salary of those who have Sports
as hobby.
SQL>
select
empno,name,salary
from
surbhi_emp.empno=star.sno AND Hobbies = 'Sports';
EMPNO NAME
surbhi_emp,star
where
SALARY
Vinod
8500
COUNT(EMPNO)
-----------2
1
1
1
Q3. Write a query to show sno, name, salary and hobby in descending order of
salary.
SQL> select sno,name,salary,hobbies from surbhi_emp,star where
surbhi_emp.empno=star.sno order by salary desc;
SNO NAME
SALARY HOBBIES
8500 Sports
128 Abhi
7500 Gardening
127 Manoj
6000 Writing
124 Abhay
123 Amit
5500 Music
5000 Music
Q4. Show the appointment date and native place of those whose name starts
with A or ends in d.
SQL> select Appointment,N_Place from star,surbhi_emp where
surbhi_emp.empno=star.sno AND Name like 'A%' OR Name like '%d';
APPOINTMENT
N_PLACE
-------------------- ---------25-JAN-06
Delhi
25-JAN-06
Delhi
22-DEC-06
Delhi
19-AUG-07
Allahabad
19-AUG-07
Delhi
14-APR-04
Delhi
13-MAR-08
Delhi
13-MAR-08
Mumbai
8 rows selected.
Question 7. Create the table as shown below and perform the following query:-
Deptno
10
dname
ACCOUNTING
RESEARCH
SALES
OPERATIONS
20
30
40
empno
ename
Job
mgr
7839
KING
null
7698
BLAKE
PRESIDEN
T
MANAGER
7839
loc
NEW YORK
DALLAS
CHICAGO
BOSTON
hiredat
e
17-111981
1-5-1981
sal
comm
deptno
5000
null
10
2850
null
30
7782
CLARK
MANAGER
7839
9-6-1981
2450
null
10
7566
7788
7902
JONES
SCOTT
FORD
MANAGER
ANALYST
ANALYST
7839
7566
7566
2-4-1981
13-JUL-87
3-12-1981
2975
3000
3000
null
null
null
20
20
20
7369
SMITH
CLERK
7902
800
null
20
7499
ALLEN
7698
1600
300
30
7521
WARD
7698
22-2-1981
1250
500
30
7654
MARTIN
7698
28-9-1981
1250
1400
30
7844
TURNER
7698
8-9-1981
1500
30
7876
7900
7934
ADAMS
JAMES
MILLER
SALESMA
N
SALESMA
N
SALESMA
N
SALESMA
N
CLERK
CLERK
CLERK
17-121980
20-2-1981
7788
7698
7782
13-JUL-87
3-12-1981
23-1-1982
51
null
null
null
20
30
10
950
1300
Queries:-
1. Display the names of all the employees who are working as clerks and
drawing a salary more than 3000.
2. Display the names of employees who are working as clerks,salesman or
analyst and drawing a salary more than 3000.
3. Display the list of employees who have joined the company before
30-JUN-90 or after 31-DEC-90.
4. Display the names of employees working in depart number 10 or 20 or 40
or employees working as CLERKS,SALESMAN or ANALYST.
5. Display name,salary,hra,pf,da,total salary for each employee. The output
should be in the order of total salary,hra 15% of salary,da 10% of salary,pf
5% salary,total salary will be(salary+hra+da)-pf.
6. Display depart numbers and total number of employees working in each
department.
7. Display the various jobs and total salary for each job.
8. Display the total salary drawn by ANALYST working in depart number 40.
9. Display the names of employees whose names have second alphabet A in
their names.
10.Display the maximum salary being paid to CLERK.
11.Display the names of the employee in descending order of salary.
2
3
4
5
6
7
8
9
10
11
12
(
empno number(4),
ename varchar(10),
job varchar(12),
mgr number(10),
hiredate date,
sal number(8),
comm number(8),
deptno number(2),
constraint fk_deptno foreign key (deptno) references department (deptno)
);
Table created.
SQL> insert into employee values(7839, 'KING', 'PRESIDENT', null,to_date('17-111981','dd-mm-yyyy'),5000, null, 10);
1 row created.
SQL> insert into employee values(7698, 'BLAKE', 'MANAGER', 7839,to_date('1-51981','dd-mm-yyyy'),2850, null, 30);
1 row created.
SQL> insert into employee values(7782, 'CLARK', 'MANAGER', 7839,to_date('9-61981','dd-mm-yyyy'),2450, null, 10);
1 row created.
SQL> insert into employee values(7566, 'JONES', 'MANAGER', 7839,to_date('2-41981','dd-mm-yyyy'),2975, null, 20);
1 row created.
SQL> insert into employee values(7788, 'SCOTT', 'ANALYST', 7566,'13-JUL-87',3000,
null, 20);
1 row created.
SQL> insert into employee values(7902, 'FORD', 'ANALYST', 7566,to_date('3-121981','dd-mm-yyyy'),3000, null, 20);
1 row created.
SQL> insert into employee values(7369, 'SMITH', 'CLERK', 7902,to_date('17-121980','dd-mm-yyyy'),800, null, 20);
1 row created.
SQL> insert into employee values(7499, 'ALLEN', 'SALESMAN', 7698,to_date('20-21981','dd-mm-yyyy'),1600, 300, 30);
1 row created.
SQL> insert into employee values(7521, 'WARD', 'SALESMAN', 7698,to_date('22-21981','dd-mm-yyyy'),1250, 500, 30);
1 row created.
SQL> insert into employee values(7654, 'MARTIN', 'SALESMAN', 7698,to_date('28-91981','dd-mm-yyyy'),1250, 1400, 30);
1 row created.
SQL> insert into employee values(7844, 'TURNER', 'SALESMAN', 7698,to_date('8-91981','dd-mm-yyyy'),1500, 0, 30);
1 row created.
SQL> insert into employee values(7876, 'ADAMS', 'CLERK', 7788,'13-JUL-87',1100, null,
20);
1 row created.
SQL> insert into employee values(7900, 'JAMES', 'CLERK', 7698,to_date('3-12-1981','ddmm-yyyy'),950, null, 30);
1 row created.
SQL> insert into employee values(7934, 'MILLER', 'CLERK', 7782,to_date('23-11982','dd-mm-yyyy'),1300, null, 10);
1 row created.
7521
7654
7844
7876
7900
7934
WARD
SALESMAN
MARTIN
SALESMAN
TURNER
SALESMAN
ADAMS
CLERK
JAMES
CLERK
MILLER
CLERK
7698 22-FEB-81
1250
7698 28-SEP-81
1250
7698 08-SEP-81
1500
7788 13-JUL-87
1100
7698 03-DEC-81
950
7782 23-JAN-82
1300
500
1400
0
30
30
30
20
30
10
Q1. Display the names of all the employees who are working as clerks and
drawing a salary more than 3000.
SQL> select ename from employee where job=CLERK and sal>3000;
no rows selected
Q2. Display the names of employees who are working as clerks, salesman or
analyst and drawing a salary more than 3000.
SQL> select ename from employee where job=CLERK OR job=SALESMAN OR
job=ANALYST AND SAL>3000;
ENAME
---------SMITH
ALLEN
WARD
MARTIN
TURNER
ADAMS
JAMES
MILLER
8 rows selected.
Q3. Display the list of employees who have joined the company before 30-JUN90 or after 31-DEC-90.
SQL> select ename from employee where hiredate < 30-JUN-1990 or hiredate >31DEC-90;
ENAME
---------KING
BLAKE
CLARK
JONES
SCOTT
FORD
SMITH
ALLEN
WARD
MARTIN
TURNER
ADAMS
JAMES
MILLER
14 rows selected.
Q4.Display the names of employees working in depart number 10 or 20 or 40
or employees working as CLERKS, SALESMAN or ANALYST.
SQL> select ename from employee where deptno in(10,20,30) or job
in('CLERK','SALESMAN','ANALYST');
ENAME
---------KING
BLAKE
CLARK
JONES
SCOTT
FORD
SMITH
ALLEN
WARD
MARTIN
TURNER
ADAMS
JAMES
MILLER
14 rows selected.
Q5. Display name,salary,hra,pf,da,total salary for each employee. The output
should be in the order of total salary, hra 15% of salary, da 10% of salary, pf
5% salary, total salary will be(salary+hra+da)-pf.
SQL> select ename,sal,sal/100*15 as hra,sal/100*5 as pf,sal/100*10 as da,
sal+sal/100*15+sal/100*10-sal/100*5 as total from employee;
ENAME
SAL
HRA
---------- ---------- ---------- ---------KING
5000
750
BLAKE
2850
427.5
CLARK
2450
367.5
JONES
2975
446.25
SCOTT
3000
450
FORD
3000
450
PF
DA
TOTAL
---------- ---------250
500
6000
142.5
285
3420
122.5
245
2940
148.75
297.5
3570
150
300
3600
150
300
3600
SMITH
ALLEN
WARD
MARTIN
TURNER
ADAMS
JAMES
MILLER
800
120
1600
240
1250
187.5
1250
187.5
1500
225
1100
165
950
142.5
1300
195
40
80
62.5
62.5
75
55
47.5
65
80
960
160
1920
125
1500
125
1500
150
1800
110
1320
95
1140
130
1560
Q6. Display depart numbers and total number of employees working in each
department.
SQL> select deptno,count(deptno)from employee group by deptno;
DEPTNO COUNT(DEPTNO)
---------- ------------30
6
20
5
10
3
Q7. Display the various jobs and total salary for each job.
SQL> select job,sum(sal) from employee group by job;
JOB
SUM(SAL)
------------ ---------CLERK
4150
SALESMAN
5600
PRESIDENT
5000
MANAGER
8275
ANALYST
6000
Q8. Display the total salary drawn by ANALYST working in depart number 40.
SQL> select sum(sal) from employee where job=ANALYST and deptno=40;
SUM(SAL)
---------Q9. Display the names of employees whose names have second alphabet A in
their names.
SQL> select ename from employee where ename like '_A%';
ENAME
---------WARD
MARTIN
JAMES
Q10. Display the maximum salary being paid to CLERK.
SQL> select max(sal) from employee where job = 'CLERK';
MAX(SAL)
---------1300
Q11. Display the names of the employee in descending order of salary.
SQL> select ename,sal from employee order by sal;
ENAME
SAL
---------- ---------SMITH
800
JAMES
950
ADAMS
1100
MARTIN
1250
WARD
1250
MILLER
1300
TURNER
1500
ALLEN
1600
CLARK
2450
BLAKE
2850
JONES
2975
FORD
3000
SCOTT
3000
KING
5000
14 rows selected.
Q12. Display the name of the employee along with their annual
salary(sal*12).The name of the employee earning highest annual salary
should appear first.
SQL> select ename,sal*12 as Annualsal from employee;
ENAME
ANNUALSAL
---------- ---------KING
60000
BLAKE
34200
CLARK
29400
JONES
35700
SCOTT
36000
FORD
36000
SMITH
9600
ALLEN
19200
WARD
MARTIN
TURNER
ADAMS
JAMES
MILLER
15000
15000
18000
13200
11400
15600
14 rows selected.
Q13. Display the depart numbers and total salary for each department.
SQL> select deptno,sum(sal) from employee group by deptno;
DEPTNO SUM(SAL)
---------- ---------30
9400
20
10875
10
8750
Q14. Display the depart numbers and max salary for each department.
SQL> select deptno,max(sal) from employee group by deptno;
DEPTNO MAX(SAL)
---------- ---------30
2850
20
3000
10
5000
Q15. Display the various jobs and total salary for each job.
SQL> select job,sum(sal) from employee group by job;
JOB
SUM(SAL)
------------ ---------CLERK
4150
SALESMAN
5600
PRESIDENT
5000
MANAGER
8275
ANALYST
6000
Q2. Display the names of salesman who earns a salary more than the highest
salary of any clerk.
SQL> select ename from employee where job='SALESMAN' and sal>(select max(sal)
from employee where job='CLERK');
ENAME
---------ALLEN
TURNER
Q3. Display the names of clerks who earn a salary more than the lowest salary
of any salesman.
SQL> select ename from employee where job='CLERK' and sal>(select min(sal) from
employee where job='SALESMAN');
ENAME
---------MILLER
Q4. Display the names of the employees who earn highest salary in their
respective departments.
SQL> select ename,sal,deptno from employee where sal in(select max(sal) from
employee group by deptno);
ENAME
SAL DEPTNO
---------- ---------- -------KING
5000
10
BLAKE
2850
30
SCOTT
3000
20
FORD
3000
20
Q5. Display the employee names who are working in accounting department.
SQL> select ename from department,employee where dept.deptno = emp.deptno AND
Dname = 'ACCOUNTING';
ENAME
---------KING
CLARK
MILLER
Q6. Display the names of employees from department number 10 with salary
greater than that of any employee working in other department.
SQL> select ename from employee where deptno=10 and sal>any(select sal from
employee where deptno not in 10);
ENAME
---------KING
CLARK
MILLER
Q7. Display the names of the employees from department number 10 with
salary greater than that of all employee working in other departments.
SQL> select ename from employee where deptno=10 and sal>all(select sal from
employee where deptno not in 10);
ENAME
---------KING
Q8. Display the maximum salary being paid to depart number 20.
SQL> select max(sal) from employee where deptno = 20;
MAX(SAL)
---------3000
Q9. Display the average salary drawn by MANAGERS.
SQL> select avg(sal) from employee where job=MANAGER;
AVG(SAL)
---------2758.33333
Question 9. Create the table(s) as shown below and perform the following query:Student
Snum
101
102
103
104
105
Class
Cname
First
Second
Third
Fourth
Enrolled
Sname
101
102
103
104
105
Sname
Harsh
Divakar
Stella
Shikha
gunjan
Major
History
Maths
Science
History
Maths
Meets_at
Nine
Eleven
Ten
Nine
Cname
First
Second
First
Third
Second
Level
JR
JR
SR
JR
SR
Room
R128
R101
R128
R121
Faculty
FID
201
202
203
Age
12
14
15
16
16
FID
201
202
203
203
Fname
I.Teach
J.Peter
S.Basra
Dept_id
1
1
3
Queries:-
1. Find the name of all juniors (level = JR) who are enrolled in a class taught
by I.teach.
2. Find the age of the oldest student who is either a history major or enrolled in
a course taught by I.teach.
3. Find the names of all classes that either meet in room R128 or have five or
more students enrolled.
4. Find the names of all students who are enrolled in two classes tht meet at the
same time.
5. Find the names for faculty members for whom the combined enrolment of
the courses that they teach is less than five.
6. Print the level and the average age of students for that level, for each level.
7. Print the level and the average age of students for that level, for all levels
except JR.
8. Find the names of students enrolled in the maximum number of classes.
9. Find the names of students not enrolled in any class.
3
4
5
6
7
8
snum number(5),
sname varchar(10),
major varchar(10),
lev varchar(10),
age number(3)
);
Table created.
SQL> insert into stud values(101,'harsh','history','jr',12);
1 row created.
SQL> insert into stud values(102,'divakar','maths','jr',14);
1 row created.
SQL> insert into stud values(103,'stella','science','sr',15)
1 row created.
SQL> insert into stud values(104,'shikha','history','jr',16)
1 row created.
SQL> insert into stud values(105,'gunjan','maths','sr',16);
1 row created.
SQL> select * from stud;
SNUM SNAME
MAJOR
LEV
AGE
---------- ---------- ---------- ---------- ---------101 harsh
history jr
12
102 divakar maths
jr
14
103 stella
science sr
15
104 shikha
history jr
16
105 gunjan
maths
sr
16
SQL> create table class
2 (
3 cname varchar(10),
4 meets varchar(10),
5 room varchar(10),
6 fid number(5)
7 );
Table created.
SQL> insert into class values('first','nine','r128',201);
1 row created.
SNUM CNAME
---------- ---------101 first
102 second
103 first
104 third
105 second
SQL> create table faculty
2 (
3 fid number(5),
4 fname varchar(10),
5 deptid number(2)
6 );
Table created.
SQL> insert into faculty values(201,'i.tech',1);
1 row created.
SQL> insert into faculty values(202,'j.peter',1);
1 row created.
SQL> insert into faculty values(203,'s.basra',3);
1 row created.
SQL> select * from faculty;
FID FNAME
DEPTID
---------- ---------- ---------201 i.tech
1
202 j.peter
1
203 s.basra
3
Q1. Find the name of all juniors (level = JR) who are enrolled in a class taught
by I.teach.
SQL> select distinct s.sname from stud s, class c, enrolled e, faculty f where s.snum =
e.snum and e.cname= c.cname and c.fid = f.fid and f.fname ='i.tech' and s.lev ='jr';
SNAME
---------harsh
Q2. Find the age of the oldest student who is either a history major or
enrolled in a course taught by I.teach.
SQL> select max(s.age) from student s where (s.major ='history') or s.snum in (select
e.snum from class c, enrolled e, faculty f where e.cname= c.cname and c.fid = f.fid and
f.fname ='i.tech');
MAX(S.AGE)
---------16
Q3. Find the names of all classes that either meet in room R128 or have five
or more students enrolled.
SQL> select c.cname from class c where c.room ='r128' or c.cname in (select e.cname
from enrolled e group by e.cname having count(*) >= 5);
CNAME
---------first
third
Q4. Find the names of all students who are enrolled in two classes that meet
at the same time.
SQL> select distinct s.sname from student s where s.snum in ( select e1.snum from
enrolled e1, enrolled e2, class c1, class c2 where e1.snum = e2.snum and e1.cname <
> e2.cname and e1.cname = c1.cname and e2.cname = c2.cname and c1.meets =
c2.meets);
no rows selected
Q5. Find the names for faculty members for whom the combined enrolment of
the courses that they teach is less than five.
SQL> select distinct f.fname from faculty f where 5 > (select count(e.snum) from class
c, enrolled e where c.cname = e.cname and c.fid = f.fid);
FNAME
----------
i.tech
j.peter
s.basra
Q6. Print the level and the average age of students for that level, for each
level.
SQL> select s.lev, avg(s.age) from student s group by s.lev;
LEV
AVG(S.AGE)
---------- ---------sr
15.5
jr
14
Q7. Print the level and the average age of students for that level, for all levels
except JR.
SQL> select s.lev, avg(s.age) from students s where s.lev <> 'jr' group by s.lev;
LEV
AVG(S.AGE)
---------- ---------sr
15.5
Q8. Find the names of students enrolled in the maximum number of classes.
SQL> select distinct s.sname from student s where s.snum in (select e.snum from
enrolled e group by e.snum);
SNAME
---------gunjan
divakar
harsh
shikha
stella
Q9. Find the names of students not enrolled in any class.
SQL> select distinct s.sname from student s where s.snum not in (select e.snum from
enrolled e);
no rows selected
Question 10. Create the table(s) as shown below and perform the following query:-
Works
Pname
Harry
Sumit
Cname
FBC
JBC
Salary
2500
10000
Lives
Name
Harry
Sumit
Street
Raj nagar
Shastri Nagar
City
GZB
FBD
Located_in
Cname
city
FBC
noida
JBC
GZB
Jyoti
Sarah
Robert
FBC
SBC
SBC
25230
3500
26323
Jyoti
Sarah
Robert
Sector 19
Raj Nagar
Sector 16
Noida
FBD
noida
SBC
FBD
Queries:-
PNAME
CNAME
SALARY
---------- ----- ----------
Harry
FBC
2500
Sumit
JBC
10000
Jyoti
FBC
25230
Sarah
SBC
3500
Robert
SBC
26323
Noida
Sarah
Raj nagar
FBD
Robert
Sector 16
Noida
Jyoti
Noida
Q3. Find the persons who works for company FBC with a salary of more than
10000 along with the street and cities where they live.
SQL> select Pname,Street,City from Works,Lives where Works.Pname = Lives.Name
AND Cname = 'FBC' AND Salary > 10000;
PNAME
STREET
CITY
---------- --------------- ---------Jyoti
Sector 19
Noida
Q4. Find the names of the persons who do not work for company FBC.
SQL> select Pname from Works where Cname != 'FBC';
PNAME
---------Sumit
Sarah
Robert
1.
2.
3.
4.
5.
To reverse a number and print , i.e, if num is 677 then it should print 776.
To print a Fibonacci series.
To check a number is Armstrong or not.
To print the factorial of a given number.
To evaluate whether a given number is prime or not.
5. i number(3);
6. begin
7. for
8. loop
9. dbms_output.put_line(a ,b);
10. c:=a+b;
11. a :=b;
12. b:=c;
13. i:=i+1;
14. end loop;
15. end;
16. /
0
1
1
2
3
5
8
13
21
34
PL/SQL procedure successfully completed.
Q3. To check a number is Armstrong or not.
SQL> set serveroutput on;
SQL> declare
2 pnum number(5);
3 tot number(5);
4 lp number(3);
5 tmp number(5);
6 begin
7 pnum:=&pnum;
8 tmp:=pnum;
9 tot:=0;
10 while tmp>0
11 loop
12 lp:=tmp mod 10;
13 tot:= tot + (lp*lp*lp);
14 tmp:=floor(tmp/10);
15 end loop;
16 if(tot = pnum) then
17 dbms_output.put_line(pnum||' is armstrong.');
18 else
19 dbms_output.put_line(pnum||' is not armstrong.');
20 end if;
21 end;
22 /
Enter value for pnum: 153
old 7: pnum:=&pnum;
new 7: pnum:=153;
153 is armstrong.
PL/SQL procedure successfully completed.
Q4. To print the factorial of a given number.
SQL> set serveroutput on;
SQL> declare
2 n number;
3 i number;
4 f number:=1;
5 begin
6 n:=&n;
7 for i in 1..n
8 loop
9 f:=f*i;
10 end loop;
11 dbms_output.put_line(n||'! = '||f);
12 end;
13 /
Enter value for n: 5
old 6: n:=&n;
new 6: n:=5;
5! = 120
PL/SQL procedure successfully completed.
Q5. To evaluate whether a given number is prime or not.
SQL> set serveroutput on;
SQL> declare
2 n number;
3 i number;
4 counter number;
5 begin
6 n:=&n;
7 i:=1;
8 counter:=0;
9 if n=1 then
10 dbms_output.put_line('1 is neither prime nor composite.');
11 elsif n=2 then
12 dbms_output.put_line('2 is even prime');
13 else
14 for i in 1..n loop
15 if mod(n,i)=0 then
16 counter:=counter+1;
17 end if;
18 end loop;
19 end if;
20 if counter=2 then
21 dbms_output.put_line(n||' is a prime No.');
22 else
23 dbms_output.put_line(n||' is a not prime No.');
24 end if;
25 end;
26 /
Enter value for n: 5
old 6: n:=&n;
new 6: n:=5;
5 is a prime No.
PL/SQL procedure successfully completed.
Q7. To get a number from keyboard and if it zero print natural number, else
print not a natural number.
SQL> set serveroutput on;
SQL> declare
2 n number;
3 begin
4 n:=&n;
5 if (n = 0) then
6 dbms_output.put_line('Natural No.');
7 else
8 dbms_output.put_line('Not a Natural No.');
9 end if;
10 end;
11 /
Enter value for n: 0
old 4: n:=&n;
new 4: n:=0;
Natural No.
PL/SQL procedure successfully completed.
Q8. To find the area and perimeter of given circle.
SQL> set serveroutput on;
SQL> create or replace function area
2 (r IN number)
3 return number
4 As
5 a number(5);
6 begin
7 a:= (3.14*r*r);
8 return t;
9 dbms_output.put_line('Area = '||t);
10 end area;
11 /
Function created.
SQL> create or replace function perimeter
2 (r IN number)
3 return number
4 As
5 p number(5);
6 begin
7 p:= (2*3.14*r);
8 return p;
9 dbms_output.put_line('Perimeter = '||p);
10 end perimeter;
11 /
Function created.
SQL> declare
2 r number(5);
3 a number(5);
4 p number(5);
5 begin
6 r:=&radius;
7 a:=area(r);
8 dbms_output.put_line('Area = '||a);
9 p:=perimeter(r);
10 dbms_output.put_line('Perimeter = '||p);
11 end;
12 /
No Data Found
PL/SQL procedure successfully completed.