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

Assignment 4

The document contains a series of SQL commands for a database engineering assignment, including operations such as inserting, updating, deleting, and selecting records from various tables like Salesman, Product, and Employee. It demonstrates the use of SQL functions, joins, and conditions to manipulate and retrieve data effectively. The commands cover a range of scenarios, including filtering, aggregating, and handling null values.

Uploaded by

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

Assignment 4

The document contains a series of SQL commands for a database engineering assignment, including operations such as inserting, updating, deleting, and selecting records from various tables like Salesman, Product, and Employee. It demonstrates the use of SQL functions, joins, and conditions to manipulate and retrieve data effectively. The commands cover a range of scenarios, including filtering, aggregating, and handling null values.

Uploaded by

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

Database Engineering Assignment 4

Answers
1) Insert a record into Salesman table:

INSERT INTO Salesman (SId, SName, Location)


VALUES (11, 'Elizabeth', 'London');

2) Insert a record into Product table:

INSERT INTO Product (ProdId, PDesc, Price, Category, Discount)


VALUES (110, 'Bat', 50, 'Sports', NULL);

3) Update discount of all products in 'Sports' category:

UPDATE Product SET Discount = 25 WHERE Category = 'Sports';

4) Update price for Trouser in 'Apparel' category:

UPDATE Product SET Price = 50 WHERE PDesc = 'Trouser' AND Category = 'Apparel';

5) Update Salesman with SID 3:

UPDATE Salesman SET SName = 'Jenny', Location = 'Bristol' WHERE SID = 3;

6) Delete records from SaleDetail table for SaleId 1004:

DELETE FROM SaleDetail WHERE SaleId = 1004;

7) Delete records from SaleDetail table where quantity is more than 5:

DELETE FROM SaleDetail WHERE Quantity > 5;

8) Display Product Id, Product Description and Category for 'electronics' products (case-
insensitive):

SELECT ProdId, PDesc, Category FROM Product WHERE LOWER(Category) = 'electronics';

9) Display Product Id, first 5 characters of description, and Category:

SELECT ProdId, LEFT(PDesc, 5), Category FROM Product;


10) Display Product description and discount, with 'No Description' for NULL values:

SELECT COALESCE(PDesc, 'No Description'), Discount FROM Product;

11) Display Product Id, Category, Price, Discount in descending order of Category and
ascending order of Price:

SELECT ProdId, Category, Price, Discount FROM Product ORDER BY Category DESC, Price
ASC;

12) Display Product Id, Category, and Discount for 'Sports' or 'Apparel' categories in
ascending order of Category and Discount:

SELECT ProdId, Category, Discount FROM Product WHERE Category IN ('Sports', 'Apparel')
ORDER BY Category ASC, Discount ASC;

13) Display number of computers by their Make year:

SELECT MYEAR, COUNT(*) AS TOTAL FROM Computer GROUP BY MYEAR;

14) Display number of computers by their Make, case-insensitive for 'DELL':

SELECT MAKE, COUNT(*) AS TOTAL FROM Computer WHERE LOWER(MAKE) = 'dell'


GROUP BY MAKE;

15) Fetch departments with average salary > 50000:

SELECT DEPT, ROUND(AVG(SALARY), 2) AS AVGSAL FROM Employee GROUP BY DEPT


HAVING AVG(SALARY) > 50000;

16) Display Designation and Minimum Salary for those with no bonus and salary < 40000:

SELECT DESIGNATION, MIN(SALARY) FROM Employee WHERE BONUS IS NULL AND


SALARY < 40000 GROUP BY DESIGNATION;

17) Display department and designation-wise maximum salary in range 35000 to 75000:

SELECT DEPT, DESIGNATION, MAX(SALARY) FROM Employee WHERE MAX(SALARY)


BETWEEN 35000 AND 75000 GROUP BY DEPT, DESIGNATION;

18) Display Salesmen details using UNION ALL:

(SELECT SID, SName, Location FROM Salesman WHERE SName LIKE '%e%' AND Location
LIKE '%o%')
UNION ALL
(SELECT SID, SName, Location FROM Salesman WHERE SName LIKE '%a%' AND Location
LIKE '%a%');

19) Display Product details with discount < 10 or category 'Sports' (without duplicates):

SELECT DISTINCT ProdId, PDesc, Category, Discount FROM Product WHERE Discount < 10
OR Category = 'Sports';

20) Use INNER JOIN to display employee and computer details for 2013:

SELECT Employee.ID, Employee.ENAME, Computer.COMPID, Computer.MODEL FROM


Employee INNER JOIN Computer ON Employee.COMPID = Computer.COMPID WHERE
MYEAR = 2013;

21) Display make and number of computers allocated (minimum 2):

SELECT MAKE, COUNT(*) AS NUMBER_OF_COMPUTERS FROM Computer INNER JOIN


Employee ON Employee.COMPID = Computer.COMPID GROUP BY MAKE HAVING COUNT(*)
>= 2;

22) Display computer and employee details with 'Not assigned' for unallocated computers
(Outer Join):

SELECT COMPID, MAKE, MODEL, COALESCE(ENAME, 'Not assigned') FROM Computer LEFT
JOIN Employee ON Computer.COMPID = Employee.COMPID;

23) Display sale id and sale date for sales made by London salesmen:

SELECT Sale.SALEID, Sale.SLDATE FROM Sale INNER JOIN Salesman ON Sale.SID =


Salesman.SID WHERE Salesman.LOCATION = 'London';

24) Display employee and computer details, showing unassigned ones as NULL:

SELECT Employee.ID, Employee.ENAME, Employee.DEPT, COALESCE(Computer.MODEL,


'NULL') AS MODEL FROM Employee LEFT JOIN Computer ON Employee.COMPID =
Computer.COMPID;

25) Display Salesman IDs and names of those who made no sales:

SELECT Salesman.SID, Salesman.SName FROM Salesman LEFT JOIN Sale ON Salesman.SID =


Sale.SID WHERE Sale.SALEID IS NULL;

You might also like