Full Database Project Report
Full Database Project Report
Database Schema
The database schema used for this project is depicted below:
DEPT Table
INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES
(7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10),
(7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, NULL, 30),
(7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, NULL, 10),
(7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, NULL, 20),
(7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30),
(7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30),
(7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30),
(7900, 'JAMES', 'CLERK', 7698, '1981-12-02', 950, NULL, 30),
(7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, NULL, 20),
(7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, NULL, 20),
(7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000, NULL, 20),
(7876, 'ADAMS', 'CLERK', 7788, '1983-01-12', 1100, NULL, 20),
(7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, NULL, 10);
SALGRADE Table
sql
CopyEdit
SELECT *
FROM EMP E
sql
CopyEdit
SELECT ENAME
FROM EMP
sql
CopyEdit
SELECT DNAME
FROM DEPT;
sql
CopyEdit
FROM EMP
WHERE HIREDATE > '1981-01-01';
sql
CopyEdit
FROM EMP;
sql
CopyEdit
SELECT ENAME
FROM EMP
sql
CopyEdit
SELECT LOC
FROM DEPT;
sql
CopyEdit
SELECT ENAME
FROM EMP
sql
CopyEdit
SELECT ENAME
FROM EMP
sql
CopyEdit
FROM EMP
sql
CopyEdit
FROM EMP
sql
CopyEdit
FROM DEPT;
sql
CopyEdit
SELECT ENAME
FROM EMP E
sql
CopyEdit
FROM EMP E1
sql
CopyEdit
SELECT E.ENAME
FROM EMP E
sql
CopyEdit
SELECT E.ENAME
FROM EMP E
sql
CopyEdit
SELECT MAX(SAL) AS HighestSalary
FROM EMP E
sql
CopyEdit
FROM EMP E
sql
CopyEdit
FROM EMP E
GROUP BY D.DNAME
sql
CopyEdit
SELECT E.*
FROM EMP E
WHERE S.GRADE = 3;
Hard (10 Queries)
sql
CopyEdit
FROM EMP E
ON E.DEPTNO = A.DEPTNO
sql
CopyEdit
SELECT E.ENAME
FROM EMP E
ON E.DEPTNO = A.DEPTNO
sql
CopyEdit
SELECT ENAME
FROM EMP
sql
CopyEdit
SELECT E2.ENAME
FROM EMP E1
sql
CopyEdit
SELECT D.DNAME
FROM DEPT D
WHERE D.DEPTNO NOT IN (SELECT DEPTNO FROM EMP WHERE SAL > 3000);
Retrieve employees who have been working for more than 10 years.
sql
CopyEdit
SELECT ENAME
FROM EMP
sql
CopyEdit
SELECT ENAME
FROM EMP
FROM EMP
sql
CopyEdit
FROM EMP E
GROUP BY D.DNAME;
List employees and the grades they fall into based on salary.
sql
CopyEdit
FROM EMP E
sql
CopyEdit
FROM EMP E1
JOIN EMP E2
4o
You said:
ChatGPT said:
Here are 10 advanced-level SQL queries and corresponding simpler queries to help you
understand their structure and logic.
Advanced-Level Queries
Find employees whose salary is greater than the average salary of their department and
who also earn a commission.
sql
CopyEdit
FROM EMP
WHERE SAL > (SELECT AVG(SAL) FROM EMP WHERE DEPTNO = EMP.DEPTNO)
Understanding Query:
sql
CopyEdit
FROM EMP
GROUP BY DEPTNO;
Retrieve the names of employees whose salaries match the highest salary in their
department.
sql
CopyEdit
FROM EMP
Understanding Query:
sql
CopyEdit
FROM EMP
GROUP BY DEPTNO;
Find the departments where the total salary expense exceeds 10,000.
sql
CopyEdit
FROM EMP E
GROUP BY D.DNAME
Understanding Query:
sql
CopyEdit
FROM EMP E
GROUP BY D.DNAME;
Find employees who earn more than all the employees in the 'SALES' department.
sql
CopyEdit
FROM EMP
WHERE SAL > ALL (SELECT SAL FROM EMP WHERE DEPTNO = 30);
Understanding Query:
sql
CopyEdit
SELECT SAL
FROM EMP
Retrieve the name of the department with the highest average salary.
sql
CopyEdit
SELECT DNAME
FROM DEPT D
ON D.DEPTNO = A.DEPTNO
WHERE A.AvgSal = (SELECT MAX(AvgSal) FROM (SELECT AVG(SAL) AS AvgSal FROM EMP
GROUP BY DEPTNO) AS SubQuery);
Understanding Query:
sql
CopyEdit
FROM EMP
GROUP BY DEPTNO;
List employees whose salaries are higher than the salaries of their managers.
sql
CopyEdit
FROM EMP E1
JOIN EMP E2 ON E1.MGR = E2.EMPNO
Understanding Query:
sql
CopyEdit
FROM EMP;
sql
CopyEdit
Understanding Query:
sql
CopyEdit
FROM EMP;
Find the total salary paid for each job role in all departments.
sql
CopyEdit
Understanding Query:
sql
CopyEdit
FROM EMP
GROUP BY JOB;
Retrieve the details of employees who were hired earlier than their managers.
sql
CopyEdit
FROM EMP E1
Understanding Query:
sql
CopyEdit
FROM EMP;
sql
CopyEdit
SELECT S.GRADE, AVG(E.SAL) AS AvgSalary
FROM EMP E
GROUP BY S.GRADE
ORDER BY S.GRADE;
Understanding Query:
sql
CopyEdit
FROM SALGRADE;
Nested Subqueries: Queries like #1, #2, and #5 use subqueries to calculate intermediate
results, such as averages or maximums, which are then compared in the main query.
Joins: Queries like #6 and #9 use self-joins to compare data between employees and their
managers.
Aggregations: Queries like #3, #8, and #10 use aggregation functions (SUM, AVG) to group
and summarize data.
1. Insertion Anomalies
Missing Data:
If a new employee is added to the EMP table with an invalid or missing DEPTNO, the
insertion fails due to the foreign key constraint referencing the DEPT table.
CopyEdit
Unnecessary Duplication:
If department details are redundantly stored in the EMP table, any new employee in the
same department would duplicate department data.
A new employee cannot be inserted into the EMP table unless their DEPTNO exists in the
DEPT table.
sql
CopyEdit
If a new employee’s SAL is not within the ranges defined in the SALGRADE table,
inconsistencies arise when joining SALGRADE to determine salary grades.
sql
CopyEdit
VALUES (8003, 'NEW_EMP', 'CLERK', '2025-02-07', 10000, 10); -- Salary doesn't match any
grade
2. Deletion Anomalies
sql
CopyEdit
Deleting an employee from the EMP table also deletes their commission (COMM) and hire
date (HIREDATE), losing valuable information.
sql
CopyEdit
DELETE FROM EMP WHERE EMPNO = 7654; -- Deletes MARTIN's hire date and commission
If foreign key constraints are set to cascade deletions, deleting a department record in DEPT
could unintentionally delete all employees in that department.
sql
CopyEdit
DELETE FROM DEPT WHERE DEPTNO = 20; -- Deletes JONES, FORD, SMITH, etc.
Deleting a manager (MGR) from EMP could leave employees reporting to that manager
without a valid MGR value.
Example: If EMPNO = 7839 (KING) is deleted, employees reporting to him lose their
manager reference.
sql
CopyEdit
DELETE FROM EMP WHERE EMPNO = 7839; -- Orphaned managers for CLARK, BLAKE,
JONES
3. Update Anomalies
Inconsistent Updates:
sql
CopyEdit
UPDATE EMP
SET DEPTNO = 50
Updating a department number in DEPT without updating the related rows in EMP creates
mismatches.
sql
CopyEdit
UPDATE DEPT
SET DEPTNO = 50
sql
CopyEdit
UPDATE EMP
Updating the MGR field to create a circular reporting hierarchy leads to logical errors.
Example: Setting MGR = 7698 (BLAKE) to report to 7654 (MARTIN), who already reports to
7698.
sql
CopyEdit
UPDATE EMP
If the schema is denormalized (e.g., storing department names in EMP), updating one
instance of department data leads to inconsistencies unless all instances are updated.
Key Takeaways
Insertion Anomalies: Occur due to missing or invalid data, constraint violations, and
dependency issues.
Normalization Process
To normalize the given database schema (EMP, DEPT, SALGRADE) up to BCNF and ensure it
satisfies 3NF, let us go step-by-step.
Definition: A table is in 1NF if all columns contain atomic (indivisible) values and each
record is unique.
All columns in EMP, DEPT, and SALGRADE tables contain atomic values (e.g.,
ENAME is a single string, SAL is a single value).
1. It is in 1NF.
2. It does not have partial dependency (no non-prime attribute depends on a part of
the composite key).
EMP: The primary key is EMPNO. All non-key attributes (ENAME, JOB, MGR,
HIREDATE, SAL, COMM, DEPTNO) depend entirely on EMPNO.
No partial dependency exists.
DEPT: The primary key is DEPTNO. Attributes (DNAME, LOC) depend entirely on
DEPTNO.
No partial dependency exists.
SALGRADE: The primary key is GRADE. Attributes (LOSAL, HISAL) depend entirely
on GRADE.
No partial dependency exists.
1. It is in 2NF.
EMP:
DEPT:
SALGRADE:
1. It is in 3NF.
EMP:
SALGRADE:
4o
You said:
Consider some example and create anamolies and then apply normal form give 10 for each
examples for above database
ChatGPT said:
To create anomalies in the database and resolve them using normal forms, we will consider
some examples of insertion, deletion, and update anomalies. These anomalies will
demonstrate how normalization (up to BCNF) resolves the issues.
1. Insertion Anomalies
2. Deletion Anomalies
Example Anomalies:
5. Deleting an employee with a specific salary grade leaves that grade unused.
3. Update Anomalies
Example Anomalies:
1. Updating a department name in one record but not others leads to inconsistencies.
8. Updating a record with duplicate values (e.g., same EMPNO) creates redundancies.
1. 1NF:
2. 2NF:
3. 3NF:
4. BCNF:
Example Resolutions:
1. Separate DEPT into its own table to avoid redundancy and inconsistencies.
sql
CopyEdit
DNAME VARCHAR(50),
LOC VARCHAR(50)
);
sql
CopyEdit
HISAL DECIMAL(10, 2)
);
3. Use foreign keys to maintain referential integrity between EMP and DEPT.
sql
CopyEdit
ALTER TABLE EMP ADD CONSTRAINT FK_DEPT FOREIGN KEY (DEPTNO) REFERENCES
DEPT(DEPTNO);