Dbms Imp Quest Ans
Dbms Imp Quest Ans
This table is not in 1NF because the Courses column contains a list of courses,
which is not atomic.
To convert it into 1NF, we need to ensure each field contains only atomic
values:
1 Alice Math
1 Alice English
2 Bob Science
2 Bob History
This table is not in 2NF because ProductName and UnitPrice depend only on
ProductID, not on the entire primary key OrderID and ProductID.
To convert it into 2NF, we split the table into two:
• Orders:
1 101 4
2 102 2
• Products:
1 John 10 Sales
2 Jane 20 HR
This table is not in 3NF because DeptName depends on DeptID, which in turn
depends on EmpID.
To convert it into 3NF, we split the table into two:
• Employees:
1 John 10
2 Jane 20
• Departments:
DeptID DeptName
10 Sales
20 HR
Citations
This explanation is based on the provided material from the uploaded PDF
files, specifically covering the sections on normalization and the definitions of
1NF, 2NF, and 3NF(Module 4 VC).
2) 2. Retrieve the name and address of all employees who work for the
‘Research’ department
sql
Copy code
SELECT E.name, E.address
FROM EMPLOYEE E, DEPARTMENT D
WHERE E.department_id = D.id AND D.name = 'Research';
3. List all project numbers for projects that involve an employee whose last
name is ‘Smith’
sql
Copy code
SELECT DISTINCT P.project_number
FROM PROJECT P, WORKS_ON W, EMPLOYEE E
WHERE P.project_number = W.project_number AND W.ssn = E.ssn AND
E.lname = 'Smith';
4. List the names of managers who have at least one dependent
sql
Copy code
SELECT E.Fname, E.Lname
FROM EMPLOYEE E
WHERE EXISTS (SELECT * FROM DEPENDENT D WHERE E.Ssn = D.Essn)
AND EXISTS (SELECT * FROM DEPARTMENT DEPT WHERE E.Ssn =
DEPT.Mgr_ssn);
5. Find the sum of the salaries of all employees, the maximum salary, the
minimum salary, and the average salary
sql
Copy code
SELECT SUM(Salary), MAX(Salary), MIN(Salary), AVG(Salary)
FROM EMPLOYEE;
6. For each project, retrieve the project number, the project name, and the
number of employees who work on that project
sql
Copy code
SELECT P.project_number, P.project_name, COUNT(*)
FROM PROJECT P, WORKS_ON W
WHERE P.project_number = W.project_number
GROUP BY P.project_number, P.project_name;
7. List the names of all employees who have two or more dependents
sql
Copy code
SELECT E.Lname, E.Fname
FROM EMPLOYEE E
WHERE (SELECT COUNT(*) FROM DEPENDENT D WHERE E.Ssn = D.Essn) >= 2;
8. Retrieve the names and addresses of employees who work on at least one
project located in ‘Houston’
sql
Copy code
SELECT DISTINCT E.name, E.address
FROM EMPLOYEE E, WORKS_ON W, PROJECT P
WHERE E.ssn = W.ssn AND W.project_number = P.project_number AND
P.location = 'Houston';
9. For each department, retrieve the department number, the number of
employees, and their average salary
sql
Copy code
SELECT D.department_number, COUNT(*), AVG(E.salary)
FROM DEPARTMENT D, EMPLOYEE E
WHERE D.department_number = E.department_number
GROUP BY D.department_number;
10. Retrieve the names of employees who do not have any dependents
sql
Copy code
SELECT E.name
FROM EMPLOYEE E
WHERE NOT EXISTS (SELECT * FROM DEPENDENT D WHERE E.ssn = D.ssn);
11. Retrieve the department names and the number of employees working in
each department
sql
Copy code
SELECT D.department_name, COUNT(*)
FROM DEPARTMENT D, EMPLOYEE E
WHERE D.department_id = E.department_id
GROUP BY D.department_name;
12. Retrieve the names of employees who are directly supervised by ‘John
Smith’
sql
Copy code
SELECT E.name
FROM EMPLOYEE E, EMPLOYEE S
WHERE E.supervisor_ssn = S.ssn AND S.name = 'John Smith';
13. Retrieve the names of employees who work on all projects that ‘John
Smith’ works on
sql
Copy code
SELECT DISTINCT E.name
FROM EMPLOYEE E
WHERE NOT EXISTS (
SELECT P.project_number
FROM PROJECT P, WORKS_ON W, EMPLOYEE S
WHERE P.project_number = W.project_number AND W.ssn = S.ssn AND
S.name = 'John Smith'
EXCEPT
SELECT P.project_number
FROM PROJECT P, WORKS_ON W
WHERE P.project_number = W.project_number AND W.ssn = E.ssn
);
14. For each project, retrieve the project number, the project name, and the
total hours worked on that project
sql
Copy code
SELECT P.project_number, P.project_name, SUM(W.hours)
FROM PROJECT P, WORKS_ON W
WHERE P.project_number = W.project_number
GROUP BY P.project_number, P.project_name;
15. Retrieve the names of employees who have no supervisors
sql
Copy code
SELECT E.name
FROM EMPLOYEE E
WHERE E.supervisor_ssn IS NULL;
16. Retrieve the names of employees who work in the department that has
the employee with the highest salary
sql
Copy code
SELECT E.name
FROM EMPLOYEE E, DEPARTMENT D
WHERE E.department_id = D.department_id
AND D.department_id = (
SELECT E1.department_id
FROM EMPLOYEE E1
ORDER BY E1.salary DESC
LIMIT 1
);
17. Retrieve the names of employees who have the same job title as their
supervisor
sql
Copy code
SELECT E.name
FROM EMPLOYEE E, EMPLOYEE S
WHERE E.supervisor_ssn = S.ssn AND E.job_title = S.job_title;
18. Retrieve the names of employees who have changed their job title
sql
Copy code
SELECT DISTINCT E.name
FROM EMPLOYEE_HISTORY EH, EMPLOYEE E
WHERE EH.ssn = E.ssn AND EH.job_title <> E.current_job_title;
19. Retrieve the names of employees who work on more than one project
sql
Copy code
SELECT E.name
FROM EMPLOYEE E, WORKS_ON W
WHERE E.ssn = W.ssn
GROUP BY E.name
HAVING COUNT(W.project_number) > 1;
20. Retrieve the name of the department with the highest total salary
sql
Copy code
SELECT D.department_name
FROM DEPARTMENT D, EMPLOYEE E
WHERE D.department_id = E.department_id
GROUP BY D.department_name
ORDER BY SUM(E.salary) DESC
LIMIT 1;
These answers are based on the SQL syntax and examples provided in the
documents you uploaded
12. Explain with the neat diagram the state transition diagram of a
Transaction
Transaction States and Additional Operations
A transaction is an atomic unit of work that should either be completed in its
entirety or
not done at all. For recovery purposes, the system keeps track of start of a
transaction,
termination, commit or aborts.
BEGIN_TRANSACTION: marks the beginning of transaction execution
READ or WRITE: specify read or write operations on the database items that
are
executed as part of a transaction
END_TRANSACTION: specifies that READ and WRITE transaction operations
have
ended and marks the end of transaction execution
COMMIT_TRANSACTION: signals a successful end of the transaction so that
any
changes (updates) executed by the transaction can be safely committed to the
database and will not be undone
ROLLBACK: signals that the transaction has ended unsuccessfully, so that any
changes or effects that the transaction may have applied to the database must
be
undone
A transaction goes into active state immediately after it starts execution and
can
execute read and write operations.
When the transaction ends it moves to partially committed state.
At this end additional checks are done to see if the transaction can be
committed or not.
If these checks are successful the transaction is said to have reached commit
point and
enters committed state. All the changes are recorded permanently in the db.
A transaction can go to the failed state if one of the checks fails or if the
transaction is
aborted during its active state. The transaction may then have to be rolled
back to undo
the effect of its write operation.
Terminated state corresponds to the transaction leaving the system. All the
information
about the transaction is removed from system tables.