0% found this document useful (0 votes)
41 views48 pages

Database Management Lab File CSE201

This lab file outlines a series of practical experiments and assignments related to Database Management Systems, submitted by Yash Vardhan to Dr. Juhi Singh. It includes various SQL commands and queries for implementing Data Definition Language (DDL) and Data Manipulation Language (DML), as well as tasks involving constraints, keys, aggregate functions, and joins. The document serves as a comprehensive guide for students to apply their knowledge of databases using Oracle Database Express Edition.

Uploaded by

pranuchandra44
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)
41 views48 pages

Database Management Lab File CSE201

This lab file outlines a series of practical experiments and assignments related to Database Management Systems, submitted by Yash Vardhan to Dr. Juhi Singh. It includes various SQL commands and queries for implementing Data Definition Language (DDL) and Data Manipulation Language (DML), as well as tasks involving constraints, keys, aggregate functions, and joins. The document serves as a comprehensive guide for students to apply their knowledge of databases using Oracle Database Express Edition.

Uploaded by

pranuchandra44
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

A

LAB FILE
ON
DATABASE MANAGEMENT SYSTEMS
(COURSE CODE – CSE201)

SUBMITTED BY: SUBMITTED TO:


Name: Yash Vardhan Dr. Juhi Singh
Enrollment No.: A2305223479 Assistant Professor
Class & Section: 5CSE-7Y CSE Department

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


AMITY SCHOOL OF ENGINEERING AND TECHNOLOGY
AMITY UNIVERSITY UTTAR PRADESH, NOIDA
SESSION: ODD SEM (2025-26)
Index

Student Name: Yash Vardhan Enrollment No.: A2305223479

[Link]. EXPERIMENT NAME DATE SIGNATURE

1 To implement Data Definition Language (DDL) 23/07/2025


Commands

2 To implement Data Manipulation Language (DML) 30/07/2025


Commands

3 To implement SQL Commands using Constraints and 13/08/2025


Keys

4 To implement SQL Commands using Aggregate 20/08/2025


Functions

5 To implement SQL Commands using JOINS and 27/08/2025


HAVING Clause

6 Assignment 1 03/09/2025

7 Assignment 2 10/08/2025

8 Assignment 3 17/09/2025

9 To implement Views in SQL 01/10/2025

10 To implement PL/SQL Commands 15/10/2025

11 To implement Cursors and Triggers in PL/SQL 22/10/2025


PRACTICAL 1

Aim: To implement Data Definition Language (DDL) commands.


Software Used: Oracle Database Express Edition

Code and Outputs:


(i) Creating a table

CREATE TABLE Students (


student_id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
course VARCHAR(50)
);

(ii) Altering the table: Add a column

ALTER TABLE Students ADD email VARCHAR(100);

(iii) Altering the table: Drop a column

ALTER TABLE Students DROP COLUMN email;


(iv) Altering the table: Rename the table

ALTER TABLE Students RENAME TO Student_Info;


PRACTICAL 2

Aim: To implement Data Manipulation Language (DML) commands.


Software Used: Oracle Database Express Edition

Code and Outputs:


(i) Inserting data:

INSERT INTO Student_Info (student_id, name, age, course)


VALUES (1, 'Rahul', 21, 'Computer Science'),
(2, 'Smakhisha', 22, 'Geoinformatics'),
(3, 'Amit', 20, 'Mathematics');
SELECT * FROM Student_Info;

(ii) Updating data:

UPDATE Student_Info SET age = 23 WHERE student_id = 2;


SELECT * FROM Student_Info;

(iii) Deleting data:

DELETE FROM Student_Info WHERE student_id = 3;


SELECT * FROM Student_Info;
PRACTICAL 3

Aim: To implement SQL commands using constraints and keys.


Software Used: Oracle Database Express Edition

Code and Outputs:


(i) Creating Department and Employee tables:

CREATE TABLE Department (


dept_id INT PRIMARY KEY,
dept_name VARCHAR(50) UNIQUE
);

CREATE TABLE Employee (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
age INT CHECK (age > 18),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Department(dept_id)
);

(ii) Inserting data into Department:

INSERT INTO Department VALUES (101, 'IT'), (102, 'HR');


SELECT * FROM Department;

(iii) Inserting data into Employee:

INSERT INTO Employee VALUES


(1, 'Anjali', 25, 101),
(2, 'Rahul', 30, 101),
(3, 'Priya', 28, 102);
SELECT * FROM Employee;
PRACTICAL 4

Aim: To implement SQL commands using aggregate functions.


Software Used: Oracle Database Express Edition

Code and Outputs:

(i) Count total employees:

SELECT COUNT(*) AS total_employees FROM Employee;

(ii) Average age of employees:

SELECT AVG(age) AS avg_age FROM Employee;

(iii) Oldest and youngest employees:

SELECT MAX(age) AS oldest, MIN(age) AS youngest FROM Employee;


PRACTICAL 5

Aim: To implement SQL commands using joins and HAVING clause.


Software Used: Oracle Database Express Edition

Code and Outputs:

(i) Inner Join Employee and Department:

SELECT e.emp_id, e.emp_name, d.dept_name


FROM Employee e
JOIN Department d ON e.dept_id = d.dept_id;

(ii) Group by department to count employees:

SELECT d.dept_name, COUNT(e.emp_id) AS emp_count


FROM Department d
JOIN Employee e ON d.dept_id = e.dept_id
GROUP BY d.dept_name;

(iii) Using HAVING to filter departments with more than 1 employee:

SELECT d.dept_name, COUNT(e.emp_id) AS emp_count


FROM Department d
JOIN Employee e ON d.dept_id = e.dept_id
GROUP BY d.dept_name
HAVING COUNT(e.emp_id) > 1;
Assignment 1

Aim: To implement a relational database system by creating tables, defining


relationships with constraints and keys, and executing SQL queries. The purpose
is to apply Data Definition Language (DDL) and Data Manipulation Language
(DML) commands, use aggregate functions, perform joins, and format query
outputs to retrieve and analyse meaningful information from the database.

Software Used: Oracle Database Express Edition

Question 1: Create the given tables and solve given queries.

Code and Outputs:

(i) Creating tables Course, Course_fee, Student, Installment, and


Course_taken

CREATE TABLE Course (


course_no CHAR(4) PRIMARY KEY,
course_name VARCHAR(20)
);

CREATE TABLE Course_fee (


course_no CHAR(4),
full_part CHAR(1),
fees INT(10),
PRIMARY KEY(course_no, full_part),
FOREIGN KEY(course_no) REFERENCES Course(course_no)
);

CREATE TABLE Student (


prospectus_no INT(10) PRIMARY KEY,
name VARCHAR(20),
address VARCHAR(30),
phone_no BIGINT(11),
D_O_B DATE,
total_amt DECIMAL(10,2),
amt_paid DECIMAL(10,2),
installment CHAR(1)
);

CREATE TABLE Installment (


prospectus_no INT(10),
installment_amt DECIMAL(10,2),
due_dt DATE,
paid CHAR(1),
PRIMARY KEY(prospectus_no, due_dt),
FOREIGN KEY(prospectus_no) REFERENCES Student(prospectus_no) ON
DELETE CASCADE
);
CREATE TABLE Course_taken (
prospectus_no INT(10),
course_no CHAR(4),
start_dt DATE,
full_part CHAR(1),
time_slot CHAR(2),
performance VARCHAR(20),
FOREIGN KEY(prospectus_no) REFERENCES Student(prospectus_no),
FOREIGN KEY(course_no) REFERENCES Course(course_no)
);

(ii) Inserting data into Course, Course_fee, Student, Installment, and


Course_taken

INSERT INTO Course VALUES ('100', 'History'), ('101', 'Geography');

INSERT INTO Course_fee VALUES


('100', 'F', 8000), ('100', 'P', 4000),
('101', 'F', 10000), ('101', 'P', 5000);

INSERT INTO Student VALUES


(1, 'Anjali', 'Mumbai', 90000000001, '2005-05-12', 8050.75, 4025.50, 'I'),
(2, 'Rohan', 'Lucknow', 90000000002, '2004-07-23', 10050.25, 5050.75, 'F');

INSERT INTO Installment VALUES


(1, 4025.50, '2025-09-01', 'P'),
(2, 5050.75, '2025-09-05', 'U');

INSERT INTO Course_taken VALUES


(1, '100', '2025-08-01', 'F', 'M1', 'Good'),
(2, '101', '2025-08-05', 'P', 'E2', 'Excellent');

SQL Queries:

1. Retrieve name and course no of all students.

SELECT [Link], c.course_no


FROM Student s
JOIN Course_taken c ON s.prospectus_no = c.prospectus_no;
2. List names of students who have paid the full amount at admission.

SELECT name
FROM Student
WHERE total_amt = amt_paid;

3. Find names of students starting with 'A'.

SELECT name
FROM Student
WHERE name LIKE 'A%';

4. Print names of students whose total amount is not equal to amount paid.

SELECT name
FROM Student
WHERE total_amt != amt_paid;

5. Count no. of students who joined in current year/month.

SELECT COUNT(*) AS total_students


FROM Student
WHERE YEAR(D_O_B) = YEAR(CURDATE())
OR MONTH(D_O_B) = MONTH(CURDATE());
6. Determine maximum and minimum course fees.
SELECT MAX(fees) AS max_fee, MIN(fees) AS min_fee
FROM Course_fee;

7. Increase fee of History by 50%.

UPDATE Course_fee
SET fees = fees * 1.5
WHERE course_no = '100';
SELECT * FROM Course_fee;

8. Print details of courses whose fees are between 5000 and 10000.

SELECT * FROM Course_fee


WHERE fees BETWEEN 5000 AND 10000;
9. Display admission date in Date, Month, Year format.

SELECT name, DATE_FORMAT(D_O_B, '%d-%M-%Y') AS admission_date


FROM Student;

10. Find course with maximum number of students.

SELECT c.course_name, COUNT(ct.prospectus_no) AS total_students


FROM Course_taken ct
JOIN Course c ON ct.course_no = c.course_no
GROUP BY c.course_name
ORDER BY total_students DESC
LIMIT 1;

11. Change course_name from 'Geography' to 'Geography Advanced'.

UPDATE Course
SET course_name = 'Geography Advanced'
WHERE course_name = 'Geography';
SELECT * FROM Course;

12. Display admission date in DD-MONTH-YYYY format.

SELECT name, DATE_FORMAT(D_O_B, '%d-%M-%Y') AS formatted_date


FROM Student;
13. Get sum of amount to be collected this month.

SELECT SUM(total_amt - amt_paid) AS total_due


FROM Student;

14. Find course with maximum students in current month.

SELECT c.course_name, COUNT(ct.prospectus_no) AS total_students


FROM Course_taken ct
JOIN Course c ON ct.course_no = c.course_no
WHERE MONTH(ct.start_dt) = MONTH(CURDATE())
GROUP BY c.course_name
ORDER BY total_students DESC
LIMIT 1;

15. Select students who have not yet paid full fees.

SELECT name
FROM Student
WHERE total_amt != amt_paid;
Question 2: Create the following tables and answer the queries: (Take
appropriate data types and relationships to define the columns and then insert
relevant data).

Code and Outputs:

(i) Creating tables Supplier, Parts, Project and SPJ.

CREATE TABLE Supplier (


sno INT PRIMARY KEY,
sname VARCHAR(50),
status INT,
city VARCHAR(50)
);

CREATE TABLE Parts (


pno INT PRIMARY KEY,
pname VARCHAR(50),
color VARCHAR(20),
weight DECIMAL(10,2),
city VARCHAR(50)
);

CREATE TABLE Project (


jno INT PRIMARY KEY,
jname VARCHAR(50),
city VARCHAR(50)
);

CREATE TABLE SPJ (


sno INT,
pno INT,
jno INT,
qty INT,
PRIMARY KEY (sno, pno, jno),
FOREIGN KEY (sno) REFERENCES Supplier(sno),
FOREIGN KEY (pno) REFERENCES Parts(pno),
FOREIGN KEY (jno) REFERENCES Project(jno)
);

(ii) Inserting data into Supplier, Parts, Project and SPJ.

INSERT INTO Supplier VALUES


(1, 'S1', 20, 'London'),
(2, 'S2', 30, 'Paris'),
(3, 'S3', 10, 'Paris'),
(4, 'S4', 20, 'London');
INSERT INTO Parts VALUES
(1, 'P1', 'Red', 12.5, 'London'),
(2, 'P2', 'Blue', 15.0, 'Paris'),
(3, 'P3', 'Green', 17.0, 'Rome'),
(4, 'P4', 'Red', 10.5, 'Paris');

INSERT INTO Project VALUES


(1, 'J1', 'London'),
(2, 'J2', 'Paris'),
(3, 'J3', 'Rome');

INSERT INTO SPJ VALUES


(1, 1, 1, 200),
(1, 2, 2, 150),
(2, 1, 1, 300),
(3, 3, 3, 100),
(4, 4, 2, 250);

SQL Queries:

1. Get sno values for suppliers who supply project j1.

SELECT DISTINCT sno


FROM SPJ
WHERE jno = 1;

2. Get sno values for suppliers who supply project j1 with part p1.

SELECT sno
FROM SPJ
WHERE jno = 1 AND pno = 1;
3. Get jname values for projects supplied by supplier s1.

SELECT DISTINCT [Link]


FROM Project j
JOIN SPJ sp ON [Link] = [Link]
WHERE [Link] = 1;

4. Get color values for parts supplied by supplier s1.

SELECT DISTINCT [Link]


FROM Parts p
JOIN SPJ sp ON [Link] = [Link]
WHERE [Link] = 1;

5. Get pno values for parts supplied to any project in London.

SELECT DISTINCT pno


FROM SPJ sp
JOIN Project j ON [Link] = [Link]
WHERE [Link] = 'London';

6. Get sno values for suppliers who supply project j1 with a red part.
SELECT DISTINCT [Link]
FROM SPJ sp
JOIN Parts p ON [Link] = [Link]
WHERE [Link] = 1 AND [Link] = 'Red';

7. Get sno values for suppliers who supply a London or Paris project with a
red part.

SELECT DISTINCT [Link]


FROM SPJ sp
JOIN Parts p ON [Link] = [Link]
JOIN Project j ON [Link] = [Link]
WHERE ([Link] = 'London' OR [Link] = 'Paris') AND [Link] = 'Red';

8. Get pno values for parts supplied to any project by a supplier in the same
city.

SELECT DISTINCT [Link]


FROM SPJ sp
JOIN Supplier s ON [Link] = [Link]
JOIN Project j ON [Link] = [Link]
WHERE [Link] = [Link];
9. Get pno values for parts supplied to any project in London by a supplier
in London.

SELECT DISTINCT [Link]


FROM SPJ sp
JOIN Supplier s ON [Link] = [Link]
JOIN Project j ON [Link] = [Link]
WHERE [Link] = 'London' AND [Link] = 'London';

10. Find project numbers (jno) supplied by suppliers from different cities.

SELECT DISTINCT [Link]


FROM Project j
WHERE EXISTS (
SELECT 1
FROM SPJ sp
JOIN Supplier s ON [Link] = [Link]
WHERE [Link] = [Link]
AND [Link] <> [Link]
);

11. Find city pairs where a supplier’s city supplies a project in another city.

SELECT DISTINCT [Link] AS supplier_city, [Link] AS project_city


FROM SPJ sp
JOIN Supplier s ON [Link] = [Link]
JOIN Project j ON [Link] = [Link];
12. Get sno values for suppliers who supply the same part to all projects.

SELECT sno
FROM SPJ
GROUP BY sno, pno
HAVING COUNT(DISTINCT jno) = (SELECT COUNT(*) FROM Project);

13. Get pno values for parts supplied to all projects in London.

SELECT pno
FROM SPJ sp
JOIN Project j ON [Link] = [Link]
WHERE [Link] = 'London'
GROUP BY pno
HAVING COUNT(DISTINCT [Link]) = (SELECT COUNT(*) FROM Project
WHERE city='London');

14. Find supplier names who supply at least one red part.

SELECT DISTINCT [Link]


FROM Supplier s
JOIN SPJ sp ON [Link] = [Link]
JOIN Parts p ON [Link] = [Link]
WHERE [Link] = 'Red';
15. Get total quantity of part p1 supplied by supplier s1.

SELECT SUM(qty) AS total_qty


FROM SPJ
WHERE sno = 1 AND pno = 1;

16. Get the total number of projects supplied by supplier s3.

SELECT COUNT(DISTINCT jno) AS total_projects


FROM SPJ
WHERE sno = 3;

17. Change color of all red parts to orange.

UPDATE Parts
SET color = 'Orange'
WHERE color = 'Red';

18. Get sname values for suppliers who supply to both projects j1 and j2.

SELECT [Link]
FROM Supplier s
JOIN SPJ sp ON [Link] = [Link]
WHERE [Link] IN (1,2)
GROUP BY [Link]
HAVING COUNT(DISTINCT [Link]) = 2;
19. Find (supplier city, part no, project city) triples.

SELECT DISTINCT [Link] AS supplier_city, [Link], [Link] AS project_city


FROM SPJ sp
JOIN Supplier s ON [Link] = [Link]
JOIN Project j ON [Link] = [Link];

20. Get jnames for those projects which are supplied by supplier S2.

SELECT DISTINCT [Link] AS supplier_city, [Link], [Link] AS project_city


FROM SPJ sp
JOIN Supplier s ON [Link] = [Link]
JOIN Project j ON [Link] = [Link];
Question 3: Create the required Tables.
Code and Outputs:
(i) Creating Tables Department, Employee and Salgrade.

CREATE TABLE Department (


deptno INT PRIMARY KEY,
dname VARCHAR(20),
location VARCHAR(20)
);

CREATE TABLE Employee (


empno INT PRIMARY KEY,
ename VARCHAR(20),
job VARCHAR(20),
mgr INT,
hiredate DATE,
sal DECIMAL(10,2),
comm DECIMAL(10,2),
deptno INT,
FOREIGN KEY (deptno) REFERENCES Department(deptno)
);

CREATE TABLE Salgrade (


grade INT PRIMARY KEY,
losal DECIMAL(10,2),
hisal DECIMAL(10,2)
);

(ii) Inserting Data into Department, Employee, Salgrade.

INSERT INTO Department VALUES


(10, 'Accounting', 'New York'),
(20, 'Research', 'Dallas'),
(30, 'Sales', 'Chicago'),
(40, 'Operations', 'Boston'),
(50, 'Systems', 'Delhi'),
(60, 'Marketing', 'Mumbai');

INSERT INTO Employee VALUES


(101, 'Smith', 'Clerk', 105, '1983-06-15', 800.00, NULL, 20),
(102, 'Allen', 'Salesman', 108, '1981-02-20', 1600.50, 300.00, 30),
(103, 'Ward', 'Salesman', 108, '1981-02-22', 1250.75, 500.00, 30),
(104, 'Jones', 'Manager', 109, '1981-04-02', 2975.20, NULL, 20),
(105, 'Blake', 'Manager', 109, '1981-05-01', 2850.00, NULL, 30),
(106, 'Clark', 'Manager', 109, '1981-06-09', 2450.00, NULL, 10),
(107, 'Scott', 'Analyst', 104, '1983-12-09', 3000.00, NULL, 20),
(108, 'King', 'President', NULL, '1981-11-17', 5000.00, NULL, 10),
(109, 'Miller', 'Clerk', 106, '1982-01-23', 1300.25, NULL, 10),
(110, 'Ravi', 'Analyst', 104, '1983-07-11', 3200.00, NULL, 50),
(111, 'Amit', 'Salesman', 105, '1984-05-19', 1800.90, 200.00, 60);

INSERT INTO Salgrade VALUES


(1, 700.00, 1200.00),
(2, 1201.00, 1400.00),
(3, 1401.00, 2000.00),
(4, 2001.00, 3000.00),
(5, 3001.00, 5000.00);

SQL Queries:
1. Display each employee name and hiredate of systems department.

SELECT [Link], [Link]


FROM Employee e
JOIN Department d ON [Link] = [Link]
WHERE [Link] = 'Systems';

2. Write query to calculate length of service of each employee.

SELECT ename, TIMESTAMPDIFF(YEAR, hiredate, CURDATE()) AS


years_of_service
FROM Employee;
3. Find the second maximum salary of all employees.

SELECT MAX(sal) AS second_max_salary


FROM Employee
WHERE sal < (SELECT MAX(sal) FROM Employee);

4. Display all employee name and department name in department name


order.

SELECT [Link], [Link]


FROM Employee e
JOIN Department d ON [Link] = [Link]
ORDER BY [Link];
5. Find the name of lowest paid employee for each manager.

SELECT mgr, ename, sal


FROM Employee e
WHERE sal = (
SELECT MIN(sal)
FROM Employee
WHERE mgr = [Link]
);

6. Display the department that has no employee.

SELECT [Link]
FROM Department d
LEFT JOIN Employee e ON [Link] = [Link]
WHERE [Link] IS NULL;

7. Find the employees who earn the maximum salary in each job type.

SELECT job, ename, sal


FROM Employee
WHERE sal IN (
SELECT MAX(sal)
FROM Employee
GROUP BY job
)
ORDER BY sal DESC;

8. In which year did most people join the company?

SELECT YEAR(hiredate) AS join_year, COUNT(*) AS total


FROM Employee
GROUP BY YEAR(hiredate)
ORDER BY total DESC
LIMIT 1;

9. Employees who earn greater than average of their department.

SELECT [Link], [Link], [Link]


FROM Employee e
JOIN Department d ON [Link] = [Link]
WHERE sal > (SELECT AVG(sal) FROM Employee WHERE deptno = [Link]);
10. List employees having salary between 10000 and 20000.

SELECT ename, sal


FROM Employee
WHERE sal BETWEEN 10000 AND 20000;

11. Employees hired during 1983.

SELECT ename, hiredate


FROM Employee
WHERE YEAR(hiredate) = 1983;

12. Update salaries of all employees in marketing department by 15%.

UPDATE Employee
SET sal = sal * 1.15
WHERE deptno = (SELECT deptno FROM Department WHERE dname =
'Marketing');

SELECT ename, sal FROM Employee WHERE deptno = 60;

13. Get gross salaries of all employees.

SELECT ename, sal + IFNULL(comm,0) AS gross_salary


FROM Employee;
14. Get names of employees and their manager’s name.

SELECT [Link] AS employee, [Link] AS manager


FROM Employee e
LEFT JOIN Employee m ON [Link] = [Link];

15. Employees earning >1500 with name, location, department

SELECT [Link], [Link], [Link]


FROM Employee e
JOIN Department d ON [Link] = [Link]
WHERE [Link] > 1500;
16. Show all employees in Dallas.

SELECT [Link], [Link]


FROM Employee e
JOIN Department d ON [Link] = [Link]
WHERE [Link] = 'Dallas';

17. List employees (except clerks) with job, salary, grade, dept.

SELECT [Link], [Link], [Link], [Link], [Link]


FROM Employee e
JOIN Department d ON [Link] = [Link]
JOIN Salgrade s ON [Link] BETWEEN [Link] AND [Link]
WHERE [Link] <> 'Clerk'
ORDER BY [Link];
18. Find employees earning minimum salary in their job.

SELECT job, ename, sal


FROM Employee e
WHERE sal = (SELECT MIN(sal) FROM Employee WHERE job = [Link])
ORDER BY sal DESC;

19. Find most recently hired employees in each department.

SELECT [Link], [Link], [Link]


FROM Employee e
JOIN Department d ON [Link] = [Link]
WHERE [Link] = (SELECT MAX(hiredate) FROM Employee WHERE deptno =
[Link])
ORDER BY [Link];

20. Find difference between highest and lowest salaries.

SELECT MAX(sal) - MIN(sal) AS salary_difference


FROM Employee;
ASSIGNMENT 2

Aim: To create a database named COMPANY consisting of two tables – EMP


(Employee) and DEPT (Department), insert the given records into these tables,
and perform SQL queries to retrieve and analyse employee information such as
job roles, hire dates, salaries, commissions, and departmental details.
Software Used: Oracle database Express Edition

Question: Create the COMPANY database with EMP and DEPT tables, insert
data, and query employee details by job, department, salary, and hire date.
Code and Outputs:
(i) Creating tables DEPT and EMP

CREATE TABLE DEPT (


DEPTNO INT PRIMARY KEY,
DNAME VARCHAR(20),
LOC VARCHAR(20)
);

CREATE TABLE EMP (


EMPNO INT PRIMARY KEY,
ENAME VARCHAR(20),
JOB VARCHAR(15),
MGR INT,
HIREDATE DATE,
SAL DECIMAL(10,2),
COMM DECIMAL(10,2),
DEPTNO INT,
FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO)
);

(ii) Inserting data into DEPT and EMP

INSERT INTO DEPT VALUES


(10, 'Accounting', 'New York'),
(20, 'Research', 'Dallas'),
(30, 'Sales', 'Chicago'),
(40, 'Operations', NULL);

INSERT INTO EMP VALUES


(7369,'Smith','Clerk',7902,'1980-12-17',800,NULL,20),
(7499,'Allen','Salesman',7698,'1981-02-20',1600,300,30),
(7521,'Ward','Salesman',7698,'1981-02-22',1250,500,30),
(7566,'Jones','Manager',7839,'1981-04-02',2975,NULL,20),
(7654,'Martin','Salesman',7698,'1981-09-28',1250,1400,30),
(7698,'Blake','Manager',7839,'1981-05-01',2850,NULL,30),
(7782,'Clark','Manager',7839,'1981-06-09',2450,NULL,10),
(7788,'Scott','Analyst',7566,'1982-12-09',3000,NULL,20),
(7839,'King','President',NULL,'1981-11-17',5000,NULL,10),
(7844,'Turner','Salesman',7698,'1981-09-08',1500,0,30),
(7876,'Adams','Clerk',7788,'1983-01-12',1100,NULL,20),
(7900,'James','Clerk',7698,'1981-12-03',950,NULL,30),
(7902,'Ford','Analyst',7566,'1981-12-04',3000,NULL,20),
(7934,'Miller','Clerk',7782,'1982-01-23',1300,NULL,10);

SQL Queries:
1. Names of Analysts and Salesmen.

SELECT ENAME FROM EMP WHERE JOB IN ('Analyst','Salesman');

2. Employees who joined before 30-Sep-81.

SELECT ENAME, HIREDATE FROM EMP WHERE HIREDATE < '1981-09-30';


3. Employees who are not Managers.

SELECT ENAME FROM EMP WHERE JOB <> 'Manager';

4. Employees with empno 7369, 7521, 7839, 7934, 7788.

SELECT ENAME FROM EMP WHERE EMPNO IN (7369,7521,7839,7934,7788);


5. Employees not in dept 30, 40, 10.

SELECT ENAME FROM EMP WHERE DEPTNO NOT IN (30,40,10);

6. Employees who joined between 30-Jun and 31-Dec 81.

SELECT ENAME, HIREDATE FROM EMP


WHERE HIREDATE BETWEEN '1981-06-30' AND '1981-12-31';

7. Different designations.

SELECT DISTINCT JOB FROM EMP;


8. Employees not eligible for commission.

SELECT ENAME FROM EMP WHERE COMM IS NULL OR COMM=0;

9. Employee who does not report to anybody.

SELECT ENAME, JOB FROM EMP WHERE MGR IS NULL;

10. Employees not assigned to any department.

SELECT ENAME FROM EMP WHERE DEPTNO IS NULL;

11. Employees eligible for commission.

SELECT ENAME FROM EMP WHERE COMM IS NOT NULL AND COMM>0;
12. Employees whose names start or end with “S”.

SELECT ENAME FROM EMP WHERE ENAME LIKE 'S%' OR ENAME LIKE
'%S';

13. Employees whose second character is “i”.

SELECT ENAME FROM EMP WHERE ENAME LIKE '_i%';

14. Number of employees.

SELECT COUNT(*) AS NumEmployees FROM EMP;

15. Number of designations.

SELECT COUNT(DISTINCT JOB) AS NumDesignations FROM EMP;


16. Total salary paid.

SELECT SUM(SAL) AS TotalSalary FROM EMP;

17. Max, Min, Avg salary.

SELECT MAX(SAL) AS MaxSalary, MIN(SAL) AS MinSalary, AVG(SAL) AS


AvgSalary FROM EMP;

18. Max salary paid to a salesman.

SELECT MAX(SAL) AS MaxSalesmanSalary FROM EMP WHERE


JOB='Salesman';
ASSIGNMENT 3

Aim: To create a database named COMPANY consisting of two tables – EMP


(Employee) and DEPT (Department), insert the given records into these tables,
and perform SQL queries to retrieve and analyse employee information such as
job roles, hire dates, salaries, commissions, and departmental details.
Software Used: Oracle database Express Edition

Question: Create the COMPANY database with EMP and DEPT tables, insert
data, and query employee details by job, department, salary, and hire date.
Code and Outputs:
(i) Creating tables DEPT and EMP

CREATE TABLE DEPT (


DEPTNO INT PRIMARY KEY,
DNAME VARCHAR(20),
LOC VARCHAR(20)
);

CREATE TABLE EMP (


EMPNO INT PRIMARY KEY,
ENAME VARCHAR(20),
JOB VARCHAR(15),
MGR INT,
HIREDATE DATE,
SAL DECIMAL(10,2),
COMM DECIMAL(10,2),
DEPTNO INT,
FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO)
);

(ii) Inserting data into DEPT and EMP

INSERT INTO DEPT VALUES


(10, 'Accounting', 'New York'),
(20, 'Research', 'Dallas'),
(30, 'Sales', 'Chicago'),
(40, 'Operations', NULL);

INSERT INTO EMP VALUES


(7369,'Smith','Clerk',7902,'1980-12-17',800,NULL,20),
(7499,'Allen','Salesman',7698,'1981-02-20',1600,300,30),
(7521,'Ward','Salesman',7698,'1981-02-22',1250,500,30),
(7566,'Jones','Manager',7839,'1981-04-02',2975,NULL,20),
(7654,'Martin','Salesman',7698,'1981-09-28',1250,1400,30),
(7698,'Blake','Manager',7839,'1981-05-01',2850,NULL,30),
(7782,'Clark','Manager',7839,'1981-06-09',2450,NULL,10),
(7788,'Scott','Analyst',7566,'1982-12-09',3000,NULL,20),
(7839,'King','President',NULL,'1981-11-17',5000,NULL,10),
(7844,'Turner','Salesman',7698,'1981-09-08',1500,0,30),
(7876,'Adams','Clerk',7788,'1983-01-12',1100,NULL,20),
(7900,'James','Clerk',7698,'1981-12-03',950,NULL,30),
(7902,'Ford','Analyst',7566,'1981-12-04',3000,NULL,20),
(7934,'Miller','Clerk',7782,'1982-01-23',1300,NULL,10);

SQL Queries:
1. Names of Analysts and Salesmen.

SELECT ENAME FROM EMP WHERE JOB IN ('Analyst','Salesman');

2. Employees who joined before 30-Sep-81.

SELECT ENAME, HIREDATE FROM EMP WHERE HIREDATE < '1981-09-30';


3. Employees who are not Managers.

SELECT ENAME FROM EMP WHERE JOB <> 'Manager';

4. Employees with empno 7369, 7521, 7839, 7934, 7788.

SELECT ENAME FROM EMP WHERE EMPNO IN (7369,7521,7839,7934,7788);


5. Employees not in dept 30, 40, 10.

SELECT ENAME FROM EMP WHERE DEPTNO NOT IN (30,40,10);

6. Employees who joined between 30-Jun and 31-Dec 81.

SELECT ENAME, HIREDATE FROM EMP


WHERE HIREDATE BETWEEN '1981-06-30' AND '1981-12-31';

7. Different designations.

SELECT DISTINCT JOB FROM EMP;


8. Employees not eligible for commission.

SELECT ENAME FROM EMP WHERE COMM IS NULL OR COMM=0;

9. Employee who does not report to anybody.

SELECT ENAME, JOB FROM EMP WHERE MGR IS NULL;

10. Employees not assigned to any department.

SELECT ENAME FROM EMP WHERE DEPTNO IS NULL;

11. Employees eligible for commission.

SELECT ENAME FROM EMP WHERE COMM IS NOT NULL AND COMM>0;
12. Employees whose names start or end with “S”.

SELECT ENAME FROM EMP WHERE ENAME LIKE 'S%' OR ENAME LIKE
'%S';

13. Employees whose second character is “i”.

SELECT ENAME FROM EMP WHERE ENAME LIKE '_i%';

14. Number of employees.

SELECT COUNT(*) AS NumEmployees FROM EMP;

15. Number of designations.

SELECT COUNT(DISTINCT JOB) AS NumDesignations FROM EMP;


16. Total salary paid.

SELECT SUM(SAL) AS TotalSalary FROM EMP;

17. Max, Min, Avg salary.

SELECT MAX(SAL) AS MaxSalary, MIN(SAL) AS MinSalary, AVG(SAL) AS


AvgSalary FROM EMP;

18. Max salary paid to a salesman.

SELECT MAX(SAL) AS MaxSalesmanSalary FROM EMP WHERE


JOB='Salesman';

You might also like