Create Database with name – CompanyDB
Tables:
1. Employees - Stores employee details
2. Departments - Stores department details
3. Projects - Stores project details
4. EmployeeProjects - Many-to-Many relation between Employees and Projects
5. Salaries - Stores salary details of employees
Q1. Table Creation Questions
1. Creating the Database:
o Write a SQL statement to create a database named CompanyDB and ensure
that all further queries use this database.
2. Departments Table:
o Create a table named Departments with the following columns:
▪ DepartmentID (Primary Key, Auto-increment, Integer)
▪ DepartmentName (Should not be NULL, String of 50 characters)
▪ Location (String of 50 characters)
3. Employees Table:
o Create a table named Employees with the following fields:
▪ EmployeeID (Primary Key, Auto-increment, Integer)
▪ FirstName (String of 50 characters)
▪ LastName (String of 50 characters)
▪ Email (Unique, String of 100 characters)
▪ HireDate (Date format)
▪ DepartmentID (Foreign Key referencing Departments)
o Ensure that if a department is deleted, the DepartmentID in the Employees
table is set to NULL.
4. Projects Table:
o Write a SQL statement to create a table Projects with the following columns:
▪ ProjectID (Primary Key, Auto-increment, Integer)
▪ ProjectName (String of 100 characters)
▪ Budget (Decimal format with 10 digits, 2 decimal places)
▪ StartDate (Date format)
▪ EndDate (Date format)
5. EmployeeProjects Table (Many-to-Many Relationship):
o Create a join table EmployeeProjects to establish a many-to-many
relationship between Employees and Projects.
o Include the following columns:
▪ EmployeeID (Foreign Key referencing Employees)
▪ ProjectID (Foreign Key referencing Projects)
▪ Role (String of 50 characters)
o Ensure that if an employee or project is deleted, their related records in
EmployeeProjects are also deleted.
6. Salaries Table:
o Create a table Salaries that stores salary information for employees with the
following columns:
▪ SalaryID (Primary Key, Auto-increment, Integer)
▪ EmployeeID (Foreign Key referencing Employees, must be unique)
▪ Salary (Decimal format with 10 digits, 2 decimal places)
▪ Bonus (Decimal format with 10 digits, 2 decimal places)
▪ EffectiveDate (Date format)
o Ensure that if an employee is deleted, their salary information is also
removed.
# Insert values given below
Q2. Questions to Test Constraints & Modifications
7. Modify the Employees Table:
o Add a new column PhoneNumber (String of 15 characters) to the Employees
table.
8. Drop & Alter Table Constraints:
o Write a query to remove the foreign key constraint between Employees and
Departments.
o Write a query to change the Salary column in Salaries to allow up to 12 digits
with 2 decimal places instead of 10.
9. Delete a Table:
o Write a SQL command to completely remove the EmployeeProjects table
from the database.
10. Truncate a Table (Reset Data without Dropping Structure):
o Write a SQL command to remove all data from the Projects table while
keeping its structure intact.
Q3. Scenario-Based Questions
11. Many-to-Many Relationship:
o Why do we need the EmployeeProjects table instead of adding a ProjectID
column in Employees?
12. Foreign Key Behavior:
o If a department is deleted, what happens to the employees in that
department based on the ON DELETE SET NULL rule?
13. Cascading Deletion:
o If an employee is deleted, what happens to their salary record and project
assignments?
14. Unique Constraint:
o Why is the Email column in Employees marked as UNIQUE?
15. Normalization & Efficiency:
o Why do we store Salary in a separate table instead of keeping it in
Employees?
Q1. Retrieve employee details along with department names
Q2. Retrieve project details along with employees working on them
Q3. Find the number of employees in each department
Q4. Find the total salary expense per department
Q5. List employees sorted by their salary in descending order
Q6. Employees in the IT or HR department hired after 2019
Q7. Get a list of all employees and project managers (distinct results)
**** Scenario-Based Queries
Scenario 1: Find Employees Without Any Projects
Scenario 2: Find Top 2 Highest-Paid Employees
Scenario 3: Calculate the Average Salary in Each Department
Scenario 4: Employees Working on More Than One Project