0% found this document useful (0 votes)
48 views3 pages

Lab 1 Algebraic Query Solutions

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

Lab 1 Algebraic Query Solutions

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

Lab 1: Algebraic Query Language

Exercise 1: Relational Algebra Queries

1. Find name and sell price of televisions supplied by Samsung.

SELECT Name, SellPrice FROM Product JOIN Supplier ON Product.SupplierCode =

Supplier.SupplierCode WHERE Product.Name = 'Television' AND Supplier.SupplierName =

'Samsung'

2. Find name and address of all suppliers who supply television product.

SELECT Supplier.SupplierName, Supplier.Address FROM Product JOIN Supplier ON

Product.SupplierCode = Supplier.SupplierCode WHERE Product.Type = 'Television'

3. Find name of all employees who were born in 1983.

SELECT FullName FROM Employee WHERE BirthDate = 1983

4. Find name and type of all products sold on '23/05/2018'.

SELECT Product.Name, Product.Type FROM Product JOIN InvoiceLine ON Product.ProductCode =

InvoiceLine.ProductCode JOIN Invoice ON InvoiceLine.InvoiceID = Invoice.InvoiceID WHERE

Invoice.SellDate = '23/05/2018'

5. Find name of female employees who sold televisions.

SELECT Employee.FullName FROM Product JOIN InvoiceLine ON Product.ProductCode =

InvoiceLine.ProductCode JOIN Invoice ON InvoiceLine.InvoiceID = Invoice.InvoiceID JOIN

Employee ON Invoice.EmployeeID = Employee.EmployeeID WHERE Employee.Gender = 'Nu' AND

Product.Name = 'Television'

6. Find name and address of suppliers who supply both television and mobile.

SELECT Supplier.SupplierName, Supplier.Address FROM Product JOIN Supplier ON

Product.SupplierCode = Supplier.SupplierCode WHERE Product.Type = 'Television' INTERSECT

SELECT Supplier.SupplierName, Supplier.Address FROM Product JOIN Supplier ON

Product.SupplierCode = Supplier.SupplierCode WHERE Product.Type = 'Mobile'

7. List name and price of all products sold by employee 'Nguyen Van A' in April 2018.
SELECT Product.Name, Product.SellPrice FROM Product JOIN InvoiceLine ON

Product.ProductCode = InvoiceLine.ProductCode JOIN Invoice ON InvoiceLine.InvoiceID =

Invoice.InvoiceID JOIN Employee ON Invoice.EmployeeID = Employee.EmployeeID WHERE

Employee.FullName = 'Nguyen Van A' AND Invoice.SellDate BETWEEN '01/04/2018' AND

'30/04/2018'

8. Find name and price of all mobile products of Samsung sold in April 2018.

SELECT Product.Name, Product.SellPrice FROM Product JOIN InvoiceLine ON

Product.ProductCode = InvoiceLine.ProductCode JOIN Invoice ON InvoiceLine.InvoiceID =

Invoice.InvoiceID JOIN Supplier ON Product.SupplierCode = Supplier.SupplierCode WHERE

Product.Type = 'Mobile' AND Supplier.SupplierName = 'Samsung' AND Invoice.SellDate BETWEEN

'01/04/2018' AND '30/04/2018'

9. Find the product with highest SellPrice.

SELECT Name FROM Product WHERE SellPrice = (SELECT MAX(SellPrice) FROM Product)

10. Find the amount (quantity * sellPrice) of each invoice line of product sold on 30/04/2018.

SELECT InvoiceLine.Quantity * Product.SellPrice FROM InvoiceLine JOIN Product ON

InvoiceLine.ProductCode = Product.ProductCode JOIN Invoice ON InvoiceLine.InvoiceID =

Invoice.InvoiceID WHERE Invoice.SellDate = '30/04/2018'

Exercise 2: Constraints in Relational Algebra

1. The sell price must be higher than the purchase price.

CHECK (SellPrice > PurchasePrice)

2. A product of Samsung must be television, mobile, or tablet.

CHECK (SupplierName = 'Samsung' AND Type IN ('Television', 'Mobile', 'Tablet'))

3. No supplier of mobiles or tablets may also supply food.

SELECT SupplierCode FROM Product WHERE Type IN ('Mobile', 'Tablet') INTERSECT

SELECT SupplierCode FROM Product WHERE Type = 'Food' = EMPTY

4. No product may appear more than one time in an invoice.


UNIQUE (ProductCode, InvoiceID) in InvoiceLine

5. The quantity of each product in each invoice should be greater than 0.

CHECK (Quantity > 0)

6. There is no invoice without a product.

CHECK (InvoiceID IN (SELECT InvoiceID FROM InvoiceLine))

7. If purchase price is less than 500,000 VND, the sell price could not be greater than

9,000,000 VND.

CHECK (PurchasePrice < 500000 -> SellPrice <= 9000000)

8. The sell price could not be greater than 2 times the purchase price.

CHECK (SellPrice <= 2 * PurchasePrice)

9. The gender of an employee should be 'Nam' or 'Nu'.

CHECK (Gender IN ('Nam', 'Nu'))

10. With the same purchase price, the sell price of two products could not have a difference

of more than 0.5 times the purchase price.

CHECK (ABS(SellPrice1 - SellPrice2) <= 0.5 * PurchasePrice)

You might also like