You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Q1. Show each order along with the product name and price
SELECTo.order_id, o.customer_name, p.product_name, p.priceFROM orders o
JOIN products p ONo.product_id=p.product_id;
Q2. Show all products even if they were never ordered
SELECTp.product_name, o.order_idFROM products p
LEFT JOIN orders o ONp.product_id=o.product_id;
Q3. Show orders for only 'Electronics' category
SELECTo.order_id, p.product_name, p.categoryFROM orders o
JOIN products p ONo.product_id=p.product_idWHEREp.category='Electronics';
Q4. List all orders sorted by product price (high to low)
SELECTo.order_id, p.product_name, p.priceFROM orders o
JOIN products p ONo.product_id=p.product_idORDER BYp.priceDESC;
Q5. Show number of orders placed for each product
SELECTp.product_name, COUNT(o.order_id) AS total_orders
FROM products p
LEFT JOIN orders o ONp.product_id=o.product_idGROUP BYp.product_name;
Q6. Show total revenue earned per product
SELECTp.product_name, SUM(o.quantity*p.price) AS revenue
FROM products p
JOIN orders o ONp.product_id=o.product_idGROUP BYp.product_name;
Q7. Show products where total order revenue > βΉ2000
SELECTp.product_name, SUM(o.quantity*p.price) AS total_revenue
FROM products p
JOIN orders o ONp.product_id=o.product_idGROUP BYp.product_nameHAVINGSUM(o.quantity*p.price) >2000;
Q8. Show unique customers who ordered 'Fitness' products
SELECT DISTINCTo.customer_nameFROM orders o
JOIN products p ONo.product_id=p.product_idWHEREp.category='Fitness';
SELECTs.student_name, c.course_nameFROM student_courses sc
JOIN students s ONsc.student_id=s.student_idJOIN courses c ONsc.course_id=c.course_id;
Q2. List all courses taken by 'Simran'
SELECTc.course_nameFROM student_courses sc
JOIN students s ONsc.student_id=s.student_idJOIN courses c ONsc.course_id=c.course_idWHEREs.student_name='Simran';
π Views
View 1: Available Fitness Products
CREATEVIEWavailable_fitness_productsASSELECT product_id, name, price, stock_quantity
FROM products
WHERE category ='Fitness'AND is_available = TRUE;
View 2: Low Stock Products
CREATEVIEWlow_stock_productsASSELECT name, category, stock_quantity
FROM products
WHERE stock_quantity <30;
βοΈ Stored Procedure
Add Product Procedure
CREATE PROCEDURE add_product(
p_name VARCHAR,
p_sku CHAR(8),
p_price NUMERIC,
p_qty INT,
p_category TEXT
)
LANGUAGE plpgsql
AS $$
BEGININSERT INTO products(name, sku_code, price, stock_quantity, category)
VALUES (p_name, p_sku, p_price, p_qty, p_category);
RAISE NOTICE 'Product added successfully!';
END;
$$;