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

SQL Solution

Uploaded by

yuhiwala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL Solution

Uploaded by

yuhiwala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Here are the SQL answers for each question based on the provided `Product` table:

---

1. **Create the `Product` table as shown above.**


```sql
CREATE TABLE Product (
ProductID INT,
ProductName VARCHAR(50),
Category VARCHAR(50),
Price DECIMAL(10, 2),
Stock INT,
SupplierID INT
);

2. **Retrieve the names of all products priced between 200 and 500.**

SELECT ProductName
FROM Product
WHERE Price BETWEEN 200 AND 500;

3. **Display the total `Stock` available for each `Category`.**

SELECT Category, SUM(Stock) AS TotalStock


FROM Product
GROUP BY Category;

4. **Delete products that are out of stock (i.e., `Stock` = 0).**

DELETE FROM Product


WHERE Stock = 0;

5. **Update the `Price` of all `Furniture` items by reducing it by 10%.**


UPDATE Product
SET Price = Price * 0.9
WHERE Category = 'Furniture';
Here are the SQL answers for each question based on the provided `Customer` table:

1. **List all unique cities where customers are located.**

SELECT DISTINCT City


FROM Customer;

2. **Retrieve the `CustomerName` and `LoyaltyPoints` for customers aged between 25 and 40.**

SELECT CustomerName, LoyaltyPoints


FROM Customer
WHERE Age BETWEEN 25 AND 40;

3. **Find the average `LoyaltyPoints` of customers from New York.**


SELECT AVG(LoyaltyPoints) AS AverageLoyaltyPoints
FROM Customer
WHERE City = 'New York';

4. **Update the `LoyaltyPoints` by adding 100 points for all customers aged 30 and above.**
UPDATE Customer
SET LoyaltyPoints = LoyaltyPoints + 100
WHERE Age >= 30;

5. **Delete records of customers with less than 600 `LoyaltyPoints`.**


DELETE FROM Customer
WHERE LoyaltyPoints < 600;
Here are the SQL answers for each question based on the provided `Sales` table:

1. **Display the total sales (`TotalAmount`) for each `ProductID`.**

SELECT ProductID, SUM(TotalAmount) AS TotalSales

FROM Sales

GROUP BY ProductID;

2. **Find the total quantity sold for each month in 2023.**

SELECT EXTRACT(YEAR FROM SaleDate) AS Year, EXTRACT(MONTH FROM SaleDate) AS Month,


SUM(QuantitySold) AS TotalQuantity

FROM Sales

WHERE EXTRACT(YEAR FROM SaleDate) = 2023

GROUP BY Year, Month;

3. **Retrieve sales records where the `QuantitySold` is greater than 2.**

SELECT *

FROM Sales

WHERE QuantitySold > 2;

4. **Delete sales records where `TotalAmount` is less than 500.**

DELETE FROM Sales

WHERE TotalAmount < 500;

5. **Retrieve the maximum `TotalAmount` for each `ProductID`.**

SELECT ProductID, MAX(TotalAmount) AS MaxTotalAmount

FROM Sales

GROUP BY ProductID;
Here are the SQL answers for each question based on the provided `Supplier` table:

1. **List all suppliers located in New York.**

SELECT *

FROM Supplier

WHERE City = 'New York';

2. **Count the number of suppliers from each city.**

SELECT City, COUNT(*) AS SupplierCount

FROM Supplier

GROUP BY City;

3. **Retrieve supplier names that have "Tech" in their `SupplierName`.**

SELECT SupplierName

FROM Supplier

WHERE SupplierName LIKE '%Tech%';

4. **Update the `ContactNumber` for the supplier with `SupplierID` 103 to "555-9999".**

UPDATE Supplier

SET ContactNumber = '555-9999'

5. **Delete suppliers from cities where there are more than one supplier.**

DELETE FROM Supplier

WHERE City IN (

SELECT City

FROM Supplier

GROUP BY City

HAVING COUNT(*) > 1

);
Here are the SQL answers for each question based on the provided `Employee` table:

1. **Add a new column `Experience` (in years) to the `Employee` table.**

ALTER TABLE Employee

ADD COLUMN Experience INT;

2. **Increase the `Salary` by 5% for employees in the `HR` department.**

UPDATE Employee

SET Salary = Salary * 1.05

WHERE Department = 'HR';

3. **Retrieve names of employees who earn more than 60000 and are in the `IT` department.**

SELECT Name

FROM Employee

WHERE Salary > 60000 AND Department = 'IT';

4. **Display the `Department` and the total `Salary` paid to employees in each department.**

SELECT Department, SUM(Salary) AS TotalSalary

FROM Employee

GROUP BY Department

5. **Delete records of employees who joined before `2019`.**

DELETE FROM Employee

WHERE JoinDate < '2019-01-01';

You might also like