0% found this document useful (0 votes)
6 views32 pages

Labb

The document is a lab record for a Database Management Systems (DBMS) course, detailing various SQL queries and experiments conducted by a student named Ruchitha S. It includes multiple experiments focusing on different SQL operations such as SELECT, INSERT, UPDATE, DELETE, and creating views and tables. Each experiment contains specific tasks with corresponding SQL queries to demonstrate the student's understanding of database concepts.

Uploaded by

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

Labb

The document is a lab record for a Database Management Systems (DBMS) course, detailing various SQL queries and experiments conducted by a student named Ruchitha S. It includes multiple experiments focusing on different SQL operations such as SELECT, INSERT, UPDATE, DELETE, and creating views and tables. Each experiment contains specific tasks with corresponding SQL queries to demonstrate the student's understanding of database concepts.

Uploaded by

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

Page |1

SCHOOL OF COMPUTER SCIENCE AND IT


DEPARTMENT OF CS & IT
BCA PROGRAMME
SEMESTER: III

DBMS LAB RECORD

SUBMITTED BY: FACULTY IN-CHARGE


NAME: RUCHITHA.S PROF. SOUMYA.K
USN NO: 22BCAR0471
COURSE: BCA (CS)
SECTION: “J” SIGNATURE
Page |2

EXPERIMENT-1

1. To Write a query to display the empno, ename, job, hiredate, provide alias for each
column, display employees who is working as PRESIDENT or ANALYST, and
employees belonging to deptno 10 or 20,and ename should not starts with S.

 select deptno as d, empno as e, ename as n, job as j, hiredate as h from emp where (job
in ('MANAGER','ANALYST')) and deptno!=40 AND deptno!=30 and job not like
'S%';

2. WAQ to display ename, salary, comm, calculate total salary and annual salary of all
employees whose salary ranges from 1000 and 2500, and employees are belong to
department 10 or 30. And who is working as SALESMAN.

 select empno,ename,sal,comm,(sal+comm) as "Total_sal", (sal*12) as "annual_sal"


from emp where sal between 1000 and 4000 and deptno=40 or deptno=20 and
job='CLERK';
Page |3

3. Write a query to display ENAME, SAL, DEPTNO, display only employees who is not
working under any manager.

 Select ename,sal,deptno from emp where mgr is NULL;

4. Write a query to display ENAME, SAL, HIRDATE display Details of employees who
have been with the company for more than thirty years, who is not drawing salary from
1500 to 3000.

 Select ENAME, SAL, HIREDATE from emp where to_char (sysdate-hiredate)>=30


and SAL NOT BETWEEN 1500 AND 3000;
Page |4

5. WAQ to display ename, deptno, salary, comm and find increment of salary by 10%,
and increment of commission by 5% for all employees and sort salary and department
number.

 Select ename,deptno,sal,comm,sal+sal*1.1 as increment_sal, comm+comm*1.5 as


increment_comm from emp order by sal asc;
Page |5

EXPERIMENT-2

1. WAQ to display the details of all employees whose name is Smith. But you are not
sure in which case enames are stored. Which statement will list all the employees whose
ename is Smith?

 select empno, upper(ename) from emp where ename like '%SMITH';

2. WAQ to display ename, mgr, sal and comm where employees are not working under
any manager as a value as NO MANAGER and comm as NO COMMISSION where
employees are not taking any commission from the deptno 10 0r 20.

 select ename, NVL(to_char(mgr), 'NO manager') as manager,


NVL(to_char(comm),'no commission') as commission from emp;
Page |6

3. WAQ to display the sum of the salary of all employees according to department and
job whose sum of the salary more than 5000, sort the deptno column in ascending order.

 select sum(sal), deptno from emp group by deptno having sum(sal)>300 order by
deptno

4. WAQ to display ename and commission,if commission is null then replace with salary
and if salary is also null then replace with 1000.

 select ename, NVL(comm, NVL(sal, 1000)) as commission_or_salary, sal from emp;


Page |7

5. Write a query to display maximum average salary of the employees according to


deptno.

 select deptno, avg(sal) from emp group by deptno order by avg(sal) DESC FETCH
FIRST ROW ONLY;
Page |8

EXPERIMENT-3

1. Write a query to hike the salary of the employees below jobs using CASE Job is
CLERK hike by 15% If job is SALESMAN hike by 10% Or if job is ANALYST by 12%

 select job, sal, case job when 'CLERK' then sal*1.15 when 'MANAGER' then sal*1.1
when 'ANALYST' then sal*1.12 else sal end as new_sal from emp;

2. Write a query to display the location from the department table as short Eg: NewYork
as NY, Dallas DS, Chicago CG, and BOSTON as BT.

 SELECT deptno, location, CASE WHEN location='BOSTON' THEN 'BT' WHEN


location ='DALLAS' THEN 'DS' WHEN location ='CHICAGO' THEN 'CG' ELSE
location END AS short_loc FROM dept;
Page |9

3. Write a query to find the sum of salary by grouping according to job and deptno having
cross tabulation.

 select job,
sum(case when deptno=10 then sal else 0 end) as dept_10,
sum(case when deptno=20 then sal else 0 end) as dept_20,
sum(case when deptno=30 then sal else 0 end) as dept_30,
sum(case when deptno=40 then sal else 0 end) as dept_40
from emp
group by job;

4. Write a query find the sum of the salary for job and find the grand total.

 SELECT job, SUM(sal) AS total_sal from emp group by ROLLUP(job);


P a g e | 10

EXPERIMENT-4

1. Management wants to know the maximum salary of all the employees for each deptno,
and display only if maximum salary greater than 2800 ,sort maximum salary in
descending order.

select deptno,max(sal) as max_sal from emp group by deptno having max(sal)>2800


order by max_sal desc;

2. WAQ to display employee name sal, comm, department name, location and calculate
total salary, annual salary for the department name RESEARCH or OPERATIONS, sort
annual salary in ascending order

 select e.ename,e.empno,e.sal,d.deptno,d.location,d.dname,(e.sal*12)as
"annualsal",(e.sal+e.comm)as "totalsal" from emp e ,dept d where d.dname
in('ACCOUNTING','RESEARCH') and e.deptno = d.deptno order by e.deptno asc;
P a g e | 11

3. Write a query to display the list of employees working under which manager.

 select ename, mgr from emp where mgr is not null group by ename, mgr;

4. Write a query to display ename and salary in dollar and prefix left white space of
salary by special character by * and employees belongs to deptno 30 and 40, who is
working as salesman and clerk.

 select ename, '$'||sal||'*' as formatted_salary from emp where (deptno in (20,30)) and
(job in ('SALESMAN', 'CLERK'));
P a g e | 12

5. Write a query to find the sum of the salary for each department name and for each
location, display only sum of the salary should not be equal to 10000.

 select d.dname, d.location, sum(e.sal) as salary from emp e, dept d group by dname,
location having sum(sal)>10000;
P a g e | 13

EXPERIMENT-5

1. WAQ to display ename , hiredate, sal and job where salary should be similar to
CLARK‟s salary.

 SELECT ename, hiredate, sal, job


FROM emp
WHERE sal = (SELECT sal FROM emp WHERE ename = 'FORD');

2. WAQ to display which employee is getting minimum salary from the deptno 30.

 SELECT ename, sal, job


FROM emp
WHERE deptno = 30 AND sal = (SELECT MIN(sal) FROM emp WHERE deptno =
30);
P a g e | 14

3. WAQT to display employee name, job and there department name whose job should
be similar to JONES job and deptno similar to empno 7934.

 SELECT e1.ename, e1.job, d.dname


FROM emp e1,dept d
WHERE e1.job = (SELECT job FROM emp WHERE ename = 'JONES')
AND e1.deptno = (SELECT deptno FROM emp WHERE empno = 7566) AND
e1.deptno=d.deptno;

4. WAQ to display empno,ename,job and salary where the salary should be less than
CLERK‟s salary.and job job should not be equal to SALESMAN.

 SELECT empno,ename, sal, job


FROM emp
WHERE sal<= (select sal from emp where ename='JONES') AND JOB!='salesman';
P a g e | 15

EXPERIMENT-6

1. WAQ To Display empno, ename, deptno and location of employee KING and salary
between 1000$ to 2000$.

 select e.empno,e.ename, e.deptno,d.location from emp e join dept d on


e.deptno=d.deptno where e.ename ='FORD'and e.sal between 1000 and 5000000;

2. WAQ to find average salary for each job,deptno and loc and display only details if
average salary greater than 5000$. (Use natural join)

 select e.job, e.deptno, d.location, avg(e.sal) as avgsalary from emp e join dept d on
e.deptno= d.deptno group by e.job, e.deptno, d.location having avg(e.sal)>5000;
P a g e | 16

3. Write a query to display all matching and non-matching details from emp .

 select e.*, d.* from emp e full join dept d on e.deptno=d.deptno;

4. Write a query to display all matching and non-matching records from emp as left table
and dept table as right.

 select e.*, d.* from emp e left join dept d on e.deptno=d.deptno;


P a g e | 17

5. Write a query to display all matching and non matching records from dept as right
table.

 select e.*, d.* from emp e right join dept d on e.deptno=d.deptno;


P a g e | 18

EXPERIMENT-7

1. Write a query to insert the three records to emp table at run time.

 insert into emp values(:empno,:ename,:job,:hiredate,:sal,:comm,:deptno);

2. Write a Query to update the salary of the employee by 15%, where the employee
should work under the deptno of SMITH

 update emp set sal = sal+(sal*15/100) where deptno=(select deptno from emp where
ename='Smith');
P a g e | 19

3. Write a query to delete the record from emp table where the employee should be
working as similar to JONES.

 delete emp where job in (select job from emp where ename='JONES');

4. Write a query to delete deptno 10 from dept table. Mention the error if there is any
error exists.

 delete dept where deptno=10;


P a g e | 20

5. Write a query to insert single record to emp table.

 insert into emp(empno, ename, job, mgr,


hiredate,sal,comm,deptno)values(101,'john','IT',7909,'12/2/2023',75000,4000,40);
P a g e | 21

EXPERIMENT-8

1. Create the table called product with attributes pno, pname, pprice, qty and total add the
constraint not null for the required columns and add check constraint to price >0

 create table product (pno int, pname varchar(20) not null, pprice numeric(5,2) check
(pprice>0), qty int not null, total numeric(5,2)not null);

2. Add the constraint primary key to pno by modifying the table.

 alter table product8 add primary key (pno);


P a g e | 22

3. Insert five records to product table at run time.

 insert into product8 values(:pno, :pname,:pprice,:qty,:total);

4. Then add new column special_offer

 alter table product8 add column special_offer varchar(10);


P a g e | 23

5. Delete first three records.

6. Truncate the table product

TRUNCATE TABLE product;


P a g e | 24

EXPERIMENT-9

1. Create a view empvu20 which has all employees details work for the deptno 20.

 create view empvu20 as select*from emp where deptno=20;

2. Write a query to create view on emp and dept table which has details like ename,
deptno, sal, dname and loc for deptno 10, and salary should be between 1000 to 2000 and
who are not drawing commission.

 CREATE VIEW emp_dept_view AS


SELECT e.ename, e.deptno, e.sal, d.dname, d.location
FROM emp e
JOIN dept d ON e.deptno = d.deptno
WHERE e.sal BETWEEN 1000 AND 2000 AND e.comm IS NULL;
P a g e | 25

3. Write a query to create view as EMPDETAILS from the tables EMP, DEPT and
SALGRADE which contains empno, ename, job, sal, deptno, dname,loc and grade,
where employees working as ANALYST and are not belongs to deptno 10 and 20.

 CREATE VIEW EMPDETAILS AS SELECT e.empno, e.ename, e.job, e.sal,


e.deptno, d.dname, d.location, s.grade FROM emp e JOIN dept d ON e.deptno =
d.deptno JOIN salgrade s ON e.sal BETWEEN s.losal AND s.hisal WHERE e.job =
'ANALYST' AND e.deptno NOT IN (30, 20);

4. Modify the EMPDETAILS view by using required clause. Add an alias for each
column name

 CREATE OR REPLACE VIEW EMPDETAILS AS SELECT e.empno AS


employee_number,e.ename AS employee_name,e.job AS employee_job,e.sal AS
employee_salary,e.deptno AS department_number,d.dname AS
department_name,d.location AS department_location,s.grade AS sal_grade FROM
emp e JOIN dept d ON e.deptno = d.deptno JOIN SALGRADE s ON e.sal
BETWEEN s.losal AND s.hisal WHERE e.job = 'ANALYST' AND e.deptno NOT
IN (10, 20);
P a g e | 26

5. Create a complex view dept_sum_view, store dname, minimum salary, maximum


salary and average salary department wise.

 CREATE VIEW dept_sum_view AS


SELECT d.dname,
MIN(e.sal) AS min_salary,
MAX(e.sal) AS max_salary,
AVG(e.sal) AS avg_salary
FROM dept d
LEFT JOIN emp e ON d.deptno = e.deptno
GROUP BY d.dname;
P a g e | 27

EXPERIMENT-10

i. Create table job_hostory By Copying the structure data from emp table.

 create table job_history as select*from emp where rownum<0;

2. Insert three records to emp table.

 delete job_history;
create table job_history as select*from emp where rownum<4;
select*from job_history;
P a g e | 28

3. WAQ to display unique records from both table emp and job_history

 select*from emp union select*from job_history;

4. WAQ to Display the employees work for the company from since from beginning to
till date from both the table emp and job_history

 select*from emp intersect select*from job_history where hiredate=(select


min(hiredate)from emp);
P a g e | 29

5. WAQ to display employees joined recently leaving who has joined since beginning.

 select*from emp minus select*from job_history;


P a g e | 30

EXPERIMENT-11

1. Create the new user called JAINBCAIIISEM with the password jain.

 create user JAINBCAIIISEM IDENTIFIED BY jain;

2. Change the password jain as BCA for the user JAINBCAIIISEM.

 alter user JAINBCAIIISEM IDENTIFIED BY Alok;

3. Provide the permission to the user JAINBCAIIISEM for create session, creating table,
view.

 Grant create session,create table, create view to JAINBCAIIISEM;

4. Take the permission create view from the user JAINBCAIIISEM

 Revoke insert,update,delete on emp from JAINBCAIIISEM;

5. Take back all the DML permission from the user JAINBCAIIISEM

 Revoke insert,update,delete on emp from JAINBCAIIISEM;

6. Create the role called HR and provide the privileges create table, create view to HR
.
 Create table,create view to HR;

7. Provide the HR role to ram and shyam.

 GRANT HR.role to ram,shyam;

8. Remove the created role HR and remove ram and shyam.

 Drop user ram,shyam and drop role HR.role;


P a g e | 31

EXPERIMENT-12

1. Write a PL/SQL program to create the trigger where data can jot be edited on Saturday
and Sunday on emp table.

 CREATE OR REPLACE TRIGGER prevent_weekend_edits


BEFORE INSERT OR UPDATE OR DELETE ON emp
FOR EACH ROW
DECLARE
day NUMBER;
BEGIN
-- Get the day of the week (1 = Sunday, 2 = Monday, ..., 7 = Saturday)
SELECT TO_CHAR(SYSDATE, 'D') INTO day FROM DUAL;
-- Check if it's Saturday (day = 7) or Sunday (day = 1)
IF day IN (1, 7) THEN
-- Prevent the operation and raise an error
RAISE_APPLICATION_ERROR(-20001, 'Editing data on Saturday or Sunday
is not allowed.');
END IF;
END;
/
P a g e | 32

2. Write a PL/SQL program by creating the trigger while deleting the records from dept
table; it should update the old record to the dept_history table

 CREATE OR REPLACE TRIGGER dept_delete_trigger


BEFORE DELETE ON dept
FOR EACH ROW
BEGIN
INSERT INTO new_dept (deptno, dname, location)
VALUES (:OLD.deptno, :OLD.dname, :OLD.location);
END;
/

You might also like