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

DBMS_I_Practical_solution

The document outlines a series of SQL assignments involving the creation of various database tables such as 'cust', 'movie', 'dept', 'emp', 'doctor', 'patient', 'treatment', and 'appointment', along with their respective fields and constraints. It also includes multiple queries for data retrieval, insertion, updates, and alterations, demonstrating operations like single table retrieval, group by, joins, and subqueries. The assignments cover a wide range of SQL functionalities, including constraints, functions, date queries, and data manipulation.

Uploaded by

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

DBMS_I_Practical_solution

The document outlines a series of SQL assignments involving the creation of various database tables such as 'cust', 'movie', 'dept', 'emp', 'doctor', 'patient', 'treatment', and 'appointment', along with their respective fields and constraints. It also includes multiple queries for data retrieval, insertion, updates, and alterations, demonstrating operations like single table retrieval, group by, joins, and subqueries. The assignments cover a wide range of SQL functionalities, including constraints, functions, date queries, and data manipulation.

Uploaded by

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

Assignment -1-A

Customer Master Table: Cust

Create Table cust

Cust_id Varchar2(5),

lname char(10),

fname char(10),

area varchar2(10),

phone_no number(10)

);

o/p: Table Created

Insert Record:

insert into cust values ('&cust_id','&lname','&fname','&area', &phone_no);

Movie Master Table: movie

Create Table movie

mv_no number(5),
title varchar2(25),

type varchar2(10),

star varchar2(25),

price number(8,2)

);

o/p: Table Created

Insert Record:

insert into movie values (&mv_no,'&title','&type','&start','&price);

Assignment -1-B - Single Table Retrieval:


1. Display entire content of cust table.

Ans: select * from cust;

2. Display customer first name with their area.

Ans: select fname,area from cust;

3.Display customer with their fname and last name whose cust_id is a01 or a02.

Ans: Select fname, lname from cust where cust_id='a01' or cust_id='a02';

4. Display all the records from cust table whose cust_id is a02 and area is Surat.
Ans: select * from cust where cust_id='a02' and area='Surat';

5. Display customer details who does not have phone number.

Ans: select * from cust where phone_no is null;

Assignment -2 -(Table Creation with Constraints)


Table Name: dept

Create Table cust

deptno number(2),

dname varchar2(15),

loc varchar(15)

);

o/p: Table Created

Insert Record:

insert into dept values (&deptno,'&dname','&loc');

Table Name: EMP

create table emp


(

empno number(4) primary key,

ename varchar2(10),

job varchar2(9),

mgr number(4),

hiredate date,

sal number(7,2),

comm number(7,2),

deptno number(2) references dept1(deptno)

);

Table Created

--> insert into emp


values(&empno,'&ename','&job',&mgr,'&hiredate',&sal,&comm,&deptno);

Table Name: Doctor

create table doctor

d_id varchar2(3) check(d_id like 'd%'),

dname varchar2(25),

dcity varchar2(20)check(dcity='anand' or dcity='baroda' or dcity='nadiad'),

spac varchar2(20)check(spac='ent' or spac='dental' or spac='ortho' or


spac='eye'),

primary key (d_id)


);

Table Created

--> insert into doctor values(&d_id,'&dname','&dcity',&spac');

Table Name: patient

create table patient

p_id varchar2(3) check(p_id like 'P%'),

pname varchar2(25),

bdate date,

paddr varchar2(35),

gender char(1) default 'M',

check(gender in('M', 'F')),

primary key (p_id)

);

Table Created

--> insert into patient values('&p_id','&pname','&bdate','&paddr','&gender');

Table Name: treatment

create table treatment

t_id varchar2(3) check(t_id like 'T%'),

tdetails varchar2(50),
charges number(5( check(charges >0),

primary key (t_id)

);

Table Created

--> insert into treatment values('&t_id','&tdetails','&charges');

Table Name: appointment

create table appointment

a_id number(6) primary key,

d_id varchar2(3) references doctor(d_id),

p_id varchar2(3) references patient(p_id),

t_id varchar2(3) references treatment(t_id),

app_date date,

duration number(4),

status char(1) check(status in('c','i'))

);

Table Created

--> insert into appointment


values(&a_id,'&d_id','&p_id','&t_id','&app_date','&duration',&status');

Assignment - 3- single table retrieval


1.select empno,ename from emp;

2.select distinct job from emp;

3 & 7.select * from emp where deptno <>20; OR select * from emp where deptno
not in (20);

4. select * from emp where ename like 'J%' or like 'j%';

5.select ename from emp where comm is not null; OR select ename from emp
where comm >0 ;

6.select * from emp where deptno=10 and hiredate > '01-sep-81';

8. select ename "empname", job, sal "salary" from emp;

9. elect ename,sal,sal*10/100 ''PF'', sal*0.50 "HRA", sal * 0.50 "DA",


sal+(sal*0.50)+(sal * 0.30) - (sal*0.10) "gross" from wmp order by sal;

10.select ename, sal812 "Annual salary" from emp;

11.select ename from emp where ename loke 'J%' OR ename like 'j%';

12.select ename from emp where ename like 'J_N%';

13. select * from emp where job in('clerk','salesman','analyst');

14.select * from emp where ename like 'J%' and job='clerk';

15select * from emp order by ename desc;

Assignment - 4 - Queries using function

1.select empno,upper(ename),sal from emp;

2.select empno,lower(ename),job from emp;


3.select max(sal),min(sal) from emp;

4. select sum(sal),avg(sal) from emp where deptno=10;

5. select count(deptno) from emp where deptno in(20,30);

Assignment - 5 - Queries using Date

1.select empno,ename,sal,to_char(hiredate,'ddth mon,yyyy')"hiredate" from emp;

2.select empno,ename,sal,to_char(hiredate,'ddspth month,yy')"hiredate" from emp;

3. select empno,ename,job,sal from emp where to_char(hiredate,'mon')='sep';

4. select empno,ename,comm from emp where


trim(to_char(hiredate,'day'))='monday';

5.select sysdate+15 from dual;

6. select * from emp where to_char(sysdate,'yyyy')-to_char (hiredate,'yyyy')>20;

Assignment - 6 - Group by...having


1. select dept.dname,count(*) from emp,dept where emp.deptno=dept.deptno group
by emp.deptno,dept.dname;

2. select deptno,job,count(*) from emp where job in ('Salesman','Clerk') group by


deptno,job;

3. select deptno,max(sal) from emp group by deptno;

4. select dept.dname,avg(sal) from emp,dept where emp.deptno=dept.deptno group


by emp.deptno,dept.dname;

5. select job,sum(comm) from emp group by job;

6. select deptno,job,sum(sal) from emp where job in('Clerk','Analyst') group by


deptno,job;

7.select emp.deptno,dept.dname,max(sal),emp.ename from emp,dept where


dept.deptno=emp.deptno group by emp.deptno,

dept.dname,emp.ename having max(sal)>=5000;

8.select deptno,sum(sal),count(*) from emp group by deptno having count(*) >2;


9.select deptno,sum(sal),count(*) from emp group by deptno having
sum(sal)<2000;

O/P: No rows

10. select paddr,count(*) from patient group by paddr;

11. select spec,count(*) from doctor group by spec;

12. select paddr,count(*) from patient where gender='M' group by paddr;

13. Select count(*) from doctor where spec='Eye' group by spec;

Assignment - 7 - Join

1. select a.ename,b.dname from emp a,dept b where a.deptno=b.deptno and


b.dname='Accounting';

2.select a.deptno,b.dname,a.ename from emp a,dept b where a.deptno=b.deptno


and b.dname='Sales' and a.sal > 2000;

3. select a.ename,b.dname from emp a,dept b where a.deptno=b.deptno and


b.dname='Research' and a.ename like 'A%';
4. select emp.ename,dept.dname,emp.sal from emp,dept where
emp.deptno=dept.deptno and emp.job in('Clerk','Salesman');

5. select b.dname,a.ename,b.deptno from emp a, dept b where a.deptno=b.deptno


and a.hiredate <'01-SEP-81';

6. select doctor.dname,patient.pname,treatment.tdetails,treatment.charges from


doctor,patient,treatment,appointment where doctor.d_id=

appointment.d_id and patient.p_id=appointment.p_id and


treatment.t_id=appointment.t_id;

7. select patient.pname,appointment.app_date from patient,appointment where


patient.p_id=appointment.p_id;

8. select doctor.dname,doctor.spec,appointment.app_date from doctor,appointment


where doctor.d_id=appointment.d_id;

9. select tdetails from treatment where charges > 500;

10. select patient.pname,appointment.app_date from patient,appointment where


patient.p_id=appointment.p_id and to_char(app_date,'MON')

='DEC';
11. select doctor.dname,patient.pname,treatment.tdetails,treatment,charges from
doctor,appointment,patient, treatement where

doctor.d_id=appointment.d_id and patient.p_id=appointment.d_id and


treatment.t_id=appointment.t_id;

12.select patient.pname,treatment.tdetails,treatment.charges,appointment.app_date
from patient,treatment,appointment, doctor where

patient.d_id=appointment.d_id and dname='Dharmendra patel';

13. select pname from patient,appointment,doctor where


patient.p_id=appointment.p_id and doctor.d_id=appointment.d_id and

doctor.dname='Mehul Patel';

14. select patient.pname from appointment,patient where


appointment.p_id=patient.p_id and status='I';

15. select patient.pname,to_char(sysdate,'yyyy')-


to_char(patient.bdate,'yyyy')as"age" from appointment,patient where

patient.p_id=appointment.p_id and patient.gender='M' and status='I';

16. select a.ename,b.ename from emp a,emp b where a.mgr=b.empno;


Assignment - 8 - Group by..having(multiple Tables)

1. select emp.deptno,dept.dname,count(*) from emp, dept where


emp.deptno=dept.deptno group by emp.deptno,dept.dname;

2. select dept.dname,sum(sal) from emp,dept where emp.deptno=dept.deptno and


emp.job='Clerk' group by emp.deptno,dept.dname;

3. select dept.dname,min(sal) from emp,dept where emp.deptno=dept.deptno and


job='salesman' group by emp.deptno,dept.dname having

min(sal) > 4000;

o/p: No Rows

4. select doctor.dname as"doctor name", sum(treatment charges) as "ernings" from


doctor,appointment,treatment where

doctor.d_id=appointment.t_id and treatment.t_id=appointment.t_id and


doctor.dname='Mehul Patel' group by appointment.d_id,doctor.dname;

5. select doctor.dname from doctor, appointment where


doctor.d_id=appointment.d_id and appointment.status='I' group by

appointment.d_id,doctor.dname having count(*) >=2;


6. select doctor.dname,count(*) as"total patient" from doctor,appointment where
doctor.d_id=appointment.d_id group by

appointment.d_id,doctor.dname;

Assignment - 9 - Sub query

1. select empno,ename,sal from emp where sal=(select sal from emp where
ename='Ward');

2. select empno,ename,job from emp deptno=(select deptno from dept where


dname ='Salesman');

3. select tdetails from treatment where t_id in(select t_id from appointment where
d_id in(select d_id from doctor where dcity='Anand'));

4. select pname from patient where to_char(sysdate,'yyyy')-


to_char(bdate,'yyyy')>28 and p_id in (select p_id from appointment where

status='c');

5. select dname from doctor where d_id in (select d_id from appointment where
p_id in(select p_id from patient where gender='M'));
Assignment - 10 - Updating and Alteration of table and table data

1. alter table cust modify cust_id varchar2(20);

2. alter table cust mofify (lname varchar2(15),fname varchar2(15));

3. alter table cust add gender char(1);

4. update movie set star ='Axay kumar' where mv_no=1;

5. update cust set area='surat' where cust_id = 'a01';

You might also like