Files 3 2022 March NotesHubDocument 1647680510
Files 3 2022 March NotesHubDocument 1647680510
DECLARE
a number;
b number;
c number;
PROCEDURE findMin(x IN number, y IN number, z OUT number) IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
/
This procedure computes the square of value of a passed value. This example shows
how we can use the same parameter to accept a value and then return another result.
DECLARE
a number;
PROCEDURE squareNum(x IN OUT number) IS
BEGIN
x := x * x;
END;
BEGIN
a:= 23;
squareNum(a);
dbms_output.put_line(' Square of (23): ' || a);
END;
/
DECLARE
a number;
b number;
c number;
FUNCTION findMax(x IN number, y IN number)
RETURN number
IS
z number;
BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y;
END IF;
RETURN z;
END;
BEGIN
a:= 23;
b:= 45;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' || c);
END;
/
--3. Create a procedure to display the employee names, salary with salary>10000
CREATE OR REPLACE PROCEDURE getSalary
AS
BEGIN
FOR rec in (SELECT NAME,Salary,ADDRESS FROM CUSTOMER where salary >1000)
LOOP
dbms_output.put_line ('Employee Name: ' || rec.NAME ||', Salary is: ' ||
rec.SALARY);
END LOOP;
END getSalary;
/
EXECUTE getSalary;
--4. Create a function returning the total number of customers in the created
table.
CREATE OR REPLACE FUNCTION totalCustomers
RETURN number IS
total number(2) := 0;
BEGIN
SELECT count(*) into total
FROM customer;
RETURN total;
END;
/
DECLARE
c number(2);
BEGIN
c := totalCustomers();
dbms_output.put_line('Total no. of Customers: ' || c);
END;
/
/*Query 0: Retrieve the birthdate and address of the employee whose name is 'John
B. Smith'.*/
SELECT BDATE, ADDRESS FROM EMPLOYEE WHERE FNAME='John' AND MINIT='B' AND
LNAME='Smith';
/*Query 1: Retrieve the name and address of all employees who work for the
'Research' department.*/
SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DNAME='Research' AND
DNUMBER=DNO ;
SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE WHERE DNO IN (SELECT DNUMBER FROM
DEPARTMENT WHERE DNAME='Research');
SELECT FNAME, LNAME, ADDRESS FROM (EMPLOYEE JOIN DEPARTMENT ON DNUMBER=DNO) WHERE
DNAME='Research';
/*Query 2: For every project located in 'Stafford', list the project number, the
controlling department number, and the department manager's
last name, address, and birthdate.*/
SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION='Stafford';
SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROM (PROJECT JOIN DEPARTMENT ON
DNUM=DNUMBER) JOIN EMPLOYEE ON MGRSSN=SSN WHERE PLOCATION='Stafford';
/*Query 3: Retrieve the name of each employee who works on all the projects
controlled by department number 5.*/
SELECT FNAME, LNAME FROM EMPLOYEE WHERE (SELECT PNO FROM WORKS_ON WHERE
SSN=ESSN) CONTAINS (SELECT PNUMBER FROM PROJECT WHERE DNUM=5) ;
/*Query 4: Make a list of all project numbers for projects that involve an employee
whose last name is 'Smith'
as a worker or as a manager of the department that controls the project.*/
SELECT PNAME FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN
AND LNAME='Smith';
SELECT PNAME FROM PROJECT, WORKS_ON, EMPLOYEE WHERE PNUMBER=PNO AND ESSN=SSN AND
LNAME='Smith';
/*Query 8: For each employee, retrieve the employee's name, and the name of his or
her immediate supervisor.*/
SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME FROM EMPLOYEE E S WHERE
E.SUPERSSN=S.SSN;
SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME FROM (EMPLOYEE E LEFT OUTER JOIN
EMPLOYEES ON E.SUPERSSN=S.SSN);
/*Query 12: Retrieve the name of each employee who has a dependent with the same
first name as the employee.*/
SELECT E.FNAME, E.LNAME FROM EMPLOYEE E, DEPENDENT D WHERE E.SSN=D.ESSN AND
E.FNAME=D.DEPENDENT_NAME;
SELECT FNAME, LNAME FROM EMPLOYEE WHERE EXISTS (SELECT * FROM DEPENDENT WHERE
SSN=ESSN AND FNAME=DEPENDENT_NAME);
/*Query 13: Retrieve the social security numbers of all employees who work on
project number 1, 2, or 3.*/
SELECT DISTINCT ESSN FROM WORKS_ON WHERE PNO IN (1, 2, 3);
/*Query 14: Retrieve the names of all employees who do not have supervisors.*/
SELECT FNAME, LNAME FROM EMPLOYEE WHERE SUPERSSN IS NULL ;
/*Query 15: Find the maximum salary, the minimum salary, and the average salary
among all employees.*/
SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROM EMPLOYEE;
/*Query 16: Find the maximum salary, the minimum salary, and the average salary
among employees who work for the 'Research' department.*/
SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROM EMPLOYEE, DEPARTMENT WHERE
DNO=DNUMBER AND DNAME='Research';
/*Queries 17 and 18: Retrieve the total number of employees in the company (Q17),
and the number of employees in the 'Research' department (Q18).*/
SELECT COUNT (*) FROM EMPLOYEE;
SELECT COUNT (*) FROM EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER AND
DNAME='Research';
/*Query 20: For each department, retrieve the department number, the number of
employees in the department, and their average salary.*/
SELECT DNO, COUNT (*), AVG (SALARY) FROM EMPLOYEE GROUP BY DNO;
/*Query 21: For each project, retrieve the project number, project name, and the
number of employees who work on that project.*/
SELECT PNUMBER, PNAME, COUNT (*) FROM PROJECT, WORKS_ON WHERE PNUMBER=PNO GROUP BY
PNUMBER, PNAME;
/*Query 22: For each project on which more than two employees work , retrieve the
project number, project name, and the number
of employees who work on that project.*/
SELECT PNUMBER, PNAME, COUNT (*) FROM PROJECT, WORKS_ON WHERE PNUMBER=PNO GROUP
BY PNUMBER, PNAME HAVING COUNT (*) > 2;
/*Query 25: Retrieve all employees whose address is in Houston, Texas. Here, the
value of the ADDRESS attribute must contain the substring 'Houston,TX'.*/
SELECT FNAME, LNAME FROM EMPLOYEE WHERE ADDRESS LIKE '%Houston,TX%';
/*Query 26: Retrieve all employees who were born during the 1950s. Here, '5' must
be the 8th character of the string (according to our format for date),
so the BDATE value is '_______5_', with each underscore as a place holder for a
single arbitrary character.*/
SELECT FNAME, LNAME FROM EMPLOYEE WHERE BDATE LIKE '_______5_';
/*Query 27: Show the effect of giving all employees who work on the 'ProductX'
project a 10% raise.*/
SELECT FNAME, LNAME, 1.1*SALARY FROM EMPLOYEE, WORKS_ON, PROJECT WHERE SSN=ESSN
AND PNO=PNUMBER AND PNAME='ProductX';
/*Query 28: Retrieve a list of employees and the projects each works in, ordered by
the employee's department, and within each department ordered
alphabetically by employee last name.*/
SELECT DNAME, LNAME, FNAME, PNAME FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT
WHERE DNUMBER=DNO AND SSN=ESSN AND PNO=PNUMBER ORDER BY DNAME, LNAME;