DBMS practical practice
DBMS practical practice
DESCRIBE product
DESCRIBE customer
'Display the details of all products of which the cost is more than 1500'
SELECT * FROM product
WHERE prod_cost > 1500;
'Display the details of all products of which the cost is more than 1500 but less than 25000'
SELECT * FROM product
WHERE prod_cost > 1500 and prod_cost < 25000;
'Display the details of all products of which the cost is more than the products of category
clothing'
SELECT * FROM product
WHERE prod_cost > (SELECT MAX(prod_cost) FROM product WHERE prod_catg =
'Clothes');
'Display the details of all products which are manufactured after 30th June 2023'
SELECT * FROM product
WHERE prod_mfg > '30-Jun-2023';
'Display all products whose product name starts with character "S"'
SELECT * FROM product
WHERE SUBSTR(prod_name, 1, 1) = 'S';
'Display the details of all products which have the same brand and cost is more than 2000'
SELECT * FROM product
WHERE prod_brand IN (SELECT prod_brand FROM product GROUP BY prod_brand
HAVING COUNT(*) > 1) and prod_cost > 2000;
'Display the details of all products of which the cost is more than the cost of products with
brand <most frequentlly occured>'
SELECT * FROM product
WHERE prod_cost > (
SELECT MAX(prod_cost)
FROM product
WHERE prod_brand = (
SELECT prod_brand
FROM (
SELECT prod_brand, COUNT(*) AS brand_count
FROM product
GROUP BY prod_brand
ORDER BY brand_count DESC
)
WHERE ROWNUM = 1
)
);
'Display customer details who have purchased chair using nested query'
SELECT * FROM customer
WHERE cust_id = (SELECT cust_id FROM order_on WHERE prod_id = (SELECT prod_id
FROM product WHERE prod_name = 'Chair'))
'Display customer details who have purchased <specific object> using join'
SELECT customer.* FROM customer
JOIN order_on ON customer.cust_id = order_on.cust_id
JOIN product ON order_on.prod_id = product.prod_id
WHERE product.prod_name = 'Chair';
'Display customer details who have purchased <specific catg> using join'
SELECT customer.* FROM customer
JOIN order_on ON customer.cust_id = order_on.cust_id
JOIN product ON order_on.prod_id = product.prod_id
WHERE product.prod_catg = 'Furniture';
'Display product details purchased by people age less than 30 using join'
SELECT product.* FROM product
JOIN order_on ON product.prod_id = order_on.prod_id
JOIN customer ON order_on.cust_id = customer.cust_id
WHERE customer.age < 30;
'Display customer details who have purchased items with cost more than 25000 using join'
SELECT customer.* FROM customer
JOIN order_on ON customer.cust_id = order_on.cust_id
JOIN product ON order_on.prod_id = product.prod_id
WHERE product.prod_cost > 25000;
'Display the names and contact number whose customer id is same as 1005'
SELECT cust_name, mob_no FROM customer
WHERE cust_id = 1005
'Display the names of products of which the cost is more than the average cost of the products
of clothing category'
SELECT * FROM product WHERE prod_cost > (SELECT AVG(prod_cost) FROM product
WHERE prod_catg = 'Clothes')
'Display the name of the product which has 2nd max cost (By using nested query only)'
SELECT * FROM product WHERE prod_cost = (SELECT MAX(prod_cost)FROM product
WHERE prod_cost < (SELECT MAX(prod_cost) FROM product WHERE prod_cost))
'Display the category of all products for which more than 3 products are available'
SELECT p.prod_catg, SUM(o.order_qty) as total_order_qty
FROM product p
JOIN order_on o ON p.prod_id = o.prod_id
GROUP BY p.prod_catg
HAVING SUM(o.order_qty) > 3;
'Display the names of the cost of all products of which the cost is less than the max cost of
the product of electronic category'
SELECT prod_name FROM product WHERE prod_cost < (SELECT MAX(prod_cost)
FROM product WHERE prod_catg = 'Electronics')
'Diplay the details of all products whose mode of pay is not UPI'
SELECT p.* FROM product p, order_on o WHERE o.prod_id = p.prod_id AND
o.mode_pay != 'UPI'
'Diplay the details of all customers whose mode of pay is not UPI'
SELECT c.* FROM customer c, order_on o WHERE o.cust_id = c.cust_id AND
o.mode_pay != 'UPI'
'Display the product name, brand of all products ordered in year 2023'
SELECT p.prod_name AS NAME, p.prod_brand AS BRAND, EXTRACT(YEAR FROM
o.ord_date) AS order_date
FROM product p
JOIN order_on o ON p.prod_id = o.prod_id
WHERE EXTRACT(YEAR FROM o.ord_date) = 2023
DESCRIBE order_on
' display all customer contact details along with their city and age.'
CREATE VIEW customer_contact_info AS
SELECT cust_id, cust_name, mob_no, cust_city, cust_street, age
FROM customer;
'view will summarize orders by date, showing the total quantity ordered on each day and the
most frequent payment mode used.'
CREATE VIEW orders_summary_by_date AS
SELECT ord_date, COUNT(order_id) AS total_orders, SUM(order_qty) AS total_quantity,
mode_pay
FROM order_on
GROUP BY ord_date, mode_pay;
'view shows the details of all available products (where prod_cost is greater than 100).'
CREATE VIEW available_products AS
SELECT prod_id, prod_name, prod_brand, prod_catg,prod_cost, prod_mfg
FROM product
WHERE prod_cost > 100;
'view will show the details of the orders placed, along with the expected delivery date.'
CREATE VIEW order_delivery_status AS
SELECT o.order_id,c.cust_name,p.prod_name,o.ord_date AS order_date,o.order_dod AS
delivery_date
FROM order_on o
JOIN customer c ON o.cust_id = c.cust_id
JOIN product p ON o.prod_id = p.prod_id;
'Displays the total spending of each customer by calculating the total amount they spent
across all orders.'
CREATE VIEW customer_spending AS
SELECT c.cust_id, c.cust_name, SUM(p.prod_cost * o.order_qty) AS total_spent
FROM customer c
JOIN order_on o ON c.cust_id = o.cust_id
JOIN product p ON o.prod_id = p.prod_id
GROUP BY c.cust_id, c.cust_name;
'Shows the most popular products by calculating the total quantity sold for each product.'
CREATE VIEW top_selling_products AS
SELECT p.prod_id, p.prod_name, SUM(o.order_qty) AS total_quantity_sold
FROM product p
JOIN order_on o ON p.prod_id = o.prod_id
GROUP BY p.prod_id, p.prod_name
ORDER BY total_quantity_sold DESC;
'Summarizes product sales by category, showing the total quantity sold and revenue generated
for each category.'
CREATE VIEW product_sales_by_category AS
SELECT p.prod_catg, SUM(o.order_qty) AS total_quantity_sold, SUM(p.prod_cost *
o.order_qty) AS total_revenue
FROM product p
JOIN order_on o ON p.prod_id = o.prod_id
GROUP BY p.prod_catg;
'Shows all orders that have been placed but are yet to be delivered (where the delivery date is
in the future or null).'
CREATE VIEW pending_orders AS
SELECT o.order_id, c.cust_name, p.prod_name, o.ord_date, o.order_dod AS
expected_delivery
FROM order_on o
JOIN customer c ON o.cust_id = c.cust_id
JOIN product p ON o.prod_id = p.prod_id
WHERE o.order_dod >= SYSDATE OR o.order_dod IS NULL;
'Lists customers who have placed more than a certain number of orders.'
CREATE VIEW frequent_customers AS
SELECT c.cust_id, c.cust_name, COUNT(o.order_id) AS total_orders
FROM customer c
JOIN order_on o ON c.cust_id = o.cust_id
GROUP BY c.cust_id, c.cust_name
HAVING COUNT(o.order_id) > 1; -- Replace '5' with the desired threshold for frequent
customers