Sie sind auf Seite 1von 57

Question 1: Create the following table and solve the given queries:

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:-

1. Write a query to select customer Id and name of those customers belonging


to Germany.
2. Write a query to display complete information of customer whose
Amount>5000.
3. Write a query to select customer Id and country of customer whose name
containing a substring as et.
4. Write a query to display the average of amount of all customer.
5. Write a query to display the complete information of Peter.
6. Write a query to display the information of customer whose Amount>5000
and Amount<7000.
7. Write a query to select customer Id and state of those customers whose name
contains h as a third character.
8. Write a query to display the maximum amount.
9. Write a query to display the complete information of customer belonging to
Australia.
10.Write a query to display name of customer whose Amount>2000 and
Amount<5000.
11.Write a query to select customer Id and phone of customer whose name
starts with Pe.
12.Write a query to display maximum amount for country Germany.
13.Write a query to display the complete information of Smith.
14.Write a query to select customer Id and state of customer whose name
contain o as a second character.
15.Write a query to select customer Id and country of customer whose name
containing oh as a substring.

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

Q12. Write a query to display maximum amount for country Germany.


SQL> select max(Amount) as Maximum from Customer where Country = 'Germany';
MAXIMUM
---------8977
Q13. Write a query to display the complete information of Smith.
SQL> select * from Customer where Name = 'Smith';
CUST_ID NAME
STATE
COUNTRY
AMOUNT
PHONE
---------- ---------- ---------- ---------- ---------- ---------33
Smith
Phoenix
Australia
4523
34558
Q14. Write a query to select customer Id and state of customer whose name contain o as a second
character.
SQL> select Cust_Id,State from Customer where Name like '_o%';
CUST_ID STATE
---------- -------------------11 Caribou
Q15. Write a query to select customer Id and country of customer whose name containing oh as a
substring.
SQL> select Cust_Id,Country from Customer where Name like '%oh%';
CUST_ID COUNTRY
---------- -------------------11 Germany

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:-

1. Write a query to display complete information of ICICI Bank.


2. Write a query to select Id and name whose amount>50000.
3. Write a query to select name whose branch has pur as a substring.
4. Write a query to display maximum of amount as MAXIMUM.
5. Write a query to display name and branch whose number of account>50.
6. Write a query to display average of amount as AVERAGE for Delhi Branch.
7. Write a query to select name whose branch name has g as a substring.
8. Write a query to select minimum amount as MINIMUM among all banks.
9. Write a query to display Id and name whose interest>5 and interest<8.
10.WAQ to display branch name whose amount > 20000 and < 55000.
11.Write a query to count Id of HDFC bank.
12.Write a query to display the sum of amount as SUM for Delhi branch.
13.Write a query to update Delhi branch by Bangalore where amount>60000.
14.Write a query to delete information of Yes bank.
15.Write a query to display name of bank where branch is Delhi and
amount>50000.
16.Write a query to display branch of bank belongs to HDFC and city may be
Agra or Jaipur.
Solution:
SQL> create table Surbhi_bank
2 (
3 ID number(10),
4 NAME varchar(20),
5 BRANCH varchar(20),
6 ACCOUNT number(20),
7 INTEREST number(20),
8 Amount number(20)
9 );
Table created.
SQL> insert into Surbhi_bank values(10,'ICICI','Delhi',34,2,56000);
1 row created.
SQL> insert into Surbhi_bank values(20,'HDFC','Agra',56,5,43255);
1 row created.

SQL> insert into Surbhi_bank values(30,'IDBI','Delhi',77,3,67345);


1 row created.
SQL> insert into Surbhi_bank values(40,'HDFC','Jaipur',89,7,87623);
1 row created.
SQL> insert into Surbhi_bank values(50,'YES','Nagpur',20,5,45213);
1 row created.
SQL> select * from Surbhi_bank;
ID NAME
BRANCH
ACCOUNT INTEREST AMOUNT
---------- ---------- ---------- ---------- ---------- ------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
Q1. Write a query to display complete information of ICICI Bank.
SQL> select * from Surbhi_bank where Name = 'ICICI';
ID NAME
BRANCH
ACCOUNT INTEREST AMOUNT
---------- ---------- ---------- ---------- ---------- ------10 ICICI
Delhi
34
2 56000
Q2. Write a query to select Id and name whose amount>50000.
SQL> select ID,Name from Surbhi_bank where Amount > 50000;
ID NAME
---------- ---------10 ICICI
30 IDBI
40 HDFC

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.

SQL> select * from Surbhi_bank;


ID NAME
BRANCH
ACCOUNT INTEREST AMOUNT
---------- ---------- ---------- ---------- ---------- ------10 ICICI
Delhi
34
2 56000
20 HDFC
Agra
56
5 43255
30 IDBI
Bangalore
77
3 67345
40 HDFC
Jaipur
89
7 87623

Q14. Write a query to delete information of Yes bank.


SQL> delete from Surbhi_bank where Name = 'YES';
1 row deleted.
SQL> select * from Surbhi_bank;
ID NAME
BRANCH
ACCOUNT INTEREST AMOUNT
---------- ---------- ---------- ---------- ---------- ------10 ICICI
Delhi
34
2 56000
20 HDFC
Agra
56
5 43255
30 IDBI
Delhi
77
3 67345
40 HDFC
Jaipur
89
7 87623
Q15. Write a query to display name of bank where branch is Delhi and amount>50000.
SQL> select Name from Surbhi_bank where Branch = 'Delhi' and Amount > 50000;
NAME
---------ICICI
Q16. Write a query to display branch of bank belongs to HDFC and city may be Agra or Jaipur.
SQL> select Branch from Surbhi_bank where Name = 'HDFC' and Branch = 'Agra' or
Branch = 'Jaipur';
BRANCH
---------Agra
Jaipur

Question 3. Refer table Bank as in Q2 and perform the following queries

1.
2.
3.
4.

WAQ to select maximum amount of HDFC bank.


WAQ to display complete information for Delhi branch.
WAQ to find distinct bank name.
WAQ to arrange the data according to amount available.

5. WAQ to delete all data from bank table.


6. WAQ to select name and ID of bank where ID belongs to hdfc or yes bank.
7. WAQ to select name and branch of bank where no. of account between 50
and 90.
8. WAQ to select complete details of all bank whose interest between 2 to 6
and belong to IDBI and HDFC bank.
9. WAQ to add a new column no_user in bank table with char datatype.
10.WAQ to modify the data type of no_user column from char to int.
11.WAQ to update the value no_user = 5 for ICICI and HDFC bank.
12.WAQ to list the details of bank whose no of user column contains null value.
Solution:
Q1. WAQ to select maximum amount of HDFC bank.
SQL> select Max(Amount) from Surbhi_bank where Name = 'HDFC';
MAX(AMOUNT)
----------87623
Q2. WAQ to display complete information for Delhi branch.
ID NAME
BRANCH
ACCOUNT INTEREST AMOUNT
---------- ---------- ---------- ---------- ---------- ------10 ICICI
Delhi
34
2 56000
30 IDBI
Delhi
77
3 67345
Q3. WAQ to find distinct bank name.
SQL> select distinct name from Surbhi_bank;
NAME
-------------------ICICI
HDFC
YES
IDBI

Q4. WAQ to arrange the data according to amount available.


SQL> select * from Surbhi_bank order by Amount;
ID NAME
BRANCH
ACCOUNT INTEREST AMOUNT
---------- ---------- ---------- ---------- ---------- ------20 HDFC
Agra
56
5 43255

50 YES
Nagpur
10 ICICI
Delhi
30 IDBI
Delhi
40 HDFC
Jaipur

20
34
77

5 45213
2 56000
3 67345
89
7 87623

Q5. WAQ to delete all data from bank table.


SQL> delete from Surbhi_bank;
5 rows deleted.
Q6. WAQ to select name and ID of bank where ID belongs to hdfc or yes bank.
SQL> select name,id from Surbhi_bank where name = 'HDFC' OR name='YES';
NAME
ID
-------------------- ---------HDFC
20
HDFC
40
YES
50
Q7. WAQ to select name and branch of bank where no. of account between 50 and 90.
SQL> select name,branch from Surbhi_bank where Account>50 AND Account<90;
NAME
BRANCH
-------------------- -------------------HDFC
Agra
IDBI
Delhi
HDFC
Jaipur
Q8. WAQ to select complete details of all bank whose interest between 2 to 6 and belong to IDBI and
HDFC bank.
ID NAME
BRANCH
ACCOUNT INTEREST AMOUNT
---------- ---------- ---------- ---------- ---------- ------20 HDFC
Agra
56
5 43255
30 IDBI
Delhi
77
3 67345
50 YES
Nagpur
20
5 45213
Q9. WAQ to add a new column no_user in bank table with char datatype.
SQL> alter table Surbhi_bank Add no_user varchar(10);
Table altered.
SQL> select *from Surbhi_bank;
ID NAME
BRANCH
ACCOUNT INTEREST AMOUNT NO_USER
---------- ---------- ---------- ---------- ---------- ------- -------10 ICICI
Delhi
34
2 56000
20 HDFC
Agra
56
5 43255

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. WAQ to show all information about the students of history department.


2. WAQ to list the names of female students who are in Hindi department.
3. WAQ to list names of all students with their date of admission in
ascending order.
4. WAQ to display students Name, Fee, Age for male students only.
5. WAQ to count the number of student with age < 23.
6. WAQ to insert a new row in the student table with the following data:
9,Zaheer,36,Computer, {12/03/97}, 230, M
7. Select count (distinct department) from student;
8. Select max (age) from student where sex =F;
9. Select Avg(fee) from student where dateofadm < {01/01/98};
10.Select sum (fee) from student where dateofadm < {01/01/98};
Solution:SQL> create table Student
2 (
3 No number(5),
4 Name varchar(10),
5 Age number(5),
6 Department varchar(10),
7 Dateofadm date,
8 Fee number(5),
9 Sex varchar(5)
10 );
Table created.
SQL> insert into student values(1,'Pankaj',24,'Computer','10-Jan-1997',120,'M');
1 row created.
SQL> insert into student values(2,'Shalini',21,'History','24-Mar-1998',200,'F'
1 row created.
SQL> insert into student values(3,'Sanjay',22,'Hindi','12-Dec-1996',300,'M');
1 row created.
SQL> insert into student values(4,'Sudha',25,'History','01-Jul-1999',400,'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

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
9 Zaheer
36 Computer 12-MAR-97
230 M
9

rows selected.

Q7. Select count (distinct department) from student.


SQL> select count (distinct department) from student;
COUNT(DISTINCTDEPARTMENT)
------------------------3
Q8. Select max (age) from student where sex =F.
SQL> select max(age) from student where Sex = 'F';
MAX(AGE)
---------25
Q9. Select Avg(fee) from student where dateofadm < {01/01/98}.
SQL> select Avg(fee) from student where Dateofadm < '01-Jan-1998';
AVG(FEE)
---------218.333333

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

Question 5. Given the following tables for a database LIBRARY:

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;

Solution:SQL> create table Library


2 (
3 Book_ID varchar(6) primary key,
4 Book_Name varchar(15),
5 Author_Name varchar(20),
6 Publishers varchar(15),
7 Price number(5),

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

SQL> create table Books


2 (
3 Book_ID varchar(8) primary key,
4 Quantity_Issued number(5)
5 );
Table created.
SQL> insert into Books values('T0001',4);
1 row created.

QTY

SQL> insert into Books values('C0001',5);


1 row created.
SQL> insert into Books values('F0001',2);
1 row created.
SQL> select * from Books;
BOOK_ID
QUANTITY_ISSUED
------------------------------ --------------T0001
4
C0001
5
F0001
2
Q1. To show book name, author name and price of book of First Publ.
publishers.
SQL> select Book_Name,Author_Name,Price from Library where Publishers = 'First
Publ.';
BOOK_NAME
AUTHOR_NAME
--------------- -------------------- ---------The Tears
William Hopkins
Thunderbolts Anna Roberts

PRICE
650
750

Q2. To list the names from books of text type.


SQL> select Book_Name from Library where Type = 'Text';
BOOK_NAME
--------------My First C++
C++ Brainworks

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

Q4. To increase the price of all books of EPB publishers by 50.


SQL> update Library set Price = Price + 50 where Publishers = 'EPB';
2 rows updated.
SQL> select * from Library;
BOOK_ID BOOK_NAME
AUTHOR_NAME
PUBLISHERS
PRICE TYPE
-------- --------------- -------------------- --------------- ---------- ---------- ---------C0001 Fast Cook
Lata Kapoor
EPB
405 Cookery
5
F0001 The Tears
William Hopkins
First Publ.
650 Fiction
20
T0001 My First C++ Brian & Brooke
EPB
400 Text
10
T0002 C++ Brainworks A.W. Rossaine
TDH
350 Text
15
F0002 Thunderbolts Anna Roberts
First Publ.
750 Fiction
50

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.

SQL> select count(*) from Library;


COUNT(*)
---------5

Q8. select max(Price) from books where quantity >= 15.


SQL> select max(Price) from Library where Qty >= 15;
MAX(PRICE)
---------750
Q9. select book_Name, Author_Name from book where Publishers = EPB.
SQL> select Book_Name,Author_Name from Library where Publishers = 'EPB';
BOOK_NAME
AUTHOR_NAME
--------------- -------------------Fast Cook
Lata Kapoor
My First C++ Brian & Brooke
Q10. select count (Distinct Publishers) from books where price > = 400.
SQL> select count(Distinct Publishers) from Library where Price > = 400;
COUNT(DISTINCTPUBLISHERS)
------------------------2

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.

Solution:SQL> create table surbhi_emp


2 (
3 empno number(5) primary key,
4 name varchar(10),
5 DOB date,
6 n_place varchar(10),
7 hobbies varchar(10)
8 );
Table created.
SQL> insert into Surbhi_emp values(123,'Amit','23-Jan-1965','Delhi','Music');
1 row created.
SQL> insert into Surbhi_emp values(127,'Manoj','12-Dec-1976','Mumbai','Writing');
1 row created.
SQL> insert into Surbhi_emp values(124,'Abhay','11-Aug-1975','Allahabad','Music');
1 row created.
SQL> insert into Surbhi_emp values(125,'Vinod','04-Apr-1977','Delhi','Sports');
1 row created.
SQL> insert into Surbhi_emp values(128,'Abhi','10-Mar-1974','Mumbai','Gardening');
1 row created.
SQL> insert into Surbhi_emp values(129,'Ramesh','28-Oct-1981','Pune','Sports');
1 row created.
SQL> select * from surbhi_emp;
EMPNO NAME

DOB

N_PLACE

---------- ---------- --------- ---------- ----------

HOBBIES

123 Amit

23-JAN-65 Delhi

Music

127 Manoj

12-DEC-76 Mumbai

124 Abhay

11-AUG-75 Allahabad Music

125 Vinod

04-APR-77 Delhi

128 Abhi

10-MAR-74 Mumbai

129 Ramesh

Writing

Sports

28-OCT-81 Pune

Gardening
Sports

SQL> create table star


2 (
3 sno number(5) primary key,
4 area varchar(10),
5 Appointment date,
6 salary number(8) check(salary>4000 AND salary<10000),
7 Retirement date,
8 Dept varchar(10)
9 );
SQL> insert into star values(123,'Agra','25-Jan-2006',5000,'25-Jan-2026','Marketing');
1 row created.
SQL> insert into star values(127,'Mathura','22-Dec-2006',6000,'22-Dec-2026','Finance');
1 row created.
SQL> insert into star values(124,'Agra','19-Aug-2007',5500,'19-Aug-2016','Marketing');
1 row created.
SQL> insert into star values(125,'Delhi','14-Apr-2004',8500,'14-Apr-2018','Sales');
1 row created.
SQL> insert into star values(128,'Pune','13-Mar-2008',7500,'13-Mar-2028','Sales');
1 row created.
SQL> select * from star;
SNO AREA

APPOINTMENT

SALARY RETIREMENT DEPT

---------- ---------- ---------- ---------- ---------- ---------123 Agra


127 Mathura

25-JAN-06
22-DEC-06

5000 25-JAN-26 Marketing


6000 22-DEC-26 Finance

124 Agra

19-AUG-07

125 Delhi

14-APR-04

128

Pune

5500 19-AUG-16 Marketing


8500 14-APR-18 Sales

13-MAR-08

7500 13-MAR-28 Sales

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

---------- ---------- ---------125

Vinod

8500

Q2. Write a query to show number of employees area wise.


SQL> select count(empno) from surbhi_emp,star where surbhi_emp.empno=star.sno
group by area;

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

---------- ---------- ---------- ---------125 Vinod

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.

12.Display the name of the employee along with their annual


salary(sal*12).The name of the employee earning highest annual salary
should apper first.
13.Display the depart numbers and total salary for each department.
14.Display the depart numbers and max salary for each department.
15.Display the various jobs and total salary for each job.
Solution:SQL> create table Department
2 (
3 deptno number(2) Primary key,
4 dname varchar(15),
5 loc varchar(15)
6 );
Table created.
SQL> insert into department values(10, 'ACCOUNTING', 'NEW YORK')
1 row created.
SQL> insert into department values(20, 'RESEARCH', 'DALLAS');
1 row created.
SQL> insert into department values(30, 'SALES', 'CHICAGO');
1 row created.
SQL> insert into department values(40, 'OPERATIONS', 'BOSTON');
1 row created.

SQL> select * from department;


DEPTNO
DNAME
LOC
---------- --------------- --------------10 ACCOUNTING
NEW YORK
20 RESEARCH
DALLAS
30 SALES
CHICAGO
40 OPERATIONS
BOSTON
SQL> create table employee

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.

SQL> select * from employee;


EMPNO ENAME
JOB
MGR HIREDATE
SAL
COMM
DEPTNO
---------- ---------- ------------ ---------- --------- ---------- ---------- ------7839 KING
PRESIDENT
17-NOV-81
5000
10
7698 BLAKE
MANAGER
7839 01-MAY-81
2850
30
7782 CLARK
MANAGER
7839 09-JUN-81
2450
10
7566 JONES
MANAGER
7839 02-APR-81
2975
20
7788 SCOTT
ANALYST
7566 13-JUL-87
3000
20
7902 FORD
ANALYST
7566 03-DEC-81
3000
20
7369 SMITH
CLERK
7902 17-DEC-80
800
20
7499 ALLEN
SALESMAN
7698 20-FEB-81
1600
300
30

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

Question 8. Refer table Employee as in Q7 and perform the following queries :-

1. Display the employee number and name for employee


working as clerk and earning highest salary among clerks.
2. Display the names of salesman who earns a salary more
than the highest
salary of any clerk.
3. Display the names of clerks who earn a salary more than the
lowest
salary of any salesman.
4. Display the names of the employees who earn highest salary
in their
respective departments.
5. Display the employee names who are working in accounting
department.
6. Display the names of employees from department number
10 with salary
greater than that of any employee working in other
department.
7. Display the names of the employees from department
number 10 with
salary greater than that of all employee working in other
departments.
8. Display the maximum salary being paid to depart number
20.
9. Display the average salary drawn by MANAGERS.
Solution:Q1. Display the employee number and name for employee working as clerk
and earning highest salary among clerks.
SQL> select empno,ename from emp where job='CLERK' and sal=(select max(sal) from
employee where job='CLERK');
EMPNO ENAME
---------- ---------7934 MILLER

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.

Solution:SQL> create table stud


2 (

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.

SQL> insert into class values('second','eleven','r101',202);


1 row created.
SQL> insert into class values('third','ten','r128',203);
1 row created.
SQL> insert into class values('fourth','nine','r121',203);
1 row created.
SQL> select * from class;
CNAME
MEETS
ROOM
FID
---------- ---------- ---------- ---------first
nine
r128
201
second
eleven
r101
202
third
ten
r128
203
fourth
nine
r121
203
SQL> create table enrolled
2 (
3 snum number(5),
4 cname varchar(10)
5 );
Table created.
SQL> insert into enrolled values(101,'first');
1 row created.
SQL> insert into enrolled values(102,'second');
1 row created.
SQL> insert into enrolled values(103,'first');
1 row created.
SQL> insert into enrolled values(104,'third');
1 row created.
SQL> insert into enrolled values(105,'second');
1 row created.
SQL> select * from enrolled;

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:-

1. Find the names of persons who works for company FBC.


2. Find the names of persons who works for company FBC along with the
cities they live in.
3. Find the persons who works for company FBC with a salary of more than
10000 along with the street and cities where they live.
4. Find the names of the persons who do not work for company FBC.
Solution:SQL> create table Works
2 (
3 Pname varchar(10) Primary key,
4 Cname varchar(5),
5 Salary number(10)
6 );
Table created.
SQL> insert into works values('Harry','FBC',2500);
1 row created.
SQL> insert into works values('Sumit','JBC',10000);
1 row created.
SQL> insert into works values('Jyoti','FBC',25230);
1 row created.
SQL> insert into works values('Sarah','SBC',3500);
1 row created.
SQL> insert into works values('Robert','SBC',26323);
1 row created.
SQL> select * from works;

PNAME
CNAME
SALARY
---------- ----- ----------

Harry

FBC

2500

Sumit

JBC

10000

Jyoti

FBC

25230

Sarah

SBC

3500

Robert

SBC

26323

SQL> create table Lives


2 (
3 Name varchar(10) Primary key,
4 Street varchar(15),
5 City varchar(10)
6 );
Table created.
SQL> insert into lives values('Harry','Raj nagar','GZB');
1 row created.
SQL> insert into lives values('Sumit','Shastri Nagar','FBD');
1 row created.
SQL> insert into lives values('Jyoti','Sector 19','Noida');
1 row created.
SQL> insert into lives values('Sarah','Raj nagar','FBD');
1 row created.
SQL> insert into lives values('Robert','Sector 16','Noida');
1 row created.

SQL> select * from lives;


NAME
STREET
CITY
---------- --------------- ---------Harry
Raj nagar
GZB
Sumit
Jyoti

Shastri Nagar FBD


Sector 19

Noida

Sarah

Raj nagar

FBD

Robert

Sector 16

Noida

SQL> create table located


2 (
3 Cname varchar(5) Primary key,
4 City varchar(10)
5 );
Table created.
SQL> insert into located values('FBC','Noida');
1 row created.
SQL> insert into located values('JBC','GZB');
1 row created.
SQL> insert into located values('SBC','FBD');
1 row created.
SQL> select * from located;
CNAME CITY
----- ---------FBC Noida
JBC GZB
SBC FBD
Q1. Find the names of persons who works for company FBC.
SQL> select Pname from Works where Cname = 'FBC';
PNAME
---------Harry
Jyoti
Q2.Find the names of persons who works for company FBC along with the
cities they live in.
SQL> select Pname, City from Works, Lives where Works.Pname = Lives.Name AND
Cname = 'FBC';
PNAME
CITY
---------- ---------Harry
GZB

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

Question 11. Write Pl/SQL code for:-

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.

6. To perform the addition of two numbers.


7. To get a number from keyboard and if it zero print natural number, else
print not a natural number.
8. To find the area and perimeter of given circle.
9. To calculate the net salary if dfa is 30% of basic, hra is 10% of basic and pf
is 7%. If basic salary is less than 8000, pf is 10% if basic sal between 8000
to 160000.
10.To select record of emp table with cursor.
11.To raise an error if no data found.
Solution:Q1. To reverse a number and print, i.e, if num is 677 then it should print 776.
SQL> set serveroutput on;
SQL> declare
2 no number(5);
3 a number(5);
4 rev number(5) :=0;
5 begin
6 no:=&number;
7 while no>0
8 loop
9 a:=no mod 10;
10 rev:=a+(rev*10);
11 no:=floor(no/10);
12 end loop;
13 dbms_output.put_line('Reverse number is: '||rev);
14 end;
15 /
Enter value for number: 677
old 6: no:=&no;
new 6: no:=677;
Reverse number is: 776
PL/SQL procedure successfully completed.

Q2. To print a Fibonacci series.


SQL> set serveroutput on;
SQL> declare
2. a number(3) :=0;
3. b number(3) :=1;
4. c number(3) :=0;

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.

Q6. To perform the addition of two numbers.


SQL> set serveroutput on;
SQL> declare
2 n1 number;
3 n2 number;
4 sum1 number;
5 begin
6 n1:=&n1;
7 n2:=&n2;
8 sum1:=n1+n2;
9 dbms_output.put_line('sum of 2 nos is '||sum1);
10 end;
11 /
Enter value for n1: 5
old 6: n1:=&n1;
new 6: n1:=5;
Enter value for n2: 8
old 7: n2:=&n2;
new 7: n2:=8;
sum of 2 nos is 13
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 /

Enter value for radius: 3


old 6: r:=&r;
new 6: r:=3;
Area = 28
Perimeter = 19
PL/SQL procedure successfully completed.
Q9. To calculate the net salary if dfa is 30% of basic, hra is 10% of basic and pf
is 7% if basic salary is less than 8000, pf is 10% if basic sal between 8000 to
160000.
SQL> set serveroutput on;
SQL> declare
2 ename varchar2(15);
3 basic number;
4 da number;
5 hra number;
6 pf number;
7 netsalary number;
8 begin
9 ename:='&ename';
10 basic:=&basic;
11 da:=basic * (30/100);
12 hra:=basic * (10/100);
13 if (basic < 8000) then
14 pf:=basic * (7/100);
15 elsif (basic >= 8000 and basic <= 16000) then
16 pf:=basic * (10/100);
17 end if;
18 netsalary:=basic + da + hra -pf;
19 dbms_output.put_line('Employee name : ' || ename);
20 dbms_output.put_line('Providend Fund : ' || pf);
21 dbms_output.put_line('Net salary : ' || netsalary);
22 end;
23 /
Enter value for ename: Surbhi
old 9: ename:='&ename';
new 9: ename:='Surbhi';
Enter value for basic: 10000
old 10: basic:=&basic;
new 10: basic:=10000;
Employee name : Surbhi
Providend Fund : 1000
Net salary : 13000
PL/SQL procedure successfully completed.
Q10. To select record of customer table with cursor.
SQL> create table Customers
2 (

3 Id number(5) primary key,


4 Name varchar(10)
5 );
Table created.
SQL> insert into Customers values(1,'Ajit');
1 row created.
SQL> insert into Customers values(2,'Geetansh');
1 row created.
SQL> insert into Customers values(3,'Surbhi');
1 row created.
SQL> insert into Customers values(4,'Aparna');
1 row created.
SQL> insert into Customers values(5,'Akshita');
1 row created.
SQL> select * from customers;
ID NAME
---------- ---------1 Ajit
2 Geetansh
3 Surbhi
4 Aparna
5 Akshita
SQL> set serveroutput on;
SQL> declare
2 customer_rec customers%rowtype;
3 begin
4 select * into customer_rec from customers where id = 3;
5 dbms_output.put_line('Customer ID: ' || customer_rec.id);
6 dbms_output.put_line('Customer Name: ' || customer_rec.name);
7 end;
8 /
Customer ID: 3
Customer Name: Surbhi
PL/SQL procedure successfully completed.
Q11. To raise an error if no data found.

SQL> create table Customers


2 (
3 Id number(5) primary key,
4 Name varchar(10)
5 );
Table created.
SQL> insert into Customers values(1,'Aparna');
1 row created.
SQL> insert into Customers values(2,'ajit');
1 row created.
SQL> insert into Customers values(3,'akshita');
1 row created.
SQL> insert into Customers values(4,'vipul');
1 row created.
SQL> insert into Customers values(5,'shivani');
1 row created.
SQL> select * from customers;
ID NAME
---------- ---------1 Aparna
2 ajit
3 akshita
4 vipul
5 shivani
SQL> set serveroutput on;
SQL> declare
2 c_id customers.id%type := 8;
3 c_name customers.name%type;
4 begin
5 select name into c_name from customers where id = c_id;
6 dbms_output.put_line('Name: '||c_name);
7 EXCEPTION
8 when no_data_found then
9 dbms_output.put_line('No Data Found');
10 when others THEN
11 dbms_output.put_line('Error');
12 end;
13 /

No Data Found
PL/SQL procedure successfully completed.

Das könnte Ihnen auch gefallen