Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views14 pages

ADBMS Lab

The document outlines various lab questions and tasks related to SQL and MongoDB, including creating databases, tables, and performing CRUD operations. It covers topics such as managing employee salaries, inserting and filtering orders, and using advanced SQL joins and aggregation functions. Additionally, it includes tasks for MongoDB, such as inserting documents, updating records, and applying aggregation to calculate averages.
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)
8 views14 pages

ADBMS Lab

The document outlines various lab questions and tasks related to SQL and MongoDB, including creating databases, tables, and performing CRUD operations. It covers topics such as managing employee salaries, inserting and filtering orders, and using advanced SQL joins and aggregation functions. Additionally, it includes tasks for MongoDB, such as inserting documents, updating records, and applying aggregation to calculate averages.
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
You are on page 1/ 14

MySql Lab Questions

Lab Question 1: Manage Employee Salaries

Qs: Create a database named CompanyDB.

Step 1: Create a table inside this DB to store employee salary details (id, name, department,
salary).

Step 2: Add the following information into the DB:

1, "David", "HR", 35000


2, "Sophia", "IT", 50000
3, "Michael", "Finance", 45000

Step 3: Retrieve all employees who earn more than 40000.

Lab Question 2: Insert and Filter Orders

Qs: Create a database named Orders DB.

Step 1: Create a table inside this DB to store order details (order_id, customer_name,
product_name, total_price).

Step 2: Add the following information into the DB:

1, "Jack", "Smartphone", 600


2, "Olivia", "Tablet", 300
3, "William", "Laptop", 1000

Step 3: Select all orders where total_price is greater than 500.

Lab Question 4: Store and Search Book Records

Qs: Create a database named LibraryDB.

Step 1: Create a table inside this DB to store book information (book_id, title, author,
published_year).

Step 2: Add the following information into the DB:

1, "The Great Gatsby", "F. Scott Fitzgerald", 1925


2, "To Kill a Mockingbird", "Harper Lee", 1960
3, "1984", "George Orwell", 1949
Step 3: Retrieve all books published before the year 1950.

Lab 7:

Lab Question Statement:

You are working on a university database that stores information about students, courses, and
enrollments. Your task is to:

1. Create three tables:


o Students (student_id, student_name)
o Courses (course_id, course_name)
o Enrollments (enrollment_id, student_id, course_id)
2. Insert sample data into these tables.
3. Write a complex SQL query that:
o Retrieves all students along with the courses they are enrolled in (INNER JOIN).
o Lists all courses along with the students enrolled in them, including courses with no
students (LEFT JOIN).
o Displays all students and the courses they are taking, ensuring students without courses
are also listed (RIGHT JOIN).

__________________________________________________

Lab 8: Using GROUP BY and HAVING in SQL

Lab Question Statement:

You are working on a Sales Database that stores information about orders placed by customers.
Your task is to:

1. Create a table named Orders with the following columns:


o order_id (Primary Key)
o customer_name (VARCHAR)
o order_amount (DECIMAL)
o order_date (DATE)
2. Insert sample data into the table.
3. Write an SQL query that:
o Groups orders by customer name.
o Calculates the total amount spent by each customer.
o Displays only those customers who have spent more than $500 in total purchases.

Lab Question 1: Advanced SQL Joins (INNER JOIN, LEFT JOIN, RIGHT
JOIN)
Lab Question Statement:

You are working on a Company Database that stores information about employees,
departments, and projects. Your task is to:

1. Create three tables:

 Employees (emp_id, emp_name, dept_id, project_id, salary)


 Departments (dept_id, dept_name)
 Projects (project_id, project_name)

2. Insert sample data into these tables.


3. Write a complex SQL query that:

 Retrieves employee names, their department names, and their project names.
 Uses INNER JOIN to match employees with their respective departments.
 Uses LEFT JOIN to include employees even if they are not assigned a project.
 Uses RIGHT JOIN to include all departments, even if they have no employees assigned.

Solution:

-- Step 1: Create Database


CREATE DATABASE CompanyDB;
USE CompanyDB;

-- Step 2: Create Tables

CREATE TABLE Departments (


dept_id INT PRIMARY KEY,
dept_name VARCHAR(100) NOT NULL
);

CREATE TABLE Projects (


project_id INT PRIMARY KEY,
project_name VARCHAR(100) NOT NULL
);

CREATE TABLE Employees (


emp_id INT PRIMARY KEY AUTO_INCREMENT,
emp_name VARCHAR(100) NOT NULL,
dept_id INT,
project_id INT,
salary DECIMAL(10,2),
FOREIGN KEY (dept_id) REFERENCES Departments(dept_id),
FOREIGN KEY (project_id) REFERENCES Projects(project_id)
);
-- Step 3: Insert Sample Data

INSERT INTO Departments (dept_id, dept_name) VALUES


(1, 'HR'),
(2, 'IT'),
(3, 'Finance');

INSERT INTO Projects (project_id, project_name) VALUES


(101, 'Project Alpha'),
(102, 'Project Beta'),
(103, 'Project Gamma');

INSERT INTO Employees (emp_name, dept_id, project_id, salary) VALUES


('Alice', 1, 101, 50000),
('Bob', 2, 102, 60000),
('Charlie', 2, NULL, 55000), -- No project assigned
('David', 3, 103, 70000),
('Eve', NULL, 101, 45000); -- No department assigned

-- Step 4: Create Complex Query with INNER JOIN, LEFT JOIN, RIGHT JOIN

SELECT
e.emp_name,
d.dept_name,
p.project_name
FROM Employees e
INNER JOIN Departments d ON e.dept_id = d.dept_id -- Match employees with departments
LEFT JOIN Projects p ON e.project_id = p.project_id -- Include employees even if they have no project
RIGHT JOIN Departments d2 ON e.dept_id = d2.dept_id; -- Include all departments, even if no
employees are assigned

------------------------------------------------------------------------------------------------------------------------------------------

Lab 2: Using GROUP BY and HAVING in SQL

Lab Question Statement:

You are working on a Company Database that stores employee salary details. Your task is to:
1. Create a table called Employees with the following columns:
o emp_id (Primary Key)
o emp_name
o dept_id (Foreign Key)
o salary
2. Insert sample data into the table.
3. Write an SQL query that:
o Groups employees by department (dept_id).
o Calculates the average salary per department.
o Filters the results to show only departments where the average salary is greater than
50,000 using HAVING.

-- Step 1: Create Database


CREATE DATABASE CompanyDB;
USE CompanyDB;

-- Step 2: Create Table


CREATE TABLE Employees (
emp_id INT PRIMARY KEY AUTO_INCREMENT,
emp_name VARCHAR(100) NOT NULL,
dept_id INT,
salary DECIMAL(10,2)
);

-- Step 3: Insert Sample Data


INSERT INTO Employees (emp_name, dept_id, salary) VALUES
('Alice', 1, 60000),
('Bob', 1, 50000),
('Charlie', 2, 70000),
('David', 2, 65000),
('Eve', 3, 40000),
('Frank', 3, 42000),
('Grace', 2, 80000);

-- Step 4: Write Query Using GROUP BY and HAVING


SELECT
dept_id,
AVG(salary) AS avg_salary
FROM Employees
GROUP BY dept_id
HAVING AVG(salary) > 50000;
-------------------------------------------------------------------------------------------------

Lab 3: Using WITH Clause to Create a Common Table Expression (CTE) in SQL

Lab Question Statement:

You are working on a Company Database that stores employee salary details and department
information. Your task is to:

1. Create two tables:


o Employees (emp_id, emp_name, dept_id, salary)
o Departments (dept_id, dept_name)
2. Insert sample data into these tables.
3. Write a complex query that:
o Uses a Common Table Expression (CTE) to find the average salary per department.
o Retrieves employees who earn more than the average salary of their department.
o Uses INNER JOIN to display their department names.

Solution:

-- Step 1: Create Database


CREATE DATABASE CompanyDB;
USE CompanyDB;

-- Step 2: Create Tables

CREATE TABLE Departments (


dept_id INT PRIMARY KEY,
dept_name VARCHAR(100) NOT NULL
);

CREATE TABLE Employees (


emp_id INT PRIMARY KEY AUTO_INCREMENT,
emp_name VARCHAR(100) NOT NULL,
dept_id INT,
salary DECIMAL(10,2),
FOREIGN KEY (dept_id) REFERENCES Departments(dept_id)
);

-- Step 3: Insert Sample Data

INSERT INTO Departments (dept_id, dept_name) VALUES


(1, 'HR'),
(2, 'IT'),
(3, 'Finance');

INSERT INTO Employees (emp_name, dept_id, salary) VALUES


('Alice', 1, 60000),
('Bob', 1, 50000),
('Charlie', 2, 70000),
('David', 2, 65000),
('Eve', 3, 40000),
('Frank', 3, 42000),
('Grace', 2, 80000);

-- Step 4: Create a CTE to Find Employees Earning More than Avg Salary of Their Department

WITH AvgSalary AS (
-- Calculate the average salary per department
SELECT dept_id, AVG(salary) AS avg_salary
FROM Employees
GROUP BY dept_id
)
SELECT e.emp_name, e.salary, d.dept_name, a.avg_salary
FROM Employees e
INNER JOIN AvgSalary a ON e.dept_id = a.dept_id
INNER JOIN Departments d ON e.dept_id = d.dept_id
WHERE e.salary > a.avg_salary;
---------------------------------------------------------------------------------------------------------------------

Mongo DB Lab
Lab 1: Introduction to MongoDB and Basic Commands
Objective: Install MongoDB and practice basic Mongo shell commands.

Tasks:

 Launch Mongo shell using mongo.


 Create a database:
use studentDB

Insert data into a collection:

db.students.insertOne({name: "Ali", rollno: 101, marks: 85})

Display all documents:

db.students.find()

Check current database:

Show all databases:

show dbs

Lab 2: CRUD Operations in MongoDB

Objective: Learn Create, Read, Update, and Delete operations.

Tasks:

 Insert multiple documents:

db.students.insertMany([

{name: "Sara", rollno: 102, marks: 90},

{name: "Usman", rollno: 103, marks: 78}

])

Read documents:

db.students.find()

db.students.find({marks: {$gt: 80}})

Update a document:

db.students.updateOne({rollno: 103}, {$set: {marks: 88}})

Delete a document:

db.students.deleteOne({rollno: 102})

Lab 3: Data Filtering and Projection

Objective: Use filters and projection in MongoDB queries.

Tasks:
Retrieve specific fields only:

db.students.find({}, {name: 1, marks: 1, _id: 0})

Apply conditions:

db.students.find({marks: {$gte: 80}})

db.students.find({$or: [{marks: {$gt: 85}}, {rollno: 103}]})

Count documents:

db.students.countDocuments()

Lab Question Statement:

Create a MongoDB collection named students inside a database called collegeDB. Perform the
following tasks to demonstrate all basic CRUD operations:

1. Create: Insert at least two documents into the students collection. Each document
should include fields: name, roll_no, and cgpa.
2. Read: Display all documents from the collection.
3. Update: Update the cgpa of the student with roll_no: "BSCS1001".
4. Delete: Delete the student document with roll_no: "BSCS1002".

Solution:

// 1. Switch to or create the database

use collegeDB

// 2. Insert two student documents

db.students.insertMany([

{ name: "Ali Raza", roll_no: "BSCS1001", cgpa: 3.5 },

{ name: "Sara Khan", roll_no: "BSCS1002", cgpa: 3.1 }

])

// 3. Display all documents (Read)

db.students.find()

// 4. Update CGPA of the student with roll_no "BSCS1001"

db.students.updateOne(

{ roll_no: "BSCS1001" },

{ $set: { cgpa: 3.8 } }


)

// 5. Delete the student with roll_no "BSCS1002"

db.students.deleteOne({ roll_no: "BSCS1002" })

---------------------------
Lab Question Statement:

Create a MongoDB collection named students inside a database called collegeDB. Perform the
following tasks to demonstrate basic CRUD operations and implement the concept of keys:

1. Create: Insert at least two documents into the students collection. Each document
should include the fields: roll_no, name, cgpa. The roll_no should act as a unique key
(simulate primary key behavior in MongoDB).
2. Read: Display all student documents using the find() method.
3. Update: Update the cgpa of the student with roll_no: "BSCS1001".
4. Delete: Delete the student whose roll_no is "BSCS1002".
5. Key Constraint: Create a unique index on the roll_no field to ensure no duplicate roll
numbers are allowed in the collection.

// 1. Use or create the database


use collegeDB
// 2. Create the 'students' collection and insert documents
db.students.insertMany([
{ roll_no: "BSCS1001", name: "Ali Raza", cgpa: 3.6 },
{ roll_no: "BSCS1002", name: "Fatima Noor", cgpa: 3.4 }
])
// 3. Read all student documents
db.students.find()
// 4. Update CGPA of student with roll_no "BSCS1001"
db.students.updateOne(
{ roll_no: "BSCS1001" },
{ $set: { cgpa: 3.9 } }
)
// 5. Delete student with roll_no "BSCS1002"
db.students.deleteOne({ roll_no: "BSCS1002" })
// 6. Create a unique index on 'roll_no' to simulate a primary key
db.students.createIndex({ roll_no: 1 }, { unique: true })
-------------------------------
Lab Question Statement:

Create a MongoDB database collegeDB and a collection students. Perform the following tasks
to demonstrate basic CRUD operations and apply aggregation functions:

1. Insert at least four documents into the students collection with the fields: roll_no,
name, department, and cgpa.
2. Use the find() method to display all student records.
3. Use updateOne() to change the cgpa of a student with a specific roll_no.
4. Use deleteOne() to remove a student with a particular roll_no.
5. Use the aggregation framework to calculate the average CGPA for each department
using the $group and $avg operators.

// 1. Use or create the database


use collegeDB
// 2. Insert multiple student documents
db.students.insertMany([
{ roll_no: "BSCS1001", name: "Ali Raza", department: "CS", cgpa: 3.7 },
{ roll_no: "BSCS1002", name: "Sara Khan", department: "CS", cgpa: 3.9 },
{ roll_no: "BSIT1003", name: "Ahmad Bilal", department: "IT", cgpa: 3.4 },
{ roll_no: "BSIT1004", name: "Zara Yousaf", department: "IT", cgpa: 3.2 }
])
// 3. Read all student records
db.students.find()
// 4. Update CGPA of a student
db.students.updateOne(
{ roll_no: "BSCS1001" },
{ $set: { cgpa: 3.85 } }
)
// 5. Delete a student by roll number
db.students.deleteOne({ roll_no: "BSIT1004" })
// 6. Aggregate: Average CGPA per department
db.students.aggregate([
{
$group: {
_id: "$department",
average_cgpa: { $avg: "$cgpa" }
}
}
])
--------------------------------------------
Lab Question Statement:

Create a relational table named students with the following fields:

 student_id (INTEGER, should be the Primary Key)


 name (VARCHAR, should be NOT NULL)
 email (VARCHAR, must be UNIQUE)
 cgpa (DECIMAL, must satisfy a CHECK constraint: CGPA should be between 2.0 and
4.0)

Perform the following tasks:

1. Create the table students with all the above-mentioned constraints.


2. Insert at least two records into the table.
3. Write a query to display only the student_id, name, and cgpa (i.e., project selected
columns only, not all).

Solution:
-- 1. Create the table with constraints
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
cgpa DECIMAL(3, 2) CHECK (cgpa BETWEEN 2.0 AND 4.0)
);
-- 2. Insert sample records
INSERT INTO students (student_id, name, email, cgpa)
VALUES
(1, 'Ali Raza', '[email protected]', 3.5),
(2, 'Sara Khan', '[email protected]', 3.8);
-- 3. Project only specific columns
SELECT student_id, name, cgpa FROM students;

You might also like