Chapter 4 Lab - Retrieving Data From Two or More Tables
Chapter 4 Lab - Retrieving Data From Two or More Tables
Exercise Instructions
1. Type your name and the date into the space provided.
2. Use the SQL Server Management Studio and the IST272EagleCorp database to
complete this lab.
3. Write T-SQL statements to query the tables contained in the IST272EagleCorp
database and complete each of the exercises in this lab per the directions provided
below.
4. Upload and submit before the due date.
Exercises
1. Write a SELECT statement that uses an inner join and returns all
columns from the Machine table and all columns from the the
MachineProcess table.
Paste below the code you wrote and type the number of rows
returned for exercise 1:
SELECT *
FROM Machine M JOIN MachineProcess MP ON M.MachineID = MP.MachineID
------------------------------------------------
2. Write a SELECT statement that uses an inner join and returns four
columns:
Only return rows where the PartNumber has one of the following values
('PS-001','PS-002','PS-003', 'PS-004') and the MachineID has one of the
following values('LABEL','SEAL’)
Paste below the code you wrote and the run results you
obtained for exercise 2:
------------------------------------------------
3. Generate the same result set described in exercise 2, but use the
implicit join syntax.
Hint: If the number of rows that comes back is different than what
you got for exercise 2, you probably forgot to include the join
condition in the WHERE clause (see page 139 of the book).
Paste below the code you wrote and the run results you
obtained for exercise 3:
SELECT M.MachineID, ManufacturerName, TimeStandard, PartNumber
FROM Machine M, MachineProcess MP
WHERE M.MachineID = MP.MachineID AND
PartNumber IN ('PS-001','PS-002','PS-003', 'PS-004') AND
M.MachineID IN ('LABEL', 'SEAL')
------------------------------------------------
4. Write a SELECT statement that uses an inner join and returns four
columns:
------------------------------------------------
5. Write a SELECT statement that returns four columns from three tables,
all using column aliases:
C Customer Table
CO CustOrder Table
COL CustOrderLine Table
Only include rows in the result set that have a partnumber that begins
with PR and a CompanyName that is not NULL.
Paste below the code you wrote and the run results you
obtained for exercise 5:
------------------------------------------------
The result set should have one row for each employee whose SalaryWage is
the same as another employee’s SalaryWage. Sort the final result set by
SalaryWage.
Paste below the code you wrote and the run results you
obtained for exercise 6:
SELECT E1 .LastName, E1 .FirstName, E1 .SalaryWage
FROM Employee E1 JOIN Employee E2
On E1 .EmployeeID <> E2 .EmployeeID
WHERE E1. SalaryWage = E2 .SalaryWage
Order by E1. SalaryWage
------------------------------------------------
7. Write a SELECT statement that returns three columns:
The result set should have at least one row for each employee regardless
of whether or not the employee has ever packed an order.
Paste below the code you wrote and type the number of rows
returned for exercise 7:
------------------------------------------------
8. Modify the solution to exercise 7 to filter for Employees
who never packed an order (only include rows in the result set for
employees that have never packed an order).
Paste below the code you wrote and type the number of rows
returned for exercise 8:
SELECT Employee .EmployeeID, Employee .LastName,
PackingSlip .ShippedDate
FROM Employee
FULL OUTER JOIN PackingSlip ON Employee .EmployeeID =
PackingSlip .EmployeeID
WHERE PackingSlip .ShippedDate IS NULL
Rows Returned: 37
------------------------------------------------
9. Generate a result set that contains two columns:
CompanyName and Phone. The result set should have one row for each
Customer with a CompanyName that is not null and one row for each
Shipper with a ShipperName that is not null with following exception:
Do not include
Paste below the code you wrote and type the number of rows
returned for exercise 9:
SELECT CompanyName, Phone
FROM Customer
WHERE CompanyName IS NOT NULL
UNION
SELECT ShipperName, Phone
FROM Shipper
WHERE ShipperName IS NOT NULL
Rows Returned: 82
------------------------------------------------
10. Write a SELECT statement that returns for each order line item with
a status of PENDING and an Order Date > ‘2017-03-30’, the Order ID and
Order Date along with the First Name, Last Name and Phone of the
customer who placed the order. List the order information only once.
Paste below the code you wrote and the run results you
obtained for exercise 10:
Chapter 4 Query Lab
------------------------------------------------