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

SQL (Need Editing)

1. The document describes three database tables - EMP, WORKS, and COMPANY - that contain information about employees, their employers, salaries, and locations. Data is inserted into these tables. 2. Queries are listed to find information from the tables such as the company with the most employees, average salaries, and employee details based on location, company and manager. 3. The tables and data provided are for a basic example to demonstrate creating, populating and querying multiple database tables with interrelated data.

Uploaded by

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

SQL (Need Editing)

1. The document describes three database tables - EMP, WORKS, and COMPANY - that contain information about employees, their employers, salaries, and locations. Data is inserted into these tables. 2. Queries are listed to find information from the tables such as the company with the most employees, average salaries, and employee details based on location, company and manager. 3. The tables and data provided are for a basic example to demonstrate creating, populating and querying multiple database tables with interrelated data.

Uploaded by

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

INDEX

SL ASSIGNMENT & DATABASE DATE


No

TABLE
EMP(e_name,street,city)
WORKS(e_name,company_name,salary)
COMPANY(company_name,city)
1 MANAGERS(e_name,M_name)

QUERY
1. Find the company with most employee.
2. Find the company with smallest payroll.
3. Find all employee who earn more than average salary
of all employee of their company.
4. Find the number of employee for each company.
5. Find the name of the employee and corresponding
company name who get minimum salary of their
respective company

TABLE
PATIENT(p_id,p_name,p_age,p_address)
DOCTOR(d_id,d_name,d_address)
ADMITTED(p_id,date_of_admission)
ATTEND(p_id,d_id)

2 QUERY
1. List the name of the patient decending order order of
age with their doctor name
2. List the name of the patients who have the same
address as the doctor id ‘301’
3. List the name of the doctor who treat the patients
with the date of admission between 01-sep-2018 and
20-nov-2018
4. List the total number of doctor who checks the same
patient more than three times

Teacher’s Signature
SL ASSIGNMENT & DATABASE DATE
No

TABLE

BOOK(acc_no,title,publisher,no_of_copies,subject_catagory)
BORROWER(card_no,b_name,b_address)
ISSUE(acc_no,card_no,date_of_issue,date_of_return)
SUPPLIER(s_name,acc_no,price)
3 QUERY

1. List the name of the borrower who have issued some


books with title containing the word ‘database’
2. List the publisher of all books in the library such that
the name of publisher appears only once
3. List the accession number of all books issued between
02-aug-18 and 27-sep-18
4. List the accession number and titles of all books
containing less than five copies

TABLE
EMP(e_name,street,city)
WORKS(e_name,company_name,salary)
COMPANY(company_name,city)
MANAGERS(e_name,M_name)

QUERY
4 1. Find the name,street and city of residence of all
employees who works for ‘CTS’ and earn more than
25000
2. Find all employees in the database who live in the
same city as the company for which they work.
3. Find all employees in the database who live in the
same city on the same street as do their manager.
SL ASSIGNMENT & DATABASE DATE
No
TABLE
BOOK(book_id,book_name,author_code,date_of_purch
ase , current_stock)
AUTHOR(author_code,author_name)
5
QUERY
1. List book id,book name and author name whose current
stock is more than five.
2. List book id,book name,author name and date of
purchase which is purchased after 06-sep-18

TABLE
EMP2(e_no,e_name,dept_no,b_sal,job_status)
PROJECT(p_no,p_name)
WORK(p_no,e_no,e_job)
6 DEPARTMENT(dept_no,d_name)

QUERY

1. Change the name of the employee with e_no=5


2. Delete the project with p_no=1002
3. Find the number and name of the employees whose
employee number are 5,7,10,12
4. Find the name of all employees who do not work in the
department where ‘pampa’ is working.
5. Find the name of those employee of the dept_no=3 who
get more salary than the highest paid employee with
dept_no=5.
6. Display the name of those employee whose job status
begins with ‘office’
7. Display the name of those employees whose employee
number is odd.
DATABASE-1
EMP(e_name,street,city)
WORKS(e_name,company_name,salary)
COMPANY(company_name,city)
MANAGERS(e_name,M_name)

SQL> create table emp(e_name varchar2(20),street varchar2(20),city varchar2(20));

Table created.

SQL> desc emp;

Name Null? Type


----------------------------------------- -------- ----------------------------
E_NAME VARCHAR2(20)
STREET VARCHAR2(20)
CITY VARCHAR2(20)

SQL> insert into emp values('&e_name','&street','&city');


Enter value for e_name: jyoti
Enter value for street: nh40
Enter value for city: kolkata
old 1: insert into emp values('&e_name','&street','&city')
new 1: insert into emp values('jyoti','nh40','kolkata')
1 row created.

SQL> /
Enter value for e_name: susmita
Enter value for street: nh60
Enter value for city: kolkata
old 1: insert into emp values('&e_name','&street','&city')
new 1: insert into emp values('susmita','nh60','kolkata')

1 row created.

SQL> /
Enter value for e_name: parimal
Enter value for street: nh40
Enter value for city: kolkata
old 1: insert into emp values('&e_name','&street','&city')
new 1: insert into emp values('parimal','nh40','kolkata')

1 row created.

SQL> /
Enter value for e_name: prasenjit
Enter value for street: nh60
Enter value for city: kolkata
old 1: insert into emp values('&e_name','&street','&city')
new 1: insert into emp values('prasenjit','nh60','kolkata')

1 row created.

SQL> /
Enter value for e_name: pampa
Enter value for street: nh40
Enter value for city: kolkata
old 1: insert into emp values('&e_name','&street','&city')
new 1: insert into emp values('pampa','nh40','kolkata')

1 row created.

SQL> /
Enter value for e_name: mouli
Enter value for street: nh60
Enter value for city: kolkata
old 1: insert into emp values('&e_name','&street','&city')
new 1: insert into emp values('mouli','nh60','kolkata')

1 row created.
SQL> select * from emp;

E_NAME STREET CITY


-------------------- -------------------- --------------------
jyoti nh40 kolkata
susmita nh60 kolkata
parimal nh40 kolkata
prasenjit nh60 kolkata
pampa nh40 kolkata
mouli nh60 kolkata

6 rows selected.
SQL> create table works(e_name varchar2(20),company_name varchar2(20),salary
number(10));

Table created.

SQL> insert into works values('&e_name','&company_name','&salary');


Enter value for e_name: jyoti
Enter value for company_name: tcs
Enter value for salary: 20000
old 1: insert into works values('&e_name','&company_name','&salary')
new 1: insert into works values('jyoti','tcs','20000')

1 row created.

SQL> /
Enter value for e_name: susmita
Enter value for company_name: tcs
Enter value for salary: 18000
old 1: insert into works values('&e_name','&company_name','&salary')
new 1: insert into works values('susmita','tcs','18000')

1 row created.
SQL> /
Enter value for e_name: parimal
Enter value for company_name: cts
Enter value for salary: 15000
old 1: insert into works values('&e_name','&company_name','&salary')
new 1: insert into works values('parimal','cts','15000')

1 row created.
SQL> /
Enter value for e_name: prasenjit
Enter value for company_name: wipro
Enter value for salary: 25000
old 1: insert into works values('&e_name','&company_name','&salary')
new 1: insert into works values('prasenjit','wipro','25000')

1 row created.

SQL> /
Enter value for e_name: pampa
Enter value for company_name: tcs
Enter value for salary: 10000
old 1: insert into works values('&e_name','&company_name','&salary')
new 1: insert into works values('pampa','tcs','10000')

1 row created.

SQL> /
Enter value for e_name: mouli
Enter value for company_name: cts
Enter value for salary: 30000
old 1: insert into works values('&e_name','&company_name','&salary')
new 1: insert into works values('mouli','cts','30000')

1 row created.

SQL> select * from works;


E_NAME COMPANY_NAME SALARY
-------------------- -------------------- ----------
jyoti tcs 20000
susmita tcs 18000
parimal cts 15000
prasenjit wipro 25000
pampa tcs 10000
mouli cts 30000

6 rows selected.
SQL> create table company(company_name varchar2(20),city varchar2(20));

Table created.

SQL> desc company;


Name Null? Type
----------------------------------------- -------- ----------------------------
COMPANY_NAME VARCHAR2(20)
CITY VARCHAR2(20)
SQL> insert into company values('&company_name','&city');
Enter value for company_name: tcs
Enter value for city: kolkata
old 1: insert into company values('&company_name','&city')
new 1: insert into company values('tcs','kolkata')

1 row created.

SQL> /
Enter value for company_name: cts
Enter value for city: bangalore
old 1: insert into company values('&company_name','&city')
new 1: insert into company values('cts','bangalore')

1 row created.

SQL> /
Enter value for company_name: wipro
Enter value for city: chennai
old 1: insert into company values('&company_name','&city')
new 1: insert into company values('wipro','chennai')

1 row created.

SQL> select * from company;

COMPANY_NAME CITY
-------------------- --------------------
tcs kolkata
cts bangalore
wipro chennai

SQL> create table managers(e_name varchar2(20),manager_name varchar2(20));

Table created.

SQL> desc managers;


Name Null? Type
----------------------------------------- -------- ----------------------------
E_NAME VARCHAR2(20)
MANAGER_NAME VARCHAR2(20)

SQL> insert into managers values('&e_name','&manager_name');


Enter value for e_name: jyoti
Enter value for manager_name: susanta
old 1: insert into managers values('&e_name','&manager_name')
new 1: insert into managers values('jyoti','susanta')

1 row created.

SQL> /
Enter value for e_name: susmita
Enter value for manager_name: jyoti
old 1: insert into managers values('&e_name','&manager_name')
new 1: insert into managers values('susmita','jyoti')

1 row created.

SQL> /
Enter value for e_name: parimal
Enter value for manager_name: mouli
old 1: insert into managers values('&e_name','&manager_name')
new 1: insert into managers values('parimal','mouli')

1 row created.

SQL> /
Enter value for e_name: prasenjit
Enter value for manager_name: soumen
old 1: insert into managers values('&e_name','&manager_name')
new 1: insert into managers values('prasenjit','soumen')

1 row created.

SQL> /
Enter value for e_name: pampa
Enter value for manager_name: jyoti
old 1: insert into managers values('&e_name','&manager_name')
new 1: insert into managers values('pampa','jyoti')

1 row created.

SQL> /
Enter value for e_name: mouli
Enter value for manager_name: santanu
old 1: insert into managers values('&e_name','&manager_name')
new 1: insert into managers values('mouli','santanu')

1 row created.

SQL> select * from managers;

E_NAME MANAGER_NAME
-------------------- --------------------
jyoti susanta
susmita jyoti
parimal mouli
prasenjit soumen
pampa jyoti
mouli santanu

6 rows selected.

-: QUERIES :-

1. Find the company with most employee.


SQL> select company_name,count(e_name) from works group by company_name
having count(e_name)=(select max(count(e_name)) from works group by
company_name);
COMPANY_NAME COUNT(E_NAME)
-------------------- -------------
tcs 3

2. Find the company with smallest payroll.


SQL> select company_name from works group by company_name having
sum(salary)=(select min(sum(salary)) from works group by company_name);

COMPANY_NAME
--------------------
wipro
3. Find all employee who earn more than average salary of all employee of
their company.
SQL> select works.e_name,works.company_name,works.salary from works,(select
company_name,avg(salary)Average from works group by company_name)T where
works.salary>T.Average and works.company_name=T.company_name;

E_NAME COMPANY_NAME SALARY


-------------------- -------------------- ----------
susmita tcs 18000
jyoti tcs 20000
mouli cts 30000
4. Find the number of employee for each company.
SQL> select company_name,count(e_name) from works group by company_name;
COMPANY_NAME COUNT(E_NAME)
-------------------- -------------
wipro 1
tcs 3
cts 2
5. Find the name of the employee and corresponding company name who
get minimum salary of their respective company
SQL> select works.e_name,works.company_name,works.salary from works,(select
company_name,min(salary)Min_sal from works group by company_name)M where
works.salary=M.Min_sal and works.company_name=M.company_name;

E_NAME COMPANY_NAME SALARY


-------------------- -------------------- ----------
prasenjit wipro 25000
pampa tcs 10000
parimal cts 15000

-: DATABASE-2 :-

PATIENT(p_id,p_name,p_age,p_address)
DOCTOR(d_id,d_name,d_address)
ADMITTED(p_id,date_of_admission)
ATTEND(p_id,d_id)

SQL> create table patient(p_id number(20),p_name varchar2(20),p_age


number(10),p_address varchar2(20));

Table created.

SQL> desc patient;


Name Null? Type
----------------------------------------- -------- ----------------------------
P_ID NUMBER(20)
P_NAME VARCHAR2(20)
P_AGE NUMBER(10)
P_ADDRESS VARCHAR2(20)

SQL> insert into patient values('&p_id','&p_name','&p_age','&p_address');


Enter value for p_id: 1000
Enter value for p_name: suman
Enter value for p_age: 32
Enter value for p_address: kgp
old 1: insert into patient values('&p_id','&p_name','&p_age','&p_address')
new 1: insert into patient values('1000','suman','32','kgp')

1 row created.

SQL> /
Enter value for p_id: 1001
Enter value for p_name: sumit
Enter value for p_age: 46
Enter value for p_address: mdn
old 1: insert into patient values('&p_id','&p_name','&p_age','&p_address')
new 1: insert into patient values('1001','sumit','46','mdn')

1 row created.

SQL> /
Enter value for p_id: 1002
Enter value for p_name: sourav
Enter value for p_age: 21
Enter value for p_address: digha
old 1: insert into patient values('&p_id','&p_name','&p_age','&p_address')
new 1: insert into patient values('1002','sourav','21','digha')

1 row created.

SQL> /
Enter value for p_id: 1003
Enter value for p_name: puja
Enter value for p_age: 20
Enter value for p_address: pku
old 1: insert into patient values('&p_id','&p_name','&p_age','&p_address')
new 1: insert into patient values('1003','puja','20','pku')

1 row created.

SQL> select * from patient;

P_ID P_NAME P_AGE P_ADDRESS


---------- -------------------- ---------- --------------------
1000 suman 32 kgp
1001 sumit 46 mdn
1002 sourav 21 digha
1003 puja 20 pku

SQL> create table doctor(d_id number(10),d_name varchar2(20),d_address varchar2(20));

Table created.
SQL> desc doctor;
Name Null? Type
----------------------------------------- -------- ----------------------------
D_ID NUMBER(10)
D_NAME VARCHAR2(20)
D_ADDRESS VARCHAR2(20)

SQL> insert into doctor values('&d_id','&d_name','&d_address');


Enter value for d_id: 300
Enter value for d_name: p_roy
Enter value for d_address: mum
old 1: insert into doctor values('&d_id','&d_name','&d_address')
new 1: insert into doctor values('300','p_roy','mum')

1 row created.

SQL> /
Enter value for d_id: 301
Enter value for d_name: b_sen
Enter value for d_address: mdn
old 1: insert into doctor values('&d_id','&d_name','&d_address')
new 1: insert into doctor values('301','b_sen','mdn')

1 row created.

SQL> /
Enter value for d_id: 302
Enter value for d_name: s_gupta
Enter value for d_address: kol
old 1: insert into doctor values('&d_id','&d_name','&d_address')
new 1: insert into doctor values('302','s_gupta','kol')

1 row created.

SQL> /
Enter value for d_id: 303
Enter value for d_name: d_bera
Enter value for d_address: hda
old 1: insert into doctor values('&d_id','&d_name','&d_address')
new 1: insert into doctor values('303','d_bera','hda')

1 row created.
SQL> select * from doctor;

D_ID D_NAME D_ADDRESS


---------- -------------------- --------------------
300 p_roy mum
301 b_sen mdn
302 s_gupta kol
303 d_bera hda

SQL> create table admitted(p_id number(20),d_o_a date);

Table created.

SQL> desc admitted;


Name Null? Type
----------------------------------------- -------- ----------------------------
P_ID NUMBER(20)
D_O_A DATE

SQL> insert into admitted values('&p_id','&d_o_a');


Enter value for p_id: 1000
Enter value for d_o_a: 1-oct-18
old 1: insert into admitted values('&p_id','&d_o_a')
new 1: insert into admitted values('1000','1-oct-18')
1 row created.

SQL> /
Enter value for p_id: 1001
Enter value for d_o_a: 15-sep-18
old 1: insert into admitted values('&p_id','&d_o_a')
new 1: insert into admitted values('1001','15-sep-18')

1 row created.

SQL> /
Enter value for p_id: 1002
Enter value for d_o_a: 21-nov-18
old 1: insert into admitted values('&p_id','&d_o_a')
new 1: insert into admitted values('1002','21-nov-18')

1 row created.

SQL> /
Enter value for p_id: 1003
Enter value for d_o_a: 29-nov-18
old 1: insert into admitted values('&p_id','&d_o_a')
new 1: insert into admitted values('1003','29-nov-18')

1 row created.

SQL> select * from admitted;


P_ID D_O_A
---------- ---------
1000 01-OCT-18
1001 15-SEP-18
1002 21-NOV-18
1003 29-NOV-18

SQL> create table attend(p_id number(10),d_id number(10));

Table created.

SQL> desc attend;


Name Null? Type
----------------------------------------- -------- ----------------------------
P_ID NUMBER(10)
D_ID NUMBER(10)

SQL> insert into attend values('&p_id','&d_id');


Enter value for p_id: 1000
Enter value for d_id: 301
old 1: insert into attend values('&p_id','&d_id')
new 1: insert into attend values('1000','301')

1 row created.

SQL> /
Enter value for p_id: 1001
Enter value for d_id: 300
old 1: insert into attend values('&p_id','&d_id')
new 1: insert into attend values('1001','300')

1 row created.

SQL> /
Enter value for p_id: 1002
Enter value for d_id: 301
old 1: insert into attend values('&p_id','&d_id')
new 1: insert into attend values('1002','301')

1 row created.

SQL> /
Enter value for p_id: 1000
Enter value for d_id: 301
old 1: insert into attend values('&p_id','&d_id')
new 1: insert into attend values('1000','301')
1 row created.

SQL> /
Enter value for p_id: 1003
Enter value for d_id: 302
old 1: insert into attend values('&p_id','&d_id')
new 1: insert into attend values('1003','302')

1 row created.

SQL> /
Enter value for p_id: 1001
Enter value for d_id: 303
old 1: insert into attend values('&p_id','&d_id')
new 1: insert into attend values('1001','303')

1 row created.

SQL> /
Enter value for p_id: 1002
Enter value for d_id: 302
old 1: insert into attend values('&p_id','&d_id')
new 1: insert into attend values('1002','302')

1 row created.
SQL> /
Enter value for p_id: 1000
Enter value for d_id: 301
old 1: insert into attend values('&p_id','&d_id')
new 1: insert into attend values('1000','301')

1 row created.

SQL> select * from attend;

P_ID D_ID
---------- ----------
1000 301
1001 300
1002 301
1000 301
1003 302
1001 303
1002 302
1000 301

8 rows selected.
-: QUERIES :-
1. List the name of the patient decending order order of age with their
doctor name
SQL> select p_name,p_age,d_name from patient,doctor,attend where
patient.p_id=attend.p_id and attend.d_id=doctor.d_id order by p_age desc;

P_NAME P_AGE D_NAME


-------------------- ---------- --------------------
sumit 46 p_roy
sumit 46 d_bera
suman 32 b_sen
suman 32 b_sen
suman 32 b_sen
sourav 21 s_gupta
sourav 21 b_sen
puja 20 s_gupta

8 rows selected.
2. List the name of the patients who have the same address as the doctor id
‘301’
SQL> select p_name,p_address from patient where p_address=(select d_address from
doctor where d_id=301);

P_NAME P_ADDRESS
-------------------- --------------------
sumit mdn

3. List the name of the doctor who treat the patients with the date of
admission between 01-sep-2018 and 20-nov-2018
SQL> select d_name from doctor where d_id in(select d_id from attend where p_id
in(select p_id from admitted where d_o_a between '01-sep-18' and '20-nov-18'));

D_NAME
--------------------
p_roy
b_sen
d_bera

4. List the total number of doctor who checks the same patient more than
three times
SQL> select count(d_id) from(select d_id,count(*) from attend group by p_id,d_id
having count(*)>2);

COUNT(D_ID)
-----------
1

DATABASE-3
BOOK(acc_no,title,publisher,no_of_copies,subject_catagory)
BORROWER(card_no,b_name,b_address)
ISSUE(acc_no,card_no,date_of_issue,date_of_return)
SUPPLIER(s_name,acc_no,price)

SQL> create table book(acc_no number(10),title varchar2(20),publisher


varchar(20),no_of_copies number(20),subject_catagory varchar2(20));

Table created.
SQL> insert into book
values('&acc_no','&title','&publisher','&no_of_copies','&subject_catagor');
Enter value for acc_no: 2000

SQL> /
Enter value for acc_no: 2000
Enter value for title: java
Enter value for publisher: mcgrawhill
Enter value for no_of_copies: 100
Enter value for subject_catagory: programming
old 1: insert into book
values('&acc_no','&title','&publisher','&no_of_copies','&subject_catagory')
new 1: insert into book values('2000','java','mcgrawhill','100','programming')

1 row created.

SQL> /
Enter value for acc_no: 2001
Enter value for title: sqldatabse
Enter value for publisher: bpb
Enter value for no_of_copies: 4
Enter value for subject_catagory: database
old 1: insert into book
values('&acc_no','&title','&publisher','&no_of_copies','&subject_catagory')
new 1: insert into book values('2001','sqldatabse','bpb','4','database')

1 row created.
SQL> /
Enter value for acc_no: 2002
Enter value for title: c++
Enter value for publisher: mcgrawhill
Enter value for no_of_copies: 50
Enter value for subject_catagory: programming
old 1: insert into book
values('&acc_no','&title','&publisher','&no_of_copies','&subject_catagory')
new 1: insert into book values('2002','c++','mcgrawhill','50','programming')

1 row created.

SQL> /
Enter value for acc_no: 2003
Enter value for title: python
Enter value for publisher: pearson
Enter value for no_of_copies: 150
Enter value for subject_catagory: programming
old 1: insert into book
values('&acc_no','&title','&publisher','&no_of_copies','&subject_catagory')
new 1: insert into book values('2003','python','pearson','150','programming')
1 row created.

SQL>select * from book;

ACC_NO TITLE PUBLISHER NO_OF_COPIES SUBJECT_CATAGORY


---------- -------------------- -------------------- ------------ --------------------
2000 java mcgrawhill 100 programming
2001 sqldatabse bpb 4 database
2002 c++ mcgrawhill 50 programming
2003 python pearson 150 programming

SQL> create table borrower(card_no varchar2(20),b_name varchar2(20),b_address


varchar2(20));

Table created.

SQL> insert into borrower values('&card_no','&b_name','&b_address');


Enter value for card_no: cs001
Enter value for b_name: jyoti
Enter value for b_address: pku
old 1: insert into borrower values('&card_no','&b_name','&b_address')
new 1: insert into borrower values('cs001','jyoti','pku')

1 row created.

SQL> /
Enter value for card_no: cs002
Enter value for b_name: susmita
Enter value for b_address: mdn
old 1: insert into borrower values('&card_no','&b_name','&b_address')
new 1: insert into borrower values('cs002','susmita','mdn')

1 row created.

SQL> /
Enter value for card_no: cs003
Enter value for b_name: mouli
Enter value for b_address: kol
old 1: insert into borrower values('&card_no','&b_name','&b_address')
new 1: insert into borrower values('cs003','mouli','kol')

1 row created.

SQL> /
Enter value for card_no: cs004
Enter value for b_name: parimal
Enter value for b_address: hda
old 1: insert into borrower values('&card_no','&b_name','&b_address')
new 1: insert into borrower values('cs004','parimal','hda')

1 row created.

SQL> select * from borrower;

CARD_NO B_NAME B_ADDRESS


-------------------- -------------------- --------------------
cs001 jyoti pku
cs002 susmita mdn
cs003 mouli kol
cs004 parimal hda

SQL> create table issue(acc_no number(10),card_no varchar2(20),d_o_i date,d_o_r date);

Table created.

SQL> insert into issue values('&acc_no','&card_no','&d_o_i','&d_o_r');


Enter value for acc_no: 2000
Enter value for card_no: cs001
Enter value for d_o_i: 1-aug-18
Enter value for d_o_r: 8-aug-18
old 1: insert into issue values('&acc_no','&card_no','&d_o_i','&d_o_r')
new 1: insert into issue values('2000','cs001','1-aug-18','8-aug-18')
1 row created.

SQL> /
Enter value for acc_no: 2001
Enter value for card_no: cs002
Enter value for d_o_i: 9-sep-18
Enter value for d_o_r: 17-aug-18
old 1: insert into issue values('&acc_no','&card_no','&d_o_i','&d_o_r')
new 1: insert into issue values('2001','cs002','9-sep-18','17-aug-18')

1 row created.

SQL> /
Enter value for acc_no: 2002
Enter value for card_no: cs003
Enter value for d_o_i: 12-may-18
Enter value for d_o_r: 20-may-18
old 1: insert into issue values('&acc_no','&card_no','&d_o_i','&d_o_r')
new 1: insert into issue values('2002','cs003','12-may-18','20-may-18')

1 row created.

SQL> /
Enter value for acc_no: 2003
Enter value for card_no: cs004
Enter value for d_o_i: 20-sep-18
Enter value for d_o_r: 28-sep-18
old 1: insert into issue values('&acc_no','&card_no','&d_o_i','&d_o_r')
new 1: insert into issue values('2003','cs004','20-sep-18','28-sep-18')

1 row created.

SQL> select * from issue;

ACC_NO CARD_NO D_O_I D_O_R


---------- -------------------- --------- ---------
2000 cs001 01-AUG-18 08-AUG-18
2001 cs002 09-SEP-18 17-AUG-18
2002 cs003 12-MAY-18 20-MAY-18
2003 cs004 20-SEP-18 28-SEP-18

SQL> create table supplier(s_name varchar2(20),acc_no number(20),price number(20));

Table created.

SQL> insert into supplier values('&s_name','&acc_no','&price');


Enter value for s_name: suman
Enter value for acc_no: 2000
Enter value for price: 300
old 1: insert into supplier values('&s_name','&acc_no','&price')
new 1: insert into supplier values('suman','2000','300')

1 row created.

SQL> /
Enter value for s_name: souvil
Enter value for acc_no: 2001
Enter value for price: 450
old 1: insert into supplier values('&s_name','&acc_no','&price')
new 1: insert into supplier values('souvil','2001','450')

1 row created.

SQL> /
Enter value for s_name: suman
Enter value for acc_no: 2002
Enter value for price: 250
old 1: insert into supplier values('&s_name','&acc_no','&price')
new 1: insert into supplier values('suman','2002','250')

1 row created.
SQL> /
Enter value for s_name: raj
Enter value for acc_no: 2003
Enter value for price: 500
old 1: insert into supplier values('&s_name','&acc_no','&price')
new 1: insert into supplier values('raj','2003','500')

1 row created.

SQL> select * from supplier;

S_NAME ACC_NO PRICE


---------- ---------- ----------
suman 2000 300
souvil 2001 450
suman 2002 250
raj 2003 500

-: QUERIES :-
1. List the name of the borrower who have issued some books with title
containing the word ‘database’
SQL> select b_name from borrower,book,issue where borrower.card_no=issue.card_no
and issue.acc_no=book.acc_no and title like '%databse%';

B_NAME
--------------------
Susmita

2. List the publisher of all books in the library such that the name of
publisher appears only once
SQL> select distinct publisher from book;

PUBLISHER
--------------------
bpb
pearson
mcgrawhill

3. List the accession number of all books issued between 02-aug-18 and
27-sep-18
SQL> select acc_no from issue where d_o_i between '01-mar-18' and '15-sep-18';

ACC_NO
----------
2000
2001
2002

4. List the accession number and titles of all books containing less than
five copies
SQL> select acc_no,title from book where no_of_copies<5;

ACC_NO TITLE
---------- --------------------
2001 sqldatabse

DATABASE-4
EMP(e_name,street,city)
WORKS(e_name,company_name,salary)
COMPANY(company_name,city)
MANAGERS(e_name,M_name)
SQL> create table emp(e_name varchar2(20),street varchar2(20),city varchar2(20));

Table created.

SQL> desc emp;

Name Null? Type


----------------------------------------- -------- ----------------------------
E_NAME VARCHAR2(20)
STREET VARCHAR2(20)
CITY VARCHAR2(20)

SQL> insert into emp values('&e_name','&street','&city');


Enter value for e_name: jyoti
Enter value for street: nh40
Enter value for city: kolkata
old 1: insert into emp values('&e_name','&street','&city')
new 1: insert into emp values('jyoti','nh40','kolkata')

1 row created.

SQL> /
Enter value for e_name: susmita
Enter value for street: nh60
Enter value for city: kolkata
old 1: insert into emp values('&e_name','&street','&city')
new 1: insert into emp values('susmita','nh60','kolkata')

1 row created.

SQL> /
Enter value for e_name: parimal
Enter value for street: nh40
Enter value for city: kolkata
old 1: insert into emp values('&e_name','&street','&city')
new 1: insert into emp values('parimal','nh40','kolkata')

1 row created.

SQL> /
Enter value for e_name: prasenjit
Enter value for street: nh60
Enter value for city: kolkata
old 1: insert into emp values('&e_name','&street','&city')
new 1: insert into emp values('prasenjit','nh60','kolkata')

1 row created.
SQL> /
Enter value for e_name: pampa
Enter value for street: nh40
Enter value for city: kolkata
old 1: insert into emp values('&e_name','&street','&city')
new 1: insert into emp values('pampa','nh40','kolkata')

1 row created.

SQL> /
Enter value for e_name: mouli
Enter value for street: nh60
Enter value for city: kolkata
old 1: insert into emp values('&e_name','&street','&city')
new 1: insert into emp values('mouli','nh60','kolkata')

1 row created.

SQL> select * from emp;

E_NAME STREET CITY


-------------------- -------------------- --------------------
jyoti nh40 kolkata
susmita nh60 kolkata
parimal nh40 kolkata
prasenjit nh60 kolkata
pampa nh40 kolkata
mouli nh60 kolkata

6 rows selected.
SQL> create table works(e_name varchar2(20),company_name varchar2(20),salary
number(10));

Table created.

SQL> insert into works values('&e_name','&company_name','&salary');


Enter value for e_name: jyoti
Enter value for company_name: tcs
Enter value for salary: 20000
old 1: insert into works values('&e_name','&company_name','&salary')
new 1: insert into works values('jyoti','tcs','20000')
1 row created.

SQL> /
Enter value for e_name: susmita
Enter value for company_name: tcs
Enter value for salary: 18000
old 1: insert into works values('&e_name','&company_name','&salary')
new 1: insert into works values('susmita','tcs','18000')

1 row created.
SQL> /
Enter value for e_name: parimal
Enter value for company_name: cts
Enter value for salary: 15000
old 1: insert into works values('&e_name','&company_name','&salary')
new 1: insert into works values('parimal','cts','15000')

1 row created.

SQL> /
Enter value for e_name: prasenjit
Enter value for company_name: wipro
Enter value for salary: 25000
old 1: insert into works values('&e_name','&company_name','&salary')
new 1: insert into works values('prasenjit','wipro','25000')

1 row created.

SQL> /
Enter value for e_name: pampa
Enter value for company_name: tcs
Enter value for salary: 10000
old 1: insert into works values('&e_name','&company_name','&salary')
new 1: insert into works values('pampa','tcs','10000')

1 row created.

SQL> /
Enter value for e_name: mouli
Enter value for company_name: cts
Enter value for salary: 30000
old 1: insert into works values('&e_name','&company_name','&salary')
new 1: insert into works values('mouli','cts','30000')

1 row created.

SQL> select * from works;


E_NAME COMPANY_NAME SALARY
-------------------- -------------------- ----------
jyoti tcs 20000
susmita tcs 18000
parimal cts 15000
prasenjit wipro 25000
pampa tcs 10000
mouli cts 30000

6 rows selected.
SQL> create table company(company_name varchar2(20),city varchar2(20));

Table created.

SQL> desc company;


Name Null? Type
----------------------------------------- -------- ----------------------------
COMPANY_NAME VARCHAR2(20)
CITY VARCHAR2(20)

SQL> insert into company values('&company_name','&city');


Enter value for company_name: tcs
Enter value for city: kolkata
old 1: insert into company values('&company_name','&city')
new 1: insert into company values('tcs','kolkata')

1 row created.

SQL> /
Enter value for company_name: cts
Enter value for city: bangalore
old 1: insert into company values('&company_name','&city')
new 1: insert into company values('cts','bangalore')

1 row created.

SQL> /
Enter value for company_name: wipro
Enter value for city: chennai
old 1: insert into company values('&company_name','&city')
new 1: insert into company values('wipro','chennai')

1 row created.

SQL> select * from company;

COMPANY_NAME CITY
-------------------- --------------------
tcs kolkata
cts bangalore
wipro chennai

SQL> create table managers(e_name varchar2(20),manager_name varchar2(20));

Table created.

SQL> desc managers;


Name Null? Type
----------------------------------------- -------- ----------------------------
E_NAME VARCHAR2(20)
MANAGER_NAME VARCHAR2(20)

SQL> insert into managers values('&e_name','&manager_name');


Enter value for e_name: jyoti
Enter value for manager_name: susanta
old 1: insert into managers values('&e_name','&manager_name')
new 1: insert into managers values('jyoti','susanta')

1 row created.

SQL> /
Enter value for e_name: susmita
Enter value for manager_name: jyoti
old 1: insert into managers values('&e_name','&manager_name')
new 1: insert into managers values('susmita','jyoti')

1 row created.

SQL> /
Enter value for e_name: parimal
Enter value for manager_name: mouli
old 1: insert into managers values('&e_name','&manager_name')
new 1: insert into managers values('parimal','mouli')

1 row created.

SQL> /
Enter value for e_name: prasenjit
Enter value for manager_name: soumen
old 1: insert into managers values('&e_name','&manager_name')
new 1: insert into managers values('prasenjit','soumen')

1 row created.
SQL> /
Enter value for e_name: pampa
Enter value for manager_name: jyoti
old 1: insert into managers values('&e_name','&manager_name')
new 1: insert into managers values('pampa','jyoti')

1 row created.

SQL> /
Enter value for e_name: mouli
Enter value for manager_name: santanu
old 1: insert into managers values('&e_name','&manager_name')
new 1: insert into managers values('mouli','santanu')

1 row created.

SQL> select * from managers;

E_NAME MANAGER_NAME
-------------------- --------------------
jyoti susanta
susmita jyoti
parimal mouli
prasenjit soumen
pampa jyoti
mouli santanu

6 rows selected.

-: QUERIES :-
1. Find the name,street and city of residence of all employees who works for
‘CTS’ and earn more than 25000
SQL> select emp.e_name,street,emp.city from emp,works where
emp.e_name=works.e_name and works.company_name='cts' and works.salary>25000;

E_NAME STREET CITY


-------------------- -------------------- --------------------
mouli nh60 kolkata

2. Find all employees in the database who live in the same city as the
company for which they work.
SQL> select emp.e_name from emp,works,company where
emp.e_name=works.e_name and works.company_name=company.company_name
and company.city=emp.city;

E_NAME
--------------------
jyoti
susmita
pampa

3. Find all employees in the database who live in the same city on the same
street as do their manager.
SQL> select emp.e_name from emp,(select street,emp.city from emp where emp.e_name
in(select manager_name from managers))M where emp.street=M.street and
emp.city=M.city;

E_NAME
--------------------
jyoti
susmita
parimal
prasenjit
pampa
mouli

6 rows selected.
DATABASE-5

BOOK(book_id,book_name,author_code,date_of_purchase
, current_stock)
AUTHOR(author_code,author_name)
SQL> create table book1(book_id number(20),book_name varchar2(20),author_code
number(20),d_o_p date,current_stock number(20));

Table created.

SQL> insert into book1


values('&book_id','&book_name','&author_code','&d_o_p','&current_stock');
Enter value for book_id: 5000
Enter value for book_name: java
Enter value for author_code: 1000
Enter value for d_o_p: 1-aug-18
Enter value for current_stock: 2
old 1: insert into book1
values('&book_id','&book_name','&author_code','&d_o_p','&current_stock')
new 1: insert into book1 values('5000','java','1000','1-aug-18','2')

1 row created.

SQL> /
Enter value for book_id: 5001
Enter value for book_name: c++
Enter value for author_code: 1001
Enter value for d_o_p: 1-sep-18
Enter value for current_stock: 3
old 1: insert into book1
values('&book_id','&book_name','&author_code','&d_o_p','&current_stock')
new 1: insert into book1 values('5001','c++','1001','1-sep-18','3')

1 row created.

SQL> /
Enter value for book_id: 5002
Enter value for book_name: python
Enter value for author_code: 1002
Enter value for d_o_p: 9-oct-18
Enter value for current_stock: 10
old 1: insert into book1
values('&book_id','&book_name','&author_code','&d_o_p','&current_stock')
new 1: insert into book1 values('5002','python','1002','9-oct-18','10')

1 row created.
SQL> /
Enter value for book_id: 5003
Enter value for book_name: dbms
Enter value for author_code: 1003
Enter value for d_o_p: 20-dec-18
Enter value for current_stock: 20
old 1: insert into book1
values('&book_id','&book_name','&author_code','&d_o_p','&current_stock')
new 1: insert into book1 values('5003','dbms','1003','20-dec-18','20')

1 row created.

SQL> select * from book1;

BOOK_ID BOOK_NAME AUTHOR_CODE D_O_P CURRENT_STOCK


---------- -------------------- ----------- --------- -------------
5000 java 1000 01-AUG-18 2
5001 c++ 1001 01-SEP-18 3
5002 python 1002 09-OCT-18 10
5003 dbms 1003 20-DEC-18 20
SQL> create table author(author_code number(20),author_name varchar2(20));

Table created.

SQL> insert into author values('&author_code','&author_name');


Enter value for author_code: 1000
Enter value for author_name: balagurusamy
old 1: insert into author values('&author_code','&author_name')
new 1: insert into author values('1000','balagurusamy')

1 row created.

SQL> /
Enter value for author_code: 1001
Enter value for author_name: kanetkar
old 1: insert into author values('&author_code','&author_name')
new 1: insert into author values('1001','kanetkar')

1 row created.

SQL> /
Enter value for author_code: 1002
Enter value for author_name: martin
old 1: insert into author values('&author_code','&author_name')
new 1: insert into author values('1002','martin')

1 row created.

SQL> /
Enter value for author_code: 1003
Enter value for author_name: silberschatz
old 1: insert into author values('&author_code','&author_name')
new 1: insert into author values('1003','silberschatz')

1 row created.

SQL> select * from author;

AUTHOR_CODE AUTHOR_NAME
-------------------- --------------------
1000 balagurusamy
1001 kanetkar
1002 martin
1003 silberschatz
-: QUERIES :-
1. List book id,book name and author name whose current stock is more
than five.
SQL> select book_id,book_name,author_name from book1,author where
book1.author_code=author.author_code and current_stock>5;

BOOK_ID BOOK_NAME AUTHOR_NAME


---------- -------------------- --------------------
5002 python martin
5003 dbms silberschatz

2. List book id,book name,author name and date of purchase which is


purchased after 06-sep-18.
SQL> select book_id,book_name,author_name,d_o_p from book1,author where
book1.author_code=author.author_code and d_o_p >'05-sep-18';

BOOK_ID BOOK_NAME AUTHOR_NAME D_O_P


---------- -------------------- -------------------- ---------
5002 python martin 09-OCT-18
5003 dbms silberschatz 20-DEC-18

DATABASE-6
EMP2(e_no,e_name,dept_no,b_sal,job_status)
PROJECT(p_no,p_name)
WORK(p_no,e_no,e_job)
DEPARTMENT(dept_no,d_name)
SQL> create table emp2(e_no number(10),e_name varchar2(20),dept_no number(20),b_sal
number(20),job_status varchar2(20));

Table created.

SQL> insert into emp2 values('&e_no','&e_name','&dept_no','&b_sal','&job_status');


Enter value for e_no: 5
Enter value for e_name: jyoti
Enter value for dept_no: 2
Enter value for b_sal: 20000
Enter value for job_status: officework
old 1: insert into emp2 values('&e_no','&e_name','&dept_no','&b_sal','&job_status')
new 1: insert into emp2 values('5','jyoti','2','20000','officework')

1 row created.

SQL> /
Enter value for e_no: 6
Enter value for e_name: susmita
Enter value for dept_no: 3
Enter value for b_sal: 60000
Enter value for job_status: manager
old 1: insert into emp2 values('&e_no','&e_name','&dept_no','&b_sal','&job_status')
new 1: insert into emp2 values('6','susmita','3','60000','manager')

1 row created.

SQL> /
Enter value for e_no: 7
Enter value for e_name: mouli
Enter value for dept_no: 5
Enter value for b_sal: 40000
Enter value for job_status: engineer
old 1: insert into emp2 values('&e_no','&e_name','&dept_no','&b_sal','&job_status')
new 1: insert into emp2 values('7','mouli','5','40000','engineer')

1 row created.

SQL> /
Enter value for e_no: 10
Enter value for e_name: parimal
Enter value for dept_no: 3
Enter value for b_sal: 45000
Enter value for job_status: designer
old 1: insert into emp2 values('&e_no','&e_name','&dept_no','&b_sal','&job_status')
new 1: insert into emp2 values('10','parimal','3',',45000','designer')

1 row created.

SQL> /
Enter value for e_no: 12
Enter value for e_name: pampa
Enter value for dept_no: 5
Enter value for b_sal: 50000
Enter value for job_status: tester
old 1: insert into emp2 values('&e_no','&e_name','&dept_no','&b_sal','&job_status')
new 1: insert into emp2 values('12','pampa','5','50000','tester')

1 row created.
SQL> select * from emp2;

E_NO E_NAME DEPT_NO B_SAL JOB_STATUS


---------- -------------------- ---------- ---------- --------------------
5 jyoti 2 20000 officework
6 susmita 3 60000 manager
7 mouli 5 40000 engineer
10 parimal 3 45000 designer
12 pampa 5 50000 tester

SQL> create table project(p_no number(10),p_name varchar2(20));

Table created.

SQL> insert into project values('&p_no','&p_name');


Enter value for p_no: 1000
Enter value for p_name: androidAppdesign
old 1: insert into project values('&p_no','&p_name')
new 1: insert into project values('1000','androidAppdesign')

1 row created.

SQL> /
Enter value for p_no: 1001
Enter value for p_name: AI
old 1: insert into project values('&p_no','&p_name')
new 1: insert into project values('1001','AI')

1 row created.

SQL> /
Enter value for p_no: 1002
Enter value for p_name: dotnet
old 1: insert into project values('&p_no','&p_name')
new 1: insert into project values('1002','dotnet')

1 row created.

SQL> select * from project;

P_NO P_NAME
---------- --------------------
1000 androidAppdesign
1001 AI
1002 dotnet
SQL> create table work(p_no number(10),e_no number(10),e_job varchar2(20));

Table created.

SQL> insert into work values('&p_no','&e_no','&e_job');


Enter value for p_no: 1000
Enter value for e_no: 5
Enter value for e_job: developer
old 1: insert into work values('&p_no','&e_no','&e_job')
new 1: insert into work values('1000','5','developer')

1 row created.

SQL> /
Enter value for p_no: 1001
Enter value for e_no: 6
Enter value for e_job: projectmanager
old 1: insert into work values('&p_no','&e_no','&e_job')
new 1: insert into work values('1001','6','projectmanager')

1 row created.

SQL> /
Enter value for p_no: 1002
Enter value for e_no: 7
Enter value for e_job: engineer
old 1: insert into work values('&p_no','&e_no','&e_job')
new 1: insert into work values('1002','7','engineer')

1 row created.

SQL> /
Enter value for p_no: 1001
Enter value for e_no: 10
Enter value for e_job: programmer
old 1: insert into work values('&p_no','&e_no','&e_job')
new 1: insert into work values('1001','10','programmer')

1 row created.

SQL> /
Enter value for p_no: 1002
Enter value for e_no: 12
Enter value for e_job: apptester
old 1: insert into work values('&p_no','&e_no','&e_job')
new 1: insert into work values('1002','12','apptester')
1 row created.

SQL> select * from work;

P_NO E_NO E_JOB


---------- ---------- --------------------
1000 5 developer
1001 6 projectmanager
1002 7 engineer
1001 10 programmer
1002 12 apptester

SQL> create table department(dept_no number(10),d_name varchar2(20));

Table created.

SQL> insert into department values('&dept_no','&d_name');


Enter value for dept_no: 2
Enter value for d_name: software
old 1: insert into department values('&dept_no','&d_name')
new 1: insert into department values('2','software')

1 row created.

SQL> /
Enter value for dept_no: 3
Enter value for d_name: productmarketing
old 1: insert into department values('&dept_no','&d_name')
new 1: insert into department values('3','productmarketing')

1 row created.

SQL> /
Enter value for dept_no: 5
Enter value for d_name: hardware
old 1: insert into department values('&dept_no','&d_name')
new 1: insert into department values('5','hardware')

1 row created.

SQL> select * from department;

DEPT_NO D_NAME
---------- --------------------
2 software
3 productmarketing
5 hardware
-: QUERIES :-
1. Change the name of the employee with e_no=5
SQL> update emp2 set e_name='&e_name' where e_no='&e_no';
Enter value for e_name: raj
Enter value for e_no: 5
old 1: update emp2 set e_name='&e_name' where e_no='&e_no'
new 1: update emp2 set e_name='raj' where e_no='5'

1 row updated.

SQL> select * from emp2;

E_NO E_NAME DEPT_NO B_SAL JOB_STATUS


---------- -------------------- ---------- ---------- --------------------
5 raj 2 20000 officework
6 susmita 3 60000 manager
7 mouli 5 40000 engineer
9 parimal 3 45000 designer
12 pampa 5 50000 tester

2. Delete the project with p_no=1002


SQL> delete from project where p_no='&p_no';
Enter value for p_no: 1002
old 1: delete from project where p_no='&p_no'
new 1: delete from project where p_no='1002'

1 row deleted.

SQL> select * from project;

P_NO P_NAME
---------- --------------------
1000 androidAppdesign
1001 AI
3. Find the number and name of the employees whose employee number
are 5,7,10,12
SQL> select e_no,e_name from emp2 where e_no in(5,7,10,12);

E_NO E_NAME
---------- --------------------
5 raj
7 mouli
10 parimal
12 pampa
4. Find the name of all employees who do not work in the department
where ‘pampa’ is working.
SQL> select e_name from emp2 where dept_no not in(select dept_no from emp2
where e_name='pampa');

E_NAME
--------------------
raj
susmita
parimal

5. Find the name of those employee of the dept_no=3 who get more
salary than the highest paid employee with dept_no=5.
SQL> select e_name from emp2 where dept_no=3 and b_sal>(select max(b_sal) from
emp2 where dept_no=5);

E_NAME
--------------------
Susmita

6. Display the name of those employee whose job status begins with
‘office’
SQL> select e_name from emp2 where substr(job_status,1,6)='office';

E_NAME
--------------------
raj

7. Display the name of those employees whose employee number is


odd.

SQL> select e_name,e_no from emp2 where mod(e_no,2)=1;

E_NAME E_NO
-------------------- ----------
raj 5
mouli 7
Teacher’s Signature
Dept. of Computer Science

Panskura Banamali College

You might also like