dbms practical file
dbms practical file
LAB ASSIGNMENT - 1
Q1. Introduction to E-R Diagram, Design an E-R Diagram for the Student-
Project, Patient-Doctor Database.
SOL:
Introduction to E-R Diagram
An Entity-Relationship (E-R) Diagram is a visual tool used in database design
to represent the structure of a database. It illustrates the entities (objects or
concepts), their attributes (properties), and the relationships between them. E-R
diagrams are essential for understanding the data requirements and designing a
relational database.
1. Student-Project Database
Relationships:
Assign:
Links Student and Project.
A student can be assigned to one or more projects, and a project can be
assigned to one or more students (many-to-many relationship).
2. Patient-Doctor Database
2. Doctor:
Attributes: Doc.ID (Primary Key), Name, Specialist, Dep.id (Foreign Key)
3. Department:
Attributes: Dep.ID (Primary Key), Name, Location, Head
Relationships:
Consult: Links Patient, Doctor, and Department.
Attributes: P.ID (from Patient), Doc.ID (from Doctor), Dep.ID (from
Department)
DBMS 102P 04321102024
DBMS 102P 04321102024
LAB ASSIGNMENT – 2
Sol:
DBMS 102P 04321102024
LAB ASSIGNMENT - 3
Sol:
1. STUDENT TABLE:
CMD:
CREATE TABLE Student (
USN VARCHAR(20) PRIMARY KEY,
SName VARCHAR(100) NOT NULL,
Address VARCHAR(255),
Phone VARCHAR(15),
Gender CHAR(1)
);
Output:
2. SEMSEC TABLE:
CMD:
CREATE TABLE SEMSEC (
SSID VARCHAR(20) PRIMARY KEY,
Sem INT,
Sec VARCHAR(10)
);
Output:
DBMS 102P 04321102024
3. CLASS TABLE:
CMD:
CREATE TABLE Class (
USN VARCHAR(20),
SSID VARCHAR(20),
PRIMARY KEY (USN, SSID),
FOREIGN KEY (USN) REFERENCES Student(USN),
FOREIGN KEY (SSID) REFERENCES SEMSEC(SSID)
);
Output:
4. SUBJECT TABLE:
CMD:
CREATE TABLE Subject (
Subcode VARCHAR(20) PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
Sem INT,
Credits INT
);
Output:
DBMS 102P 04321102024
5. IAMARKS TABLE:
CMD:
CREATE TABLE IAMARKS (
USN VARCHAR(20),
Subcode VARCHAR(20),
SSID VARCHAR(20),
Test1 INT,
Test2 INT,
Test3 INT,
FinalIA INT,
PRIMARY KEY (USN, Subcode, SSID),
FOREIGN KEY (USN) REFERENCES Student(USN),
FOREIGN KEY (Subcode) REFERENCES Subject(Subcode),
FOREIGN KEY (SSID) REFERENCES SEMSEC(SSID)
);
Output:
DBMS 102P 04321102024
LAB ASSIGNMENT - 4
Sol:
Output:
DBMS 102P 04321102024
Output:
Output:
Output:
DBMS 102P 04321102024
LAB ASSIGNMENT - 5
Sol:
Create table student
CMD:
CREATE TABLE Student (
USN VARCHAR(20) PRIMARY KEY,
SName VARCHAR(100) NOT NULL,
Address VARCHAR(255),
Phone VARCHAR(15),
Gender CHAR(1)
);
desc Student;
Output:
CMD:
INSERT INTO Student
(USN, SName, Address, Phone, Gender) VALUES
('S001', 'John Doe', '123 Main St', '555-1234', 'M'),
('S002', 'Jane Smith', '456 Elm St', '555-5678', 'F'),
('S003', 'Alice Johnson', '789 Oak St', '555-8765', 'F'),
('S004', 'Bob Brown', '321 Pine St', '555-4321', 'M'),
('S005', 'Charlie Davis', '654 Maple St', '555-6789', 'M');
DBMS 102P 04321102024
Output:
Output:
Output:
DBMS 102P 04321102024
Output:
Output:
DBMS 102P 04321102024
LAB ASSIGNMENT – 6
SOL:
CMD:
SELECT * FROM STUDENT;
Output:
CMD:
SELECT * FROM STUDENT
WHERE ADDRESS = 'Saadh Nagar';
DBMS 102P 04321102024
Output:
3. Use of operators
CMD:
Output:
CMD:
SELECT * FROM STUDENT
WHERE ADDRESS = 'gurugram' AND GENDER = 'M';
Output:
DBMS 102P 04321102024
c. Conditional Operator
CMD:
Output:
4. Sorting Data
1. Ascending Order by NAME :
CMD:
SELECT * FROM STUDENT
ORDER BY NAME ASC;
DBMS 102P 04321102024
Output:
LAB ASSIGNMENT – 7
Q.7 Create table using following integrity Constraints: Primary Key, Unique
Key, Not Null, Check, Default, and Foreign Key.
Sol:
-- Parent Table: Departments
CREATE TABLE Departments (
DeptID INT PRIMARY KEY, -- Primary Key
DeptName VARCHAR(100) UNIQUE NOT NULL -- Unique + Not Null
);
LAB ASSIGNMENT – 8
Q.8. Write queries to execute the following Aggregate functions: Sum, Avg,
Count, and Minimum and Maximum value of a numeric column of a table using
aggregate function.
Sol:
-- Step 1: Create a table named 'Sales'
CREATE TABLE Sales (
id INT PRIMARY KEY,
product_name VARCHAR(100),
quantity INT,
price DECIMAL(10, 2)
);
-- a) SUM of price
SELECT SUM(price) AS total_price FROM Sales;
-- c) COUNT of records
SELECT COUNT(*) AS total_records FROM Sales;
-- d) MINIMUM price
SELECT MIN(price) AS lowest_price FROM Sales;
-- e) MAXIMUM price
SELECT MAX(price) AS highest_price FROM Sales;
DBMS 102P 04321102024
LAB ASSIGNMENT – 9
Q.9 Retrieve data from a table using alias names.
Sol:
CREATE TABLE Pup (
id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50)
);
INSERT INTO Pup (id, first_name, last_name, department)
VALUES
(1, 'John', 'Doe', 'Sales'),
(2, 'Jane', 'Smith', 'HR'),
(3, 'Michael', 'Brown', 'IT'),
(4, 'Emily', 'Davis', 'Marketing');
SELECT
e.first_name AS fname,
e.last_name AS lname,
e.department AS dept
FROM
Pup AS e;
Output:
DBMS 102P 04321102024
LAB ASSIGNMENT – 10
Q.10. Retrieve data from a table using nested queries.
Sol:
CREATE TABLE SOS (
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(50),
Salary INT
);
INSERT INTO SOS (EmpID, Name, Department, Salary) VALUES
(1, 'Alice', 'HR', 50000),
(2, 'Bob', 'IT', 70000),
(3, 'Charlie', 'Finance', 60000),
(4, 'David', 'IT', 75000),
(5, 'Eve', 'HR', 52000);
LAB ASSIGNMENT – 11
Q. 11 Retrieve data from more than one table using inner join, left
join, right join and full outer join.
Sol:
CREATE TABLE JOIN2(
ID INT PRIMARY KEY,
NAME VARCHAR(50),
SALARY NUMERIC,
MARKS INT,
CITY VARCHAR(50)
);
INSERT INTO JOIN2 VALUES (166, 'Ayush', 900000, 98, 'DELHI');
INSERT INTO JOIN2 VALUES (15, 'Kartik', 900000, 65, 'GHAZIABAD');
INSERT INTO JOIN2 VALUES (143, 'Gaurav', 755555, 64, 'MUMBAI');
INSERT INTO JOIN2 VALUES (104, 'Pankaj', 533333, 44, 'BHAVALI');
INSERT INTO JOIN2 VALUES (105, 'Aradhya', 53663333, 98, 'PRAYAGRAJ');
INSERT INTO JOIN2 VALUES (101, 'ARya', 3354333, 98, 'PUNE');
INSERT INTO JOIN2 VALUES (102, 'OKAYY BHAI', 30000, 95, 'RAJASTHAN');
Master Table:
-- INNER JOIN
SELECT * FROM
JOIN1 AS c
INNER JOIN JOIN2 AS p
ON c.ID = p.ID;
Output:
DBMS 102P 04321102024
-- LEFT JOIN
SELECT * FROM
JOIN1 AS c
LEFT JOIN JOIN2 AS p
ON c.ID = p.ID;
Output:
-- RIGHT JOIN
SELECT * FROM
JOIN1 AS c
RIGHT JOIN JOIN2 AS p
ON c.ID = p.ID;
Output:
DBMS 102P 04321102024
LAB ASSIGNMENT – 12
Q.12 Create index on a column of a table.
Sol:
CREATE TABLE SQL1 (
id INT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO SQL1 (id, name, department, salary) VALUES
(1, 'Alice Johnson', 'Engineering', 75000),
(2, 'Bob Smith', 'Sales', 60000),
(3, 'Carol White', 'Engineering', 80000),
(4, 'David Brown', 'Marketing', 55000),
(5, 'Eva Green', 'Sales', 62000),
(6, 'Frank Black', 'Engineering', 77000),
(7, 'Grace Hall', 'Marketing', 53000);
Select * from SQL1;
-- Create an Index
Select * from SQL1;
CREATE INDEX idx_department ON SQL1(department);
-- Use the Index
SELECT * FROM SQL1 WHERE department = 'Engineering';
DBMS 102P 04321102024
LAB ASSIGNMENT – 13
Q. 13 Consider the following schema of a library management system. Write
the SQL queries for the questions given below:
EMPLOYEE (EmployeeID, EmployeeName, Street, City)
COMPANY (CompanyID, CompanyName, City)
WORKS (EmployeeID, CompanyID, Salary) MANAGES (EmployeeID,
ManagerID) Create the appropriate tables for the given schemas and describe it.
i) Find the names of all employees who work for First Bank Corporation.
ii) Find the names and cities of residence of all employees who work for the
First Bank Corporation.
iii) Find the names, street, and cities of residence of all employees who work
for First.
iv) Find the employees who live in the same cities as the companies for
which they work.
v) Find all employees who live in the same cities and on the same streets as
do their Managers.
vi) Find all employees who do not work for First Bank Corporation.
vii) Find all employees who earn more than every employee of Small Bank
Corporation.
viii) Find all companies located in every city in which Small Bank
Corporation is located.
ix) Find all employees who earn more than the average salary of all
employees of their
Company.
x) Find those companies whose employees earn a higher salary, on average,
than the average salary of first Bank Corporation.
Sol:
Table Creation
CREATE TABLE EMPLOYEE (
EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(100),
Street VARCHAR(100),
City VARCHAR(100)
);
Insert Data :
-- Insert into EMPLOYEE
INSERT INTO EMPLOYEE (EmployeeID, EmployeeName, Street, City) VALUES
(1, 'Alice Johnson', '123 Maple St', 'New York'),
(2, 'Bob Smith', '456 Oak Ave', 'Los Angeles'),
(3, 'Carol White', '789 Pine Rd', 'Chicago'),
(4, 'David Brown', '321 Birch Blvd', 'New York'),
(5, 'Eve Black', '654 Cedar Ln', 'Boston');
SQL Queries
i) Find the names of all employees who work for First Bank Corporation.
SELECT E.EmployeeName
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
JOIN COMPANY C ON W.CompanyID = C.CompanyID
WHERE C.CompanyName = 'First Bank Corporation';
ii) Find the names and cities of residence of all employees who work for the
First Bank Corporation.
SELECT E.EmployeeName, E.City
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
JOIN COMPANY C ON W.CompanyID = C.CompanyID
WHERE C.CompanyName = 'First Bank Corporation';
iii) Find the names, street, and cities of residence of all employees who work for
First.
SELECT E.EmployeeName, E.Street, E.City
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
JOIN COMPANY C ON W.CompanyID = C.CompanyID
WHERE C.CompanyName LIKE 'First%';
DBMS 102P 04321102024
iv) Find the employees who live in the same cities as the companies for which
they work.
SELECT DISTINCT E.EmployeeName
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
JOIN COMPANY C ON W.CompanyID = C.CompanyID
WHERE E.City = C.City;
v) Find all employees who live in the same cities and on the same streets as do
their Managers.
SELECT E.EmployeeName
FROM EMPLOYEE E
JOIN MANAGES M ON E.EmployeeID = M.EmployeeID
JOIN EMPLOYEE MGR ON M.ManagerID = MGR.EmployeeID
WHERE E.City = MGR.City AND E.Street = MGR.Street;
vi) Find all employees who do not work for First Bank Corporation.
SELECT DISTINCT E.EmployeeName
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
JOIN COMPANY C ON W.CompanyID = C.CompanyID
WHERE C.CompanyName <> 'First Bank Corporation';
DBMS 102P 04321102024
vii) Find all employees who earn more than every employee of Small Bank
Corporation.
SELECT E.EmployeeName
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
WHERE W.Salary > ALL (
SELECT W2.Salary
FROM WORKS W2
JOIN COMPANY C2 ON W2.CompanyID = C2.CompanyID
WHERE C2.CompanyName = 'Small Bank Corporation'
);
viii) Find all companies located in every city in which Small Bank Corporation is
located.
SELECT DISTINCT C1.CompanyName
FROM COMPANY C1
WHERE NOT EXISTS (
SELECT C2.City
FROM COMPANY C2
WHERE C2.CompanyName = 'Small Bank Corporation'
AND C2.City NOT IN (
SELECT C3.City
FROM COMPANY C3
WHERE C3.CompanyID = C1.CompanyID
)
);
DBMS 102P 04321102024
ix) Find all employees who earn more than the average salary of all employees
of their Company.
SELECT E.EmployeeName
FROM EMPLOYEE E
JOIN WORKS W ON E.EmployeeID = W.EmployeeID
WHERE W.Salary > (
SELECT AVG(W2.Salary)
FROM WORKS W2
WHERE W2.CompanyID = W.CompanyID
);
DBMS 102P 04321102024