0% found this document useful (0 votes)
9 views6 pages

Model dbms-1

The document describes creating relational database tables for employees, departments, and incentives using SQL queries. It includes inserting sample data and queries to retrieve aggregated data from the tables such as average salary by department and highest paid employee.

Uploaded by

Hari Krishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Model dbms-1

The document describes creating relational database tables for employees, departments, and incentives using SQL queries. It includes inserting sample data and queries to retrieve aggregated data from the tables such as average salary by department and highest paid employee.

Uploaded by

Hari Krishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DATE:09/04/2024 MODEL EXAM

AIM:
To create the database using the relational schema
EMPLOYEE ( Emp_id, First_name, Last_name, Salary, Joining_date, Dept_id )
DEPARTMENT ( Dept_id, Dept_name, Dept_incharge )
INCENTIVES ( Emp_id, Incentive_date, Incentive_amount )

PROCEDURE:
CREATE TABLE DEPARTMENT (
Dept_id INT PRIMARY KEY,
Dept_name VARCHAR(255),
Dept_incharge VARCHAR(255)
);
CREATE TABLE EMPLOYEE (
Emp_id INT PRIMARY KEY,
First_name VARCHAR(255),
Last_name VARCHAR(255),
Salary DECIMAL(10, 2),
Joining_date DATE,
Dept_id INT,
FOREIGN KEY (Dept_id) REFERENCES DEPARTMENT(Dept_id)
);

CREATE TABLE INCENTIVES (


Emp_id INT,
Incentive_date DATE,
Incentive_amount DECIMAL(10, 2),
FOREIGN KEY (Emp_id) REFERENCES EMPLOYEE(Emp_id)
);

INSERT INTO DEPARTMENT (Dept_id, Dept_name, Dept_incharge)VALUES(1, 'Engineering',


'Aruna Sakthi');
INSERT INTO DEPARTMENT (Dept_id, Dept_name, Dept_incharge)VALUES(2, 'Marketing', 'Jane
Smith');
INSERT INTO DEPARTMENT (Dept_id, Dept_name, Dept_incharge)VALUES(3, 'Finance', 'Alice
Johnson');

INSERT INTO EMPLOYEE (Emp_id, First_name, Last_name, Salary, Joining_date,


Dept_id)VALUES(1, 'Swetha', 'Rameshwar', 60000.00, '01-01-2022', 1);
INSERT INTO EMPLOYEE (Emp_id, First_name, Last_name, Salary, Joining_date,
Dept_id)VALUES(2, 'Jane', 'Smith', 55000.00, '01-01-2022', 2);
INSERT INTO EMPLOYEE (Emp_id, First_name, Last_name, Salary, Joining_date,
Dept_id)VALUES(3, 'Alice', 'Johnson', 58000.00, '01-01-2022', 3);
INSERT INTO EMPLOYEE (Emp_id, First_name, Last_name, Salary, Joining_date,
Dept_id)VALUES(4, 'Bob', 'Brown', 52000.00, '01-01-2022', 1);
INSERT INTO EMPLOYEE (Emp_id, First_name, Last_name, Salary, Joining_date,
Dept_id)VALUES(5, 'Charlie', 'Davis', 59000.00, '01-01-2022', 2);
INSERT INTO EMPLOYEE (Emp_id, First_name, Last_name, Salary, Joining_date,
Dept_id)VALUES(6, 'David', 'Wilson', 54000.00, '01-01-2022', 3);

INSERT INTO INCENTIVES (Emp_id, Incentive_date, Incentive_amount)VALUES(1, '02-01-2022',


2000.00);
INSERT INTO INCENTIVES (Emp_id, Incentive_date, Incentive_amount)VALUES(2, '02-01-2022',
2500.00);
INSERT INTO INCENTIVES (Emp_id, Incentive_date, Incentive_amount)VALUES(3, '02-01-2022',
1800.00);
INSERT INTO INCENTIVES (Emp_id, Incentive_date, Incentive_amount)VALUES(4, '02-01-2022',
2200.00);
INSERT INTO INCENTIVES (Emp_id, Incentive_date, Incentive_amount)VALUES(5, '02-01-2022',
2100.00);
INSERT INTO INCENTIVES (Emp_id, Incentive_date, Incentive_amount)VALUES(6, '02-01-2022',
1900.00);
1. Get department wise minimum salary from employee table order by salary.
SELECT Dept_id, MIN(Salary) AS Min_Salary
FROM EMPLOYEE
GROUP BY Dept_id
ORDER BY Min_Salary;

2. Give the average salary received by each department employees.


SELECT e.Dept_id, AVG(e.Salary) AS Avg_Salary
FROM EMPLOYEE e
GROUP BY e.Dept_id;

3. Get Employee ID's of those employees who didn't receive incentives without using sub query.
SELECT e.Emp_id
FROM EMPLOYEE e
LEFT JOIN INCENTIVES i ON e.Emp_id = i.Emp_id
WHERE i.Emp_id IS NULL;
4. Update the incentives of 'Your Name' to 20,000.
UPDATE INCENTIVES
SET Incentive_amount = 20000
WHERE Emp_id = '1';
SELECT * FROM INCENTIVES;

5. List the employees who are from the same department as ‘Alice’.
SELECT e.First_name, e.Last_name
FROM EMPLOYEE e
JOIN EMPLOYEE a ON e.Dept_id = a.Dept_id
WHERE a.First_name = 'Alice';

6. Increment the least incentive received by of each employee by 10%.


UPDATE INCENTIVES
SET Incentive_amount = Incentive_amount * 1.10
WHERE (Emp_id, Incentive_amount) IN (
SELECT i.Emp_id, MIN(i.Incentive_amount)
FROM INCENTIVES i
GROUP BY i.Emp_id
);
SELECT * FROM INCENTIVES;
7. Display the employee ID and Name of Employee(First name & Last name) who is getting high
incentive.

SELECT e.Emp_id, e.First_name, e.Last_name


FROM EMPLOYEE e
JOIN INCENTIVES i ON e.Emp_id = i.Emp_id
WHERE i.Incentive_amount = (
SELECT MAX(Incentive_amount)
FROM INCENTIVES
);

8. Display the total Salary of Employee.


SELECT e.Emp_id, e.First_name, e.Last_name, e.Salary, SUM(i.Incentive_amount) AS
Total_Incentive
FROM EMPLOYEE e
LEFT JOIN INCENTIVES i ON e.Emp_id = i.Emp_id
GROUP BY e.Emp_id, e.First_name, e.Last_name, e.Salary;
RESULT:
Thus for the given schema output is verified and executed successfully.

You might also like