0% found this document useful (0 votes)
53 views

Lesson 05 Retail Mart Management Solution

1. The document provides SQL code to create a database named "sqlbasics" and use that database. It then creates 3 tables - a product table, customer table, and sales table. 2. SQL code is provided to insert sample values into each of the 3 tables. 3. Additional SQL queries modify the tables by adding/removing columns, changing data types, renaming tables, and dropping columns. 4. Queries are written to select data from the tables based on various conditions like category, quantity, price range etc. and order results. 5. The last query combines the sales and product tables to include duplicate rows.

Uploaded by

Tejas G Srikanth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Lesson 05 Retail Mart Management Solution

1. The document provides SQL code to create a database named "sqlbasics" and use that database. It then creates 3 tables - a product table, customer table, and sales table. 2. SQL code is provided to insert sample values into each of the 3 tables. 3. Additional SQL queries modify the tables by adding/removing columns, changing data types, renaming tables, and dropping columns. 4. Queries are written to select data from the tables based on various conditions like category, quantity, price range etc. and order results. 5. The last query combines the sales and product tables to include duplicate rows.

Uploaded by

Tejas G Srikanth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SQL Training

Lesson-End Project Solution


Retail Mart Management

1. Write a query to create a database named SQL basics.

SQL code:
CREATE DATABASE sqlbasics;

2. Write a query to select SQL basics

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

SQL code: Product table


CREATE TABLE lep_4.product (
p_code varchar(45) NOT NULL,
p_name varchar(45) NOT NULL,
stock varchar(45) NOT NULL,
price INT NOT NULL,
category varchar(45) NOT NULL,
PRIMARY KEY(p_code));

SQL code: Customer table


CREATE TABLE lep_4.customer (
c_id VARCHAR(45) NOT NULL,
c_name varchar(45) NOT NULL,
c_location varchar(45) NOT NULL,
c_phoneno int NOT NULL,
PRIMARY KEY(c_id));
SQL code: Sales table
CREATE TABLE lep_4.sales (
order_date DATE NOT NULL,
order_no varchar(45) NOT NULL,
c_id varchar(45) NOT NULL,
c_name varchar(45) NOT NULL,
p_code varchar(45) NOT NULL,
p_name varchar(45) NOT NULL,
qty INT NOT NULL,
price INT NOT NULL,
PRIMARY KEY(order_date));

4. Write a query to insert values into the customer, product, and sales tables

Insert values into customer table

SQL code:
INSERT INTO lep_4.customer(c_id,c_name,c_location,c_phoneno) VALUES
('9212','Jessica','banglore','1233435');

Insert values into sales table

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');

Insert values into product table

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:

You might also like