You are on page 1of 4

BADM 352

Lab 3: SQL JOIN

Exercise 1:
USE sales;

###QUESTION 1 As you can see in the relational model, PRODUCT and VENDOR are related
by the key V_CODE. Natural join the two tables. How many rows does the natural join return?
SELECT * FROM PRODUCT NATURAL JOIN VENDOR; 14 rows

###QUESTION 2 Inner join the PRODUCT table and VENDOR table. What is the join
condition, and how many rows does the inner join return?
SELECT * FROM PRODUCT INNER JOIN VENDOR ON PRODUCT.V_CODE =
VENDOR.V_CODE; 14 rows

###QUESTION 3 Inner join the PRODUCT table and VENDOR table without join condition,
and how many rows does this inner join return?
SELECT * FROM PRODUCT INNER JOIN VENDOR; 176 rows

m
er as
###QUESTION 4 Left join the PRODUCT table and VENDOR table. What is the join condition,

co
and how many rows does the left join return?

eH w
SELECT * FROM PRODUCT LEFT JOIN VENDOR ON PRODUCT.V_CODE =
VENDOR.V_CODE; 16 rows

o.
rs e
ou urc
###QUESTION 5 Right join the PRODUCT table and VENDOR table. What is the join
condition, and how many rows does the right join return?
SELECT * FROM PRODUCT RIGHT JOIN VENDOR ON PRODUCT.V_CODE =
o

VENDOR.V_CODE; 19 rows
aC s

###QUESTION 6 Full join the PRODUCT table and VENDOR table. How many rows does the
vi y re

full join return?


SELECT * FROM PRODUCT LEFT JOIN VENDOR ON PRODUCT.V_CODE =
VENDOR.V_CODE UNION SELECT * FROM PRODUCT RIGHT JOIN VENDOR ON
ed d

PRODUCT.V_CODE = VENDOR.V_CODE; 21 rows


ar stu

###QUESTION 7 You may already notice that the above six joins return different number of
rows. Explain the differences.
Natural join finds matching column and value to join table while inner join joins on a certain
is

condition. Inner join needs a joint condition to match columns or it would return all rows (does
not match). Left join matches all matched rows of the left table to the right table as well as the
Th

unmatched values in the left table. Right join does the opposite, it matches all matched rows of
the right table to the left table as well as the unmatched values in the right table. Full join returns
all matched rows of right and left table as well as all the unmatched rows of the right and left
sh

tables.

###QUESTION 8 Apply different joins on other tables (e.g., what if two tables are related, what
if two tables are not related), and understand the differences between outcomes of different joins
. (You don’t need to answer this question in your submission)

This study source was downloaded by 100000775457090 from CourseHero.com on 10-25-2021 14:14:02 GMT -05:00

https://www.coursehero.com/file/37181405/Lab-3docx/
BADM 352
Lab 3: SQL JOIN

###QUESTION 9 List the vendors which do not supply products. Use join clause.
SELECT VENDOR.V_CODE,V_NAME,V_CONTACT,P_CODE FROM PRODUCT RIGHT
JOIN VENDOR ON PRODUCT.V_CODE = VENDOR.V_CODE WHERE P_CODE IS NULL;

###QUESTION 10 List all vendors located in ‘FL’, and the products they supply. Note that it’s
possible that the vendor does not supply any products.
SELECT VENDOR.V_CODE, V_NAME, V_CONTACT, V_AREACODE, V_PHONE,
V_STATE, V_ORDER, P_CODE, P_DESCRIPT FROM PRODUCT RIGHT JOIN VENDOR
ON PRODUCT.V_CODE = VENDOR.V_CODE WHERE V_STATE ='FL' ORDER BY
V_CODE ASC;

###QUESTION 11List all employees with their managers’ names. (hint: join EMP table to
itself).
SELECT M.EMP_NUM, M.EMP_LNAME, M.EMP_FNAME,N.EMP_NUM,
N.EMP_LNAME, N.EMP_FNAME FROM EMP M LEFT JOIN EMP N ON M.EMP_MGR =

m
N.EMP_NUM ORDER BY M.EMP_NUM ASC;

er as
co
###QUESTION 12 Generate a listing of all purchases made by the customers. (Hint: Multiple

eH w
related tables need to be joined).
SELECT

o.
rs e
CUSTOMER.CUS_CODE,INVOICE.INV_NUMBER,INVOICE.INV_DATE,PRODUCT.P_DE
ou urc
SCRIPT,LINE.LINE_UNITS,LINE.LINE_PRICE FROM CUSTOMER, INVOICE, LINE,
PRODUCT WHERE CUSTOMER.CUS_CODE=INVOICE.CUS_CODE AND
INVOICE.INV_NUMBER = LINE.INV_NUMBER AND PRODUCT.P_CODE =
o

LINE.P_CODE ORDER BY INVOICE.CUS_CODE,


INVOICE.INV_NUMBER,PRODUCT.P_DESCRIPT;
aC s
vi y re

###QUESTION 13 The results in question 12 show the sales history on 2014-01- 16 and 2014-
01-17. Based on the query in question 12, write a new query to computer each day’s total sales
amount.
ed d

SELECT INV_DATE, SUM(LINE.LINE_PRICE*LINE.LINE_UNITS) FROM INVOICE JOIN


ar stu

LINE ON INVOICE.INV_NUMBER=LINE.INV_NUMBER JOIN PRODUCT ON


LINE.P_CODE=PRODUCT.P_CODE GROUP BY DAY(INV_DATE);

###QUESTION 14 Generate a listing of customer purchases, including a new column called


is

subtotal (unit_price * units_bought) for each line in the invoice.


SELECT CUSTOMER.CUS_CODE, INVOICE.INV_NUMBER, P_DESCRIPT, LINE_UNITS,
Th

LINE_PRICE, LINE_UNITS*LINE_PRICE AS SUBTOTAL FROM CUSTOMER JOIN


INVOICE ON CUSTOMER.CUS_CODE=INVOICE.CUS_CODE JOIN LINE ON
INVOICE.INV_NUMBER=LINE.INV_NUMBER JOIN PRODUCT ON
sh

LINE.P_CODE=PRODUCT.P_CODE ORDER BY CUSTOMER.CUS_CODE ASC;

###QUESTION 15 Modify the query used in question 14 to produce the summary shown below,
i.e., return purchase amount foreach customer.
SELECT CUSTOMER.CUS_CODE, CUS_BALANCE, SUM(LINE_UNITS*LINE_PRICE)
AS Total_Purchase FROM CUSTOMER JOIN INVOICE ON

This study source was downloaded by 100000775457090 from CourseHero.com on 10-25-2021 14:14:02 GMT -05:00

https://www.coursehero.com/file/37181405/Lab-3docx/
BADM 352
Lab 3: SQL JOIN

CUSTOMER.CUS_CODE=INVOICE.CUS_CODE JOIN LINE ON


INVOICE.INV_NUMBER=LINE.INV_NUMBER GROUP BY CUS_CODE;

###QUESTION 16 Modify the query in question 14 to include the number of individual product
purchases made by each customer. (In other words, if the customer’s invoice is based on three
products, one per LINE_NUMBER, you would count three product purchases. If you examine
the original invoice data, you will note that customer 10011 generated three invoices, which
contained a total of six lines, each representing a product purchase.)
SELECT CUSTOMER.CUS_CODE, CUS_BALANCE, SUM(LINE_UNITS*LINE_PRICE)
AS Total_Purchase, COUNT(INVOICE.INV_NUMBER) AS Number_Of_Purchases FROM
CUSTOMER JOIN INVOICE ON CUSTOMER.CUS_CODE=INVOICE.CUS_CODE JOIN
LINE ON INVOICE.INV_NUMBER=LINE.INV_NUMBER JOIN PRODUCT ON
LINE.P_CODE=PRODUCT.P_CODE GROUP BY CUSTOMER.CUS_CODE;

###QUESTION 17 Create a query to compute the average purchase amount made by each

m
customer, and round the results to 2 decimals. (Hint: use the results of problem 16 as the basis

er as
for this query.)

co
SELECT CUSTOMER.CUS_CODE, AVG(LINE_UNITS*LINE_PRICE) AS

eH w
AVG_PURCASE_AMOUNT FROM CUSTOMER JOIN INVOICE ON
CUSTOMER.CUS_CODE=INVOICE.CUS_CODE JOIN LINE ON

o.
rs e
INVOICE.INV_NUMBER=LINE.INV_NUMBER JOIN PRODUCT ON
ou urc
LINE.P_CODE=PRODUCT.P_CODE GROUP BY CUSTOMER.CUS_CODE;
o

###QUESTION 18 Based on the results in question 14, write a query to show the invoices and
invoice totals as shown below.
aC s

SELECT CUSTOMER.CUS_CODE,
vi y re

INVOICE.INV_NUMBER,SUM(LINE_UNITS*LINE_PRICE) AS Invoice_Total FROM


CUSTOMER JOIN INVOICE ON CUSTOMER.CUS_CODE=INVOICE.CUS_CODE JOIN
LINE ON INVOICE.INV_NUMBER=LINE.INV_NUMBER JOIN PRODUCT ON
ed d

LINE.P_CODE=PRODUCT.P_CODE GROUP BY INVOICE.INV_NUMBER ORDER BY


ar stu

CUSTOMER.CUS_CODE ASC;

Exercise 2:
is

USE world;
Th

###QUESTION 19 Left JOIN City table with Country table, and count the number of rows in the
result.
SELECT COUNT(*) FROM City LEFT JOIN Country ON City.CountryCode = Country.Code;
sh

4079 rows
###QUESTION 20 Right JOIN City table with Country table, and count the number of rows in
the result.
SELECT COUNT(*) FROM City RIGHT JOIN Country ON City.CountryCode = Country.Code;
4086 rows

This study source was downloaded by 100000775457090 from CourseHero.com on 10-25-2021 14:14:02 GMT -05:00

https://www.coursehero.com/file/37181405/Lab-3docx/
BADM 352
Lab 3: SQL JOIN

###QUESTION 21The above two have different numbers. Explain why.


Because left join contains null value from City table while right join contains null value from
Country table .

m
er as
co
eH w
o.
rs e
ou urc
o
aC s
vi y re
ed d
ar stu
is
Th
sh

This study source was downloaded by 100000775457090 from CourseHero.com on 10-25-2021 14:14:02 GMT -05:00

https://www.coursehero.com/file/37181405/Lab-3docx/
Powered by TCPDF (www.tcpdf.org)

You might also like