DMS Pract Exam
DMS Pract Exam
vi) Display employee name starting with “S” and working in deptno 1002
SELECT ename
FROM EMP
WHERE ename LIKE 'S%' AND deptno = 1002;
iii) Display names of employees whose name starts with the alphabet ‘A’
SELECT ename
FROM EMP
WHERE ename LIKE 'A%';
iv) Increase the salary of employees by 20% who joined after ‘1/1/2005’
UPDATE EMP
SET salary = salary * 1.2
WHERE joiningdate > TO_DATE('01-JAN-2005', 'DD-MON-YYYY');
v) Display details of employees whose job is the same as the job of ‘Raj’
SELECT *
FROM EMP
WHERE job = (SELECT job FROM EMP WHERE ename = 'Raj');
ii) Display employee details from view emp_vu20 whose salary is greater than 50,000
SELECT *
FROM emp_vu20
WHERE salary > 50000;
iii) Delete view named as emp_vu20
DROP VIEW emp_vu20;
iv) Create view emp_dept that contains employee name, job, salary, and their department
name for employees whose job is Manager
CREATE VIEW emp_dept AS
SELECT e.ename, e.job, e.salary, d.dname
FROM EMP e
JOIN Dept d ON e.deptno = d.deptno
WHERE e.job = 'Manager';
ii) Display all books whose price is between 500 & 800
SELECT *
FROM Book_Master
WHERE price BETWEEN 500 AND 800;
iii) Display all books with details whose name starts with ‘D’
SELECT *
FROM Book_Master
WHERE book_name LIKE 'D%';
ii) Display book names from v1 having price greater than 200
SELECT book_name
FROM v1
WHERE price > 200;
ii) To display details for all employees whose salary is not in the range of Rs. 5000 and Rs.
10000
SELECT *
FROM EMP
WHERE salary NOT BETWEEN 5000 AND 10000;
iii) Change salary to 50000 for employees whose job is 'Manager' and dept_no is 10
UPDATE EMP
SET salary = 50000
WHERE job = 'Manager' AND deptno = 10;
iv) Display employee details whose name ends with ‘sh’ and name contains a maximum of
6 characters
SELECT *
FROM EMP
WHERE ename LIKE '%sh' AND LENGTH(ename) <= 6;
v) Display details of employees whose salary is greater than the minimum salary of each
department
SELECT *
FROM EMP e
WHERE salary > (SELECT MIN(salary)
FROM EMP
WHERE deptno = e.deptno);
ii) Assign CREATE, SELECT, and UPDATE privileges to user ‘Jack’ on the EMP table
GRANT CREATE, SELECT, UPDATE ON EMP TO Jack;
EXCEPTION
WHEN invalid_number THEN
DBMS_OUTPUT.PUT_LINE('Error: Invalid number, you entered an odd number.');
END;
/
ii) Display the details of employees who live in the cities Pune and Mumbai
SELECT *
FROM EMP
WHERE city IN ('Pune', 'Mumbai');
iii) Display the details of employees whose joining date is after ’01-Apr-2017’
SELECT *
FROM EMP
WHERE joiningdate > TO_DATE('01-APR-2017', 'DD-MON-YYYY');
ii) Display employee details from emp_vu20 whose salary is greater than 50,000
SELECT *
FROM emp_vu20
WHERE salary > 50000;
iii) Delete view named as emp_vu20
DROP VIEW emp_vu20;
iv) Create view emp_dept containing employee name, job, salary, and department name
for employees whose job is 'Manager'
CREATE VIEW emp_dept AS
SELECT e.ename, e.job, e.salary, d.dname
FROM EMP e
JOIN Department d ON e.deptno = d.deptno
WHERE e.job = 'Manager';
RETURN fact;
END;
/
-- Execute Function
DECLARE
input_num NUMBER := 5; -- Replace with desired input
result NUMBER;
BEGIN
result := calculate_factorial(input_num);
DBMS_OUTPUT.PUT_LINE('The factorial of ' || input_num || ' is: ' || result);
END;
/
-- Initial Insert
INSERT INTO EMP VALUES (1, 'John', 'Manager', 60000, '2010-05-10', '1985-03-25', 10);
COMMIT; -- Save the data
INSERT INTO EMP (empno, ename, job, salary, joiningdate, DOB, deptno)
VALUES (DB_SEQ.NEXTVAL, 'Leo', 'Developer', 48000, '2019-02-12', '1990-06-15', 20);
iv) List student names and marks from the ‘Computer’ department
SELECT name, marks
FROM Student
WHERE dept = 'Computer';
ii) Display records from S_View where marks are greater than 60
SELECT *
FROM S_View
WHERE marks > 60;
iii) Delete view named as S_View
DROP VIEW S_View;
iv) Display name of the employee who earned the highest salary
SELECT ename
FROM EMP
WHERE salary = (SELECT MAX(salary) FROM EMP);
UPDATE EMP
SET location = CASE
WHEN empno IN (1, 4) THEN 'Mumbai'
WHEN empno IN (2, 5) THEN 'Chennai'
ELSE 'Pune'
END;
SELECT *
FROM EMP
WHERE location IN ('Mumbai', 'Chennai');
emp_record manager_cursor%ROWTYPE;
BEGIN
OPEN manager_cursor;
LOOP
FETCH manager_cursor INTO emp_record;
EXIT WHEN manager_cursor%NOTFOUND;
CLOSE manager_cursor;
END;
/
v) Display the name of the employee who earned the highest salary
SELECT ename
FROM EMP
WHERE salary = (SELECT MAX(salary) FROM EMP);
Create Trigger
CREATE OR REPLACE TRIGGER trg_backup_emp
AFTER DELETE ON EMP
FOR EACH ROW
BEGIN
INSERT INTO backup_emp (empno, ename, job, salary, joiningdate, DOB, deptno)
VALUES
(:OLD.empno, :OLD.ename, :OLD.job, :OLD.salary, :OLD.joiningdate, :OLD.DOB, :OLD.deptno)
;
END;
/
ii) Grant create, select, insert, update, delete, drop privileges to ‘Rahul’
GRANT CREATE, SELECT, INSERT, UPDATE, DELETE, DROP TO Rahul;
v) Display details of employees whose salary is greater than 50000 and whose name
contains the pattern 'e'
SELECT *
FROM EMP
WHERE salary > 50000
AND ename LIKE '%e%';
ii) Insert records into the Candidate table using sequence DB_SEQ
-- Assuming the Candidate table has columns (candidate_id, name)
-- Example insert using sequence DB_SEQ
INSERT INTO Candidate (candidate_id, name)
VALUES (DB_SEQ.NEXTVAL, 'John Doe');
Q3: PL/SQL Program to Print Details of First Five Highest Salary Earning Employees
DECLARE
CURSOR emp_cursor IS
SELECT empno, ename, salary
FROM EMP
ORDER BY salary DESC;
v_empno EMP.empno%TYPE;
v_ename EMP.ename%TYPE;
v_salary EMP.salary%TYPE;
v_count INT := 0;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_empno, v_ename, v_salary;
EXIT WHEN emp_cursor%NOTFOUND;
v_count := v_count + 1;
CLOSE emp_cursor;
END;
/
iv) Display the name of the employee who earned the highest salary
SELECT ename
FROM EMP
WHERE salary = (SELECT MAX(salary) FROM EMP);
Q3: PL/SQL Program to Print the Greatest Number Between Three Numbers Using the
CASE Statement
DECLARE
num1 INT := 10;
num2 INT := 25;
num3 INT := 15;
greatest_num INT;
BEGIN
greatest_num := CASE
WHEN num1 >= num2 AND num1 >= num3 THEN num1
WHEN num2 >= num1 AND num2 >= num3 THEN num2
ELSE num3
END;