DBMSsolution
DBMSsolution
Create a table EMPLOYEE with following schema: (Emp_no, E_name, E_address, E_ph_no, Dept_no,
Dept_name,Job_id , Salary)
Below are the SQL queries to perform the requested tasks based on the EMPLOYEE table schema:
);
INSERT INTO EMPLOYEE (Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name, Job_id,
Salary)
VALUES
(1, 'Alice', '123 Main St', '123-456-7890', 'D10', 'HR', 'HR01', 55000.00),
(2, 'Bob', '456 Elm St', '987-654-3210', 'D20', 'IT', 'IT02', 75000.00),
(3, 'Charlie', '789 Oak St', '555-666-7777', 'D30', 'Finance', 'FIN01', 65000.00),
(4, 'David', '101 Pine St', '444-555-6666', 'D10', 'HR', 'HR02', 60000.00),
(5, 'James', '202 Birch St', '333-222-1111', 'D40', 'Sales', 'SLS01', 70000.00);
3. Display All Information in the EMPLOYEE Table
UPDATE EMPLOYEE
(Ensure there's an employee with Emp_no = 12 in the table for this query to take effect.)
Assuming the E_address or another column holds the email, replace its value with NULL:
UPDATE EMPLOYEE
ANS
);
INSERT INTO EMPLOYEE (Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name, Job_id,
Salary)
VALUES
UPDATE EMPLOYEE
If there is no employee with Emp_no = 12, ensure you add this record first.
Assuming E_address contains the email or city, and you want to delete this information:
UPDATE EMPLOYEE
Experiment No: 3
Below are the SQL statements for the described queries based on the provided schema:
1. List the Emp_no, E_name, and Salary of all employees working as MANAGER.
FROM EMPLOYEE
2. Display all the details of employees whose salary is more than the salary of any IT PROFF.
SELECT *
FROM EMPLOYEE
SELECT Salary
FROM EMPLOYEE
);
3. List the employees in ascending order of Designation who joined after 1981.
SELECT *
FROM EMPLOYEE
4. List the employees along with their Experience and Daily Salary.
Assuming Join_date exists and Experience is calculated as the difference between the current year
and the year of joining:
SELECT
E_name,
YEAR(CURDATE()) - YEAR(STR_TO_DATE(Join_date, '%d-%b-%y')) AS Experience,
Salary / 30 AS Daily_Salary
FROM EMPLOYEE;
SELECT *
FROM EMPLOYEE
6. List the employees who joined on specific dates (1-MAY-81, 3-DEC-81, 17-DEC-81, 19-JAN-80).
SELECT *
FROM EMPLOYEE
);
SELECT *
FROM EMPLOYEE
SELECT E_name
FROM EMPLOYEE
9. Display the name and the first five characters of names starting with H.
FROM EMPLOYEE
SELECT *
FROM EMPLOYEE
Experiment No: 4
ANS:
1. Display all the department numbers available with the DEPT and EMP tables, avoiding
duplicates.
Use the UNION operator to combine the department numbers from both tables and remove
duplicates:
SELECT Dept_no
FROM DEPT
UNION
SELECT Dept_no
FROM EMP;
2. Display all the department numbers available with the DEPT and EMP tables (including
duplicates).
Use the UNION ALL operator to combine the department numbers from both tables, including
duplicates:
SELECT Dept_no
FROM DEPT
UNION ALL
SELECT Dept_no
FROM EMP;
3. Display all department numbers available in EMP but not in DEPT, and vice versa.
SELECT Dept_no
FROM EMP
SELECT Dept_no
FROM DEPT
);
SELECT Dept_no
FROM DEPT
SELECT Dept_no
FROM EMP
);
FROM EMP
SELECT Dept_no
FROM DEPT
UNION
FROM DEPT
SELECT Dept_no
FROM EMP
);
Experiment No: 5
Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day(date))
ANS
1. Find all information of sailors who have reserved boat number 101.
SELECT *
FROM Sailors S
WHERE S.sid IN (
SELECT R.sid
FROM Reserves R
);
SELECT B.bname
FROM Boats B
3. Find the names of sailors who have reserved a red boat, and list in the order of age.
FROM Sailors S
ORDER BY S.age;
4. Find the names of sailors who have reserved at least one boat.
FROM Sailors S
5. Find the IDs and names of sailors who have reserved two different boats on the same day.
FROM Sailors S
6. Find the IDs of sailors who have reserved a red boat or a green boat.
FROM Reserves R
FROM Sailors S
WHERE S.age = (
SELECT MIN(S1.age)
FROM Sailors S1
);
FROM Sailors S;
9. Find the average age of sailors for each rating level.
FROM Sailors S
GROUP BY S.rating;
10. Find the average age of sailors for each rating level that has at least two sailors.
FROM Sailors S
GROUP BY S.rating
EXPERIMENT 6:
FROM Employee
GROUP BY Job;
FROM Employee
GROUP BY Manager_id;
WITH MinSalaryPerManager AS (
FROM Employee
GROUP BY Manager_id
SELECT E.*
FROM Employee E
JOIN MinSalaryPerManager M
3. Display the number of employees working in each department and their department name.
FROM Employee E
GROUP BY D.Dept_name;
SELECT *
FROM Employee
5. Show the record of employees earning a salary greater than 16000 in each department.
SELECT E.*
FROM Employee E
Example Queries:
GROUP BY: Query 1 (GROUP BY Job) and Query 3 (GROUP BY Dept_name) demonstrate
grouping for aggregate operations.
HAVING: Add conditions on grouped results (e.g., departments with more than 5
employees):
FROM Employee E
GROUP BY D.Dept_name
HAVING COUNT(E.Emp_id) > 5;
JOINS: Queries 3 and 5 use JOIN to combine Employee and Department tables.
Experiment No: 7
Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day(date))
ANS:
1. Find all information of sailors who have reserved boat number 101.
SELECT *
FROM Sailors
WHERE sid IN (
SELECT sid
FROM Reserves
);
SELECT bname
FROM Boats
WHERE bid IN (
SELECT bid
FROM Reserves
WHERE sid = (
SELECT sid
FROM Sailors
)
);
3. Find the names of sailors who have reserved a red boat, and list in the order of age.
SELECT sname
FROM Sailors
WHERE sid IN (
SELECT sid
FROM Reserves
WHERE bid IN (
SELECT bid
FROM Boats
ORDER BY age;
4. Find the names of sailors who have reserved at least one boat.
SELECT sname
FROM Sailors
WHERE sid IN (
FROM Reserves
);
5. Find the IDs and names of sailors who have reserved two different boats on the same day.
FROM Sailors
WHERE sid IN (
SELECT R1.sid
);
6. Find the IDs of sailors who have reserved a red boat or a green boat.
FROM Reserves
WHERE bid IN (
SELECT bid
FROM Boats
);
FROM Sailors
WHERE age = (
SELECT MIN(age)
FROM Sailors
);
FROM Sailors;
FROM Sailors
GROUP BY rating;
10. Find the average age of sailors for each rating level that has at least two sailors.
SELECT rating, AVG(age) AS Avg_Age
FROM Sailors
GROUP BY rating
Experiment No: 8
EMPNO NUMBER(6),
DEPTNO NUMBER(3),
CONSTRAINT chk_empno CHECK (EMPNO > 100), -- Constraint for EMPNO > 100
CONSTRAINT pk_emp PRIMARY KEY (EMPNO) -- Primary key constraint for EMPNO
);
If the constraint was not added during table creation, you can add it afterward:
If the primary key constraint was not added during table creation:
This will fail because both columns are defined as NOT NULL.
EXPERIMRNT 9.
1. Implementing a Savepoint
A Savepoint allows you to set a point within a transaction to which you can later roll back if needed.
Example Query:
BEGIN;
-- Create a Savepoint
SAVEPOINT sp1;
2. Implementing a Rollback
A Rollback undoes all the changes made after a specific savepoint or the start of a transaction.
Example Query:
ROLLBACK TO sp1;
-- At this point, the record for Bob will be undone, but the record for Alice remains.
ROLLBACK;
3. Implementing a Commit
A Commit makes all changes made in the current transaction permanent in the database.
Example Query:
-- At this point, the record for Alice is permanently saved in the database.
BEGIN;
SAVEPOINT sp1;
-- Rollback to Savepoint
ROLLBACK TO sp1;
COMMIT;
EXPERIMENT 10
1. Create a User
To allow the Dept table to have all privileges over the Emp table:
-- Create a user
-- Grant privileges