RDBMS Lab Manual W5
RDBMS Lab Manual W5
CCS 1
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
DBMS Languages
A DBMS supports various languages to interact with the database. The main types of DBMS languages
are:
1. Data Definition Language (DDL):
o Purpose: Defines the structure of the database, including creating, altering, and deleting
tables, schemas, and other database objects.
o Common Commands:
▪ CREATE: Defines a new table or database object.
▪ ALTER: Modifies an existing database object.
▪ DROP: Deletes a database object.
▪ TRUNCATE: Removes all records from a table, retaining the table structure.
2. Data Manipulation Language (DML):
o Purpose: Manages and manipulates data in the database (insert, update, delete,
retrieve).
o Common Commands:
▪ INSERT: Adds new data into a table.
▪ UPDATE: Modifies existing data in a table.
▪ DELETE: Removes data from a table.
▪ SELECT: Retrieves data from a table.
3. Data Control Language (DCL):
o Purpose: Controls access to the database and manages permissions.
o Common Commands:
CCS 2
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
o UPDATE:
UPDATE Students SET Age = 23 WHERE StudentID = 1;
o DELETE:
DELETE FROM Students WHERE Age < 18;
3. Data Control Language (DCL):
o GRANT:
GRANT SELECT, INSERT ON Students TO UserName;
o REVOKE:
REVOKE SELECT ON Students FROM UserName;
4. Transaction Control Language (TCL):
o COMMIT:
COMMIT;
o ROLLBACK:
ROLLBACK;
CCS 4
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
Banking Database
Week-2
This lab focuses on creating a database schema for a Banking Database and performing operations
using SQL. The database schema includes six tables: BRANCH, ACCOUNT, CUSTOMER,
DEPOSITOR, LOAN, and BORROWER.
Objectives
1. Learn to create relational database tables with primary and foreign keys.
1. Creating Tables
a. BRANCH Table
city VARCHAR(50),
asset VARCHAR(50)
);
b. ACCOUNT Table
branch_name VARCHAR(50),
balance FLOAT,
CCS 5
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
);
c. CUSTOMER Table
street VARCHAR(100),
city VARCHAR(50)
);
d. DEPOSITOR Table
customer_name VARCHAR(50),
account_number VARCHAR(50),
);
• Foreign Keys:
e. LOAN Table
CCS 6
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
branch_name VARCHAR(50),
amount INTEGER,
);
f. BORROWER Table
customer_name VARCHAR(50),
loan_number VARCHAR(50),
);
• Foreign Keys:
2. Inserting Data
CCS 7
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
('Bob', 'ACC002');
('Bob', 'LN002');
3. Selecting Data
CCS 8
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
FROM CUSTOMER c
Primary Keys:
• BRANCH(branch_name)
• ACCOUNT(account_number)
• CUSTOMER(customer_name)
• LOAN(loan_number)
Foreign Keys:
• ACCOUNT(branch_name) → BRANCH(branch_name)
• DEPOSITOR(customer_name) → CUSTOMER(customer_name)
• DEPOSITOR(account_number) → ACCOUNT(account_number)
• LOAN(branch_name) → BRANCH(branch_name)
• BORROWER(customer_name) → CUSTOMER(customer_name)
• BORROWER(loan_number) → LOAN(loan_number)
CCS 9
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
Week-3
Let's continue from the Banking Database and generate the SQL queries for the additional tasks. We
will focus on specific queries involving aggregate functions, comparison operators, and logical
operators. Below are the SQL queries for the required tasks.
1. List the loan number from LOAN having amount 10000 with a specific branch name.
For this query, we will filter the LOAN table for rows where the amount is 10000 and the branch_name
matches a specific branch.
SELECT loan_number
FROM LOAN
2. List the loan number with an amount between 1000 and 10000.
This query uses the BETWEEN operator to filter the LOAN table for loans where the amount is
between 1000 and 10000.
SELECT loan_number
FROM LOAN
If we want to filter customer names containing a specific substring (e.g., names containing "Ali"), we
can use the LIKE operator with wildcard characters (%).
SELECT customer_name
FROM CUSTOMER
To get the count of records (tuples) in the CUSTOMER table, we use the COUNT() aggregate function.
FROM CUSTOMER;
CCS 10
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
5. List customer name, loan number, and amount with a specific branch name.
This query involves joining the BORROWER, LOAN, and CUSTOMER tables to list the customer
name, loan number, and loan amount for a specific branch.
FROM BORROWER b
The SUM() aggregate function is used to calculate the total sum of all loan amounts.
FROM LOAN;
The AVG() function calculates the average balance from the ACCOUNT table.
FROM ACCOUNT;
FROM LOAN;
FROM ACCOUNT;
FROM LOAN;
Here we use the > operator to find accounts with a balance greater than 5000.
FROM CUSTOMER c
We use the > comparison operator to find loan amounts greater than 5000.
FROM BORROWER b
Using the <= operator, this query retrieves accounts with a balance less than or equal to 1000.
FROM ACCOUNT a
d. Find customers with loans less than 5000 and belonging to a specific branch:
FROM BORROWER b
CCS 12
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
a. Find customers who have a balance greater than 1000 and have a loan of more than 5000:
This query uses AND to combine the two conditions: balance greater than 1000 and loan amount
greater than 5000.
FROM CUSTOMER c
b. Find customers who either have a loan or an account with a balance greater than 5000:
This query uses the OR operator to find customers who meet at least one of the conditions.
SELECT c.customer_name
FROM CUSTOMER c
c. Find customers with a loan amount between 1000 and 5000, and from a specific branch:
Here, we combine AND and BETWEEN to find customers who have a loan between 1000 and 5000
and are from a specific branch.
CCS 13
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
FROM BORROWER b
d. Find customers with names starting with "A" and having an account balance greater than
2000:
This query uses both LIKE and the AND logical operator.
FROM CUSTOMER c
• List loan numbers based on conditions: Using WHERE, BETWEEN, and comparison
operators.
• Substring search: Using the LIKE operator with %.
• Count tuples: Using COUNT() aggregate function.
• Join tables for specific information: Using JOIN to combine data from multiple tables.
• Aggregate functions: Using SUM(), AVG(), MAX(), MIN(), and COUNT() for calculations.
• Comparison operators: Using =, <, >, <=, >=, and BETWEEN to filter data.
• Logical operators: Using AND, OR, and NOT to combine multiple conditions.
These queries address the tasks for retrieving data from the Banking Database, including using
aggregate functions, comparison operators, and logical operators.
CCS 14
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
Week-4
Let's design and generate SQL queries for the Accessories Database, which includes the PRODUCT,
PC, LAPTOP, and PRINTER tables, based on the requirements you provided.
a. PRODUCT Table
maker VARCHAR(50),
model VARCHAR(50),
type VARCHAR(50),
);
• Primary Key: The combination of maker and model will be the primary key to uniquely
identify each product.
b. PC Table
CREATE TABLE PC (
speed VARCHAR(50),
ram VARCHAR(50),
hdd VARCHAR(50),
removable_disk INT,
price INT
);
c. LAPTOP Table
CCS 15
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
speed VARCHAR(50),
ram VARCHAR(50),
hdd VARCHAR(50),
screen VARCHAR(50),
price INT
);
• Primary Key: model is the primary key for the LAPTOP table.
d. PRINTER Table
color BLOB,
type VARCHAR(50),
price INT
);
• Primary Key: model is the primary key for the PRINTER table.
2. Inserting Data
VALUES ('Laptop001', '2.6 GHz', '8 GB', '512 GB', '15.6 inches',
45000),
('Laptop002', '3.1 GHz', '16 GB', '1 TB', '14 inches', 70000);
3. SQL Queries
a. Find the model, speed, RAM, and HDD for all PCs whose price is under 35000.
FROM PC
• This query filters the PC table to find PCs with a price less than 35000 and retrieves the
model, speed, ram, and hdd attributes.
b. Rename the speed column to mhz and hdd column to GB in the PC table.
In SQL, renaming columns depends on the database system (e.g., MySQL, PostgreSQL, SQL Server).
For MySQL, the syntax is as follows:
ALTER TABLE PC
FROM PRODUCT
CCS 17
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
• This query selects distinct manufacturers (maker) from the PRODUCT table where the type is
'Laptop'.
d. Find all tuples in PRINTER for color.
FROM PRINTER;
• This query retrieves all model and color from the PRINTER table.
FROM PC;
• SUM() calculates the total price of all the PCs in the PC table.
b. Get the average price of all laptops.
FROM LAPTOP;
• AVG() calculates the average price of all laptops in the LAPTOP table.
c. Get the maximum price of a printer.
FROM PRINTER;
FROM PC;
FROM LAPTOP;
CCS 18
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
FROM PC
FROM LAPTOP
• This query filters laptops where the screen size is smaller than '15 inches'.
c. Find all printers with a price less than or equal to 20000.
FROM PRINTER
• This query filters printers with a price less than or equal to 20000.
d. Find all products (laptops) from a specific maker, e.g., 'HP'.
SELECT model
FROM PRODUCT
CCS 19
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
FROM PC
• This query uses the OR logical operator to filter PCs that have a price under 30000 or a
removable disk (removable_disk = 1).
b. Find all laptops with a price greater than 50000 and a screen size larger than 15 inches.
FROM LAPTOP
• This query uses the AND logical operator to find laptops that are priced above 50000 and
have a screen larger than 15 inches.
c. Find all printers where the price is either 15000 or the color is 'Black'.
FROM PRINTER
• This query uses the OR operator to filter printers that either cost 15000 or have a 'Black'
color.
d. Find all laptops where the screen size is not '15.6 inches'.
FROM LAPTOP
• This query uses the != operator to find laptops where the screen size is not '15.6 inches'.
• Find models, speed, RAM, HDD for PCs under a certain price: Using WHERE and
comparison operators.
• Rename columns in a table: Using ALTER TABLE with CHANGE COLUMN.
• Find all manufacturers of laptops: Using DISTINCT with WHERE.
• Retrieve specific columns: Using SELECT queries with relevant filters.
• Aggregate functions: SUM(), AVG(), MAX(), MIN(), COUNT().
CCS 20
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
CCS 21
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
Week -5
Creating Tables
1 CREATE TABLE CUSTOMER (
Cust_name VARCHAR(100),
City VARCHAR(50)
);
Order_date DATE,
Cust_id VARCHAR(50),
Order_amount INT,
);
Unit_price INT
);
Order_num VARCHAR(50),
Item_id VARCHAR(50),
Quantity INT,
CCS 22
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
);
City VARCHAR(50)
);
Order_num VARCHAR(50),
Warehouse_id VARCHAR(50),
Shipdate DATE,
);
Inserting Data
1 INSERT INTO CUSTOMER (Cust_id, Cust_name, City) VALUES
('I001', 100),
('I002', 200);
Queries
FROM CUSTOMER C
GROUP BY C.Cust_name;
CCS 24
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
2. List order numbers for orders shipped from all warehouses in a specific city.
FROM SHIPMENT S
SELECT
COUNT(*) AS Total_Orders,
SUM(Order_amount) AS Total_Revenue,
MAX(Order_amount) AS Max_Order_Amount,
MIN(Order_amount) AS Min_Order_Amount
FROM ORDER_TABLE;
4. Use comparison operators (Find orders with amount greater than 500)
5. Use logical operators (Find customers from New York with orders above 600)
FROM CUSTOMER C
CCS 25
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
Query:
FROM ORDER
GROUP BY cust_id;
Explanation: This query calculates the total amount spent by each customer.
b) Find the average order amount for all orders placed in the last 6 months
Query:
FROM ORDER
Explanation: Retrieves the average order amount for orders placed in the last 6 months.
c) Find the highest and lowest unit price from the ITEM table
Query:
FROM ITEM;
Explanation: Retrieves the highest and lowest unit prices from the ITEM table.
Query:
FROM SHIPMENT
GROUP BY warehouse_id;
Query:
FROM ORDERITEM
CCS 26
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
GROUP BY item_id;
a) Find all orders where the order amount is greater than 10,000
Query:
FROM ORDER
Explanation: Retrieves all orders where the order amount is greater than 10,000.
Query:
FROM CUSTOMER
Query:
FROM ITEM
Query:
FROM ORDER
CCS 27
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
Query:
FROM ORDER
GROUP BY cust_id
a) Find all customers who are either from 'New York' or 'Los Angeles'
Query:
FROM CUSTOMER
b) Find all orders with an amount greater than 5000 and placed after '2023-06-01'
Query:
FROM ORDER
c) Find all shipments made from warehouses in 'Chicago' but not from 'New York'
Query:
CCS 28
JSS SCIENCE AND TECHNOLOGY UNIVERSITY
DEPARTMENT OF COMPUTER APPLICATIONS
FROM SHIPMENT S
Explanation: Retrieves shipments made from 'Chicago' while excluding 'New York'.
Query:
SELECT order_num
FROM ORDERITEM
Query:
FROM CUSTOMER C
Replace certain value based on your inserted tuple values wherever it is applicable in queries.
CCS 29