-- DataBase creation
create database ABC_Fashion
use ABC_Fashion
--Sales Man Table Generation
create table salesman_table(salesmanId int, SalesmanName varchar(255), Commission int, City var
insert into salesman_table values(101, 'Joe', 50, 'California', 17),
(102, 'Simon', 75, 'Texas', 25),
(103, 'Jessie', 105, 'Florida', 35),
(104, 'Danny', 100, 'Texas', 22),
(105, 'Lia', 65, 'New Jersy', 30)
select * from salesman_table
--Customer Table Generation
create table customer_table(SalesmanId int, CustomerId int, CustomerName varchar(255), Purchase
insert into customer_table values(101, 2345, 'Andrew', 550),
(103, 1575, 'Lucky', 4500),
(104, 2345, 'Andrew', 4000),
(107, 3747, 'Remona', 2700),
(110, 4004, 'Julia', 4545)
select * from customer_table
--Orders table Generation
create table orders_table(OrderId int, CustomerId int, SalesmanId int, Orderdate varchar(255),
insert into orders_table values(5001, 2345, 101, '04-07-2021', 550),
(5003, 1234, 105, '15-02-2022', 1500)
select * from orders_table
--Tasks to be performed
--1. Insert a new record in your Orders table.
insert into orders_table values(5005, 1343, 108, '18-04-2025', 5254)
select * from orders_table
--2. Add Primary key constraint for SalesmanId column in Salesman table. Add default
--constraint for City column in Salesman table. Add Foreign key constraint for SalesmanId
--column in Customer table. Add not null constraint in Customer_name column for the
--Customer table.
--Adding Primary key for salesman columnin Salesman table
alter table salesman_table alter column salesmanid int not null
alter table salesman_table add constraint pri_key primary key(salesmanid)
-- Adding default constaint for city column in sales man table
alter table salesman_table add constraint df_c default 'Texas' for city
-- Adding Foreign key constraint for SalesmanId column in Customer table
-- Before altering we have to add few salesmanId in salesman Table that exists in the
-- customer Table
insert into salesman_table values(107, 'Max', 95, 'Florida', 28),
(110, 'Karen', 150, 'New Jersy', 21)
alter table customer_table add constraint fr_key foreign key(salesmanid) references salesman
--Add not null constraint in Customer_name column for the customer table.
alter table customer_table alter column customername varchar(255) not null
--3. Fetch the data where the Customer’s name is ending with ‘N’ also get the purchase
-- amount value greater than 500.
select * from customer_table where CustomerName like '%n' and PurchaseAmount > 500
--4. Using SET operators, retrieve the first result with unique SalesmanId values from two
--tables, and the other result containing SalesmanId with duplicates from two tables.
select salesmanid from salesman_table
union
select salesmanid from customer_table
select salesmanid from salesman_table
union all
select salesmanid from customer_table
--5. Display the below columns which has the matching data.
-- Orderdate, Salesman Name, Customer Name, Commission, and City which has the
-- range of Purchase Amount between 500 to 1500.
select o.orderdate, s.SalesmanName, c.customerName, s.Commission, s.City
from orders_table o
inner join
salesman_table s on o.SalesmanId = s.SalesmanId
inner join
customer_table c on o.CustomerId = c.CustomerId
where
o.amount between 500 and 1500
--6. Using right join fetch all the results from Salesman and Orders table.
select s.SalesmanId, s.SalesmanName, s.Commission, s.City, o.OrderId, o.CustomerId, o.amount,
o.Orderdate from salesman_table s
right join
orders_table o on s.SalesmanId = o.SalesmanId
select * from salesman_table
select * from customer_table
select * from orders_table