DMS Practical Questions (1)
DMS Practical Questions (1)
b. Write the SQL commands to create the EMP table with the following structure:
• hiredate as a date
Ans:
);
c.How would you alter the EMP table to assign the empno column as the primary
key.
d. Write the SQL commands to create the DEPT table with the following structure:
Ans:
);
Practical List
a. Create table for stud using attributes Rollno, Studname, Percentage. Apply primary key for rollno and
check constraint on percentage that the percentage should not be greater than 100.
Ans:
percentage NUMBER(5, 2) CHECK (percentage <= 100) -- Percentage with max value 100
);
Ans: ALTER TABLE stud ADD city VARCHAR2(50); -- Adding City column
Ans: ALTER TABLE stud MODIFY studname VARCHAR2(60); -- Increasing size from 50 to 60
Ans: INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
a. Write TCL command to save all the changes made so far in the EMP.
Ans: Commit;
b. Delete any one record in the EMP table created earlier and undo the deletion operation.
ROLLBACK;
a. Display new column named total_amount which is 200 added to the amount field.
Ans: SELECT cust_id, order_id, items, amount, (amount + 200) AS total_amount FROM Orders;
b. Display new column named offer_price which is 100 subtracted from the amount field.
Ans: SELECT cust_id, order_id, items, amount, (amount - 100) AS offer_price FROM Orders;
c. Display new column named revised_amount which is multiplied by 5 times the amount.
Ans: SELECT cust_id, order_id, items, amount, (amount * 5) AS revised_amount FROM Orders
Practical List
d. Display new column named half_amount which is divided by 2 to the amount field.
Ans: SELECT cust_id, order_id, items, amount, (amount * 5) AS revised_amount FROM Orders;
a. Display employees whose city is ‘Mumbai’ and earns more than 50000.
Ans: SELECT * FROM Emp WHERE city = 'Mumbai' AND sal > 50000;
Ans: SELECT * FROM Emp WHERE sal BETWEEN 20000 AND 50000;
Ans: SELECT * FROM Emp WHERE city IN ('Mumbai', 'Pune', 'Nashik', 'Nagpur');
emp1(empno,ename,deptno)
emp2(empno,ename,deptno)
Ans: SELECT ename FROM emp1 UNION ALL SELECT ename FROM emp2;
Ans: SELECT ename FROM emp1 UNION SELECT ename FROM emp2;
Ans: SELECT ename FROM emp1 INTERSECT SELECT ename FROM emp2;
Practical List
Ans: SELECT ename FROM emp1 WHERE deptno IS NULL UNION SELECT ename FROM emp2
a. Display the minimum, maximum, sum and average salary of all employees. Label the columns
Maximum, Minimum, Sum and Average respectively.
Ans : SELECT MIN(sal) AS Minimum, MAX(sal) AS Maximum, SUM(sal) AS Sum, AVG(sal) AS Average
FROM Emp;
c. Determine the number of managers without listing them. Label the column number of managers.
Ans: SELECT COUNT(DISTINCT mgr) AS "Number of Managers" FROM Emp WHERE mgr IS NOT NULL;
d. Write a query that will display the difference between the highest and lowest salaries. Label the
column DIFFERENCE.
Ans: SELECT deptno FROM Emp GROUP BY deptno HAVING COUNT(empno) > 5;
a. Display employee Nikhil’s employee number, name, department number, and department location.
Ans: SELECT e.empno, e.ename, e.deptno, d.location FROM Emp e JOIN Dept d ON e.deptno =
d.deptno WHERE e.ename = 'Nikhil';
Ans:
SELECT e.empno, e.ename, e.deptno
FROM Emp e
JOIN Dept d ON e.deptno = d.deptno
WHERE d.deptname = 'Sales';
c. Display the list of employees who do not work in the sales department.
Ans: SELECT e.empno, e.ename, e.deptno
FROM Emp e
JOIN Dept d ON e.deptno = d.deptno
WHERE d.deptname != 'Sales';
d. Display the employee names and salary of all employees who report to Sumit Patil.
FROM Emp e WHERE e.mgr = (SELECT empno FROM Emp WHERE ename = 'Sumit Patil');
Ans:
CREATE VIEW emp_view AS SELECT emp_no, ename, salary FROM emp;
Ans: UPDATE dept_view SET location = 'New Location' WHERE dept_no = 10;
Q.13 Write a PL/SQL program that asks the user for percentage and then assigns grades based on the
following conditions:
Distinction (>=75%)
Fail (<40).
Practical List
Ans:
DECLARE
percentage NUMBER;
grade VARCHAR2(20);
BEGIN
percentage NUMBER;
grade := 'Distinction';
ELSE
grade := 'Fail';
END IF;
END;
Practical List
Q.14 Write a PL/SQL program that asks the user for their age and then prints “You can vote" if they
Ans:
DECLARE
age NUMBER;
BEGIN
age NUMBER;
ELSE
Q.15 Write a PL/SQL program to display multiplication table of 5 using FOR loop.
Ans:
BEGIN
A END LOOP;
END;
Practical List
Q.16 Write a PL/SQL program to calculate factorial of 10 by using PL/SQL WHILE LOOP statement.
Ans:
DECLARE
num NUMBER := 10;
factorial NUMBER := 1;
i NUMBER := 1;
BEGIN
WHILE i <= num LOOP
factorial := factorial * i;
i := i + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Factorial of ' || num || ' is: ' || factorial);
END;
Q.17 Write a PL/SQL program that asks the user to input a number (1 for Monday, 2 for Tuesday, ...,
7 for Sunday) and prints the corresponding day of the week.
Ans: DECLARE
day_number NUMBER;
day_name VARCHAR2(20);
BEGIN
day_number NUMBER;
CASE day_number
WHEN 1 THEN
day_name := 'Monday';
WHEN 2 THEN
day_name := 'Tuesday';
WHEN 3 THEN
Practical List
day_name := 'Wednesday';
WHEN 4 THEN
day_name := 'Thursday';
WHEN 5 THEN
day_name := 'Friday';
WHEN 6 THEN
day_name := 'Saturday';
WHEN 7 THEN
day_name := 'Sunday';
ELSE
END CASE;
END;
Practical List
Q.18 Write a PL/SQL program for displaying details of students studying in computer department
using cursors.
Ans:
DECLARE
CURSOR student_cursor IS
FROM students
student_record student_cursor%ROWTYPE;
BEGIN
OPEN student_cursor;
LOOP
END LOOP;
CLOSE student_cursor;
END;
Practical List
Q.19 Write a PL/SQL program that asks the user to input two numbers and divide the first number by
the second. Handle the predefined exception for division by zero and display an appropriate message
if it occurs.
Ans:
DECLARE
num1 NUMBER;
num2 NUMBER;
result NUMBER;
BEGIN
num1 NUMBER:=:num1;
num2 NUMBER:=:num2;
BEGIN
DBMS_OUTPUT.PUT_LINE;
EXCEPTION
DBMS_OUTPUT.PUT_LINE(' ');
END;
Practical List
Q.20 Write a PL/SQL program that retrieves the salary of an employee based on their employee ID
(emp_id). If the employee ID does not exist in the database, handle the NO_DATA_FOUND exception
and print a message saying, "Employee ID not found.
Ans:
DECLARE
emp_id NUMBER;
emp_salary NUMBER;
BEGIN
emp_id NUMBER;
BEGIN
FROM employees
EXCEPTION
END;
END;
Practical List
Q.21 Write a PL/SQL program that asks for customer Id, when user enters invalid Id, the exception
Invalid-Id is raised.
Ans:
DECLARE
customer_id NUMBER;
Invalid_Id EXCEPTION;
BEGIN
ELSE
END IF;
EXCEPTION
END;
Practical List
Q.22 Create a stored procedure to accept name and greet user with name.
Ans:Ans:
BEGIN
END greet_user;
Q.23 Write PL/SQL function which will compute and return the maximum of two values.
Ans:
RETURN NUMBER IS
BEGIN
RETURN val1;
ELSE
RETURN val2;
END IF;
END get_max;
DECLARE
max_value NUMBER;
BEGIN
END;
Practical List
Ans:
fact NUMBER := 1;
BEGIN
IF n < 0 THEN
RETURN -1;
ELSE
fact := fact * i;
END LOOP;
END IF;
RETURN fact;
END calculate_factorial;