Employee & Project Management System (SQL Lab) π¶
Mentor's Note: This is a "Grand Finale" lab. It combines everything you've learnedβDDL, DML, Joins, and Subqueriesβinto one realistic project. Master these 23 queries, and you will be ready for any RDBMS exam! π‘
π The Scenario: The Tech Startup π’¶
Imagine you are the Database Administrator for a new tech startup in Surat.
- The Data: You have three main sets of information:
- Departments π’ (IT, HR, Sales).
- Employees π₯ (The people working there).
- Projects π» (The work being done).
- The Challenge: The CEO wants complex reports like "Who is working on what?" or "Which department has the highest budget?"
- The Result: You use SQL to link these tables and provide instant answers. β
π¨ Visual Logic: The Database Schema¶
erDiagram
DEPARTMENT ||--o{ EMPLOYEE : "has"
DEPARTMENT ||--o{ PROJECT : "manages"
DEPARTMENT {
number dept_id PK
string dept_name
string location
}
EMPLOYEE {
number emp_id PK
string emp_name
number salary
number dept_id FK
}
PROJECT {
number project_id PK
string project_name
number dept_id FK
}
π» Step 1: Building the Structure (DDL)¶
-- ποΈ Creating the Tables
CREATE TABLE DEPARTMENT (
dept_id NUMBER(5) PRIMARY KEY,
dept_name VARCHAR2(50) NOT NULL,
location VARCHAR2(50)
);
CREATE TABLE EMPLOYEE (
emp_id NUMBER(5) PRIMARY KEY,
emp_name VARCHAR2(50) NOT NULL,
salary NUMBER(10, 2),
dept_id NUMBER(5) REFERENCES DEPARTMENT(dept_id)
);
CREATE TABLE PROJECT (
project_id NUMBER(5) PRIMARY KEY,
project_name VARCHAR2(50) NOT NULL,
dept_id NUMBER(5) REFERENCES DEPARTMENT(dept_id)
);
π» Step 2: Adding the Team (DML)¶
-- π₯ Stocking the tables with sample data
INSERT INTO DEPARTMENT VALUES (101, 'IT', 'Bangalore');
INSERT INTO DEPARTMENT VALUES (102, 'HR', 'Mumbai');
INSERT INTO DEPARTMENT VALUES (103, 'Finance', 'Surat');
INSERT INTO DEPARTMENT VALUES (104, 'Admin', 'Surat');
INSERT INTO DEPARTMENT VALUES (105, 'Sales', 'Delhi');
INSERT INTO EMPLOYEE VALUES (1, 'Arjun', 55000, 101);
INSERT INTO EMPLOYEE VALUES (2, 'Priya', 62000, 101);
INSERT INTO EMPLOYEE VALUES (3, 'Rahul', 28000, 102);
INSERT INTO EMPLOYEE VALUES (4, 'Sneha', 35000, 103);
INSERT INTO EMPLOYEE VALUES (5, 'Vikram', 45000, NULL);
INSERT INTO PROJECT VALUES (501, 'Website Redesign', 101);
INSERT INTO PROJECT VALUES (502, 'Employee Portal', 102);
INSERT INTO PROJECT VALUES (503, 'Tax Filing', 103);
INSERT INTO PROJECT VALUES (504, 'Office Reno', 104);
INSERT INTO PROJECT VALUES (505, 'Legacy Migration', 101);
π» Step 3: Query Solutions (The 23 Challenges) π§©¶
πΉ Basic Joins & Display¶
- Display employee name and department name.
- List employees working in the "IT" department.
- Display employee details along with department location.
- Show project name with corresponding department name.
πΉ Outer Joins (Handling NULLs)¶
- Display all departments including those with no employees.
- Show all employees even if they are not assigned to any department.
- Display all projects including departments with no projects.
- Display all employees and departments, even if no matching records exist.
πΉ Filtering & Sorting¶
- Display employees earning more than βΉ30,000 sorted by salary.
- Display employee details who belong to departments located in βSuratβ.
- Display department-wise employee list ordered by employee name.
- Display employees who are not earning the maximum salary in their department.
πΉ Aggregates (Count, Sum, Max)¶
- Display department-wise total salary.
- Display number of employees in each department.
- Display department name and total number of employees.
- Display department name and project count for each department.
πΉ Complex Joins & Logic¶
- Display employee name, department name, and department location.
- Display project name along with employee names working in the same department.
- Display employee name and project name where employee department matches project department.
- Display employees working in departments that have projects assigned.
- Display employees whose department ID exists in both DEPARTMENT and PROJECT tables.
- Display project details for departments that have more than one employee.
- Display employee name, salary, and department name for employees earning more than the average salary.
π Sample Dry Run (Query 23)¶
Avg Salary = 48,000
| Employee | Salary | Salary > 48k? | Result |
|---|---|---|---|
| Arjun | 55,000 | β Yes | SHOW π |
| Priya | 62,000 | β Yes | SHOW π |
| Rahul | 28,000 | β No | HIDE ποΈ |
π‘ Interview Tip & Board Focus π¶
"Interviewers love this specific schema. Be prepared to explain Foreign Key constraints and what happens if you try to delete a Department that still has Employees. (Answer: It fails unless you use
ON DELETE CASCADE)!"
π‘ Pro Tip: "One step at a time is enough for me." - Mahatma Gandhi