SQL Assignment Answers
SQL Assignment Answers
A small IT firm designing business software for its clients wants to store and manage its data.
It has identified following entities for which, it will maintain data.
1. Clients
2. Employees
3. Departments
4. Projects
5. EmpProjectTasks *
* This will hold data of employees working on projects.
You are required to create the tables using the information provided below. Once tables are ready, fill
in the given data in each table.
NOTE: Submit the CREATE TABLE, INSERT and SELECT commands as answer to this
assignment.
The tables below describe attributes for each entity.
Clients
Attribute Name
Attribute Type
Constraint
Client_ID
NUMBER(4)
Primary Key
Cname
VARCHAR2(40)
Not Null
Address
VARCHAR2(30)
Email
VARCHAR2(30)
Unique
Phone
NUMBER(10)
Business
VARCHAR2(20)
Not Null
Remarks
Employees
Attribute Name
Empno
Ename
Job
Salary
Attribute Type
NUMBER(4)
VARCHAR2(20)
VARCHAR2(15)
NUMBER(7)
Constraint
Primary Key
Not Null
Remarks
Must be positive
Deptno
NUMBER(2)
Foreign Key
Departments
Attribute Name
Deptno
Dname
Loc
Attribute Type
NUMBER(2)
VARCHAR2(15)
VARCHAR2(20)
Constraint
Primary Key
Not Null
Remarks
Projects
Attribute Name
Project_ID
Descr
Attribute Type
NUMBER(3)
VARCHAR2(30)
Constraint
Primary Key
Not Null
Remarks
Start_Date
Planned_End_Date
DATE
DATE
Actual_End_date
DATE
Budget
NUMBER(10)
Must be positive
Client_ID
NUMBER(4)
Foreign Key
Description of project
like Accounting ,
Inventory, Payroll
etc.
Start date of project
Planned End date of
project
Actual End date of
project (Use CHECK
constraint)
Use CHECK constraint
to ensure budget is > 0
Client ID from Clients
table
EmpProjectTasks
Attribute Name
Project_ID
Empno
Attribute Type
NUMBER(3)
NUMBER(4)
Start_Date
DATE
End_Date
DATE
Task
VARCHAR2(25)
Not Null
Status
VARCHAR2(15)
Not Null
Clients
Client ID
Cname
1001
Constraint
Primary Key, Foreign Key
Primary Key, Foreign Key
Remarks
Composite primary key
and foreign keys
referring Projects and
Employees table
Start date when
employee begins task
on this project
End date when
employee finishes task
on this project
Task performed by
employee like
designing, coding,
review, testing etc.
Status of task like in
progress,
complete,cancelled
Phone
Business
ACME Utilities
Addres
s
Noida
contact@acmeutil.com
Manufacturing
1002
Trackon Consultants
Mumbai
consult@trackon.com
1003
MoneySaver
Distributors
Lawful Corp
Kolkata
save@moneysaver.co
m
justice@lawful.com
956788003
2
873421009
0
779988665
5
921034221
9
1004
Employees
Emp
Enam
no
e
7001
Sande
ep
7002
Rajesh
7003
Job
Analyst
Designer
7004
Madha
v
Manoj
7005
Abhay
Develope
r
Develope
r
Designer
7006
Uma
Tester
7007
Gita
7008
Priya
Tech.
Writer
Tester
7009
Nutan
7010
Smita
7011
Anand
Develope
r
Analyst
Project
Mgr
Departments
Deptno Dname
Loc
Chennai
Sala
ry
250
00
300
00
400
00
400
00
350
00
300
00
300
00
350
00
450
00
200
00
650
00
Dept
no
10
10
20
20
10
30
40
30
20
10
10
Consultant
Reseller
Professional
10
Design
Pune
20
Pune
30
Developm
ent
Testing
40
Document
Mumbai
Projects
Project_I
D
401
Descr
Mumbai
Inventory
Start_Dat
e
01-Apr-11
Planned_End_Da
te
01-Oct-11
402
Accounting
01-Aug-11
403
Payroll
404
Contact Mgmt
EmpProjectTasks
Project
Emp
Start_D
_ID
no
ate
401
7001
01-Apr11
401
7002
21-Apr11
401
7003
01-Jun11
401
7004
18-Jul11
401
7006
03-Sep11
401
7009
18-Sep11
401
7008
06-Oct11
401
7007
06-Oct11
401
7011
22-Oct11
402
7010
01-Aug11
402
7002
22-Aug11
402
7004
01-Oct11
Budge
t
150000
Client_I
D
1001
01-Jan-12
500000
1002
01-Oct-11
31-Dec-11
75000
1003
01-Nov-11
31-Dec-11
50000
1004
End_Da
te
20-Apr11
30-May11
15-Jul11
01-Sep11
15-Sep11
05-Oct11
16-Oct11
22-Oct11
31-Oct11
20-Aug11
30-Sep11
Actual_End_date
31-Oct-11
Task
Status
System
Analysis
System
Design
Coding
Complet
ed
Complet
ed
Complet
ed
Complet
ed
Complet
ed
Complet
ed
Complet
ed
Complet
ed
Complet
ed
Complet
ed
Complet
ed
In
Progress
Coding
Testing
Code
Change
Testing
Documentat
ion
Sign of
System
Analysis
System
Design
Coding
Consultants','Mumbai','consult@trackon.com',8734210090,
'Consultant');
Insert into CLIENTS
(CLIENT_ID,CNAME,ADDRESS,EMAIL,PHONE,BUSINESS)
values (1003,'MoneySaver
Distributors','Bangalore','save@moneysaver.com',7799886
655,'Reseller');
Insert into CLIENTS
(CLIENT_ID,CNAME,ADDRESS,EMAIL,PHONE,BUSINESS)
values (1004,'Lawful
Corp','Chennai','justice@lawful.com',9210342219,'Professi
onal');
CREATE TABLE DEPARTMENTS(
DEPTNO
NUMBER(2)
PRIMARY KEY,
DNAME
VARCHAR2(15) NOT NULL,
LOC
VARCHAR2(20));
Insert into DEPARTMENTS (DEPTNO,DNAME,LOC)
(10,'Design','Pune');
Insert into DEPARTMENTS (DEPTNO,DNAME,LOC)
(20,'Development','Pune');
Insert into DEPARTMENTS (DEPTNO,DNAME,LOC)
(30,'Testing','Mumbai');
Insert into DEPARTMENTS (DEPTNO,DNAME,LOC)
(40,'Document','Mumbai');
values
values
values
values
(7009,'Nutan','Developer',45000,20);
Insert into EMPLOYEES
(EMPNO,ENAME,JOB,SALARY,DEPTNO) values
(7010,'Smita','Analyst',20000,10);
Insert into EMPLOYEES
(EMPNO,ENAME,JOB,SALARY,DEPTNO) values
(7011,'Anand','Project Mgr',65000,10);
CREATE TABLE PROJECTS(
PROJECT_ID
NUMBER(3)
PRIMARY KEY,
DESCR VARCHAR2(30) NOT NULL,
START_DATE
DATE,
PLANNED_END DATE
DATE,
ACTUAL_END_DATE
DATE,
BUDGET
NUMBER(10)
CHECK(BUDGET > 0),
CLIENT_ID
NUMBER(4)
REFERENCES
CLIENTS(CLIENT_ID));
Insert into PROJECTS
(PROJECT_ID,DESCR,START_DATE,PLANNED_END_DATE,ACT
UAL_END_DATE,BUDGET,CLIENT_ID) values
(401,'Inventory',to_date('01-APR-11','DD-MONRR'),to_date('01-OCT-11','DD-MON-RR'),to_date('31-OCT11','DD-MON-RR'),150000,1001);
Insert into PROJECTS
(PROJECT_ID,DESCR,START_DATE,PLANNED_END_DATE,ACT
UAL_END_DATE,BUDGET,CLIENT_ID) values
(402,'Accounting',to_date('01-AUG-11','DD-MONRR'),to_date('01-JAN-12','DD-MON-RR'),null,500000,1002);
Insert into PROJECTS
(PROJECT_ID,DESCR,START_DATE,PLANNED_END_DATE,ACT
UAL_END_DATE,BUDGET,CLIENT_ID) values
(403,'Payroll',to_date('01-OCT-11','DD-MONRR'),to_date('31-DEC-11','DD-MON-RR'),null,75000,1003);
Insert into PROJECTS
(PROJECT_ID,DESCR,START_DATE,PLANNED_END_DATE,ACT
UAL_END_DATE,BUDGET,CLIENT_ID) values (404,'Contact
Mgmt',to_date('01-NOV-11','DD-MON-RR'),to_date('31-DEC11','DD-MON-RR'),null,50000,1004);
SELECT
SELECT
SELECT
SELECT
SELECT
*
*
*
*
*
FROM
FROM
FROM
FROM
FROM
CLIENTS;
DEPARTMENTS;
EMPLOYEES;
PROJECTS;
EMPPROJECTTASKS;
Given the tables created in Assignment #1, find solutions for the following.
1. Display customer details with business as Consultant
2. Display employee details who are not Developers
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
Given the tables created in Assignment #1, find solutions for the following.
1. Display count of clients
2. Display count of employees and sum of their salaries
3. Display max salary per department
4. Display min salary per job
5. Display average salary
6. Display sum of budget
7. Display count of Coding task
8. Display department wise count and sum of salary of employees
9. Display client names and their project desc, start date and budget
10. Display department name, employee name and job
11. Display names of employees doing System Analysis along with project name
12. Display job wise count
13. Display employee numbers not present in EmpProjectTasks table using necessary set operator
14. Display employee numbers present in both Employees and EmpProjectTasks table using
necessary set operator
15. Display all employee numbers present in both Employees and EmpProjectTasks table using
necessary set operator
SQL Assignment 3 Answers
1. SELECT COUNT(*) FROM CLIENTS;
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
Given the tables created in Assignment #1, find solutions for the following.
1. Display the project name with highest budget
2. Display employee names who have same job as Madhav
3. Display employees name and job who worked on Code Change task of project 401
4. Display client name whose projects Coding task is In Progress
5. Display department number, names and salaries of employees who are earning max salary in
their departments
6. Display name of department with highest SUM of salary
7. Create a table named CLIENT_PROJECTS using CTAS method that includes CLIENT_ID, CNAME,
ADDRESS,BUSINESS,DESCR,BUDGET columns from CLIENTS and PROJECTS tables
8. Increase salary of employees by 15% who have performed task of Testing on projects
9. Create a view named DEPT_EMP with DEPTNO,DNAME,LOC,ENAME,JOB, and SALARY columns
10. Create synonym EPT for table EmpProjectTasks
SQL Assignment 4 - Answers
1. SELECT DESCR,BUDGET FROM PROJECTS WHERE BUDGET = (SELECT MAX(BUDGET) FROM
PROJECTS);
2. SELECT ENAME,JOB FROM EMPLOYEES WHERE JOB = (SELECT JOB FROM EMPLOYEES WHERE
ENAME = 'Madhav');
3. SELECT ENAME,JOB FROM EMPLOYEES WHERE EMPNO = (SELECT EMPNO FROM
EMPPROJECTTASKS WHERE PROJECT_ID = 401 AND TASK = 'Code Change');
4. SELECT CNAME FROM CLIENTS WHERE CLIENT_ID = (SELECT CLIENT_ID FROM PROJECTS
WHERE PROJECT_ID = (SELECT PROJECT_ID FROM EMPPROJECTTASKS WHERE TASK = 'Coding'
AND STATUS = 'In Progress'));
5. SELECT DEPTNO,ENAME,SALARY FROM EMPLOYEES E WHERE SALARY = (SELECT MAX(SALARY)
FROM EMPLOYEES WHERE DEPTNO = E.DEPTNO);
6. SELECT DNAME FROM DEPARTMENTS WHERE DEPTNO = (SELECT DEPTNO FROM EMPLOYEES
GROUP BY DEPTNO HAVING SUM(SALARY) = (SELECT MAX(SUM(SALARY)) FROM EMPLOYEES
GROUP BY DEPTNO));
7. CREATE TABLE CLIENT_PROJECTS AS SELECT
C.CLIENT_ID,C.CNAME,C.ADDRESS,C.BUSINESS,P.DESCR,P.BUDGET FROM CLIENTS C, PROJECTS
P WHERE P.CLIENT_ID = C.CLIENT_ID;
8. UPDATE EMPLOYEES SET SALARY = SALARY * 1.15 WHERE EMPNO IN (SELECT EMPNO FROM
EMPPROJECTTASKS WHERE TASK = 'Testing');
9. CREATE OR REPLACE VIEW DEPT_EMP AS SELECT
D.DEPTNO,D.DNAME,D.LOC,E.ENAME,E.JOB,E.SALARY FROM DEPARTMENTS D,EMPLOYEES E
WHERE D.DEPTNO=E.DEPTNO;
10. CREATE SYNONYM EPT FOR EMPPROJECTTASKS;