ASSIGNMENT-1
1. Insert a new record in your Orders table.
INSERT INTO Orders (OrderID, CustomerID, OrderDate,
Amount)
VALUES (5003, 5678, '2024-10-26', 150.00);
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
CREATE TABLE Salesman2 (
SalesmanId INT primary key,
Name VARCHAR(255),
Commission DECIMAL(10, 2),
City VARCHAR(255),
Age INT
);
ALTER TABLE Salesman
ADD CONSTRAINT DF_City DEFAULT 'Unknown' FOR City;
ALTER TABLE Customer
ADD CONSTRAINT FK_Customer_SalesmanId
FOREIGN KEY (SalesmanId)
REFERENCES Salesman(SalesmanId);
Add default constraint for City column in Salesman table.
ALTER TABLE Salesman
ADD CONSTRAINT DF_City DEFAULT 'Unknown' FOR City;
ALTER TABLE Customer
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
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
UNION
SELECT SalesmanId
FROM Orders;
SalesmanId with duplicates from two tables
SELECT SalesmanId
FROM Salesman
INTERSECT
SELECT SalesmanId
FROM Orders;
6.Using right join fetch all the results from Salesman and
Orders table.
SELECT
s.SalesmanId,
s.Name,
s.Commission,
s.City,
o.OrderId,
o.CustomerId,
o.OrderDate,
o.Amount money
FROM
Salesman s
RIGHT JOIN
Orders o ON s.SalesmanId = o.SalesmanId;