0% found this document useful (0 votes)
9 views21 pages

Company Database Lab Work

Uploaded by

chaithrar2604
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)
9 views21 pages

Company Database Lab Work

Uploaded by

chaithrar2604
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/ 21

JNNCE ISE-Department

Assignment-I
Subject/Subject Code :DBMS Lab/BCS403
Semester/Section :4th A
Student Name :
USN :

Company Database
ER-Daigram

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

Schema Diagram

Referential Integrity

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

Database State

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

SQL Commands
/*Creation of EMPLOYEE Table*/

CREATE TABLE EMPLOYEE

( Fname varchar (15) NOT NULL,

Minit varchar(1),

Lname varchar(15) NOT NULL,

Ssn varchar(9) NOT NULL,

Bdate date,

Address varchar(30),

Sex varchar(1),

Salary float,

Super_ssn varchar(9),

Dno int NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Super_ssn) REFERENCES
EMPLOYEE(Ssn)

);

desc EMPLOYEE

/*Creation of DEPATMENT TABLE*/

CREATE TABLE DEPARTMENT

Dname varchar(15) NOT NULL,

Dnumber int NOT NULL,

Mgr_ssn varchar(9) NOT NULL,

Mgr_start_date date,

PRIMARY KEY (Dnumber),

UNIQUE(Dname),

FOREIGN KEY (Mgr_ssn) REFERENCES EMPLOYEE(Ssn)

);

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

desc DEPARTMENT

/*SQL STATEMENT TO CREATE DEPT_LOCATIONS TABLE*/

CREATE TABLE DEPT_LOCATIONS

Dnumber int NOT NULL,

Dlocation varchar(15),

PRIMARY KEY (Dnumber, Dlocation),

FOREIGN KEY (Dnumber) REFERENCES DEPARTMENT (Dnumber)

);

desc DEPT_LOCATIONS

/*SQL STATEMENT TO CREATE PROJECT TABLE*/

CREATE TABLE PROJECT

Pname varchar(15) NOT NULL,

Pnumber int NOT NULL,

Plocation varchar(15),

Dnum int NOT NULL,

PRIMARY KEY (Pnumber),

UNIQUE (Pname),

FOREIGN KEY (Dnum) REFERENCES DEPARTMENT (Dnumber)

);

desc DEPT_LOCATIONS

CREATE TABLE PROJECT

Pname varchar(15) NOT NULL,

Pnumber int NOT NULL,

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

Plocation varchar(15),

Dnum int NOT NULL,

PRIMARY KEY (Pnumber),

UNIQUE (Pname),

FOREIGN KEY (Dnum) REFERENCES DEPARTMENT (Dnumber)

);

desc PROJECT

/*SQL STATEMENT TO CREATE WORKS_ON TABLE*/

CREATE TABLE WORKS_ON

Essn varchar(9) NOT NULL,

Pno int NOT NULL,

Hours float NOT NULL,

PRIMARY KEY (Essn, Pno),

FOREIGN KEY (Essn) REFERENCES EMPLOYEE(Ssn),

FOREIGN KEY (Pno) REFERENCES PROJECT(Pnumber)

);

desc WORKS_ON

/*SQL STATEMENT TO CREATE DEPENDENT TABLE*/

CREATE TABLE DEPENDENT

Essn varchar(9) NOT NULL,

Dependent_name varchar(15) NOT NULL,

Sex varchar(1),

Bdate date,

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

Relationship varchar(8), PRIMARY KEY (Essn, Dependent_name),

FOREIGN KEY (Essn) REFERENCES EMPLOYEE(Ssn)

);

desc DEPENDENT

/*SQL STATEMENT FOR INSERTING VALUES INTO EMPLOYEE TABLE*/

INSERT INTO EMPLOYEE values ('James', 'E', 'Borg', 888665555, '10-NOV-1937', '430 Stone,
Houston, TX', 'M', 55000, NULL, 1);

INSERT INTO EMPLOYEE values ('Jennifer', 'S', 'Wallace', 987654321, '20-JUN-1941', '291 Berry,
Bellaire, TX', 'F',43000, 888665555, 4);

INSERT INTO EMPLOYEE values ('Franklin', 'B', 'Wong', 333445555, '08-DEC-1955', '638 Voss,
Houston, TX', 'M', 40000, 888665555, 5);

INSERT INTO EMPLOYEE values ('Alicia', 'J', 'Zelaya', 999887777, '19-JAN-1968', '3321 Castle,
Spring, TX', 'F', 25000, 987654321, 4);

INSERT INTO EMPLOYEE values ('Ramesh', 'K', 'Narayan', 666884444, '15-SEP-1962', '975 Fire Oak,
Humble, TX', 'M', 38000, 333445555, 5);

INSERT INTO EMPLOYEE values ('Joyce', 'A', 'English', 453453453, '31-JUL-1972', '5631 Rice,
Houston, TX', 'F', 25000, 333445555, 5);

INSERT INTO EMPLOYEE values ('Ahmad', 'V', 'Jabbar', 987987987, '29-MAR-1969', '980 Dallas,
Houston, TX', 'M', 25000, 987654321, 4);

INSERT INTO EMPLOYEE values ('John', 'B', 'Smith', 123456789, '09-JAN-1965', '731 Fondren,
Houston, TX', 'M', 30000, 333445555, 5);

select * from EMPLOYEE

/*SQL STATEMENT FOR INSERTING VALUES INTO DEPARTMENT TABLE*/

INSERT INTO DEPARTMENT values ('Research', 5, 333445555, '22-MAY-1988');

INSERT INTO DEPARTMENT values ('Administration', 4, 987654321, '01-JAN-1995');

INSERT INTO DEPARTMENT values ('Headquaters', 1, 888665555, '19-JUN-1981');

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

select * from DEPARTMENT

/*SQL STATEMENT FOR ALTERING EMPLOYEE TABLE TO ADD FOREIGN KEY


CONSTRAINT*/
ALTER TABLE EMPLOYEE
ADD CONSTRAINT FK_Manages FOREIGN KEY (Dno) REFERENCES DEPARTMENT (Dnumber);

DESC EMPLOYEE
DESC DEPARTMENT

/* SQL STATEMENT FOR INSERTING VALUES INTO DEPT_LOCATIONS TABLE */

INSERT INTO DEPT_LOCATIONS (Dnumber, Dlocation) values (1, 'Houston');


INSERT INTO DEPT_LOCATIONS (Dnumber, Dlocation) values (4, 'Stafford');

INSERT INTO DEPT_LOCATIONS (Dnumber, Dlocation) values (5, 'Bellaire');


INSERT INTO DEPT_LOCATIONS (Dnumber, Dlocation) values (5, 'Sugarland');
INSERT INTO DEPT_LOCATIONS (Dnumber, Dlocation) values (4, 'Houston');

select * from DEPT_LOCATIONS

/*SQL STATEMENT FOR INSERTING VALUES IN TO PROJECT TABLE*/

INSERT INTO PROJECT (Pname, Pnumber, Plocation, Dnum) values ('ProductX', 1, 'Bellaire', 5);
INSERT INTO PROJECT (Pname, Pnumber, Plocation, Dnum) values ('ProductY', 2, 'Sugarland', 5);
INSERT INTO PROJECT (Pname, Pnumber, Plocation, Dnum) values ('ProductZ', 3, 'Houston', 5);
INSERT INTO PROJECT (Pname, Pnumber, Plocation, Dnum) values ('Computerization', 10, 'Stafford',
4);
INSERT INTO PROJECT (Pname, Pnumber, Plocation, Dnum) values ('Reorganization', 20, 'Houston',
1);
INSERT INTO PROJECt (Pname, Pnumber, Plocation, Dnum) values ('Newbenfits', 30, 'Stafford', 4);

select * from PROJECT

/*SQL STATEMENT FOR INSERTING VALUES INTO WORKS_ON TABLE*/

desc WORKS_ON

INSERT INTO WORKS_ON (Essn, Pno, Hours) values (123456789, 1, 32.5);


INSERT INTO WORKS_ON (Essn, Pno, Hours) values (123456789, 2, 7.5);
INSERT INTO WORKS_ON (Essn, Pno, Hours) values (666884444, 3, 40.0);
INSERT INTO WORKS_ON (Essn, Pno, Hours) values (453453453, 1, 20.0);

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

INSERT INTO WORKS_ON (Essn, Pno, Hours) values (453453453, 2, 20.0);


INSERT INTO WORKS_ON (Essn, Pno, Hours) values (333445555, 2, 10.0);
INSERT INTO WORKS_ON (Essn, Pno, Hours) values (333445555, 3, 10.0);
INSERT INTO WORKS_ON (Essn, Pno, Hours) values (333445555, 10, 10.0);
INSERT INTO WORKS_ON (Essn, Pno, Hours) values (333445555, 20, 10.0);
INSERT INTO WORKS_ON (Essn, Pno, Hours) values (999887777, 30, 30.0);
INSERT INTO WORKS_ON (Essn, Pno, Hours) values (999887777, 10, 10.0);
INSERT INTO WORKS_ON (Essn, Pno, Hours) values (987987987, 10, 35.0);
INSERT INTO WORKS_ON (Essn, Pno, Hours) values (987987987, 30, 5.0);
INSERT INTO WORKS_ON (Essn, Pno, Hours) values (987654321, 30, 20.0);
INSERT INTO WORKS_ON (Essn, Pno, Hours) values (987654321, 20, 15.0);
INSERT INTO WORKS_ON (Essn, Pno, Hours) values (888665555, 20, 0.0);

select * from WORKS_ON

/*SQL STATEMENT FOR INSERTING VALUES INTO DEPENDENT TABLE*/


INSERT INTO DEPENDENT (Essn, Dependent_name, Sex, Bdate, Relationship) values (333445555,
'Alice', 'F', '05-APR-1986', 'Daughter');
INSERT INTO DEPENDENT (Essn, Dependent_name, Sex, Bdate, Relationship) values (333445555,
'Theodore', 'M', '25-OCT-1983', 'Son');
INSERT INTO DEPENDENT (Essn, Dependent_name, Sex, Bdate, Relationship) values (333445555,
'Joy', 'F', '03-MAY-1958', 'Spouse');
INSERT INTO DEPENDENT (Essn, Dependent_name, Sex, Bdate, Relationship) values (987654321,
'Abner', 'M', '28-FEB-1942', 'Spouse');
INSERT INTO DEPENDENT (Essn, Dependent_name, Sex, Bdate, Relationship) values (123456789,
'Michael', 'M', '04-JAN-1988', 'Son');
INSERT INTO DEPENDENT (Essn, Dependent_name, Sex, Bdate, Relationship) values (123456789,
'Alice', 'F', '30-DEC-1988', 'Daughter');
INSERT INTO DEPENDENT (Essn, Dependent_name, Sex, Bdate, Relationship) values (123456789,
'Elizabeth', 'F', '05-MAY-1967', 'Spouse');

/*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;

/*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 birth date.*/

SELECT Pnumber, Dnum, Lname, Address, Bdate


FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND Plocation = 'Stafford'

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

/*Queries 9 and 10. Select all EMPLOYEE Ssns (Q9) and all combinations of EMPLOYEE Ssn and
DEPARTMENT Dname (Q10) in the database.*/

SELECT Ssn FROM EMPLOYEE

SELECT E.Ssn, D.Dname


FROM EMPLOYEE as E, DEPARTMENT as D

where E.Dno=D.Dnumber;

select * from EMPLOYEE


select * from DEPARTMENT
/*Query Q1C retrieves all the attribute values of any EMPLOYEE who works in DEPARTMENT
number 5 */

SELECT * FROM EMPLOYEE WHERE Dno =5;

/* query Q1D retrieves all the attributes of an EMPLOYEE and the attributes of the DEPARTMENT in
which he or she works for every employee of the ‘Research’ department, */

SELECT * FROM EMPLOYEE, DEPARTMENT


WHERE Dname = 'Research' AND Dno = Dnumber;

/*Q10A specifies the CROSS PRODUCT of the EMPLOYEE and DEPARTMENT relations.*/

SELECT * FROM EMPLOYEE, DEPARTMENT

/* Query 11. Retrieve the salary of every employee (Q11) and all distinct salary values (Q11A). */

SELECT ALL Salary FROM EMPLOYEE;

SELECT DISTINCT Salary FROM EMPLOYEE;


/*Query 4. Make a list of all project numbers for projects that involve an employee whose last name is
‘Smith’, either as a worker or as a manager of the department that controls the project*/

SELECT DISTINCT Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE


WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND Lname = 'Smith'
UNION
SELECT DISTINCT Pnumber FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE Pnumber = Pno AND Essn = Ssn AND Lname = 'Smith' ;

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

/*Query 12. Retrieve all employees whose address is in Houston, Texas.*/


SELECT Fname, Lname FROM EMPLOYEE
WHERE Address LIKE '%Houston,TX%';

/*Query 12A. Find all employees who were born during the 1950s.*/

SELECT Fname, Lname FROM EMPLOYEE WHERE Bdate LIKE '10-NOV-37';

SELECT E.Fname AS First_name, E.Lname AS Last_name, E.Salary AS Salary


FROM EMPLOYEE E
WHERE E.Dno=5;

select * from EMPLOYEE

/*Query 13. Show the resulting salaries if every employee working on the ‘ProductX’ project is given a
10% raise.*/
SELECT E.Fname, E.Lname, 1.1 * E.Salary AS Increased_sal
FROM EMPLOYEE E, WORKS_ON W, PROJECT P
WHERE E.Ssn = W.Essn AND W.Pno = P.Pnumber AND P.Pname = 'ProductX';

/*Query 14. Retrieve all employees in department 5 whose salary is between $30,000 and $40,000. */
SELECT * FROM EMPLOYEE
WHERE (Salary BETWEEN 30000 AND 40000) AND Dno = 5;

/*Query 15. Retrieve a list of employees and the projects they are working on, ordered by department and,
within each department, ordered alphabetically by last name, then first name.

SELECT D.Dname, E.Lname, E.Fname, P.Pname


FROM DEPARTMENT D, EMPLOYEE E, WORKS_ON W, PROJECT P
WHERE D.Dnumber = E.Dno AND E.Ssn = W.Essn AND W.Pno = P.Pnumber
ORDER BY D.Dname, E.Lname, E.Fname;

SELECT D.Dname, E.Lname, E.Fname, P.Pname


FROM DEPARTMENT D, EMPLOYEE E, WORKS_ON W, PROJECT P
WHERE D.Dnumber = E.Dno AND E.Ssn = W.Essn AND W.Pno = P.Pnumber
ORDER BY D.Dname DESC, E.Lname ASC, E.Fname ASC

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

/* INSERT, DELETE, and UPDATE Statements in SQL*/


INSERT INTO EMPLOYEE VALUES ( 'Richard', 'K', 'Marini', '653298653', '10-DEC-78', '98 Oak
Forest, Katy, TX', 'M', 37000, '653298653', 4 );

select * From EMPLOYEE

INSERT INTO EMPLOYEE (Fname, Lname, Dno, Ssn) VALUES ('Richard', 'Marini', 4, '653298653');

/*integrity constraint (DB.FK_MANAGES) violated - parent key not found*/

INSERT INTO EMPLOYEE (Fname, Lname, Ssn, Dno) VALUES ('Robert', 'Hatcher', '980760540', 2);

/*cannot insert NULL into ("DB"."EMPLOYEE"."SSN")( U2A is rejected if NOT NULL checking is
provided by DBMS.)*/

INSERT INTO EMPLOYEE (Fname, Lname, Dno) VALUES ('Robert', 'Hatcher', 5);

* To create a temporary table that has the employee last name, project name, and hours per week for each
employee working on a project*/
CREATE TABLE WORKS_ON_INFO
( Emp_name VARCHAR(15),
Proj_name VARCHAR(15),
Hours_per_week DECIMAL(3,1)
);

desc WORKS_ON_INFO

INSERT INTO WORKS_ON_INFO ( Emp_name, Proj_name, Hours_per_week )


SELECT E.Lname, P.Pname, W.Hours FROM PROJECT P, WORKS_ON W, EMPLOYEE E
WHERE P.Pnumber = W.Pno AND W.Essn = E.Ssn;

select * from WORKS_ON_INFO

/*To create a table D5EMPS with a similar structure to the EMPLOYEE table and load it with the rows of
employees who work in department 5, we can write the following SQL: */
CREATE TABLE D5EMPS LIKE EMPLOYEE
SELECT E.* FROM EMPLOYEE E
WHERE E.Dno = 5;

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

/*The DELETE Command*/


DELETE FROM EMPLOYEE WHERE Lname = 'Brown';
DELETE FROM EMPLOYEE WHERE Ssn = '123456789';
DELETE FROM EMPLOYEE WHERE Dno = 5;
DELETE FROM EMPLOYEE

/* The UPDATE Command */


SET Plocation = 'Bellaire', Dnum = 5 WHERE Pnumber = 10

UPDATE EMPLOYEE SET Salary = Salary * 1.1 WHERE Dno =5;

/*More Complex SQL Retrieval Queries*/

/*Query 18. Retrieve the names of all employees who do not have supervisors.*/
SELECT Fname, Lname
FROM EMPLOYEE
WHERE Super_ssn IS NULL;

/*Query 4A. Make a list of all project numbers for projects that involve an employee whose last name is
‘Smith’, either as a worker or as a manager of the department that controls the project.*/

SELECT DISTINCT Pnumber


FROM PROJECT
WHERE Pnumber
IN
( SELECT Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND Lname = 'Smith' )
OR
Pnumber IN
( SELECT Pno
FROM WORKS_ON, EMPLOYEE
WHERE Essn = Ssn AND Lname = 'Smith' );

/*SQL allows the use of tuples of values in comparisons by placing them within parentheses. To illustrate
this, consider the following query: */
SELECT DISTINCT Essn
FROM WORKS_ON
WHERE (Pno, Hours)
IN
( SELECT Pno, Hours
FROM WORKS_ON
WHERE Essn = '123456789' );

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

/*An example is the following query, which returns the names of employees whose salary is greater than
the salary of all the employees in department 5:*/

/*An example is the following query, which returns the names of employees whose salary is greater than
the salary of all the employees in department 5: */

SELECT Lname, Fname


FROM EMPLOYEE
WHERE Salary > ALL ( SELECT Salary
FROM EMPLOYEE
WHERE Dno = 5 );

/*Query 16. Retrieve the name of each employee who has a dependent with the same first name and is the
same sex as the employee.*/

SELECT E.Fname, E.Lname


FROM EMPLOYEE E
WHERE E.Ssn
IN
( SELECT D.Essn
FROM DEPENDENT D
WHERE E.Fname = D.Dependent_name AND E.Sex = D.Sex );

/*Q16A:*/
SELECT E.Fname, E.Lname
FROM EMPLOYEE E, DEPENDENT D
WHERE E.Ssn = D.Essn AND E.Sex = D.Sex AND E.Fname = D.Dependent_name;

desc DEPENDENT

select * from DEPENDENT

/* The EXISTS and UNIQUE Functions in SQL*/


/*Query 16 in an alternative form that uses EXISTS as in Q16B: */
SELECT E.Fname, E.Lname
FROM EMPLOYEE E
WHERE
EXISTS
( SELECT * FROM DEPENDENT D
WHERE E.Ssn = D.Essn AND E.Sex = D.Sex AND E.Fname = D.Dependent_name);

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

/*Query 6. Retrieve the names of employees who have no dependents.*/

/*The query Q3: Retrieve the name of each employee who works on all the projects controlled by
department number 5*/

SELECT Fname, Lname


FROM EMPLOYEE
WHERE
NOT EXISTS
( SELECT * FROM DEPENDENT WHERE Ssn = Essn );

/*Query 7. List the names of managers who have at least one dependent.*/

SELECT Fname, Lname


FROM EMPLOYEE
WHERE
EXISTS
( SELECT * FROM DEPENDENT WHERE Ssn = Essn )
AND EXISTS ( SELECT * FROM DEPARTMENT WHERE Ssn = Mgr_ssn );

/*In Q3A, the first subquery (which is not correlated with the outer query) selects all projects controlled
by department 5, and the second subquery (which is correlated) selects all projects that the particular
employee being considered works on. If the set difference of the first subquery result MINUS (EXCEPT)
the second subquery result is empty, it means that the employee works on all the projects and is therefore
selected.*/

SELECT Fname, Lname


FROM EMPLOYEE
WHERE
NOT EXISTS
(
( SELECT Pnumber
FROM PROJECT
WHERE Dnum = 5)
EXCEPT
(
SELECT Pno
FROM WORKS_ON
WHERE Ssn = Essn) );

SELECT Fname, Lname FROM EMPLOYEE


WHERE NOT EXISTS ( ( SELECT Pnumber FROM PROJECT WHERE Dnum = 5)

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

EXCEPT ( SELECT Pno FROM WORKS_ON WHERE Ssn = Essn) );

/*The second option is shown as Q3B. Notice that we need two-level nesting in Q3B and that this
formulation is quite a bit more complex than Q3A.*/
SELECT Lname, Fname
FROM EMPLOYEE
WHERE NOT EXISTS
( SELECT * FROM WORKS_ON B
WHERE ( B.Pno IN ( SELECT Pnumber
FROM PROJECT
WHERE Dnum = 5 ) AND NOT EXISTS
( SELECT * FROM WORKS_ON C
WHERE C.Essn = Ssn
AND C.Pno = B.Pno )));

/*Explicit Sets and Renaming in SQL*/

/*Query 17. Retrieve the Social Security numbers of all employees who work on project numbers 1, 2, or
3.*/
SELECT DISTINCT Essn
FROM WORKS_ON
WHERE Pno IN (1, 2, 3);

/*Q8A shows how query Q8 from Section 4.3.2 can be slightly changed to retrieve the last name of each
employee and his or her supervisor while renaming the resulting attribute names as Employee_name and
Supervisor_name. The new names will appear as column headers for the query result.*/

/* To retrieve the last name of each employee and his or her supervisor while renaming the resulting
attribute names as Employee_name and Supervisor_name.*/

SELECT E.Lname AS Employee_name, S.Lname AS Supervisor_name


FROM EMPLOYEE E, EMPLOYEE S
WHERE E.Super_ssn = S.Ssn;

/* Joined Tables in SQL and Outer Joins*/


/* query Q1, which retrieves the name and address of every employee who works for the ‘Research’
department.*/
SELECT Fname, Lname, Address
FROM (EMPLOYEE JOIN DEPARTMENT ON Dno = Dnumber)

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

WHERE Dname = 'Research';

SELECT Fname, Lname, Address


FROM EMPLOYEE NATURAL JOIN (DEPARTMENT (Dname, Dno, Mssn, Msdate))
WHERE Dname = 'Research';

SELECT E.Lname AS Employee_name, S.Lname AS Supervisor_name


FROM (EMPLOYEE E LEFT OUTER JOIN EMPLOYEE S ON E.Super_ssn = S.Ssn);

/*Multiway Join*/

SELECT Pnumber, Dnum, Lname, Address, Bdate


FROM ( (PROJECT JOIN DEPARTMENT ON Dnum = Dnumber)
JOIN EMPLOYEE ON Mgr_ssn = Ssn)
WHERE Plocation = 'Stafford';

/*Aggregate Functions in SQL*/

/*Query 19. Find the sum of the salaries of all employees, the maximum salary, the minimum salary, and
the average salary.*/
SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary)
FROM EMPLOYEE;

SELECT SUM (Salary) AS Total_Sal,


MAX (Salary) AS Highest_Sal,
MIN (Salary) AS Lowest_Sal,
AVG (Salary) AS Average_Sal FROM EMPLOYEE;

/*Query 20. Find the sum of the salaries of all employees of the ‘Research’ department, as well as the
maximum salary, the minimum salary, and the average salary in this department.*/
SELECT SUM (Salary),
MAX (Salary),
MIN (Salary),
AVG (Salary) FROM (EMPLOYEE JOIN DEPARTMENT ON Dno = Dnumber)
WHERE Dname = 'Research';

/*Queries 21 and 22. Retrieve the total number of employees in the company (Q21) and the number of
employees in the ‘Research’ department (Q22). */

SELECT COUNT (*) FROM EMPLOYEE;

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

SELECT COUNT (*) FROM EMPLOYEE, DEPARTMENT WHERE DNO = DNUMBER AND
DNAME = 'Research';

/*Query 23. Count the number of distinct salary values in the database.*/

SELECT COUNT (DISTINCT Salary) FROM EMPLOYEE;

/* For example, to retrieve the names of all employees who have two or more dependents (Query 5), we
can write the following: */

SELECT Lname, Fname


FROM EMPLOYEE
WHERE ( SELECT COUNT (*)
FROM DEPENDENT
WHERE Ssn = Essn ) > = 2;

/*Grouping: The GROUP BY and HAVING Clauses*/


/*Query 24. 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 25. For each project, retrieve the project number, the 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 26. For each project on which more than two employees work, retrieve the project number, the
project name, and the number of employees who work on the project.*/

SELECT Pnumber, Pname, COUNT (*) FROM PROJECT, WORKS_ON


WHERE Pnumber = Pno
GROUP BY Pnumber, Pname
HAVING COUNT (*) > 2;

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

/*Query 27. For each project, retrieve the project number, the project name, and the number of employees
from department 5 who work on the project.*/

SELECT Pnumber, Pname, COUNT (*)


FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE Pnumber = Pno AND Ssn = Essn AND Dno = 5
GROUP BY Pnumber, Pname

/* the condition (SALARY > 40000) applies only to the COUNT function in the SELECT clause.
Suppose that we write the following incorrect query: */
SELECT Dno, COUNT (*)
FROM EMPLOYEE
WHERE Salary>40000
GROUP BY Dno
HAVING COUNT (*) > 5;

/*Query 28. For each department that has more than five employees, retrieve the department number and
the number of its employees who are making more than $40,000.*/

SELECT Dno, COUNT (*)


FROM EMPLOYEE
WHERE Salary>40000 AND Dno
IN ( SELECT Dno
FROM EMPLOYEE
GROUP BY Dno
HAVING COUNT (*) > 5)
GROUP BY Dno;

/*Other SQL Constructs: WITH and CASE*/

WITH BIGDEPTS (Dno) AS ( SELECT Dno


FROM EMPLOYEE
GROUP BY Dno
HAVING COUNT (*) > 5)
SELECT Dno, COUNT (*)
FROM EMPLOYEE
WHERE Salary>40000 AND Dno IN BIGDEPTS
GROUP BY Dno;

/* Employees in department 5 get a $2,000 raise, those in department 4 get $1,500 and those in
department 1 get $3,00*/

UPDATE EMPLOYEE
SET Salary = CASE WHEN Dno = 5 THEN Salary + 2000

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

WHEN Dno = 4 THEN Salary + 1500


WHEN Dno = 1 THEN Salary + 3000
ELSE Salary + 0 ;

/* An example of a recursive operation is to retrieve all supervisees of a supervisory employee e at all


levels—that is, all employees e′ directly supervised by e, all employees e′ directly supervised by each
employee e′, all employees e″′ directly supervised by each employee e″, and so on.*/
WITH RECURSIVE SUP_EMP (SupSsn, EmpSsn) AS
( SELECT SupervisorSsn, Ssn
FROM EMPLOYEE
UNION
SELECT E.Ssn, S.SupSsn
FROM EMPLOYEE E, SUP_EMP S
WHERE E.SupervisorSsn = S.EmpSsn)
SELECT* FROM SUP_EMP;

/*Views (Virtual Tables) in SQL*/

CREATE VIEW WORKS_ON1 AS SELECT Fname, Lname, Pname, Hours


FROM EMPLOYEE, PROJECT, WORKS_ON
WHERE Ssn = Essn AND Pno = Pnumber;

CREATE VIEW DEPT_INFO(Dept_name, No_of_emps, Total_sal) AS SELECT Dname, COUNT (*),


SUM (Salary)
FROM DEPARTMENT, EMPLOYEE
WHERE Dnumber = Dno GROUP BY Dname

SELECT Fname, Lname


FROM WORKS_ON1
WHERE Pname = 'ProductX';

SELECT Fname, Lname FROM EMPLOYEE, PROJECT, WORKS_ON


WHERE Ssn = Essn AND Pno = Pnumber AND Pname = 'ProductX';

select * from WORKS_ON1

UPDATE WORKS_ON1
SET Pname = 'ProductY'
WHERE Lname = 'Smith' AND Fname = 'John' AND Pname = 'ProductX';

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department

UPDATE WORKS_ON SET Pno = ( SELECT Pnumber


FROM PROJECT
WHERE Pname = 'ProductY')
WHERE Essn
IN ( SELECT Ssn
FROM EMPLOYEE
WHERE Lname = 'Smith' AND Fname = 'John' )
AND Pno = ( SELECT Pnumber
FROM PROJECT
WHERE Pname = 'ProductX' );

UPDATE PROJECT SET Pname = 'ProductY' WHERE Pname = 'ProductX';

/*Query 8. For each employee, retrieve the employee’s first and last name and the first and last name of
his or her immediate supervisor.*/

SELECT Fname, Lname


FROM EMPLOYEE
WHERE Super_ssn= Ssn;

desc EMPLOYEE
select Super_ssn,Ssn from EMPLOYEE

Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga

You might also like