Week 1_merged_merged (1) (1) (1)
Week 1_merged_merged (1) (1) (1)
of
Bachelor of Technology
By
SHREYA PAITHANI
22012686
Under the Guidance of
Mr. Rahul Singh
Assistant Professor
Department of CSE
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
GRAPHIC ERA HILL UNIVERSITY, BHIMTAL CAMPUS
SATTAL ROAD, P.O. BHOWALI DISTRICT
NAINITAL-263132
CERTIFICATE
The term work of Java Programming Lab , being submitted by SHREYA PAITHANI D/O
Mr. Suraj Singh University Roll Number 2261647 to Graphic Era Hill University, Bhimtal
Campus for the award of bona fide work carried out by her. She has worked under my
guidance and supervision and fulfilled the requirement for the submission of this work report.
Professor, Dept. of CSE, GEHU Bhimtal Campus) for allowing me to carry out this practical
work under his excellent and optimistic supervision. This has all been possible due to his
novel inspiration, able guidance and useful suggestions that have helped me in developing
I want to extend thanks to our President Prof. (Dr.) Kamal Ghanshala for providing
us all infrastructure and facilities to work in need without which this work would not be
possible.
(SHREYA PAITHANI)
WEEK
1
OUTPUT:
4. Update the table by applying some conditions.(For example:
using alter command)
SOLUTION :
ALTER TABLE Patient ADD Contact_Number VARCHAR(15);
UPDATE Patient SET Contact_Number = '9998887776'
WHERE Patient_ID = 1;
OUTPUT:
5. Apply the DELETE and DROP command, and then review the
results.
SOLUTION:
DELETE FROM bill WHERE Patient_ID = 2;
DROP TABLE Doctor_Patient;
OUTPUT:
WEEK
2
1. Create a user and provide the GRANT privileges to the user on the
database then REVOKE the given privileges.
SOLUTION:
CREATE USER 'hospital_user'@'localhost' IDENTIFIED BY 'password123';
GRANT ALL PRIVILEGES ON hospital_db.* TO 'hospital_user'@'localhost';
REVOKE ALL PRIVILEGES ON hospital_db.* FROM 'hospital_user'@'localhost';
OUTPUT:
2. Insert any five records in the previous schema and apply the
rollback. Also check the results.
SOLUTION :
START TRANSACTION;
INSERT INTO Patient (Patient_ID, Ward_ID, Name, Age, Gender,
Admission_Date, Contact_Number) VALUES
(4, 1, 'Vikas', 45, 'Male', '2024-10-06', '9876543212'),
(5, 3, 'Meera', 28, 'Female', '2024-10-06', '9876543213'),
(6, 2, 'Raj', 32, 'Male', '2024-10-06', '9876543214'),
(7, 1, 'Pooja', 23, 'Female', '2024-10-06', '9876543215'),
(8, 3, 'Kabir', 36, 'Male', '2024-10-06', '9876543216');
ROLLBACK;
SELECT * FROM Patient WHERE Patient_ID >= 3;
OUTPUT:
3.Add default, check, unique and not null constraints to the schema.
SOLUTION:
ALTER TABLE Patient MODIFY Age INT NOT NULL;
ADD CONSTRAINT chk_age CHECK (Age > 0),
ADD CONSTRAINT unique_contact UNIQUE (Contact_Number),
MODIFY Gender VARCHAR(10) DEFAULT 'Unknown';
OUTPUT:
4. Insert NULL values and check the results.
SOLUTION:
INSERT INTO Patient (Patient_ID, Ward_ID, Name, Age, Gender, Admission_Date,
Contact_Number)
VALUES (9, 1, 'Priya', NULL, NULL, '2024-10-06', NULL);
OUTPUT:
5. Add duplicate value and try to make a column as primary key,
check what happen to the table
SOLUTION:
INSERT INTO Patient (Patient_ID, Ward_ID, Name, Age, Gender,
Admission_Date, Contact_Number) VALUES (10, 2, 'Sohail', 29, 'Male', '2024-
10-06', '9876543215');
ALTER TABLE Patient ADD PRIMARY KEY (Contact_Number);
OUTPUT:
ASSIGNMENT 1
1. Create Table
1. Create a table named Employees with the following columns: o EmployeeID (integer,
primary key, auto-increment) o FirstName (varchar, 50 characters) o LastName (varchar, 50
characters) o HireDate (date) o Salary (decimal, 10, 2)
2. Create a table named Departments with the following columns: o DepartmentID (integer,
primary key) o DepartmentName (varchar, 100 characters) o Location (varchar, 100 characters)
Solution Query:
CREATE TABLE Employees (
FirstName VARCHAR(50),
HireDate DATE,
Salary DECIMAL(10, 2)
);
DepartmentName VARCHAR(100),
DepartmentLocation VARCHAR(100)
);
2. Alter Table
3. Add a column Email (varchar, 100 characters) to the Employees table.
4. Change the data type of the Salary column in the Employees table to decimal(12, 2).
Solution Query:
Solution Query:
DROP TABLE Departments;
4. Constraints
8. Create a unique constraint on the Email column in the Employees table.
10. Add a NOT NULL constraint to the LastName column in the Employees table.
Solution Query:
ALTER TABLE Employees ADD CONSTRAINT UC_Email UNIQUE(Email);
12. Create a unique index on the DepartmentName column in the Departments table.
Solution Query:
CREATE INDEX idx_LastName ON Employees(LastName);
14. Add a foreign key constraint to the Projects table linking DepartmentID to DepartmentID
in the Departments table.
Solution Query:
CREATE TABLE Projects (
ProjectName VARCHAR(100),
StartDate DATE,
EndDate DATE,
DepartmentID INT,
);
8. Table Modification
18. Alter the Employees table to set a default value of 1000.00 for the Salary column.
19. Change the HireDate column in the Employees table to allow NULL values.
20. Drop the foreign key constraint from the Employees table that links to the Departments
table.
Solution Query:
ALTER TABLE Employees ALTER COLUMN Salary SET DEFAULT 1000.00;
1. Find all employees whose job title starts with 'Manager' and were hired
between January 1, 2022, and December 31, 2023.
SOLUTION :
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
JobTitle VARCHAR(100),
HireDate DATE,
Email VARCHAR(100),
PhoneNumber VARCHAR(20),
Salary DECIMAL(10, 2)
);
SELECT *FROM Employees WHERE JobTitle LIKE 'Manager%'
AND HireDate BETWEEN '2022-01-01' AND '2023-12-31';
OUTPUT:
2. Retrieve all products where the product name contains 'Pro' and the
price is between $50 and $150.
SOLUTION:
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Price DECIMAL(10, 2)
);
SELECT *FROM Products WHERE ProductName LIKE '%Pro%'
AND Price BETWEEN 50 AND 150;
OUTPUT:
3. List all employees whose email address starts with 'johndoe' and their
salary is between $40,000 and $70,000.
SOLUTION:
SELECT *FROM Employees WHERE Email LIKE 'johndoe%'
AND Salary BETWEEN 40000 AND 70000;
OUTPUT:
4. Find all departments where the department name contains 'IT' and the
budget is between $10,000 and $50,000.
SOLUTION:
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(100),
Budget DECIMAL(15, 2)
);
SELECT *
FROM Departments
WHERE DepartmentName LIKE '%IT%'
AND Budget BETWEEN 10000 AND 50000;
OUTPUT:
5. Retrieve all projects where the project name ends with 'Launch' and the
start date is between January 1, 2024, and June 30, 2024.
SOLUTION:
CREATE TABLE Projects (
ProjectID INT PRIMARY KEY,
ProjectName VARCHAR(100),
StartDate DATE
);
SELECT *
FROM Projects
WHERE ProjectName LIKE '%Launch'
AND StartDate BETWEEN '2024-01-01' AND '2024-06-30';
OUTPUT:
6. List all employees whose phone number contains '555' and whose hire
date is between January 1, 2021, and December 31, 2022.
SOLUTION:
SELECT *FROM Employees
WHERE PhoneNumber LIKE '%555%'
AND HireDate BETWEEN '2021-01-01' AND '2022-12-31';
OUTPUT:
7. Find all customers with an address that starts with '123 Main St' and who
made purchases totaling between $200 and $1,000 in the last year.
SOLUTION:
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(100),
Address VARCHAR(200),
TotalPurchases DECIMAL(10, 2),
LastPurchaseDate DATE
);
SELECT * FROM Customers WHERE Address LIKE '123 Main St%'
AND TotalPurchases BETWEEN 200 AND 1000
AND LastPurchaseDate BETWEEN DATEADD(YEAR, -1, GETDATE())
AND GETDATE();
OUTPUT:
8. Retrieve all employees whose first name contains 'Ann' and their salary is
between $30,000 and $60,000.
SOLUTION:
SELECT *FROM Employees WHERE FirstName LIKE '%Ann%'
AND Salary BETWEEN 30000 AND 60000;
OUTPUT:
9. List all orders where the order description includes 'Special Offer' and
the order date is between July 1, 2023, and December 31, 2023.
SOLUTION:
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
OrderDescription VARCHAR(200),
OrderDate DATE
);
SELECT *
FROM Orders
WHERE OrderDescription LIKE '%Special Offer%'
AND OrderDate BETWEEN '2023-07-01' AND '2023-12-31';
OUTPUT:
10. Find all suppliers whose name contains 'Tech' and their contract end
date is between January 1, 2024, and December 31, 2024.
SOLUTION:
CREATE TABLE Suppliers (
SupplierID INT PRIMARY KEY,
SupplierName VARCHAR(100),
ContractEndDate DATE
);
SELECT *
FROM Suppliers
WHERE SupplierName LIKE '%Tech%'
AND ContractEndDate BETWEEN '2024-01-01' AND '2024-12-31';
OUTPUT:
ASSIGNMENT 3
SOLUTION:
CREATE TABLE Customers (
-> CustomerID INT PRIMARY KEY,
-> CustomerName VARCHAR(255),
-> LastName VARCHAR(255),
-> Email VARCHAR(255)
-> );
SELECT * FROM Customers
ORDER BY LastName ASC;
OUTPUT:
2. Retrieve all rows from the Orders table sorted by OrderDate in descending
order.
SOLUTION:
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
Status VARCHAR(50),
CompletionDate DATE,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
SELECT * FROM Orders ORDER BY OrderDate DESC;
OUTPUT:
3. List all products sorted first by CategoryID in ascending order and then by
ProductName in descending order.
SOLUTION :
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(255),
CategoryID INT,
Price DECIMAL(10, 2)
);
SELECT * FROM Products
ORDER BY CategoryID ASC, ProductName DESC;
OUTPUT:
4. Sort all employees by HireDate with NULL values appearing last.
SOLUTION:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(255),
DepartmentID INT,
Department VARCHAR(255),
HireDate DATE,
Salary DECIMAL(10, 2)
);
OUTPUT:
3. Retrieve all employees and sort them by the calculated column
YearsAtComp
SOLUTION:
SELECT *, DATEDIFF(YEAR, HireDate, GETDATE()) AS YearsAtCompany
FROM Employees
ORDER BY YearsAtCompany DESC;
OUTPUT:
4. Sort employees so that those in the 'Sales' department appear first, followed
by 'HR', then 'IT', and all other departments afterward.
SOLUTION:
SELECT * FROM Employees
ORDER BY
CASE
WHEN Department = 'Sales' THEN 1
WHEN Department = 'HR' THEN 2
WHEN Department = 'IT' THEN 3
ELSE 4
END;
OUTPUT:
5. List all books sorted by the length of the Title in ascending order.
SOLUTION:
CREATE TABLE Books (
BookID INT PRIMARY KEY,
Title VARCHAR(255)
);
SELECT * FROM Books
ORDER BY Title ASC;
OUTPUT:
6. Retrieve all departments sorted by the total number of employees
(EmployeeCount) in descending order.
SOLUTION:
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(255),
EmployeeCount INT
);
SELECT * FROM Departments
ORDER BY EmployeeCount DESC;
OUTPUT:
11. Find all orders where the Status is 'Completed' and sort them by
CompletionDate in ascending order. If CompletionDate is NULL, place them at
the end.
SOLUTION:
SELECT * FROM Orders
WHERE Status = 'Completed'
ORDER BY CompletionDate IS NULL, CompletionDate ASC;
OUTPUT:
12. Retrieve all employees sorted by DepartmentID in ascending order and then
by Salary in descending order.
SOLUTION:
SELECT * FROM Employees
ORDER BY DepartmentID ASC, Salary DESC;
OUTPUT:
14. Sort all products first by CategoryID in ascending order and then by Price in
descending order, and if Price is NULL, list those at the end.
SOLUTION:
SELECT * FROM Products
ORDER BY CategoryID ASC, Price IS NULL, Price DESC;
OUTPUT:
15. Retrieve all invoices sorted by InvoiceDate in ascending order and then by the
TotalAmount (calculated as Quantity * UnitPrice) in descending order.
SOLUTION:
CREATE TABLE Invoices (
InvoiceID INT PRIMARY KEY,
OrderID INT,
Quantity INT,
UnitPrice DECIMAL(10, 2),
InvoiceDate DATE,
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
);
SELECT *, (Quantity * UnitPrice) AS TotalAmount
FROM Invoices
ORDER BY InvoiceDate ASC, TotalAmount DESC;
OUTPUT:
18. Retrieve the top 10 most recent orders (OrderDate) for the year 2023, sorted
by OrderDate in descending order.
SOLUTION:
SELECT * FROM Orders
WHERE EXTRACT(YEAR FROM OrderDate)=2024
ORDER BY OrderDate DESC
LIMIT 10;
OUTPUT: