0% found this document useful (0 votes)
0 views6 pages

PR 5

The document provides a series of SQL queries for managing and analyzing data in a database. It includes simple queries to display users, orders, products, and customers, as well as operational queries for counting, averaging, and grouping data. Additionally, it features advanced queries to identify customers and products based on specific criteria, such as order frequency and stock levels.

Uploaded by

prithvipuranik1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views6 pages

PR 5

The document provides a series of SQL queries for managing and analyzing data in a database. It includes simple queries to display users, orders, products, and customers, as well as operational queries for counting, averaging, and grouping data. Additionally, it features advanced queries to identify customers and products based on specific criteria, such as order frequency and stock levels.

Uploaded by

prithvipuranik1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PR5:

A. Simple Queries
1. Display all users:

SELECT * FROM CUSTOMER;

2. Display all shipped orders (acting as active requests):

SELECT * FROM ORDERS WHERE status = 'Shipped';

3. Show all products with low stock (assuming "injured" means a problem, so we’ll filter low
quantity):

SELECT * FROM PRODUCT WHERE stock_quantity < 10;

4. List all customers from a specific location

ALTER TABLE CUSTOMER ADD city VARCHAR(100);

SELECT * FROM CUSTOMER WHERE city = 'Mumbai';

5. Find orders made by a particular custome

SELECT * FROM ORDERS WHERE customer_id = 1;

6. Find products of a specific type

SELECT * FROM PRODUCT WHERE product_name = 'Smartwatch Pro';

7. Display all products of a particular brand

SELECT * FROM PRODUCT WHERE brand_id = 1;

8. Show customers with Gmail accounts:

SELECT * FROM CUSTOMER WHERE email LIKE '%@gmail.com';

9. Show orders that are closed

SELECT * FROM ORDERS WHERE status IN ('Delivered', 'Cancelled');

10. Display products by descending product_id:

SELECT * FROM PRODUCT ORDER BY product_id DESC;


OP:
1. Count total customers (users):

SELECT COUNT(*) AS total_customers FROM CUSTOMER;

2. Count shipped orders

SELECT COUNT(*) AS active_orders FROM ORDERS WHERE status = 'Shipped';

3. Count products with low stock

SELECT COUNT(*) AS low_stock_products FROM PRODUCT WHERE stock_quantity < 10;

4. Average stock quantity

SELECT AVG(stock_quantity) AS avg_stock_quantity FROM PRODUCT;

5. Maximum stock quantity:

SELECT MAX(stock_quantity) AS max_stock_quantity FROM PRODUCT;

6. Minimum product price

SELECT MIN(price) AS min_product_price FROM PRODUCT;

7. Total number of orders

SELECT COUNT(order_id) AS total_orders FROM ORDERS;

8. Total customers using Yahoo mail:

SELECT COUNT(*) AS yahoo_users FROM CUSTOMER WHERE email LIKE '%@yahoo.com';

9. Count of products per brand

SELECT brand_id, COUNT(*) AS product_count

FROM PRODUCT

GROUP BY brand_id;

10. Number of orders per customer:

SELECT customer_id, COUNT(*) AS orders_count

FROM ORDERS

GROUP BY customer_id;
1. Customers who have placed more than 1 order

SELECT customer_id, COUNT(*) AS order_count

FROM ORDERS

GROUP BY customer_id

HAVING COUNT(*) > 1;

2. Brands with more than 1 product

SELECT brand_id, COUNT(*) AS product_count

FROM PRODUCT

GROUP BY brand_id

HAVING COUNT(*) > 1;

3. Products included in more than 1 order

SELECT product_id, COUNT(*) AS times_ordered

FROM ORDERS_ITEM

GROUP BY product_id

HAVING COUNT(*) > 1;

4. Customers who bought more than 1 unique product

SELECT oi.order_id, o.customer_id, COUNT(DISTINCT oi.product_id) AS unique_products

FROM ORDERS_ITEM oi

JOIN ORDERS o ON oi.order_id = o.order_id

GROUP BY oi.order_id, o.customer_id

HAVING COUNT(DISTINCT oi.product_id) > 1;

5. Orders with more than 2 items

SELECT order_id, COUNT(*) AS item_count

FROM ORDERS_ITEM

GROUP BY order_id

HAVING COUNT(*) > 2;

You might also like