1.
1.)select eno,ename,deptno,salary from emp where salary = (s
elect max(salary) from emp);
2.)mysql> select ename from emp where ename like "I%";
3.)mysql> select * from emp where hiredate<"1981-09-30";
4.)mysql> select * from emp order by salary desc;
5.)select count(ename) as no_of_emp, avg(salary) as avg_salary from emp where deptno=20;
6.)mysql> select hiredate,avg(salary) as avg_salary, min(salary) as minimum_salary from emp
where deptno=10 group by hiredate order by hiredate asc;
7.)select emp.ename, dept.deptname from emp inner join dept
on emp.deptno = dept.deptno;
8.)select dept.deptname, sum(emp.salary) as total_salary from emp join dept on
emp.deptno=dept.deptno group by dept.deptname;
9.)mysql> select * from emp inner join dept on emp.deptno=dept.deptno where dept.deptname
= "developer";
10.)mysql> update emp set salary = salary * 1.05 where deptno = 10;
3.
-- 1. List full details of all hotels.
SELECT *
FROM Hotel;
-- 2. How many hotels are there?
SELECT COUNT(*) AS HotelCount
FROM Hotel;
-- 3. List the price and type of all rooms at the Grosvenor Hotel.
SELECT Room.Price, Room.Type
FROM Room
JOIN Hotel ON Room.HotelNo = Hotel.HotelNo
WHERE Hotel.Name = 'Grosvenor Hotel';
-- 4. List the number of rooms in each hotel.
SELECT HotelNo, COUNT(*) AS NumberOfRooms
FROM Room
GROUP BY HotelNo;
-- 5. Update the price of all rooms by 5%.
UPDATE Room
SET Price = Price * 1.05;
-- 6. List full details of all hotels in London.
SELECT *
FROM Hotel
WHERE City = 'London';
-- 7. What is the average price of a room?
SELECT AVG(Price) AS AveragePrice
FROM Room;
-- 8. List all guests currently staying at the Grosvenor Hotel.
SELECT Guest.GuestNo, Guest.GuestName, Guest.GuestAddress
FROM Guest
JOIN Booking ON Guest.GuestNo = Booking.GuestNo
JOIN Hotel ON Booking.HotelNo = Hotel.HotelNo
WHERE Hotel.Name = 'Grosvenor Hotel'
AND CURDATE() BETWEEN Booking.DateFrom AND Booking.DateTo;
-- 9. List the number of rooms in each hotel in London.
SELECT Hotel.HotelNo, Hotel.Name, COUNT(Room.RoomNo) AS NumberOfRooms
FROM Hotel
JOIN Room ON Hotel.HotelNo = Room.HotelNo
WHERE Hotel.City = 'London'
GROUP BY Hotel.HotelNo, Hotel.Name;
-- 10. Create one view on the above database and query it.
-- Create the view
CREATE VIEW HotelRoomDetails AS
SELECT Hotel.HotelNo, Hotel.Name AS HotelName, Hotel.City, Room.RoomNo, Room.Type,
Room.Price
FROM Hotel
JOIN Room ON Hotel.HotelNo = Room.HotelNo;
-- Query the view
SELECT *
FROM HotelRoomDetails;
-- You can also query with filters as needed:
-- Example: List all rooms in London
SELECT *
FROM HotelRoomDetails
WHERE City = 'London';
4.
-- 1. What is the total revenue per night from all double rooms?
SELECT SUM(Price) AS TotalRevenue
FROM Room
WHERE Type = 'Double';
-- 2. List the details of all rooms at the Grosvenor Hotel, including the name of the guest staying
in the room, if the room is occupied.
SELECT Room.RoomNo, Room.HotelNo, Room.Type, Room.Price, Guest.GuestName
FROM Room
JOIN Hotel ON Room.HotelNo = Hotel.HotelNo
LEFT JOIN Booking ON Room.RoomNo = Booking.RoomNo AND Room.HotelNo =
Booking.HotelNo
LEFT JOIN Guest ON Booking.GuestNo = Guest.GuestNo
WHERE Hotel.Name = 'Grosvenor Hotel';
-- 3. What is the average number of bookings for each hotel in April?
SELECT HotelNo, COUNT(*) / 30.0 AS AverageBookings
FROM Booking
WHERE MONTH(DateFrom) = 4
GROUP BY HotelNo;
-- 4. Create an index on the RoomNo field
CREATE INDEX idx_roomno ON Room(RoomNo);
-- 5. List full details of all hotels.
SELECT * FROM Hotel;
-- 6. List full details of all hotels in London.
SELECT * FROM Hotel
WHERE City = 'London';
-- 7. Update the price of all rooms by 5%.
UPDATE Room
SET Price = Price * 1.05;
-- 8. List the number of rooms in each hotel in London.
SELECT Hotel.HotelNo, Hotel.Name, COUNT(Room.RoomNo) AS NumberOfRooms
FROM Hotel
JOIN Room ON Hotel.HotelNo = Room.HotelNo
WHERE Hotel.City = 'London'
GROUP BY Hotel.HotelNo, Hotel.Name;
-- 9. List all double or family rooms with a price below £40.00 per night, in ascending order of
price.
SELECT *
FROM Room
WHERE (Type = 'Double' OR Type = 'Family') AND Price < 40.00
ORDER BY Price ASC;
5.
-- 1. List full details of all hotels.
SELECT * FROM Hotel;
-- 2. How many hotels are there?
SELECT COUNT(*) AS NumberOfHotels FROM Hotel;
-- 3. List the price and type of all rooms at the Grosvenor Hotel.
SELECT Room.Price, Room.Type
FROM Room
JOIN Hotel ON Room.HotelNo = Hotel.HotelNo
WHERE Hotel.Name = 'Grosvenor Hotel';
-- 4. List the number of rooms in each hotel.
SELECT Hotel.HotelNo, Hotel.Name, COUNT(Room.RoomNo) AS NumberOfRooms
FROM Hotel
JOIN Room ON Hotel.HotelNo = Room.HotelNo
GROUP BY Hotel.HotelNo, Hotel.Name;
-- 5. List all guests currently staying at the Grosvenor Hotel.
SELECT Guest.GuestName, Guest.GuestAddress
FROM Guest
JOIN Booking ON Guest.GuestNo = Booking.GuestNo
JOIN Hotel ON Booking.HotelNo = Hotel.HotelNo
WHERE Hotel.Name = 'Grosvenor Hotel' AND CURDATE() BETWEEN Booking.DateFrom AND
Booking.DateTo;
-- 6. List all double or family rooms with a price below £40.00 per night, in ascending order of
price.
SELECT *
FROM Room
WHERE (Type = 'Double' OR Type = 'Family') AND Price < 40.00
ORDER BY Price ASC;
-- 7. How many different guests have made bookings for August?
SELECT COUNT(DISTINCT GuestNo) AS NumberOfGuests
FROM Booking
WHERE MONTH(DateFrom) = 8;
-- 8. What is the total income from bookings for the Grosvenor Hotel today?
SELECT SUM(Room.Price) AS TotalIncome
FROM Booking
JOIN Room ON Booking.RoomNo = Room.RoomNo AND Booking.HotelNo = Room.HotelNo
JOIN Hotel ON Booking.HotelNo = Hotel.HotelNo
WHERE Hotel.Name = 'Grosvenor Hotel' AND CURDATE() BETWEEN Booking.DateFrom AND
Booking.DateTo;
-- 9. What is the most commonly booked room type for each hotel in London?
SELECT Hotel.Name, Room.Type, COUNT(*) AS BookingCount
FROM Booking
JOIN Room ON Booking.RoomNo = Room.RoomNo AND Booking.HotelNo = Room.HotelNo
JOIN Hotel ON Booking.HotelNo = Hotel.HotelNo
WHERE Hotel.City = 'London'
GROUP BY Hotel.HotelNo, Room.Type
ORDER BY BookingCount DESC;
-- 10. Update the price of all rooms by 5%
UPDATE Room
SET Price = Price * 1.05;
6.
-- 1. List full details of all hotels.
SELECT * FROM Hotel;
-- 2. List full details of all hotels in London.
SELECT * FROM Hotel
WHERE City = 'London';
-- 3. List all guests currently staying at the Grosvenor Hotel.
SELECT Guest.GuestName, Guest.GuestAddress
FROM Guest
JOIN Booking ON Guest.GuestNo = Booking.GuestNo
JOIN Hotel ON Booking.HotelNo = Hotel.HotelNo
WHERE Hotel.Name = 'Grosvenor Hotel' AND CURDATE() BETWEEN Booking.DateFrom AND
Booking.DateTo;
-- 4. List the names and addresses of all guests in London, alphabetically ordered by name.
SELECT Guest.GuestName, Guest.GuestAddress
FROM Guest
JOIN Booking ON Guest.GuestNo = Booking.GuestNo
JOIN Hotel ON Booking.HotelNo = Hotel.HotelNo
WHERE Hotel.City = 'London'
ORDER BY Guest.GuestName;
-- 5. List the bookings for which no DateTo has been specified.
SELECT * FROM Booking
WHERE DateTo IS NULL;
-- 6. How many hotels are there?
SELECT COUNT(*) AS NumberOfHotels FROM Hotel;
-- 7. List the rooms that are currently unoccupied at the Grosvenor Hotel.
SELECT Room.RoomNo, Room.Type, Room.Price
FROM Room
JOIN Hotel ON Room.HotelNo = Hotel.HotelNo
LEFT JOIN Booking ON Room.RoomNo = Booking.RoomNo AND Room.HotelNo =
Booking.HotelNo
WHERE Hotel.Name = 'Grosvenor Hotel' AND (Booking.DateFrom IS NULL OR CURDATE() <
Booking.DateFrom OR CURDATE() > Booking.DateTo);
-- 8. What is the lost income from unoccupied rooms at each hotel today?
SELECT Hotel.Name, SUM(Room.Price) AS LostIncome
FROM Hotel
JOIN Room ON Hotel.HotelNo = Room.HotelNo
LEFT JOIN Booking ON Room.RoomNo = Booking.RoomNo AND Room.HotelNo =
Booking.HotelNo
WHERE Booking.RoomNo IS NULL OR CURDATE() < Booking.DateFrom OR CURDATE() >
Booking.DateTo
GROUP BY Hotel.HotelNo, Hotel.Name;
-- 9. Create index on one of the fields (e.g., RoomNo) and show its performance in a query.
CREATE INDEX idx_roomno ON Room(RoomNo);
-- Query to analyze performance (use EXPLAIN to compare before and after indexing)
EXPLAIN SELECT * FROM Room WHERE RoomNo = 101;
-- 10. Create one view on the above database and query it.
-- Creating a view that shows guest information along with their hotel name and city
CREATE VIEW GuestHotelInfo AS
SELECT Guest.GuestName, Guest.GuestAddress, Hotel.Name AS HotelName, Hotel.City
FROM Guest
JOIN Booking ON Guest.GuestNo = Booking.GuestNo
JOIN Hotel ON Booking.HotelNo = Hotel.HotelNo;
-- Querying the view
SELECT * FROM GuestHotelInfo;
7.
-- 1. Get the details of employees working on project C353
SELECT Employee.*
FROM Employee
JOIN Assigned_To ON Employee.Emp_id = Assigned_To.Emp_id
WHERE Assigned_To.Project_id = 'C353';
-- 2. Get employee number of employees working on project C353
SELECT Employee.Emp_id
FROM Employee
JOIN Assigned_To ON Employee.Emp_id = Assigned_To.Emp_id
WHERE Assigned_To.Project_id = 'C353';
-- 3. Obtain details of employees working on the Database project
SELECT Employee.*
FROM Employee
JOIN Assigned_To ON Employee.Emp_id = Assigned_To.Emp_id
JOIN Project ON Assigned_To.Project_id = Project.project_id
WHERE Project.proj_name = 'Database';
-- 4. Get details of employees working on both C353 and C354
SELECT Employee.*
FROM Employee
JOIN Assigned_To AS A1 ON Employee.Emp_id = A1.Emp_id
JOIN Assigned_To AS A2 ON Employee.Emp_id = A2.Emp_id
WHERE A1.Project_id = 'C353' AND A2.Project_id = 'C354';
-- 5. Get employee numbers of employees who do not work on project C453
SELECT Employee.Emp_id
FROM Employee
WHERE Employee.Emp_id NOT IN (
SELECT Assigned_To.Emp_id
FROM Assigned_To
WHERE Assigned_To.Project_id = 'C453'
);
-- Alternatively, using LEFT JOIN and IS NULL to find employees not working on C453:
SELECT Employee.Emp_id
FROM Employee
LEFT JOIN Assigned_To ON Employee.Emp_id = Assigned_To.Emp_id AND
Assigned_To.Project_id = 'C453'
WHERE Assigned_To.Project_id IS NULL
SET 2
1. Mongo
// 1. Insert 10 documents into the Student collection
db.Student.insertMany([
{ Roll_No: "A1", Name: "Alice", Class: "TE", Marks: 85, Address: "123 Street",
Enrolled_Courses: ["DBMS", "Math"] },
{ Roll_No: "A2", Name: "Bob", Class: "SE", Marks: 72, Address: "456 Avenue",
Enrolled_Courses: ["TOC", "DBMS"] },
{ Roll_No: "A3", Name: "Charlie", Class: "TE", Marks: 65, Address: "789 Boulevard",
Enrolled_Courses: ["Physics", "TOC"] },
{ Roll_No: "A4", Name: "David", Class: "BE", Marks: 55, Address: "321 Lane",
Enrolled_Courses: ["Math", "CS"] },
{ Roll_No: "A5", Name: "Eve", Class: "TE", Marks: 92, Address: "654 Road",
Enrolled_Courses: ["DBMS", "TOC"] },
{ Roll_No: "A6", Name: "Frank", Class: "SE", Marks: 45, Address: "987 Hill", Enrolled_Courses:
["Math", "DBMS"] },
{ Roll_No: "A7", Name: "Grace", Class: "BE", Marks: 38, Address: "852 Street",
Enrolled_Courses: ["Physics", "CS"] },
{ Roll_No: "A8", Name: "Hank", Class: "TE", Marks: 70, Address: "159 Alley",
Enrolled_Courses: ["DBMS", "TOC"] },
{ Roll_No: "A9", Name: "Ivy", Class: "SE", Marks: 82, Address: "753 Avenue",
Enrolled_Courses: ["Math", "Physics"] },
{ Roll_No: "A10", Name: "Jack", Class: "BE", Marks: 25, Address: "456 Path",
Enrolled_Courses: ["CS", "TOC"] }
]);
// 2. List the names of students who have enrolled in the course “DBMS” or “TOC”
db.Student.find(
{ Enrolled_Courses: { $in: ["DBMS", "TOC"] } },
{ Name: 1, _id: 0 }
);
// 3. List the Roll numbers and class of students who have marks more than 50 or class as TE
db.Student.find(
{ $or: [{ Marks: { $gt: 50 } }, { Class: "TE" }] },
{ Roll_No: 1, Class: 1, _id: 0 }
);
// 4. Update the entire record of roll_no A10
db.Student.updateOne(
{ Roll_No: "A10" },
{ $set: { Name: "Jack", Class: "BE", Marks: 30, Address: "456 Path Updated",
Enrolled_Courses: ["CS", "TOC"] } }
);
// 5. Display the names of students having 3rd and 4th highest marks
db.Student.find({}, { Name: 1, Marks: 1, _id: 0 })
.sort({ Marks: -1 })
.skip(2)
.limit(2);
// 6. Delete the records of students having marks less than 20
db.Student.deleteMany({ Marks: { $lt: 20 } });
// 7. Delete only the first record from the collection
db.Student.deleteOne({});
Mongo
// 1. Display all Students based on their departments along with an average Marks of a
particular department
db.Student_Data.aggregate([
{
$group: {
_id: "$Department", // Group by department
averageMarks: { $avg: "$Marks" }, // Calculate the average marks
students: { $push: { // Push all student details into an array
studentID: "$Student_ID",
studentName: "$Student_Name",
marks: "$Marks"
}}
}
}
]);
// 2. Display the number of Students associated along with a particular department
db.Student_Data.aggregate([
{
$group: {
_id: "$Department", // Group by department
studentCount: { $sum: 1 } // Count the number of students in each department
}
}
]);
// 3. Display list of Students with the highest Marks in each Department in descending order of
Marks
db.Student_Data.aggregate([
{
$group: {
_id: "$Department", // Group by department
highestMarks: { $max: "$Marks" }, // Get the highest marks
students: { $push: { // Push student details into an array
studentID: "$Student_ID",
studentName: "$Student_Name",
marks: "$Marks"
}}
}
},
{ $unwind: "$students" }, // Unwind the array to match each student
{
$match: {
"students.marks": { $eq: "$highestMarks" } // Only students with the highest marks
}
},
{ $sort: { "students.marks": -1 } } // Sort in descending order of marks
]);
// 4. Create an index on field Student_ID
db.Student_Data.createIndex({ "Student_ID": 1 });
// 5. Create an index on fields “Student_Name” and “Department”
db.Student_Data.createIndex({ "Student_Name": 1, "Department": 1 });
// 6. Drop an index on field Student_ID
db.Student_Data.dropIndex({ "Student_ID": 1 });
// 7. Drop an index on fields “Student_Name” and “Department”
db.Student_Data.dropIndex({ "Student_Name": 1, "Department": 1 });
1.
// Map function
var mapFunction3 = function() {
if (this.Price > 300) { // Filter books with Price > 300
emit(this.Author_name, this.Title); // Emit author as the key and book title as the value
}
};
// Reduce function
var reduceFunction3 = function(key, values) {
return values; // Combine the list of titles for each author
};
// Run MapReduce
db.Book.mapReduce(mapFunction3, reduceFunction3, { out: "AuthorWiseExpensiveBooks" });
// 1. Display Author wise list of books
var mapFunction1 = function() {
emit(this.Author_name, this.Title);
};
var reduceFunction1 = function(key, values) {
return values;
};
db.Book.mapReduce(mapFunction1, reduceFunction1, { out: "AuthorWiseBooks" });
// 2. Display Author wise list of books having Borrowed status as “True”
var mapFunction2 = function() {
if (this.Borrowed_status === "True") {
emit(this.Author_name, this.Title);
}
};
var reduceFunction2 = function(key, values) {
return values;
};
db.Book.mapReduce(mapFunction2, reduceFunction2, { out: "AuthorWiseBorrowedBooks" });
// 3. Display Author wise list of books having price greater than 300
var mapFunction3 = function() {
if (this.Price > 300) {
emit(this.Author_name, this.Title);
}
};
var reduceFunction3 = function(key, values) {
return values;
};
db.Book.mapReduce(mapFunction3, reduceFunction3, { out: "AuthorWiseExpensiveBooks" });
2.
-- 1. Employees who earn more than the average salary and work in any IT departments
SELECT e.First_name, e.Last_name, e.Salary
FROM Employee e
JOIN Departments d ON e.Department_id = d.Department_id
WHERE e.Salary > (SELECT AVG(Salary) FROM Employee)
AND d.Department_name = 'IT';
-- 2. Employees who earn the same salary as the minimum salary in their departments
SELECT e.First_name, e.Last_name, e.Salary
FROM Employee e
WHERE e.Salary = (
SELECT MIN(Salary)
FROM Employee
WHERE Department_id = e.Department_id
);
-- 3. Employees whose salary is above average for their departments
SELECT e.Employee_id, e.First_name, e.Last_name, e.Salary
FROM Employee e
JOIN Departments d ON e.Department_id = d.Department_id
WHERE e.Salary > (
SELECT AVG(Salary)
FROM Employee
WHERE Department_id = e.Department_id
);
-- 4. Department name, Manager name, and City
SELECT d.Department_name, m.Manager_name, l.City
FROM Departments d
JOIN Manager m ON d.Manager_id = m.Manager_id
JOIN Locations l ON d.Location_id = l.Location_id;
-- 5. Managers whose experience is more than 15 years
SELECT e.First_name, e.Last_name, e.Hire_date, e.Salary
FROM Employee e
JOIN Manager m ON e.Manager_id = m.Manager_id
WHERE DATEDIFF(CURDATE(), e.Hire_date) / 365 > 15;
4.
-- 1. Create Tables with Referential Integrity
CREATE TABLE Customer (
CustID INT PRIMARY KEY,
Name VARCHAR(50),
Cust_Address VARCHAR(100),
Phone_no VARCHAR(15),
Email_ID VARCHAR(50),
Age INT
);
CREATE TABLE Branch (
Branch_ID INT PRIMARY KEY,
Branch_Name VARCHAR(50),
Address VARCHAR(100)
);
CREATE TABLE Account (
Account_no INT PRIMARY KEY,
Branch_ID INT,
CustID INT,
open_date DATE,
Account_type VARCHAR(50),
Balance DECIMAL(15, 2),
FOREIGN KEY (Branch_ID) REFERENCES Branch(Branch_ID),
FOREIGN KEY (CustID) REFERENCES Customer(CustID)
);
-- 2. (ER Diagram should be drawn separately using a tool)
-- 3. Create a View for Saving Account
CREATE VIEW Saving_Account AS
SELECT c.CustID, c.Name, c.Cust_Address, c.Phone_no, c.Email_ID, c.Age, a.open_date,
a.Balance
FROM Customer c
JOIN Account a ON c.CustID = a.CustID
WHERE a.open_date = '2018-08-16' AND a.Account_type = 'Saving Account';
-- 4. Update the View with Cust_Address as Pune for CustID = 103
UPDATE Customer
SET Cust_Address = 'Pune'
WHERE CustID = 103;
-- 5. Create a View for Loan Account
CREATE VIEW Loan_Account AS
SELECT c.CustID, c.Name, c.Cust_Address, c.Phone_no, c.Email_ID, c.Age, a.open_date,
a.Balance
FROM Customer c
JOIN Account a ON c.CustID = a.CustID
WHERE a.open_date = '2018-02-16' AND a.Account_type = 'Loan Account';
-- 6. Create an Index on Primary Key Column of Table Customer
CREATE INDEX idx_CustID ON Customer(CustID);
-- 7. Create an Index on Primary Key Column of Table Branch
CREATE INDEX idx_BranchID ON Branch(Branch_ID);
-- 8. Create a Sequence on Customer Table (using AUTO_INCREMENT)
ALTER TABLE Customer MODIFY CustID INT AUTO_INCREMENT;
-- 9. Create Synonym 'Cust_info' for Branch Table (using a View as a workaround)
CREATE VIEW Cust_info AS
SELECT * FROM Branch;