0% found this document useful (0 votes)
14 views5 pages

S4 1. Create Three Tables Named 'Orders', 'Customers', and 'Products' With The Following Structures

The document outlines the creation of three database tables: `customers`, `products`, and `orders`, along with their respective structures and relationships. It includes SQL commands for inserting data into these tables, querying total sales, identifying customers without orders, and updating or deleting records. Additionally, it provides SQL queries for listing orders, customer-product relationships, and handling various data retrieval scenarios.

Uploaded by

Ayesha Bagwan
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)
14 views5 pages

S4 1. Create Three Tables Named 'Orders', 'Customers', and 'Products' With The Following Structures

The document outlines the creation of three database tables: `customers`, `products`, and `orders`, along with their respective structures and relationships. It includes SQL commands for inserting data into these tables, querying total sales, identifying customers without orders, and updating or deleting records. Additionally, it provides SQL queries for listing orders, customer-product relationships, and handling various data retrieval scenarios.

Uploaded by

Ayesha Bagwan
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/ 5

S4

1. Create three tables named `orders`, `customers`, and `products` with the
following structures:
- `customers` table:
- `customer_id` (Primary Key)
- `customer_name`
- `email`

- `products` table:
- `product_id` (Primary Key)
- `product_name`
- `unit_price`

- `orders` table:
- `order_id` (Primary Key)
- `customer_id` (Foreign Key referencing `customers` table)
- `product_id` (Foreign Key referencing `products` table)
- `quantity`
- `order_date`
-- Create customers table
ANS
CREATE TABLE customers (
customer_id INT AUTO_INCREMENT PRIMARY KEY,
customer_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL
);

-- Create products table


CREATE TABLE products (
product_id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
unit_price DECIMAL(10, 2) NOT NULL
);

-- Create orders table


CREATE TABLE orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
customer_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL,
order_date DATE NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);

2. Add a new customer named 'John Doe' with email '[email protected]' and 3
more to the `customers` table.
ANS
-- Insert new customers into the customers table
INSERT INTO customers (customer_name, email) VALUES
('John Doe', '[email protected]'),
('Jane Smith', '[email protected]'),
('Alice Johnson', '[email protected]'),
('Bob Brown', '[email protected]');

3. Insert the following records into the `products` table:


Product_id Product_name Unit_price
1 Laptop 1200.00
2 Smartphone 800.00
3 Headphones 100.00
4 Tablet 500.00
5 Smart Watch 300.00
ANS
-- Insert records into the products table
INSERT INTO products (product_id, product_name, unit_price) VALUES
(1, 'Laptop', 1200.00),
(2, 'Smartphone', 800.00),
(3, 'Headphones', 100.00),
(4, 'Tablet', 500.00),
(5, 'Smart Watch', 300.00);

4. Insert the following records into the `orders` table:


Order_id Customer_id Product_id Quantity Order_date
1 1 2 2 15/01/2023
2 2 1 1 16/01/2023
3 1 3 3 17/01/2023
4 3 4 1 18/01/2023
5 4 2 2 19/01/2023
ANS
-- Insert records into the orders table
INSERT INTO orders (order_id, customer_id, product_id, quantity, order_date)
VALUES
(1, 1, 2, 2, '2023-01-15'),
(2, 2, 1, 1, '2023-01-16'),
(3, 1, 3, 3, '2023-01-17'),
(4, 3, 4, 1, '2023-01-18'),
(5, 4, 2, 2, '2023-01-19');

5. List the total sales (quantity * unit_price) for each product. Include the
product name and the total sales amount, sorted in descending order of sales
amount.
ANS
SELECT
p.product_name,
SUM(o.quantity * p.unit_price) AS total_sales
FROM
products p
JOIN
orders o
ON
p.product_id = o.product_id
GROUP BY
p.product_name
ORDER BY
total_sales DESC;

6. List all customers who have not placed any orders.


ANS
SELECT u.*
FROM registered_users u
LEFT JOIN orders o ON u.user_id = o.user_id
WHERE o.order_id IS NULL;

7. List the top 3 customers with the highest total spending. Include the customer
name and their total spending amount, sorted in descending order of spending
amount.
ANS

SELECT u.user_name, SUM(o.order_amount) AS total_spending


FROM registered_users u
JOIN orders o ON u.user_id = o.user_id
GROUP BY u.user_id
ORDER BY total_spending DESC
LIMIT 3;
8. Update the unit price of the 'Tablet' product to 600.00.
Ans

UPDATE products
SET unit_price = 600.00
WHERE product_name = 'Tablet';

9. Delete the order record with `order_id` 3 from the `orders` table.
Ans
DELETE FROM orders
WHERE order_id = 3;

10. List all orders along with the customer name, product name, quantity, and
total price (quantity * unit_price). Sort the orders by order date in ascending
order.
Ans
SELECT u.user_name, p.product_name, o.quantity,
(o.quantity * p.unit_price) AS total_price, o.order_date
FROM orders o
JOIN registered_users u ON o.user_id = u.user_id
JOIN products p ON o.product_id = p.product_id
ORDER BY o.order_date ASC;

11. List the names of customers and the names of the products they ordered, but
only include customers who have placed at least one order.
Ans

SELECT u.user_name, p.product_name


FROM registered_users u
JOIN orders o ON u.user_id = o.user_id
JOIN products p ON o.product_id = p.product_id
WHERE o.order_id IS NOT NULL;

12. List all products along with the names of customers who ordered them,
including products that have not been ordered and customers who have not
placed any orders.

Ans
SELECT p.product_name, u.user_name
FROM products p
LEFT JOIN orders o ON p.product_id = o.product_id
LEFT JOIN registered_users u ON o.user_id = u.user_id
ORDER BY p.product_name;

You might also like