Lesson 05 Retail Mart Management Solution
Lesson 05 Retail Mart Management Solution
SQL code:
CREATE DATABASE sqlbasics;
SQL code:
USE sqlbasics;
3. Write a query to create a product table with the fields product code, product name,
price, stock, and category, a customer table with the fields customer ID, customer
name, customer location, and customer phone number, and a sales table with the
fields date, order number, product code, product name, quantity, and price
4. Write a query to insert values into the customer, product, and sales tables
SQL code:
INSERT INTO lep_4.customer(c_id,c_name,c_location,c_phoneno) VALUES
('9212','Jessica','banglore','1233435');
SQL code:
INSERT INTO
lep_4.sales(order_date,order_no,c_id,c_name,s_code,p_name,qty,price)VALUES
('2021-02-12','HM02','2123','Biyush','03','Pen','2','20');
SQL code:
INSERT INTO lep_4.product(p_code,p_name,stock,price,category) VALUES
('07','shampoo','90','5','hair product');
5. Write a query to add new columns, such as serial number and categories, to the
sales table
SQL code:
ALTER TABLE lep_4.sales ADD (S_no INT,categories varchar(45));
6. Write a query to change the stock field type to varchar in the product table
SQL code:
ALTER TABLE lep_4.product MODIFY stock varchar(45);
7. Write a query to change the table name from customer to customer details
SQL code:
ALTER TABLE lep_4.customer RENAME TO lep_4.customerdetails;
8. Write a query to drop the sl. no. and categories columns from the sales table
SQL code:
ALTER TABLE lep_4.sales DROP COLUMN S_no;
ALTER TABLE lep_4.sales DROP COLUMN categories;
9. Write a query to display the details where the category is stationary from the product
table
SQL code:
SELECT order_date,order_no,c_id,qty,price FROM lep_4.sales;
Output:
10. Write a query to display the details where the category is stationary from the product
table
SQL code:
SELECT * FROM lep_4.product WHERE category='Stationary';
Output:
11. Write a query to display the unique category from the product table
SQL code:
SELECT DISTINCT(category) FROM lep_4.product;
Output:
12. Write a query to display the details of the sales from the sales table where quantity
is greater than 2 and the price is less than 500
SQL code:
SELECT * FROM lep_4.sales WHERE qty>2 AND price < 500;
Output:
13. Write a query to display every customer whose name ends with an ‘a’
SQL code:
SELECT c_name FROM lep_4.customerdetails WHERE c_name LIKE '%a';
Output:
14. Write a query to display the product details in descending order of price
SQL code:
SELECT * FROM lep_4.product ORDER BY price DESC ;
Output:
15. Write a query to display the product code and category from categories that have
two or more products
SQL code:
SELECT p_code,category FROM lep_4.product GROUP BY category HAVING
COUNT(category)>=2;
Output:
16. Write a query to combine the sales and product tables based on the order number
and customer's name including duplicated rows
SQL code:
SELECT order_no,c_name FROM lep_4.sales LEFT JOIN lep_4.product ON
sales.s_code = product.p_code
UNION ALL
SELECT order_no,c_name FROM lep_4.sales RIGHT JOIN lep_4.product ON
sales.s_code = product.p_code
Output: