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

Assignment 1 mandatory

sql assignment 1

Uploaded by

vfixit19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Assignment 1 mandatory

sql assignment 1

Uploaded by

vfixit19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

create database mandassignment1and2

use mandassignment1and2

--Salesman table creation


CREATE TABLE Salesman
(
SalesmanId INT,
Name VARCHAR(255),
Commission DECIMAL(10, 2),
City VARCHAR(255),
Age INT
);

--Salesman table record insertion

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

--Customer table creation


CREATE TABLE Customer
(
SalesmanId INT,
CustomerId INT,
CustomerName VARCHAR(255),
PurchaseAmount INT,
);

--Customer table record insertion


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

--Orders table Creation


CREATE TABLE Orders (OrderId int, CustomerId int, SalesmanId int, Orderdate
Date, Amount money)

--Orders table record insertion

INSERT INTO Orders Values


(5001,2345,101,'2021-07-01',550),
(5003,1234,105,'2022-02-15',1500);

select * from Customer


select * from Orders
select * from Salesman

-- 1. Insert a new record in your Orders table.

INSERT INTO Orders Values


(5004,3747,104,'2023-08-08',2000),
(5005,1575,102,'2023-12-26',3500);
/*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.*/

ALTER TABLE Salesman ALTER COLUMN SalesmanId INT NOT NULL


ALTER TABLE Salesman ADD CONSTRAINT PK_SalesmanId PRIMARY KEY (SalesmanId);

ALTER TABLE Salesman ADD CONSTRAINT DF_City DEFAULT 'Unknown' FOR City;

ALTER TABLE Customer ADD CONSTRAINT FK_SalesmanId FOREIGN KEY (SalesmanId)


REFERENCES Salesman(SalesmanId);

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 Customer;

SELECT SalesmanId FROM Salesman


UNION ALL
SELECT SalesmanId FROM Customer;

/*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.Name, C.CustomerName, S.Commission, S.City,
C.PurchaseAmount FROM Orders O
JOIN Salesman S ON O.SalesmanId = S.SalesmanId
JOIN Customer C ON O.CustomerId = C.CustomerId
WHERE C.PurchaseAmount BETWEEN 500 AND 1500;

--6. Using right join fetch all the results from salesman orders table.

SELECT S.SalesmanId, S.Name, S.Commission, S.City, O.OrderId, O.CustomerId,


O.OrderDate, O.Amount FROM Salesman S
RIGHT JOIN Orders O ON S.SalesmanId = O.SalesmanId;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy