0% found this document useful (0 votes)
1 views18 pages

Asg12 2

The document outlines SQL queries and PL/SQL programs for a Company Database schema, including tables for EMPLOYEE, DEPARTMENT, DLOCATION, PROJECT, and WORKS_ON. It provides specific SQL tasks such as listing project numbers involving employees named 'Scott', calculating salary raises, and retrieving employee statistics. Additionally, it includes PL/SQL procedures for calculating the GCD of two numbers and fetching employees with salaries below a specified limit.

Uploaded by

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

Asg12 2

The document outlines SQL queries and PL/SQL programs for a Company Database schema, including tables for EMPLOYEE, DEPARTMENT, DLOCATION, PROJECT, and WORKS_ON. It provides specific SQL tasks such as listing project numbers involving employees named 'Scott', calculating salary raises, and retrieving employee statistics. Additionally, it includes PL/SQL procedures for calculating the GCD of two numbers and fetching employees with salaries below a specified limit.

Uploaded by

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

Assingment – 12.

Query
A. Consider the schema for Company Database:

EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)

DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)

DLOCATION (DNo,DLoc)

PROJECT (PNo, PName, PLocation, DNo)

WORKS_ON (SSN, PNo, Hours)

Write SQL queries to

1. Make a list of all project numbers for projects that involve an employee
whose last name is ‘Scott’, either as a worker or as a manager of the
department that controls the project.
2. Show the resulting salaries if every employee working on the ‘IoT’ project
is given a 10 percent raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’
department, as well as the maximum salary, the minimum salary, and the
average salary in this department
4. Retrieve the name of each employee who works on all the projects
controlled by department number 5 (use NOT EXISTS operator).
5. For each department that has more than five employees, retrieve the
department number and the number of its employees who are making
more than Rs. 6,00,000.

B. Write a program in PL/SQL to create a procedure to displays the GCD of nos.


C. Write a program in PL/SQL to create a cursor displays the name and salary of each
employee in the EMPLOYEES table whose salary is less than that specified by a passed-
in parameter value.

Answer
Consider the schema for Company Database:

EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)

CREATE TABLE EMPLOYEE (


SSN VARCHAR2(10) PRIMARY KEY,

Name VARCHAR2(50),

Address VARCHAR2(100),

Sex CHAR(1),

Salary NUMBER(10, 2),

SuperSSN VARCHAR2(10),

DNo NUMBER

);

desc EMPLOYEE

INSERT INTO EMPLOYEE VALUES ('111', 'John Scott', 'Delhi', 'M', 900000, NULL, 1);

INSERT INTO EMPLOYEE VALUES ('112', 'Sara Khan', 'Mumbai', 'F', 850000, '111', 2);

INSERT INTO EMPLOYEE VALUES ('113', 'Mike Smith', 'Pune', 'M', 600000, '112', 3);

INSERT INTO EMPLOYEE VALUES ('114', 'Rita Sharma', 'Chennai', 'F', 750000, '113', 3);
INSERT INTO EMPLOYEE VALUES ('115', 'Scott Wilson', 'Bangalore', 'M', 950000, '111', 5);

INSERT INTO EMPLOYEE VALUES ('116', 'Aman Verma', 'Kolkata', 'M', 500000, '114', 5);

INSERT INTO EMPLOYEE VALUES ('117', 'Sneha Roy', 'Hyderabad', 'F', 700000, '115', 5);

select * from EMPLOYEE;

DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)

CREATE TABLE DEPARTMENT (

DNo NUMBER PRIMARY KEY,

DName VARCHAR2(50),
MgrSSN VARCHAR2(10),

MgrStartDate DATE

);

desc DEPARTMENT

INSERT INTO DEPARTMENT VALUES (1, 'HR', '111', TO_DATE('2019-03-01', 'YYYY-


MM-DD'));

INSERT INTO DEPARTMENT VALUES (2, 'IT', '112', TO_DATE('2018-01-15', 'YYYY-MM-


DD'));

INSERT INTO DEPARTMENT VALUES (3, 'Accounts', '113', TO_DATE('2020-06-10',


'YYYY-MM-DD'));

INSERT INTO DEPARTMENT VALUES (5, 'Research', '115', TO_DATE('2017-07-20',


'YYYY-MM-DD'));
select * from DEPARTMENT;

DLOCATION (DNo,DLoc)

CREATE TABLE DLOCATION (

DNo NUMBER,

DLoc VARCHAR2(50),

PRIMARY KEY (DNo, DLoc)

);

desc DLOCATION
INSERT INTO DLOCATION VALUES (1, 'Delhi');

INSERT INTO DLOCATION VALUES (2, 'Mumbai');

INSERT INTO DLOCATION VALUES (3, 'Pune');

INSERT INTO DLOCATION VALUES (5, 'Hyderabad');

select * from DLOCATION;

PROJECT (PNo, PName, PLocation, DNo)

CREATE TABLE PROJECT (

PNo NUMBER PRIMARY KEY,

PName VARCHAR2(50),

PLocation VARCHAR2(50),
DNo NUMBER

);

desc PROJECT

INSERT INTO PROJECT VALUES (101, 'AI', 'Delhi', 5);

INSERT INTO PROJECT VALUES (102, 'IoT', 'Mumbai', 2);

INSERT INTO PROJECT VALUES (103, 'Banking', 'Pune', 3);

INSERT INTO PROJECT VALUES (104, 'Healthcare', 'Chennai', 1);

select * from PROJECT;


WORKS_ON (SSN, PNo, Hours)

CREATE TABLE WORKS_ON (

SSN VARCHAR2(10),
PNo NUMBER,

Hours NUMBER(5,2),

PRIMARY KEY (SSN, PNo)

);

desc WORKS_ON

INSERT INTO WORKS_ON VALUES ('111', 101, 20);


INSERT INTO WORKS_ON VALUES ('112', 102, 25);
INSERT INTO WORKS_ON VALUES ('113', 103, 15);
INSERT INTO WORKS_ON VALUES ('114', 104, 30);
INSERT INTO WORKS_ON VALUES ('115', 101, 22);
INSERT INTO WORKS_ON VALUES ('116', 101, 18);
INSERT INTO WORKS_ON VALUES ('117', 101, 20);
INSERT INTO WORKS_ON VALUES ('112', 101, 12);
INSERT INTO WORKS_ON VALUES ('113', 102, 14);
select * from WORKS_ON;
Write SQL queries to

1. Make a list of all project numbers for projects that involve an


employee whose last name is ‘Scott’, either as a worker or as a
manager of the department that controls the project.

SELECT DISTINCT P.PNo

FROM PROJECT P

WHERE P.DNo IN (

SELECT D.DNo

FROM DEPARTMENT D

JOIN EMPLOYEE E ON D.MgrSSN = E.SSN

WHERE E.Name LIKE '%Scott%'

);
2. Show the resulting salaries if every employee working on the ‘IoT’
project is given a 10 percent raise.

SELECT E.Name, E.Salary AS Old_Salary,

E.Salary * 1.10 AS New_Salary

FROM EMPLOYEE E

JOIN WORKS_ON W ON E.SSN = W.SSN

JOIN PROJECT P ON W.PNo = P.PNo

WHERE P.PName = 'IoT';

3. Find the sum of the salaries of all employees of the ‘Accounts’


department, as well as the maximum salary, the minimum salary,
and the average salary in this department.

SELECT

SUM(E.Salary) AS Total_Salary,

MAX(E.Salary) AS Max_Salary,

MIN(E.Salary) AS Min_Salary,

AVG(E.Salary) AS Avg_Salary

FROM EMPLOYEE E

JOIN DEPARTMENT D ON E.DNo = D.DNo

WHERE D.DName = 'Accounts';


4. Retrieve the name of each employee who works on all the projects
controlled by department number 5 (use NOT EXISTS operator).

SELECT E.Name

FROM EMPLOYEE E

WHERE NOT EXISTS (

SELECT P.PNo

FROM PROJECT P

WHERE P.DNo = 5

MINUS

SELECT W.PNo

FROM WORKS_ON W

WHERE W.SSN = E.SSN

);
5. For each department that has more than five employees, retrieve the
department number and the number of its employees who are making
more than Rs. 6,00,000.

SELECT E.DNo, COUNT(*) AS High_Earners

FROM EMPLOYEE E

WHERE E.Salary > 600000

GROUP BY E.DNo

HAVING COUNT(*) > 5;

Write a program in PL/SQL to create a procedure to displays the GCD of nos.


CREATE OR REPLACE PROCEDURE find_gcd(

num1 IN NUMBER,

num2 IN NUMBER

) IS

a NUMBER := num1;

b NUMBER := num2;

temp NUMBER;

BEGIN

-- Euclidean algorithm to find GCD

WHILE b != 0 LOOP

temp := b;

b := MOD(a, b);

a := temp;

END LOOP;

DBMS_OUTPUT.PUT_LINE('The GCD of ' || num1 || ' and ' || num2 || ' is: ' || a);

END;

/
SET SERVEROUTPUT ON;

BEGIN

find_gcd(36, 60);

END;

/
Write a program in PL/SQL to create a cursor displays the name and salary of each
employee in the EMPLOYEES table whose salary is less than that specified by a passed-in
parameter value.

CREATE OR REPLACE PROCEDURE get_low_salary_employees(p_max_salary IN


NUMBER) IS

v_name EMPLOYEE.Name%TYPE;
v_salary EMPLOYEE.Salary%TYPE;

CURSOR emp_cursor(p_salary_limit NUMBER) IS


SELECT Name, Salary
FROM EMPLOYEE
WHERE Salary < p_salary_limit;

BEGIN

OPEN emp_cursor(p_max_salary);

LOOP

FETCH emp_cursor INTO v_name, v_salary;


EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name || ', Salary: ' || v_salary);

END LOOP;
CLOSE emp_cursor;
END;
/
BEGIN
get_low_salary_employees(60000);
END;
/

You might also like