Exp.
Name: Create a database table,
Page No: 1
add constraints, insert rows, and Date: 2024-08-
S.No: 1
update and delete rows using SQL DDL 07
and DML commands.
ID: 2303921320522121
Aim:
1. Create a table called products with the following columns:
• prod_id: an integer that serves as the primary key.
• prod_name: a string to store the name of the product.
• price: a decimal value to store the price of the product.
2. Insert the following rows into the products table.
prod_id prod_name price
1 Laptop 999.99
2 Smartphone 499.99
2023-2027-IT-B
3 Tablet 299.99
3. After inserting the above rows, update the Smartphone price to 450.0
4. Delete the record of the Tablet from the table.
Note: Inserting Rows has been given to you in the editor, you need to write the
PSNA College of Engineering and Technology
remaining queries.
Source Code:
index.sql
drop table products;
create table products(prod_id int primary key,prod_name
varchar(25),price decimal(5,2));
-- Step 2: Insert rows into the products table
INSERT INTO products (prod_id, prod_name, price)
VALUES
(1, 'Laptop', 999.99),
(2, 'Smartphone', 499.99),
(3, 'Tablet', 299.99);
update products
set price= 450.0
where prod_name= 'Smartphone';
delete from products
where prod_name = 'Tablet';
Date: 2024-08-
S.No: 2 Exp. Name: SQL Exercise
Page No: 2
07
Aim:
You need to create two tables, orders and products, and establish a foreign key
ID: 2303921320522121
constraint between them.
Table Name: orders
Column
Data Type Constraints
Name
order_id SERIAL PRIMARY KEY
NOT NULL
product_id INTEGER FOREIGN KEY (product_id) REFERENCES products
(product_id)
quantity INTEGER NOT NULL
total_price DECIMAL(10,2) NOT NULL
2023-2027-IT-B
Table Name: products
Column Name Data Type Constraints
product_id SERIAL PRIMARY KEY
name VARCHAR(255) NOT NULL
price DECIMAL(10,2) NOT NULL
PSNA College of Engineering and Technology
stock INTEGER NOT NULL
Source Code:
user.sql
drop table if exists products CASCADE;
drop table if exists orders CASCADE;
create table products(product_id serial primary key,name varchar(255)
not null,price decimal(10,2),stock int not null);
create table orders(order_id serial primary key,product_id int not
null,quantity int not null,total_price decimal(10,2),foreign key
(product_id)references products (product_id));
Date: 2024-08-
S.No: 3 Exp. Name: SQL Exercise
Page No: 3
07
Aim:
Write an SQL query for the products table that selects all columns where the name
ID: 2303921320522121
column contains the following patterns:
• Any three characters followed by 'Desk'.
• Any word that starts with 'C'.
• Any word that ends with 'ir'.
• Any word that contains 'od' in the middle.
Table Design
Table Schema for products:
Column Name Data Type Constraints
product_id SERIAL PRIMARY KEY
2023-2027-IT-B
name VARCHAR(255) NOT NULL
category VARCHAR(255) NOT NULL
quantity INTEGER NOT NULL
price DECIMAL NOT NULL
Data Snapshot for products:
PSNA College of Engineering and Technology
product_id name category quantity price
1 Desk Lamp Lighting 10 29.99
2 Chair Furniture 5 89.99
3 Monitor Electronics 20 199.99
4 Office Desk Furniture 12 79.99
5 Keyboard Electronics 8 49.99
6 Desk Organizer Stationery 15 9.99
Expected Output:
The output should list all columns for products where the name column matches the
specified patterns using the LIKE clause with different wildcards.
product_id name category quantity price
2 Chair Furniture 5 89.99
Source Code:
user.sql
select * from products where name like '_desk' or name like 'C%' or
name like '%ir' or name like '%od%';
Exp. Name: Aggregate function - Date: 2024-08-
S.No: 4
Page No: 4
COUNT 07
Aim:
Write a PostgreSQL query to count the total number of products in the products table.
ID: 2303921320522121
Alias the result as total_products.
Sample Data: products
prod_id prod_name price
1 Laptop 999.99
2 Smartphone 499.99
3 Tablet 299.99
4 Monitor 199.99
5 Keyboard 49.99
2023-2027-IT-B
Expected Output:
total_products
5
Source Code:
index.sql
PSNA College of Engineering and Technology
select count (*) as total_products from products;
Date: 2024-08-
S.No: 5 Exp. Name: Aggregate function - AVG
Page No: 5
07
Aim:
Write a PostgreSQL query to calculate the average price of all products in the products
ID: 2303921320522121
table. Alias the result as average_price.
Sample Data: products
prod_id prod_name price
1 Laptop 999.99
2 Smartphone 499.99
3 Tablet 299.99
4 Monitor 199.99
5 Keyboard 49.99
2023-2027-IT-B
Expected Output:
average_price
409.99
Source Code:
index.sql
PSNA College of Engineering and Technology
select avg (price) as average_price from products;
Date: 2024-08-
S.No: 6 Exp. Name: Aggregate function - MIN
Page No: 6
07
Aim:
Write a PostgreSQL query to find the minimum price among all products in the products
ID: 2303921320522121
table. Alias the result as minimum_price.
Sample Data: products
prod_id prod_name price
1 Laptop 999.99
2 Smartphone 499.99
3 Tablet 299.99
4 Monitor 199.99
5 Keyboard 49.99
2023-2027-IT-B
Expected Output:
minimum_price
49.99
Source Code:
index.sql
PSNA College of Engineering and Technology
select min (price) as minimum_price from products;
Date: 2024-08-
S.No: 7 Exp. Name: Aggregate function - MAX
Page No: 7
07
Aim:
Write a PostgreSQL query to find the maximum price among all products in the products
ID: 2303921320522121
table. Alias the result as maximum_price.
Sample Data: products
prod_id prod_name price
1 Laptop 999.99
2 Smartphone 499.99
3 Tablet 299.99
4 Monitor 199.99
5 Keyboard 49.99
2023-2027-IT-B
Expected Output:
Source Code:
index.sql
PSNA College of Engineering and Technology
select max(price) as maximum_price from products;
Date: 2024-08-
S.No: 8 Exp. Name: Aggregate function - SUM
Page No: 8
07
Aim:
Write a PostgreSQL query to calculate the total price of all products in the products
ID: 2303921320522121
table. Alias the result as total_price.
Sample Data: Products
prod_id prod_name price
1 Laptop 999.99
2 Smartphone 499.99
3 Tablet 299.99
4 Monitor 199.99
5 Keyboard 49.99
2023-2027-IT-B
Output:
Source Code:
index.sql
PSNA College of Engineering and Technology
select sum(price) as total_price from products;
Exp. Name: Basic Data Retrieval and Date: 2024-08-
S.No: 9
Page No: 9
Join Operations 14
Aim:
Write SQL queries to explore basic data retrieval and join operations using the products
ID: 2303921320522121
and sales tables.
products:
productid productname category price quantity
1 Laptop Electronics 1000.00 50
2 Smartphone Electronics 500.00 100
3 Tablet Electronics 300.00 75
4 Headphones Accessories 150.00 200
sales:
2023-2027-IT-B
saleid productid saledate quantitysold
1 1 2024-07-01 5
2 2 2024-07-02 10
3 3 2024-07-03 8
4 1 2024-07-04 12
5 4 2024-07-05 15
PSNA College of Engineering and Technology
1. Write a query to retrieve the productname, price, and quantity from the products
table for products that belong to the "Electronics" category.
2. Write a query to retrieve the productname, saledate, and quantitysold by joining the
products and sales tables. Ensure to include only those sales where the quantitysold is
greater than 5.
Source Code:
index.sql
select productname,price,quantity from products where
category='Electronics';
select pro.productname,sa.saledate,sa.quantitysold from products pro
join sales sa on pro.productid=sa.productid where sa.quantitysold>5;
Date: 2024-08-
S.No: 10 Exp. Name: Natural Join
Page No: 10
14
Aim:
Consider two tables, class_A and class_B, with the following structure:
ID: 2303921320522121
Table: class_A
id (INT, PRIMARY KEY) name (VARCHAR(20)) marks (INT)
101 Abirami 98
102 Arthi 88
103 Babu 92
Table: class_B
id (INT, PRIMARY KEY) address (VARCHAR(20)) age (INT)
101 Trichy 20
102 Thottiam 22
2023-2027-IT-B
104 Namakkal 23
105 Salem 24
Write a PostSQL query to calculate the natural join of the two classes (class_A and
class_B) based on the common column id. Provide the result with columns id, name,
marks, address, and age.
PSNA College of Engineering and Technology
Expected Output:
id name marks address age
101 Abirami 98 Trichy 20
102 Arthi 88 Thottiam 22
Source Code:
user.sql
-- Task 1: SQL Query
select*from class_A natural join class_b;
Date: 2024-08-
S.No: 11 Exp. Name: Left Outer Join
Page No: 11
14
Aim:
Consider two tables, class_A and class_B, with the following structure:
ID: 2303921320522121
Table: class_A
id (INT, PRIMARY KEY) name (VARCHAR(20)) marks (INT)
101 Abiram 98
102 Arthi 88
103 Babu 92
Table: class_B
id (INT, PRIMARY KEY) address (VARCHAR(20)) age (INT)
101 Trichy 20
2023-2027-IT-B
102 Thottiam 22
104 Namakkal 23
105 Salem 24
Write a PostgreSQL query to perform the LEFT OUTER JOIN between class_A and class_B
on the id column. Retrieve columns: id (class_A), name, marks, id (class_B), address, age
PSNA College of Engineering and Technology
Output:
id name marks id address age
101 Abiram 98 101 Trichy 20
102 Arthi 88 102 Thottiam 22
103 Babu 92
Source Code:
user.sql
-- Task 1: SQL Query
select * from class_A left outer join class_B on class_A.id=class_B.id;
Date: 2024-08-
S.No: 12 Exp. Name: Right Outer Join
Page No: 12
14
Aim:
Consider two tables, class_A and class_B, with the following structure:
ID: 2303921320522121
Table: class_A
id (INT, PRIMARY KEY) name (VARCHAR(20)) marks (INT)
101 Abiram 98
102 Arthi 88
103 Babu 92
Table: class_B
id (INT, PRIMARY KEY) address (VARCHAR(20)) age (INT)
101 Trichy 20
102 Thottiam 22
2023-2027-IT-B
104 Namakkal 23
105 Salem 24
Write a PostgreSQL query to perform the RIGHT OUTER JOIN between class_A and
class_B on the id column. Retrieve columns: id (class_A), name, marks, id (class_B),
address, age.
PSNA College of Engineering and Technology
Expected Output:
id name marks id address age
101 Abiram 98 101 Trichy 20
102 Arthi 88 102 Thottiam 22
104 Namakkal 23
105 Salem 24
Source Code:
user.sql
-- Task 1: SQL Query
select * from class_A right outer join class_B on class_A.id=class_B.id
Exp. Name: Largest of the three Date: 2024-10-
S.No: 13
Page No: 13
numbers 15
Aim:
You are required to create a pgSQL function named find_largest_number. This function
ID: 2303921320522121
should take three integer parameters, num1, num2, and num3, and return the largest of
these three integers.
Source Code:
index.sql
CREATE OR REPLACE function find_largest_number(num1 INT, num2 INT, num3
INT)
RETURNS INT AS $$
DECLARE
largest INT;
2023-2027-IT-B
BEGIN
-- Initalize the largest number as the first number
largest := num1;
-- Compare with the second number
IF num2 > largest THEN
largest := num2;
PSNA College of Engineering and Technology
END IF;
-- Compare with the second number
If num3 > largest THEN
largest := num3;
END IF;
-- Return the largest number
RETURN largest;
END;
$$ LANGUAGE plpgsql;
Exp. Name: Remove the vowels from the Date: 2024-10-
S.No: 14
Page No: 14
string 15
Aim:
Your task is to create a pgSQL function named remove_vowels. This function should take
ID: 2303921320522121
a single text parameter input_string and return a new string with all vowels removed('a',
'e', 'i', 'o', 'u', both uppercase and lowercase).
Source Code:
index.sql
CREATE OR REPLACE FUNCTION remove_vowels(input_string TEXT)
RETURNS TEXT AS $$
BEGIN
-- Remove vowels from the input sttring using REGEXP_REPLACE
RETURN REGEXP_REPLACE (input_string, '[aeiou AEIOU]', '', 'g');
2023-2027-IT-B
END;
$$ LANGUAGE plpgsql
PSNA College of Engineering and Technology
Date: 2024-08-
S.No: 15 Exp. Name: SQL Exercise
Page No: 15
28
Aim:
You are required to write an SQL query that grants SELECT access to the user
ID: 2303921320522121
"customer1" on the "orders" table in the CodeKraft database. Assume that the user
"customer1" already exists.
Table Design
Table: orders
Column Name Data Type Constraints
order_id SERIAL PRIMARY KEY
customer_id INT NOT NULL
product_id INT NOT NULL
order_date DATE NOT NULL
2023-2027-IT-B
quantity INT NOT NULL
Expected Output
The query should grant SELECT access to "customer1" on the "orders" table in the
CodeKraft database.
Source Code:
PSNA College of Engineering and Technology
user.sql
grant select on orders to customer1;
Exp. Name: REVOKE SELECT on products Date: 2024-08-
S.No: 16
Page No: 16
Table 28
Aim:
You are required to write an SQL query that revokes SELECT access to the user "john" on
ID: 2303921320522121
the products table in the CodeKraft database. Assume that the user "john" already exists
and has access to execute the SELECT.
Table Design
Table: products
Column Name Data Type
product_id SERIAL
name VARCHAR(255)
category VARCHAR(255)
quantity INT
2023-2027-IT-B
price DECIMAL
Source Code:
user.sql
revoke select on products from john;
PSNA College of Engineering and Technology
Date: 2024-10-
S.No: 17 Exp. Name: Trigger after INSERT
Page No: 17
15
Aim:
You are tasked with managing a retail company's SQL database. The company uses a
ID: 2303921320522121
database to track product inventory and sales.
You need to create a trigger to ensure that the inventory levels are updated correctly
whenever a new sale is inserted.
Table Definitions and Data:
products:
Column Name Data Type Constraints
productid INT PRIMARY KEY
productname VARCHAR NOT NULL
category VARCHAR -
2023-2027-IT-B
price DECIMAL(10, 2) CHECK (price > 0)
quantity INT DEFAULT 0
productID productName category price quantity
1 Laptop Electronics 1000.00 50
2 Smartphone Electronics 500.00 100
PSNA College of Engineering and Technology
3 Tablet Electronics 300.00 75
sales:
Column Name Data Type Constraints
saleid INT PRIMARY KEY
productid INT REFERENCES products(productid)
saledate DATE -
quantitysold INT CHECK (quantitysold > 0)
Task
1. Create a Trigger Function:
Write a function named update_product_quantity that:
• Updates the quantity in the products table by subtracting the quantitySold from
the existing quantity whenever a new sale is inserted.
2. Create a Trigger:
Define a trigger named after_sale_insert that:
• Fires after an insert operation on the sales table.
• Executes the update_product_quantity function to update the products table.
Source Code:
index.sql
Page No: 18
-- Create the update_product_quantity function
CREATE OR REPLACE FUNCTION update_product_quantity()
RETURNS TRIGGER AS $$
BEGIN
UPDATE products
ID: 2303921320522121
SET quantity = quantity - NEW.quantitysold
WHERE productid = NEW.productid;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Create the after_sale_insert trigger
CREATE TRIGGER after_sale_insert
AFTER INSERT ON sales
FOR EACH ROW
EXECUTE PROCEDURE update_product_quantity();
2023-2027-IT-B
PSNA College of Engineering and Technology
Date: 2024-10-
S.No: 18 Exp. Name: Trigger after DELETE
Page No: 19
15
Aim:
You are tasked with managing a retail company's SQL database. The company uses a
ID: 2303921320522121
database to track product inventory and sales.
You need to create a trigger to ensure that the inventory levels are updated correctly
whenever a sale is deleted.
Table Definitions and Data:
products:
Column Name Data Type Constraints
productid INT PRIMARY KEY
productname VARCHAR NOT NULL
2023-2027-IT-B
category VARCHAR -
price DECIMAL(10, 2) CHECK (price > 0)
quantity INT DEFAULT 0
productID productName category price quantity
1 Laptop Electronics 1000.00 50
PSNA College of Engineering and Technology
2 Smartphone Electronics 500.00 100
3 Tablet Electronics 300.00 75
sales:
Column Name Data Type Constraints
saleid INT PRIMARY KEY
productid INT REFERENCES products(productid)
saledate DATE -
quantitysold INT CHECK (quantitysold > 0)
saleid productid saledate quantitysold
1 1 2024-07-01 5
2 2 2024-07-02 10
3 3 2024-07-03 8
Task
1. Create a Trigger Function:
Write a function named restore_product_quantity that:
• Updates the quantity in the products table by adding the quantitysold to the
existing quantity whenever a sale is deleted.
Page No: 20
2. Create a Trigger:
Define a trigger named after_sale_delete that:
• Fires after a delete operation on the sales table.
• Executes the restore_product_quantity function to update the products table.
ID: 2303921320522121
Source Code:
index.sql
create or replace function restore_product_quantity()
returns Trigger as $$
begin
update products
set quantity=quantity+old.quantitysold where
productid=old.productid;
return old;
2023-2027-IT-B
end;
$$ language plpgsql;
create Trigger after_sale_delete
after delete on sales for each row
execute function restore_product_quantity();
PSNA College of Engineering and Technology
Date: 2024-10-
S.No: 19 Exp. Name: Trigger after UPDATE
Page No: 21
15
Aim:
You are tasked with managing a retail company's SQL database. The company uses a
ID: 2303921320522121
database to track product inventory and sales.
You need to create a trigger to ensure that the inventory levels are updated correctly
whenever a sale is updated.
Table Definitions and Data:
products:
Column Name Data Type Constraints
productid INT PRIMARY KEY
productname VARCHAR NOT NULL
2023-2027-IT-B
category VARCHAR -
price DECIMAL(10, 2) CHECK (price > 0)
quantity INT DEFAULT 0
productID productName category price quantity
1 Laptop Electronics 1000.00 50
PSNA College of Engineering and Technology
2 Smartphone Electronics 500.00 100
3 Tablet Electronics 300.00 75
sales:
Column Name Data Type Constraints
saleid INT PRIMARY KEY
productid INT REFERENCES products(productid)
saledate DATE -
quantitysold INT CHECK (quantitysold > 0)
saleid productid saledate quantitysold
1 1 2024-07-01 5
2 2 2024-07-02 10
3 3 2024-07-03 8
Task
1. Create a Trigger Function:
Write a function named update_product_quantity that:
• Updates the quantity in the products table by adjusting the quantity(quantity =
quantity + old quantitysold - new quantitysold) based on the old and new
Page No: 22
quantitysold values whenever a sale is updated.
2. Create a Trigger:
Define a trigger named after_sale_update that:
• Fires after an update operation on the sales table.
ID: 2303921320522121
• Executes the update_product_quantity function to update the products table.
Source Code:
index.sql
create or replace function update_product_quantity()
returns trigger as $$
begin
update products
set quantity=quantity+old.quantitysold-new.quantitysold where
productid=new.productid;
2023-2027-IT-B
return new;
end;
$$ language plpgsql;
create trigger after_sale_update
after update on sales
for each row
PSNA College of Engineering and Technology
execute function update_product_quantity();
Exp. Name: Create View to Display Date: 2024-10-
S.No: 20
Page No: 23
Product Names and Prices 15
Aim:
You are required to create a view named product_prices that displays the product name
ID: 2303921320522121
and price for products in the products table.
Table: products
Column Name Data Type Constraints
product_id SERIAL PRIMARY KEY
name VARCHAR(255) NOT NULL
price DECIMAL NOT NULL
Data Snapshot for products
product_id name price
2023-2027-IT-B
1 Desk Lamp 29.99
2 Office Chair 89.99
Source Code:
user.sql
CREATE VIEW product_prices as
PSNA College of Engineering and Technology
SELECT name, price
FROM products;