Uploaded by Harrel

DbmsAssignment 102018062

advertisement
DBMS LAB
ASSIGNMENT
Shagun Sharma
102018062
COBS-3
a
ASSIGNMENT-1
1. Create table Student (Roll no, Name, DOB, Gender, Class, College, City,
Marks)
create table student (roll_no number(10), name varchar(50), dob date, gender
varchar(10), class varchar(10) , college varchar(10),city varchar(10),marks
number(10));
2. Insert 5 records in student table
insert into student values(1,'Noah','17-mar2008','male','B.tech','TIET','Patiala',20);
insert into student values(9,'Lyra','11-feb2002','female','B.tech','TIET','Patiala',75);
insert into student values(5,'Mira','8-dec2000','female','B.tech','TIET','Patiala',85);
insert into student values(44,'Zoey','9-jan2001','female','B.tech','TIET','Derabassi',15);
insert into student values(32,'Luna','15-feb2000','female','B.tech','TIET','Amritsar',90);
3. Display the information of all the students
Select *from student;
4. Display the detail structure of student table
Desc student;
5. Display Rno, Name and Class information of ‘Patiala’ students.
select roll_no,name,class from student where city='Patiala';
6. Display information on ascending order of marks
Select *from student order by asc;
7. Change the marks of Rno 5 to 89.
Update student
Set marks =89 where roll_no=5;
8. Change the name and city of Rno 9.
Update student;
Set name=’Yashvi’ , city=’New Delhi’ where roll_no=9;
9. Delete the information of ‘Amritsar’ city records
Delete student where city = ‘Amritsar’;
10.Delete the records of students where marks<30.
Delete from student where marks <30;
ASSIGNMENT-2
1. Create table emp which has the following attributes (employee table)
(empno, ename, job, sal, deptno)
create table emp(empno number(10), ename varchar(20), job varchar(10), sal
number(10), deptno number(5));
2. Insert appropriate records in above tables.
insert into emp values(1,'Raman','Salespersn',3000,03);
insert into emp values(2,'Shweta','Clerk',2300,02);
insert into emp values(3,'Vinod','Manager',10000,10);
insert into emp values(4,'Yashasvi','Salespersn',2500,03);
insert into emp values(5,'Ashu','Clerk',1800,02);
insert into emp values(6,'Priya','Secretary',6000,20);
insert into emp values(7,'Vaibhav','HR',15000,30);
select *from emp;
3. Get employee no and employee name who works in dept no 10
select empno,ename from emp where deptno = 10;
4. Display the employee names of those clerks whose salary > 2000
select ename from emp where job = 'Clerk' AND sal>2000
5. Display name and sal of Salesperson & Clerks
select ename,sal from emp where job in ('Clerk','Salespersn');
6. Display all details of employees whose salary between 2000 and 3000
select *from emp where sal between 2000 and 3000;
7. Display all details of employees whose dept no is 10, 20, or 30
select *from emp where deptno in (10,20,30);
8. Display names of those employees whose commission is NULL
alter table emp
add Commission number(10) ;
update emp set Commission = 12 where deptno in (10,20);
update emp set Commission = 2 where deptno = 02;
update emp set Commission = NULL where deptno in (03,30);
select ename from emp where Commission is NULL;
9. Display dept no & salary in ascending order of dept no and with in each
dept no salary should be in descending order.
select deptno,sal from emp order by deptno asc,sal desc;
10.Display name of employees that starts with ‘C’
select ename from emp where ename LIKE 'C%';
11.Display names of employees whose name end with C
select ename from emp where ename LIKE 'C%';
12.Display name of employees having two ‘a’ or ‘A’ chars in the name
select ename from emp where (ename like '%a%a%' or ename like '%aa%');
13.Display the name of the employees whose second char is ‘b’ or ‘B’
select ename from emp where (ename like '_b%' or ename like '_B%');
14.Display the name of the employees whose first or last char is ‘a’ or ‘A’
select ename from emp where ename like '%a' or ename like ‘a%';
ASSIGNMENT-3
1. Create table emp which has the following attributes (employee table)
(@empno, ename, job, sal, deptno) Where empno is primary key, ename
is unique, job in (Prof, AP, and Lect), sal is not NULL, and deptno is
foreign key
create table emp1 (empno number(10) primary key, ename varchar(20)
unique, job varchar(20) check(job in ('Prof','AP','Lect')), sal number(10) not
null, deptno references emp1(empno));
2. Create table dept which has the following attributes (department table)
(@deptno, dname) Where deptno is primary key, dname in (Acc, comp,
elect)
create table dept (deptno number(10) primary key, dname varchar(10) check
(dname in ('Acc','Comp','Elect')));
3. Create table S which has the following attributes (Salesperson table)
(@sno, sname, city) Where sno is primary key
create table S(sno number(10) primary key, sname varchar(20), city
varchar(20));
4. Create table P which has the following attributes (Part table) (@pno,
pname, color) Where pno is primary key
create table P(pno number(10) primary key, pname varchar(20),color
varchar(10));
5. Create table J which has the following attributes (ProJect table) (@jno,
jname, city) Where jno is primary key
create table J(jno number(10) primary key, jname varchar(20), city
varchar(20));
6. Create table SPJ which has the following attributes (@ (sno, pno, jno),
qty) Where combination of (sno, pno, jno) is primary key, also sno, pno,
jno are foreign keys
create table SPJ(sno number(10) references S(sno),pno number(10) references
P(pno),jno number(10) references J(jno), qty number(10), PRIMARY
KEY(sno,pno,jno));
7. Insert appropriate records in above tables.
insert into emp1 values(1,'Reya','Prof',50000,1);
insert into emp1 values(2,'Shreya','AP',35000,2);
insert into dept values(23,'Comp');
insert into dept values(43,'Elect');
insert into S values(10,'John','Chandigarh');
insert into S values(12,'Robin','Mohali');
insert into P values(11,'ABC','Black');
insert into P values(13,'XYZ','Red');
insert into J values(100,'Web Dev','Delhi');
insert into J values(101,'App dev','Mumbai');
insert into SPJ values(10,11,100,5);
insert into SPJ values(12,13,101,7);
select *from emp1;
select *from dept;
select *from S;
select *from P;
select *from J;
select *from SPJ;
ASSIGNMENT-4
1. Check the structure of tables.
desc emp1;
desc dept;
desc S;
desc P;
desc J;
desc SPJ;
2. Check the constraint name for applied constraints
select constraint_name from user_constraints where table_name='EMP1';
select constraint_name from user_constraints where table_name='DEPT';
select constraint_name from user_constraints where table_name='S';
select constraint_name from user_constraints where table_name='P';
select constraint_name from user_constraints where table_name='J';
select constraint_name from user_constraints where table_name='SPJ';
3. Drop the unique constraint on ENAME
select constraint_name from user_constraints where table_name='EMP1' and
constraint_type ='U';
alter table emp1
drop constraint SYS_C0086676984;
4. Drop the Foreign Key constraint on DEPTNO
select constraint_name from user_constraints where table_name='EMP1' and
constraint_type ='R';
alter table emp1
drop constraint SYS_C0086676985;
select *from user_constraints where table_name='EMP1';
5. Add Foreign Key constraint on DEPTNO
alter table emp1
add constraint fk
FOREIGN KEY (deptno) references emp1(empno);
6. Change Data type of ENAME
alter table emp1
modify ename char(25);
desc emp1;
7. Change width of DNAME
alter table dept
modify dname varchar(30);
desc dept;
8. Add COMM column in EMP table
alter table emp1
add COMM number(10);
desc emp1;
9. Drop CITY column from J table
alter table J
drop column city;
desc J;
10.Create duplicate copy of EMP table
create table emp2 as select* from emp1;
desc emp2;
11.Copy structure of DEPT table in another table with different column
names.
create table deptnew as (select deptno as departno, dname as departname
from dept);
desc deptnew;
12.Change the name and job of the employee whose EMPNO =100
update emp1
set ename = 'Jayati', Job = 'Lect' where empno =1;
select *from emp1;
13.Delete the record of employee who belong to computer department
delete from dept where deptno = 23;
select *from dept;
14.Drop DEPT Table
drop table dept;
15.Drop duplicate table of EMP table
drop table emp2;
ASSIGNMENT-5
Table emp and dept
1. List the total number of employees
select count(*) from emp;
2. List the total no of departments
select count(*) from dept;
3. List the total, maximum, & minimum salary where deptno is 30
select sum(sal) as total, max(sal), min(sal) from emp where deptno = 30;
4. Display the name of the employee getting maximum salary
select ename from emp where sal = (select max(sal) from emp);
5. Display the total salary for each department
select deptno,sum(sal) as Total_Salary from emp group by deptno;
6. Display the total salary for each job.
select job,sum(sal) as Total_Salary from emp group by job;
7. Display the total salary for each job within each department.
select job,deptno,sum(sal) as Total_Salary from emp group by job,deptno;
8. Display the average salary for each job in deptno 20.
select job,avg(sal) from emp where deptno = 20 group by job;
9. Display the total salary for each job excluding the ‘manager’ jobselect
count(*) from emp;
select job,sum(sal) as Total_Salary where job <>'Manager' group by job;
10.Display the average salary for each job in deptno 20, but only display
those jobs where total salary is greater than 2000 & display the output in
descending order of salary
select job,avg(sal) as "Average_Salary" from emp where deptno =20 group by
job having sum(sal)>2000 order by sum(sal) desc;
11.Display the total no of employees for each department excluding the
dno 10 & display only those departments where more than five()
employees work. Display the output in descending order of total no of
employees?
select count(*) "NumberOfEmployees",deptno from emp where deptno <>10
group by deptno having count(*)>=5 order by count(*) desc;
12.Display the total no of employees for each department excluding the
comp dept & display only those departments where more then five (5)
employees work. Display the output in descending order of total no of
employees?
select count(*) "Number",emp.deptno
from emp,dept
where dname<>'Comp' and emp.deptno=dept.deptno
group by emp.deptno
having count(*)>5
order by emp.deptno desc;
13.Display total number of prof in univ.
select count(*) from emp where job = 'Prof';
14.Display total number of prof/Lect in deptno 10.
select count(*) from emp where job = 'Lect' and deptno =10;
15.Display total number of prof in each dept
select count(*) from emp where job = 'Prof' group by deptno;
16.Display total number of emp working in each job in each dept
select job,deptno,count(*) total_number from emp group by deptno,job;
Queries based on Student Table
17.List the total number of students from each city.
Select city,count(*)"Student Number" from student group by city;
18.Name of city having max students.
select city from student group by city count(*) = (select max(count(*)) from
student group by city);
19.Name of Institute having min students.
select institute,count(*)"Number" from student group by institute having
count(*) = (select min(count(*)) from student group by institute);
20.Name of Institute whose student got max marks.
select institute,marks from student where marks = (select max(marks) from
student);
21.Name of institute having more than 5 students excluding ‘tu’.
Name of institute having more than 5 students excluding ‘tu’.
22.Name of Institute whose student got max marks.
select institute,marks from student where marks = (select max(marks) from
student);
ASSIGNMENT-6
1. Display total no of suppliers supplying parts
select count(*)from S;
2. Display the name of the supplier who supplies quantity greater than the
average quantity supplied
select sname from S,SP where S.sno = SP.sno and SP.qty>(select avg(qty) from
SP);
3. Display total quantity supplied by each supplier
select sno,sum(qty) "Total Quantity" from SP group by sno;
4. Display part name & total quantity supplied for each part
select pname, sum(qty) "Total Quantity" from P,SP where SP.pno = P.pno
group by pname;
5. Display total no of parts supplied by each supplier excluding suppliers of
Patiala & also display only those suppliers who supply more than five (5)
parts
select count(*),sname from s,sp where s.sno=sp.sno and s.city not in ('patiala')
group by s.sno,sname having count(*)>5;
6. For each part supplied get part number and names of all cities supplying
the part.
select pno,city from S,SP where S.sno=SP.sno;
7. Get Qty supplied for Red parts.
select SP.sno,SP.pno,qty from P,SP where P.pno = SP.pno and color = 'red';
8. Get Sname supplying Red part.
select sname from S where sno in (select sno from P,SP where p.pno=sp.pno
and color in ('red'));
9. Get Sname, Pname who supply qty more than 100.
SELECT S.sno, P.pno, SP.qty
from S
INNER JOIN SP
on S.sno = SP.sno
INNER JOIN P
on SP.pno = P.pno and SP.qty>100;
10.Get all pairs of suppliers numbers such that the two suppliers are
located in the same city.
SELECT
S1.city,
S1.sname Supplier_1,
S2.sname Supplier_2
FROM
S S1 inner join S S2 ON S1.sno > S2.sno
AND S1.city = S2.city
11.Get Sname for suppliers who supply part P2.
select sname from S where sno = (select sno from SP,P where P.pno = SP.pno
and P.pno =102)
12.Get Supplier numbers for suppliers who supply at least one part supplied
by supplier S2.
select S.sno from SP,S where SP.sno = S.sno and S.sname in('2') and qty>0;
13.Get supplier names for suppliers who do not supply part P2.
select sname from S where sno not in (select sno from P,SP where
SP.pno=P.pno and P.pno in 102);
14.Get suppliers numbers for suppliers who are located in same city as
supplier S1.
select sno, city from S where city = (select city from S where sno =1) and
sno<>1;
15.Get ename and mgrname from emp table
Select ename,mgrname from emp,mgr where emp.mgrid=mgr.mgrid;
16.Get name of emp who join the company before their managers.
Select ename from emp,mgr where emp.mgrid=mgr.mgrid and emp.joindate;
17.Get dname and ename also get those deptt which has no emp.
select dname,ename from emp,dept where emp.deptid=dept.deptid where
count(*)=0 group by deptid;
18.Get all possible parts which can be supplied by suppliers of patiala.
select P.pno p from P,SP where P.pno = SP.pno and sno in (select sno from S
where city in ('Patiala'));
ASSIGNMENT-7
1. Display ename who belong to same department as that of ‘ajay’
select ename from emp where deptno = (select deptno from emp where
ename = 'Ajay') and empno<>3;
2. Display ename who do same job as that of ‘ajay’
select ename from emp where job = (select job from emp where ename =
'Ajay') and empno<>3;
3. List all top five (5) highest paid employees from employee table
select *from emp order by sal desc fetch first 5 rows only;
4. List name of five (5) employees who has minimum pay
select ename from emp order by sal desc fetch first 5 rows only;
5. Display the detail of department where manager no is 101
select * from dept where deptno in (select deptno from emp where
manid=101);
6. List named of employees whose salary is less then any clerk and are not
clerks
select ename from emp where sal <(select min(sal) from emp where job
='Clerk') and job not in ('Clerk');
7. Display the job name with highest average salary
select job from emp group by job having avg(sal)=(select max(avg(sal)) from
emp group by job);
8. Display name of the employee(s) whose salary id greater than average
salary of department
select ename,deptno,sal from emp e1 where sal>(select avg(sal)from emp e2
where e1.deptno=e2.deptno);
9. Display name of those employee in each department who get the
highest salary
select *from emp e1 where sal = (select max(sal) from emp e2 where
e1.deptno = e2.deptno);
10.List all employees who have at least one (1) person reporting to him/her
select * from manager where managerno in (select manid from emp group by
manid having count(*)>0);
11.List of employees who do not manage any other employee
select *from manager where managerno not in (select manid from emp group
by manid having count(*)>0);
12.Display name of those employees whose salary is greater than the
lowest salary of employee belonging to deptno 20
select ename from emp where sal > (select min(sal) from emp where
deptno=20);
13.Display name of those employees whose salary is greater than the
average salary of manager.
select ename from emp where sal >(select avg(sal) from manager);
14.Display name of those employees whose salary is greater than highest
salary of all employees belonging to deptno 20
select ename from emp where sal >(select max(sal) from emp where
deptno=20);
15.List the name of the employees who earn more than the highest paid
manager.
select *from emp where sal >(select max(sal) from manager);
ASSIGNMENT-8
1. Q1) Use the following functions
1) chr (n):
select chr(89) output from dual;
2) concat(char1, char2):
select concat('Shagun',' Sharma') name from dual;
3) instr(string,char)
select instr('Shagun', 'a') A from dual;
4) length(n)
select length('ABVCDFGGD') len from dual;
5) (char1 ,n [,char2])
select LPAD('assignment',12,'*') leftpaddedstring from dual;
6) ltrim(string [,char(s)]):
select LPAD('assignment',12,'*') leftpaddedstring from dual;
7) rpad(char1 ,n [,char2]):
select rpad('Evaluation',15,'#') rightpaddedstring from dual;
8) rtrim(string [,char(s)]):
select rtrim('Evaluation
') rightrim from dual;
9) replace(char,search_string, replacement_string):
select replace('This is an assignment','a','w') replaced from dual;
10) substr(string ,position ,substring length)
select substr('Transparency',3,4) substring from dual;
11) initcap(char)
select initcap('dbms assignment') capital from dual;
12) lower(string)
select lower('SHAGUN SHARMA') Name from dual;
13) upper(string):
select upper('subject') up from dual;
14) translate(char ,from string ,to string):
select translate( '+91 25-2469782464', '0123456789-+','6789012345+-' )
encode_number from dual;
15) abs(n)
select abs(-123) from dual;
16) ceil(n)
select ceil(2.34) from dual;
17) cos(n)
select cos(0) from dual;
18) exp(n)
select exp(1) from dual;
19) floor(n)
select floor(2.34) from dual;
20) mod(m ,n):
select mod(45,2) from dual;
21) power(x ,y)
select power(2,2) from dual;
22) round(x [,y]):
select round(3.834567) from dual
23) sign(n):
select sign(-89) from dual;
24) sqrt(n);
select sqrt(36) from dual;
25) trunc(x ,n):
select trunc(36.33444443,2) from dual;
26) sysdate:
select sysdate from dual;
27) add_months(d ,n):
select add_months(sysdate,5) from dual;
28) last_day():
select last_day(sysdate) from dual;
29) months_between(date1 ,date2):
select months_between('07-Oct-2022', '07-Sep-2022') from dual;
30) next_day(date ,char)
select next_day(sysdate, 'wed') from dual;
31) greatest(expr)
select greatest(89,120,45,78,65)from dual;
32) least(expr)
select least(89,120,45,78,65)from dual;
2. Display current time in hour : min : sec format
select to_char(sysdate, 'hh-mm-ss') from dual;
3. Display salary + commission from emp table.
select sal + commission from emp;
4. Store any date value in hiredate column of table
update emp set hiredate='07-MAY-2021' where ename='Ashu';
5. Display name of employee(s) who join the company in 1985
select ename from emp where to_char(hiredate,'yyyy')=1985;
6. Display name of the employee(s) who join the company this year
select ename from emp where to_char(hiredate,'yyyy')=2022;
Download