You are on page 1of 2

MULTIPLE TABLES EXERCISE

Use Data Multiple Tables v0.xlsx

1. List client id, name (first name, mid intial, last name – concatenated) and his corresponding
orders (invoice numbers) and order date from the order table if the order was made in 2020

SELECT C.ClientNo, CONCAT(TRIM(FIRSTNAME), ' ', LEFT(TRIM(MIDNAME), 1), '


',TRIM(SURNAME)) AS Name, O.InvoiceNo, O.OrderDate FROM CLIENT C JOIN
ORDERT O ON C.CLIENTNO = O.CLIENTNO WHERE YEAR(OrderDate) = 2020
ORDER BY 1;
2. List the client id and the product codes with order qty ordered by each client

SELECT O.ClientNo, D.ProductCode, D.Quantity FROM ORDERT O INNER JOIN


DETAIL D ON O.InvoiceNo = D.InvoiceNo;
3. List the client id, the description of the product ordered including the unit cost. Display unique
records only

SELECT DISTINCT O.ClientNo, P.ProductDescription, P.UnitCost FROM PRODUCT P


INNER JOIN DETAIL D ON P.ProductCode = D.ProductCode INNER JOIN ORDERT O
ON D.InvoiceNo = O.InvoiceNo;
4. Display the amount (quantity * unit price) bought by client for product bought per the invoice.

SELECT O.ClientNo, D.InvoiceNo, D.ProductCode, (D.Quantity * P.UnitCost) AS 'Amount'


FROM ORDERT O INNER JOIN DETAIL D ON O.InvoiceNo=D.InvoiceNo INNER JOIN
PRODUCT P ON D.ProductCode=P.ProductCode;
5. Display the total amount bought by each client

SELECT CONCAT(TRIM(FIRSTNAME), ' ', LEFT(TRIM(MIDNAME), 1), '


',TRIM(SURNAME)) AS 'Client Name', SUM((D.Quantity * P.UnitCost)) AS Total FROM
CLIENT C INNER JOIN ORDERT O ON C.ClientNo = O.ClientNo INNER JOIN DETAIL
D ON O.InvoiceNo = D.InvoiceNo INNER JOIN PRODUCT P ON D.ProductCode =
P.ProductCode GROUP BY 1;
6. Display client name, credit limit, product bought (description) and the amount for product
bought.

SELECT CONCAT(TRIM(FIRSTNAME), ' ', LEFT(TRIM(MIDNAME), 1), '


',TRIM(SURNAME)) AS 'Client Name', C.CreditLimit, ProductDescription, (D.Quantity *
P.UnitCost) AS Amount FROM CLIENT C INNER JOIN ORDERT O ON C.ClientNo =
O.ClientNo INNER JOIN DETAIL D ON O.InvoiceNo = D.InvoiceNo INNER JOIN
PRODUCT P ON D.ProductCode = P.ProductCode;
7. Display the invoice number and the product details corresponding to the products bought.
Product details will include all fields in the product table
SELECT O.InvoiceNo, P.ProductCode, P.ProductDescription, P.UnitCost, P.Category
FROM ORDERT O INNER JOIN DETAIL D ON O.InvoiceNo = D.InvoiceNo INNER
JOIN PRODUCT P ON D.ProductCode = P.ProductCode;
8. Display the client id and name of client together with the invoice corresponding to the client.
Display the client even if he didn’t make any purchase

SELECT C.ClientNo, CONCAT(TRIM(FIRSTNAME), ' ', LEFT(TRIM(MIDNAME), 1), '


',TRIM(SURNAME)) AS 'Client Name', O.InvoiceNo FROM ORDERT O RIGHT JOIN
CLIENT C ON O.ClientNo = C.ClientNo ORDER BY 3;
9. Display client id and all products he purchased (product code only). Display all product codes
even if not puchased by any client

SELECT O.ClientNo, P.ProductCode FROM PRODUCT P LEFT JOIN DETAIL D ON


P.ProductCode = D.ProductCode LEFT JOIN ORDERT O ON D.InvoiceNo = O.InvoiceNo;
10. Count how many invoices each client has and display only those with more than 1 invoice.

SELECT ClientNo, COUNT(*) AS 'Invoice Count' FROM ORDERT GROUP BY 1


HAVING COUNT(*) > 1;

You might also like