0% found this document useful (0 votes)
8 views

answer

The document outlines a series of database management tasks involving the creation and manipulation of various databases and tables, including 'Country', 'StoreDB', 'Inventory', 'University', and 'Library'. Each section details SQL commands for creating tables, inserting data, updating records, and querying information based on specific criteria. The tasks include sorting, filtering, and modifying data, demonstrating fundamental operations in database management systems.

Uploaded by

Disha Datta
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 views

answer

The document outlines a series of database management tasks involving the creation and manipulation of various databases and tables, including 'Country', 'StoreDB', 'Inventory', 'University', and 'Library'. Each section details SQL commands for creating tables, inserting data, updating records, and querying information based on specific criteria. The tasks include sorting, filtering, and modifying data, demonstrating fundamental operations in database management systems.

Uploaded by

Disha Datta
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/ 10

DBMS LAB ASSIGNMENT

1. Create a Table "Country". Create the columns id (INT), Country_name (VARCHAR),


Population (BIGINT), No_of_cities (INT)VInsert 10 rows of data in the table. Sort the table
in Ascending ORDER according to No_of_cities. Select the Countries whose name starts
with 'G'. SET the No_of_cities to 4300 where the Country_name is "India". Update the
Table add a new Column Country_GDP (BIGINT). Select the Country names where the
population is greater than 17.15 crores.
ANS:
CREATE DATABASE World; Create db world and create table Country

USE World;

-- Create Table 1.1


CREATE TABLE Country (
id INT,
Country_name VARCHAR(100),
Population BIGINT,
No_of_cities INT
);

-- Insert 10 Rows 1.2


INSERT INTO Country VALUES
(1, 'India', 1400000000, 4000),
(2, 'China', 1440000000, 6500),
(3, 'USA', 331000000, 2500),
(4, 'Germany', 83000000, 800),
(5, 'Greece', 10400000, 500),
(6, 'Brazil', 213000000, 1500),
(7, 'Russia', 146000000, 1600),
(8, 'Ghana', 31000000, 300),
(9, 'France', 67000000, 1000),
(10, 'Japan', 126000000, 1200);
-- Sort by No_of_cities Ascending 1.3
SELECT * FROM Country
ORDER BY No_of_cities ASC;

-- Select countries whose name starts with 'G' 1.4


SELECT * FROM Country
WHERE Country_name LIKE 'G%';

-- Set No_of_cities to 4300 where Country_name is 'India' 1.5


UPDATE Country
SET No_of_cities = 4300
WHERE Country_name = 'India';

-- Add new column Country_GDP 1.6


ALTER TABLE Country
ADD Country_GDP BIGINT;

-- Select Country names where population > 17.15 crores (171500000) 1.7
SELECT Country_name FROM Country
WHERE Population > 171500000;
2. Create a Database "StoreDB". Create Table "Sales" (sale_id INT, product VARCHAR, quantity
INT, sale_date DATE). Insert at least 10 rows of sales data. Write a query using GROUP BY
sale_date to compute total quantity sold per day. Write a query using GROUP BY product
HAVING SUM(quantity) > 100 to list best-selling products.
Ans:
-- Create Database
CREATE DATABASE StoreDB;

-- Use Database
USE StoreDB;

-- Create Sales Table 2.1


CREATE TABLE Sales (
sale_id INT,
product VARCHAR(100),
quantity INT,
sale_date DATE
);

-- Insert 10 Rows 2.2


INSERT INTO Sales VALUES
(1, 'Pen', 20, '2025-05-01'),
(2, 'Pencil', 15, '2025-05-01'),
(3, 'Notebook', 25, '2025-05-02'),
(4, 'Eraser', 10, '2025-05-02'),
(5, 'Pen', 50, '2025-05-03'),
(6, 'Pencil', 60, '2025-05-03'),
(7, 'Notebook', 70, '2025-05-04'),
(8, 'Pen', 40, '2025-05-04'),
(9, 'Eraser', 5, '2025-05-04'),
(10, 'Pencil', 35, '2025-05-05');
-- Group by sale_date and compute total quantity per day 2.3
SELECT sale_date, SUM(quantity) AS total_quantity
FROM Sales
GROUP BY sale_date;

-- Best-selling products (sum(quantity) > 100) 2.4


SELECT product, SUM(quantity) AS total_quantity
FROM Sales
GROUP BY product
HAVING SUM(quantity) > 100;
3. Create a Database "Inventory". Create a Table "Product". Create the columns
product_id(INT), product_name(VARCHAR), stock(INT), price(DECIMAL). Insert at least 10
rows of data. ORDER the table in ASCENDING according to price. Select all the Products
with stock < 50. Select all the Products with product_name starting with "P'. DELETE all the
Products where stock is 0. DROP the column price. Show the Full table.
Ans:
-- Create Database
CREATE DATABASE Inventory;

-- Use Database
USE Inventory;

-- Create Product Table 3.1


CREATE TABLE Product (
product_id INT,
product_name VARCHAR(100),
stock INT,
price DECIMAL(10,2)
);

-- Insert 10 Rows 3.2


INSERT INTO Product VALUES
(1, 'Pencil', 120, 5.00),
(2, 'Pen', 0, 10.00),
(3, 'Notebook', 50, 30.00),
(4, 'Eraser', 40, 3.50),
(5, 'Sharpener', 35, 7.00),
(6, 'Scale', 70, 12.00),
(7, 'Marker', 20, 15.00),
(8, 'Paper', 0, 2.00),
(9, 'Printer', 10, 200.00),
(10, 'Pad', 45, 25.00);
-- Order by price ascending 3.3
SELECT * FROM Product
ORDER BY price ASC;

-- Products with stock < 50 3.4


SELECT * FROM Product
WHERE stock < 50;

-- Products starting with 'P' 3.5


SELECT * FROM Product
WHERE product_name LIKE 'P%';

-- Delete products where stock = 0 3.6


DELETE FROM Product
WHERE stock = 0;

-- Drop the column price 3.7


ALTER TABLE Product
DROP COLUMN price;

-- Show the full table


SELECT * FROM Product;
4. Create a Database "University". Create a Table "Department". Create the columns id(int),
name_of_Department(Varchar), total_no_of_Students(int). Insert atleast 10 rows of data. ORDER the
table in Ascending according to the total_no_of_Students. Select all the Departments with
total_no_of_Students > 1500. Select all the Department with their name starting with 'C'. SET the total_no
of Students to 4500 where name_of_Department is "CSE". Show the Full table.
Ans:

-- Create Database

CREATE DATABASE University;

-- Use the Database

USE University;

-- Create Table 4.1

CREATE TABLE Department (

id INT,

name_of_Department VARCHAR(100),

total_no_of_Students INT

);

-- Insert 10 Rows 4.2

INSERT INTO Department VALUES

(1, 'CSE', 4000),

(2, 'ECE', 2200),

(3, 'Mechanical', 1800),

(4, 'Civil', 1600),

(5, 'Chemical', 1400),

(6, 'Biotechnology', 1300),

(7, 'IT', 1700),

(8, 'Aerospace', 1200),

(9, 'Mathematics', 1000),

(10, 'Cybersecurity', 1900);

-- Order by total_no_of_Students Ascending 4.3

SELECT * FROM Department

ORDER BY total_no_of_Students ASC;

-- Select Departments with total_no_of_Students > 1500 4.4


SELECT * FROM Department

WHERE total_no_of_Students > 1500;

-- Select Departments with name starting with 'C' 4.5

SELECT * FROM Department

WHERE name_of_Department LIKE 'C%';

-- Set total_no_of_Students = 4500 where name_of_Department is 'CSE' 4.6

UPDATE Department

SET total_no_of_Students = 4500

WHERE name_of_Department = 'CSE';

-- Show full table 4.7

SELECT * FROM Department;


5. Create a Database "Library". Create a Table "Book". Create the columns id(INT), title (VARCHAR), author
(VARCHAR), pages (INT),bpublished_year(INT). Insert at least 10 rows of data. ORDER the table in
DESCENDING according to published_year. Select all the Books with pages > 300. Select all the Books with
title starting with 'T'. SET pages to 500 where title is "Moby Dick". Show the Full table.
Ans:
-- Create Database
CREATE DATABASE Library;

-- Use the Database


USE Library;

-- Create Table 5.1


CREATE TABLE Book (
id INT,
title VARCHAR(100),
author VARCHAR(100),
pages INT,
published_year INT
);

-- Insert 10 Rows 5.2


INSERT INTO Book VALUES
(1, 'Moby Dick', 'Herman Melville', 500, 1851),
(2, 'To Kill a Mockingbird', 'Harper Lee', 324, 1960),
(3, 'The Great Gatsby', 'F. Scott Fitzgerald', 218, 1925),
(4, '1984', 'George Orwell', 328, 1949),
(5, 'The Catcher in the Rye', 'J.D. Salinger', 277, 1951),
(6, 'The Hobbit', 'J.R.R. Tolkien', 310, 1937),
(7, 'Pride and Prejudice', 'Jane Austen', 279, 1813),
(8, 'The Road', 'Cormac McCarthy', 287, 2006),
(9, 'The Alchemist', 'Paulo Coelho', 197, 1988),
(10, 'Thinking, Fast and Slow', 'Daniel Kahneman', 499, 2011);

-- Order by published_year DESC 5.3


SELECT * FROM Book
ORDER BY published_year DESC;

-- Select Books with pages > 300 5.4


SELECT * FROM Book
WHERE pages > 300;

-- Select Books with title starting with 'T' 5.5


SELECT * FROM Book
WHERE title LIKE 'T%';

-- Set pages = 500 where title is 'Moby Dick' 5.6


UPDATE Book
SET pages = 500
WHERE title = 'Moby Dick';

-- Show full table


SELECT * FROM Book;

You might also like