2.Create the following tables with appropriate datatypes and constraints.
EMPLOYEE(Fname,Mname,Lname,SSN,Bdate,Address,Gender,Salary,SuperSSN,Dno)
DEPARTMENT (Dnumber, Dname, MgrSSN, Mgrstartdate)
DEPENDENT(ESSN,Dependent_Name,Gender,Bdate, Relationship)
SQL> CREATE TABLE DEPARTMENT(
DNUMBER INT PRIMARY KEY,
DNAME VARCHAR(15) NOT NULL,
MGRSSN CHAR(10) NOT NULL,
MGRSTARTDATE DATE NOT NULL
);
Table created.
SQL> CREATE TABLE EMPLOYEE (
FNAME VARCHAR(20) NOT NULL,
MNAME VARCHAR(20),
LNAME VARCHAR(20) NOT NULL,
SSN CHAR(10) PRIMARY KEY,
BDATE DATE NOT NULL,
ADRESS VARCHAR(50),
GENDER CHAR(1) CHECK(GENDER IN('M','F')),
SALARY DECIMAL(10,2) CHECK(SALARY>=0),
SUPERSSN CHAR(10),
DNO INT,
FOREIGN KEY(DNO) REFERENCES DEPARTMENT(DNUMBER)
);
Table created.
a. Insert 5 to10 rows into all the tables.
SQL>INSERT INTO DEPARTMENT (Dnumber, Dname, MgrSSN, MgrStartDate) VALUES
(1, 'Human Resources', '123456789', '15-jan-20');
1 row created.
SQL>INSERT INTO DEPARTMENT (Dnumber, Dname, MgrSSN, MgrStartDate) VALUES
(2, 'Finance', '987654321', '20-MAR-19');
1 row created.
SQL>INSERT INTO DEPARTMENT (Dnumber, Dname, MgrSSN, MgrStartDate) VALUES
(3, 'Engineering', '456789123', '10-MAY-21');
1 row created.
SQL>INSERT INTO DEPARTMENT (Dnumber, Dname, MgrSSN, MgrStartDate) VALUES
(4, 'Marketing', '321654987', '01-JUL-22');
1 row created.
SQL>INSERT INTO DEPARTMENT (Dnumber, Dname, MgrSSN, MgrStartDate) VALUES
(5, 'Sales', '147258369', '12-SEP-23');
1 row created.
SQL> select * from department;
DNUMBER DNAME MGRSSN MGRSTARTD
---------- --------------- ---------- ---------
1 Human Resources 123456789 15-JAN-20
2 Finance 987654321 20-MAR-19
3 Engineering 456789123 10-MAY-21
4 Marketing 321654987 01-JUL-22
5 Sales 147258369 12-SEP-23
SQL> INSERT INTO EMPLOYEE (Fname, Mname, Lname, SSN, Bdate, Adress, Gender, Salary, SuperSSN, Dno)
VALUES('suresh','kumar', 'A', '123456789', '15-jun-1985', '78Nukalamma St , vizag, AP', 'M', 60000, NULL, 1);
1 row created.
SQL> INSERT INTO EMPLOYEE (Fname, Mname, Lname, SSN, Bdate, Adress, Gender, Salary, SuperSSN, Dno)
VALUES('Dileep', 'kumar', 'B', '987654321', '20-aug-1990', '456 ramalayamSt, hyd, Telangana', 'M', 70000,
'123456789', 2);
1 row created.
SQL> INSERT INTO EMPLOYEE (Fname, Mname, Lname, SSN, Bdate, Adress, Gender, Salary, SuperSSN, Dno)
VALUES
('Harsha', 'vardhan', 'N', '456789123', '30-nov-1992', '789 church St, Vijayawada, AP', 'M', 80000, '987654321', 3);
1 row created.
SQL> INSERT INTO EMPLOYEE (Fname, Mname, Lname, SSN, Bdate, Adress, Gender, Salary, SuperSSN, Dno)
VALUES
('surya', 'kiran', 'V', '321654987', '05-dec-1988', '159 mavullamma St,Tnagar, chennai', 'M', 55000, '987654321', 4);
1 row created.
SQL> INSERT INTO EMPLOYEE (Fname, Mname, Lname, SSN, Bdate, Adress, Gender, Salary, SuperSSN, Dno)
VALUES
('Amrutha', 'rachana', 'S', '147258369', '22-jan-1995', '753churchSt, Managalore, Bangalore', 'F', 50000, '456789123',
5);
1 row created.
SQL> select * from employee;
FNAME MNAME LNAME SSN BDATE ADRESS G SALARY SUPERSSN DNO
suresh kumar A 123456789 15-JUN-85 78Nukalamma St , vizag, AP M 6000 1
Dileep kumar B 987654321 20-AUG-90 456 ramalayamSt, hyd, Telangana M 70000 123456789 2
Harsha vardhan N 456789123 30-NOV-92 789 church St, Vijayawada, AP M 80000 987654321 3
surya kiran V 321654987 05-DEC-88 159 mavullamma St,Tnagar, chennai M 55000 987654321 4
Amrutha rachana S 147258369 22-JAN-95 753churchSt, Managalore, Bangalore F 50000 456789123 5
SQL> ALTER TABLE EMPLOYEE ADD CONSTRAINT fk_SuperSSN FOREIGN KEY (SuperSSN)
REFERENCES EMPLOYEE(SSN);
Table altered.
SQL> ALTER TABLE DEPARTMENT ADD CONSTRAINT fk_MgrSSN FOREIGN KEY (MGRSSN)
REFERENCES EMPLOYEE(SSN);
Table altered.
SQL> CREATE TABLE DEPENDENT(
ESSN CHAR(10) NOT NULL,
DEPENDENT_NAME VARCHAR(20) NOT NULL,
GENDER CHAR(1) CHECK(GENDER IN('M','F')),
BDATE DATE NOT NULL,
RELATIONSHIP VARCHAR(20),
PRIMARY KEY(ESSN,DEPENDENT_NAME),
FOREIGN KEY(ESSN) REFERENCES EMPLOYEE(SSN)
);
Table created.
SQL> INSERT INTO DEPENDENT (ESSN, Dependent_Name, Gender, Bdate, Relationship) V
ALUES('123456789', 'Nitya A', 'F', '10-apr-2010', 'Daughter');
1 row created.
SQL> INSERT INTO DEPENDENT (ESSN, Dependent_Name, Gender, Bdate, Relationship) V
ALUES ('123456789', 'Rahul A', 'M', '15-jul-2012', 'Son');
1 row created.
SQL> INSERT INTO DEPENDENT (ESSN, Dependent_Name, Gender, Bdate, Relationship) V
ALUES('987654321', 'Anvika B', 'F', '20-sep-2015', 'Daughter');
1 row created.
SQL> INSERT INTO DEPENDENT (ESSN, Dependent_Name, Gender, Bdate, Relationship) V
ALUES ('456789123', 'Avinadh N', 'M', '28-feb-2018', 'Son');
1 row created.
SQL> INSERT INTO DEPENDENT (ESSN, Dependent_Name, Gender, Bdate, Relationship) V
ALUES ('321654987', 'Deepika V', 'F', '10-jun-2019', 'Daughter');
1 row created.
SQL> INSERT INTO DEPENDENT (ESSN, Dependent_Name, Gender, Bdate, Relationship) V
ALUES ('147258369', 'Vinay S', 'M', '05-nov-2020', 'Son');
1 row created.
SQL> select * from dependent;
ESSN DEPENDENT_NAME G BDATE RELATIONSHIP
---------- -------------------- - --------- --------------------
123456789 Nitya A F 10-APR-10 Daughter
123456789 Rahul A M 15-JUL-12 Son
987654321 Anvika B F 20-SEP-15 Daughter
456789123 Avinadh N M 28-FEB-18 Son
321654987 Deepika V F 10-JUN-19 Daughter
147258369 Vinay S M 05-NOV-20 Son
6 rows selected.
b. Display all employee’s names along with their department names.
SQL> select e.fname,d.dname from employee e,department d where e.dno=d.dnumber;
FNAME DNAME
-------------------- ---------------
suresh Human Resources
Dileep Finance
Harsha Engineering
surya Marketing
Amrutha Sales
c. Display all employee’s names along with their dependent details.
SQL> select e.fname,d.dependent_name from employee e,dependent d where
e.ssn=d.essn;
FNAME DEPENDENT_NAME
-------------------- --------------------
suresh Nitya A
suresh Rahul A
Dileep Anvika B
Harsha Avinadh N
surya Deepika V
Amrutha Vinay S
6 rows selected.
d. Display name and address of all employees who work for ‘CSE’ department.
SQL> SELECT Fname, Lname, Adress FROM EMPLOYEE where DNO in(select DNUMBER from
department WHERE DNAME='Sales');
FNAME LNAME ADRESS
--------------------------------------------------
Amrutha S 753churchSt, Managalore, Bangalore
e. List the names of all employees with two or more dependents.
SQL> select fname from employee where ssn in(select essn from dependent group by
essn having count(dependent_name)>=2);
FNAME
--------------------
suresh
f. List the names of employee who have no dependents.
SQL> SELECT E.Fname, E.Lname FROM EMPLOYEE E LEFT JOIN DEPENDENT D ON E.SSN = D.
ESSN WHERE D.ESSN IS NULL;
no rows selected
g. List the names of employees who have atleast one dependent.
SQL> SELECT DISTINCT E.Fname, E.Lname FROM EMPLOYEE E JOIN DEPENDENT D ON E.SSN= D.ESSN;
FNAME LNAME
-------------------- --------------------
Amrutha S
suresh A
Harsha N
surya V
Dileep B
h. List the names of the employees along with names of their supervisors using aliases.
SQL> SELECT E.Fname AS Employee_Name, S.Fname AS Supervisor_Name FROM EMPLOYEE E
LEFT JOIN EMPLOYEE S ON E.SuperSSN = S.SSN;
EMPLOYEE_NAME SUPERVISOR_NAME
-------------------- --------------------
Dileep suresh
surya Dileep
Harsha Dileep
Amrutha Harsha
suresh
i. Display name of the department and name of manager for all the departments.
SQL> SELECT D.Dname AS Department_Name, E.Fname AS Manager_First_Name, E.Lname A
S Manager_Last_Name FROM DEPARTMENT D JOIN EMPLOYEE E ON D.MgrSSN = E.SSN;
DEPARTMENT_NAME MANAGER_FIRST_NAME MANAGER_LAST_NAME
--------------- -------------------- --------------------
Human Resources suresh A
Finance Dileep B
Engineering Harsha N
Marketing surya V
Sales Amrutha S
j. Display the name of each employee who has a dependent with the same first name and gender as the
employee.
SQL> SELECT E.Fname, E.Lname FROM EMPLOYEE E JOIN DEPENDENT D ON E.SSN = D.ESSN
WHERE E.Fname = D.Dependent_Name AND E.Gender = D.Gender;
no rows selected
k. List the names of managers who have at least one dependent.
SQL> SELECT DISTINCT E.Fname, E.Lname FROM EMPLOYEE E JOIN DEPARTMENT D ON E.SSN
= D.MgrSSN JOIN DEPENDENT D2 ON E.SSN = D2.ESSN;
FNAME LNAME
-------------------- --------------------
Amrutha S
suresh A
Harsha N
surya V
Dileep B
l. Display the sum of all employees’ salaries as well as maximum, minimum and average salary in the
entire departments department wise if the department has more than two employees.
SQL> SELECT D.Dname AS Department_Name, SUM(E.Salary) AS Total_Salary,
MAX(E.Salary) AS Max_Salary, MIN(E.Salary) AS Min_Salary, AVG(E.S
alary) AS Avg_Salary FROM EMPLOYEE E JOIN DEPARTMENT D ON E.Dno = D.Dnumber GROU
P BY D.Dname HAVING COUNT(E.SSN) > 2;
no rows selected
m. List the departments of each female employee along with her name.
SQL> SELECT E.Fname, E.Lname, D.Dname AS Department_Name FROM EMPLOYEE E JOIN
DEPARTMENT D ON E.Dno = D.Dnumber WHERE E.Gender = 'F';
FNAME LNAME DEPARTMENT_NAME
-------------------- -------------------- ---------------
Amrutha S Sales
n. List all employee names and also the name of the department they manage if they happen to manage a
dept.
SQL> SELECT E.Fname, E.Lname, D.Dname AS Managed_Department FROM EMPLOYEE E LEFT
JOIN DEPARTMENT D ON E.SSN = D.MgrSSN;
FNAME LNAME MANAGED_DEPARTM
-------------------- -------------------- ---------------
suresh A Human Resources
Dileep B Finance
Harsha N Engineering
surya V Marketing
Amrutha S Sales