CREATE TABLE Salesman (
SalesmanId INT,
Name VARCHAR(255),
Commission DECIMAL(10, 2),
City VARCHAR(255),
Age INT
);
INSERT INTO Salesman (SalesmanId, Name, Commission, City, Age)
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 Jersey', 30);
CREATE TABLE Customer (
SalesmanId INT,
CustomerId INT,
CustomerName VARCHAR(255),
PurchaseAmount INT,
);
INSERT INTO Customer (SalesmanId, CustomerId, CustomerName, PurchaseAmount)
VALUES
(101, 2345, 'Andrew', 550),
(103, 1575, 'Lucky', 4500),
(104, 2345, 'Andrew', 4000),
(107, 3747, 'Remona', 2700),
(110, 4004, 'Julia', 4545);
CREATE TABLE Orders (OrderId int, CustomerId int, SalesmanId int, Orderdate Date,
Amount
money)
INSERT INTO Orders Values
(5001,2345,101,'2021-07-01',550),
(5003,1234,105,'2022-02-15',1500)
--Inserting a new record in orders table :-
insert into orders values(5005,'4321','107','10-03-2022','2000');
--Adding Primary key constraint for SalesmanId column in Salesman table:-
alter table salesman
add CONSTRAINT primary key (salesmanid);
--Adding default constraint for city column in salesman table:-
alter table Salesman
add constraint DEFAULT (city);
alter table Customer
add constraint foreign key (salesmanid);
alter table Customer
add constraint not null (customername);
--Fetching the data where the Customer’s name is ending with ‘N’:-
select * from Customer where customername like '%n';
--purchases amount value greater than 500:-
select * from customer where purchaseamount >500;
--Using SET operators, retrieve the first result with unique SalesmanId values from
two tables:-
select salesmanid from Salesman
Except
select salesmanid from Customer;
--And the other result containing SalesmanId with duplicates from two tables:-
select salesmanid from Salesman
union all
select salesmanid from Customer;
--Displaying the below columns which has the matching data - orderdate
name,customername,commission and city which has the range of purchase
between 500 to 1500:-
select orderdate,name,customername,commission,city from
Salesman,Customer,Orders where purchaseamount between 500 and 1500;
--Using right join fetch all the results from Salesman and Orders table:-
select
salesmanid,name,commission,city,age,salesmanidalesmanid,orderid,customerid,ord
erdate,amount
from Salesman
right join Orders
on salesman.SalesmanId = orders.OrderId;