Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Ameer Hamza(025) Quiz

Quiz Database System:


Paper q5:

01 CREATE TABLE SalesRep (


02 SalRepId INT PRIMARY KEY,
03 SalRepName VARCHAR(255),
04 OfficeId INT,
055 Title VARCHAR(255),
06 Age INT,
07 HireDate DATE,
08 Manager INT,
09 Quota DECIMAL(10, 2),
10 Sales DECIMAL(10, 2),
11 Target DECIMAL(10, 2)
12 );
13
14 CREATE TABLE Office (
15 OfficeId INT PRIMARY KEY,
16 City VARCHAR(255),
17 Region VARCHAR(255),
18 Target DECIMAL(10, 2),
19 Sales DECIMAL(10, 2)
20 );
21
22 CREATE TABLE Customer (
23 CustomerId INT PRIMARY KEY,
24 customerName VARCHAR(255),
25 Company VARCHAR(255),
26 CreditLimit DECIMAL(10, 2),
27 Address VARCHAR(255),
28 salRepId INT
29 );
30
31 CREATE TABLE Product (
32 ProductId INT PRIMARY KEY,
33 Description VARCHAR(255),
34 Price DECIMAL(10, 2),
35 Manufacturer VARCHAR(255),
36 Qty_On_Hand INT
37 );
38
39 CREATE TABLE Orders (
40 OrderId INT PRIMARY KEY,
41 CustomerId INT,
42 ProductId INT,
43 Qty INT,
44 Amount DECIMAL(10, 2),
45 SalRepId INT,
46 Date DATE,
47 FOREIGN KEY (CustomerId) REFERENCES Customer(CustomerId),
Ameer Hamza(025) Quiz

48 FOREIGN KEY (ProductId) REFERENCES Product(ProductId),


49 FOREIGN KEY (SalRepId) REFERENCES SalesRep(SalRepId)
50 );
51 -- Inserting data into SalesRep table
52 INSERT INTO SalesRep (SalRepId, SalRepName, OfficeId, Title, Age, HireDate, Manager,
53 Quota, Sales, Target)
54 VALUES
55 (1, 'Ali', 1, 'Sales Representative', 35, '2022-01-01', NULL, 100000, 75000, 90000),
56 (2, 'hamza', 2, 'Senior Sales Representative', 42, '2021-11-15', 1, 120000, 90000, 110000),
57 (3, 'Ahmad Khan', 1, 'Sales Representative', 28, '2023-03-20', NULL, 90000, 80000, 95000);
58
59 -- Inserting data into Office table
60 INSERT INTO Office (OfficeId, City, Region, Target, Sales)
61 VALUES
62 (1, 'New York', 'Eastern', 1000000, 1050000),
63 (2, 'Los Angeles', 'Western', 900000, 850000),
64 (3, 'Chicago', 'Central', 800000, 750000);
65
66 -- Inserting data into Customer table
67 INSERT INTO Customer (CustomerId, customerName, Company, CreditLimit, Address,
68 salRepId)
69 VALUES
70 (1, 'ABC Inc.', 'ABC Company', 10000, '123 Main St, New York', 1),
71 (2, 'XYZ Corp.', 'XYZ Company', 15000, '456 Elm St, Los Angeles', 2),
72 (3, 'LMN Enterprises', 'LMN Company', 12000, '789 Oak St, Chicago', 3);
73
74 -- Inserting data into Product table
75 INSERT INTO Product (ProductId, Description, Price, Manufacturer, Qty_On_Hand)
76 VALUES
77 (1, 'Widget', 20.99, 'Widget Co.', 100),
78 (2, 'Gadget', 15.49, 'Gadget Inc.', 75),
79 (3, 'Thingamajig', 25.99, 'Thingamajig Co.', 50);
80
81 -- Inserting data into Orders table
82 INSERT INTO Orders (OrderId, CustomerId, ProductId, Qty, Amount, SalRepId, Date)
83 VALUES
84 (1, 1, 1, 50, 1049.50, 1, '2024-05-01'),
85 (2, 2, 2, 30, 464.70, 2, '2024-05-02'),
86 (3, 3, 3, 20, 519.80, 3, '2024-05-03');
Ameer Hamza(025) Quiz

List the name and hire date of anyone with sales over 50000?
SELECT s.SalRepName, s.HireDate
FROM SalesRep s
WHERE s.Sales > 50000;

Show me the result if I raised each person’s quota by 3 percent of their year to date sales:
SELECT s.SalRepName, (s.Quota + (s.Sales * 0.03)) AS NewQuota
FROM SalesRep s;

List the offices whose sales fall below 80 percent of target?

SELECT o.OfficeId, o.City, o.Region


FROM Office o
WHERE o.Sales < (0.8 * o.Target);

List salespeople whose sales are not between 80 percent and 120 percent of the quota?
SELECT s.SalRepName, s.Sales, s.Quota
FROM SalesRep s
WHERE s.Sales NOT BETWEEN (0.8 * s.Quota) AND (1.2 * s.Quota);
Ameer Hamza(025) Quiz

List all the customers whose name starts with ‘A’ or ‘a’?
SELECT c.customerName
FROM Customer c
WHERE c.customerName LIKE 'A%' OR c.customerName LIKE 'a%';

List orders over 2500, including the name of the salesperson who took the order and the
name of the customer who placed it?
SELECT o.OrderId, s.SalRepName AS Salesperson, c.customerName AS Customer
FROM Orders o
JOIN SalesRep s ON o.SalRepId = s.SalRepId
JOIN Customer c ON o.CustomerId = c.CustomerId
WHERE o.Amount > 2500;

List the names of salespeople and their managers (Note: Manager field contains only IDs
not names):

SELECT s.SalRepName AS Salesperson, s.Manager AS ManagerID, mgr.SalRepName AS


ManagerName
FROM SalesRep s
LEFT JOIN SalesRep mgr ON s.Manager = mgr.SalRepId;

You might also like