0% found this document useful (0 votes)
129 views7 pages

Chapter 4 Lab - Retrieving Data From Two or More Tables

The document provides instructions for completing a series of SQL queries using the IST272EagleCorp database. The exercises involve: 1. Performing inner joins between tables and returning all columns. 2. Performing inner joins between tables and returning specific columns where certain criteria are met. 3. Generating the same results from exercise 2 using implicit join syntax. 4. Performing additional inner joins between tables and filtering by date. 5. Performing inner joins between three tables using column aliases and filtering by part number and company name. 6. Performing a self-join between an employee table to return employees with matching salaries. 7. Performing an outer

Uploaded by

SamNGE
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)
129 views7 pages

Chapter 4 Lab - Retrieving Data From Two or More Tables

The document provides instructions for completing a series of SQL queries using the IST272EagleCorp database. The exercises involve: 1. Performing inner joins between tables and returning all columns. 2. Performing inner joins between tables and returning specific columns where certain criteria are met. 3. Generating the same results from exercise 2 using implicit join syntax. 4. Performing additional inner joins between tables and filtering by date. 5. Performing inner joins between three tables using column aliases and filtering by part number and company name. 6. Performing a self-join between an employee table to return employees with matching salaries. 7. Performing an outer

Uploaded by

SamNGE
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/ 7

Chapter 4 Query Lab

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

Rows Returned: 957

------------------------------------------------

2. Write a SELECT statement that uses an inner join and returns four
columns:

MachineID From the Machine table

ManufacturerName From the Machine table

TimeStandard From the MachineProcess table

PartNumber From the MachineProcess table

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:

SELECT M.MachineID, ManufacturerName, TimeStandard, PartNumber


FROM Machine M JOIN MachineProcess MP ON M.MachineID = MP.MachineID
WHERE PartNumber IN ('PS-001','PS-002','PS-003', 'PS-004') AND
Chapter 4 Query Lab
M.MachineID IN ('LABEL', 'SEAL')

LABEL SERGE 20 PS-001


LABEL SERGE 20 PS-002
LABEL SERGE 20 PS-003
LABEL SERGE 20 PS-004
SEAL SERGE 50 PS-001
SEAL SERGE 50 PS-002
SEAL SERGE 50 PS-003
SEAL SERGE 50 PS-004

------------------------------------------------

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')

LABEL SERGE 20 PS-001


LABEL SERGE 20 PS-002
LABEL SERGE 20 PS-003
LABEL SERGE 20 PS-004
SEAL SERGE 50 PS-001
SEAL SERGE 50 PS-002
SEAL SERGE 50 PS-003
SEAL SERGE 50 PS-004

------------------------------------------------

4. Write a SELECT statement that uses an inner join and returns four
columns:

ShipmentID from the PackingSlip table


LastName from the Employee table
JobTitle from the Employee table
ShippedDate from the PackingSlip table
Only return rows where the ShippedDate >= '2017-02-27'
and sort the result set by LastName
Chapter 4 Query Lab
Paste below the code you wrote and type the number of rows
returned for exercise 4:

SELECT ShipmentID, LastName, JobTitle, ShippedDate


FROM PackingSlip PS JOIN Employee E ON E.EmployeeID = PS.EmployeeID
WHERE ShippedDate >= '2017-02-27'
ORDER BY LastName

Rows Returned: 561

------------------------------------------------

5. Write a SELECT statement that returns four columns from three tables,
all using column aliases:

Title CustTitle Column


Company CompanyName Column
LastName CustLastName Column
Date OrderDate Column
Part PartNumber Column

Assign the following correlation names to the tables:

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:

SELECT CustTitle AS Title, CompanyName AS Company, CustLastName AS


LastName,
OrderDate AS Date, PartNumber AS Part
FROM Customer C JOIN CustOrder CO ON C.CustomerID = CO.CustomerID JOIN
CustOrderLine COL ON CO.OrderID = COL.OrderID
WHERE PartNumber LIKE 'PR%' AND
CompanyName IS NOT NULL

Mr. South Street Rehabilitation Meers 2016-07-01 00:00:00 PRT-


006
Ms. Tippe Inn Scheetz 2016-07-02 00:00:00 PRT-003
Ms. Needle Center Plyman 2016-07-05 00:00:00 PRT-006
Ms. Computer Consultants Jones 2016-07-06 00:00:00 PRT-004
Mrs. Family Medical Center Strong 2016-07-07 00:00:00 PRT-
004
Mr. Greenpart Steet Metal Doremski 2016-07-07 00:00:00 PRT-
003
Mr. Regency Hospital Gasper 2016-07-13 00:00:00 PRT-004
Mr. Realty Specialties Tarter 2016-07-28 00:00:00 PRT-
004
Chapter 4 Query Lab
Mr. Laser Graphics Associates Kaakeh 2016-08-02 00:00:00
PRT-006
Mr. Copy Center Robles 2016-08-05 00:00:00 PRT-006
Dr. Recreation Supply Burns 2016-08-06 00:00:00 PRT-004
Mrs. Camping Supplies Deets 2016-08-09 00:00:00 PRT-004
Mrs. Family Medical Center Strong 2016-08-24 00:00:00 PRT-
006
Mr. Pools For You Ezra 2016-08-25 00:00:00 PRT-003
Mr. BMA Advertising Design Purcell 2016-08-27 00:00:00 PRT-
006
Mrs. Sharons Shamrock Rouls 2016-08-30 00:00:00 PRT-004
Mr. Regency Hospital Gasper 2016-09-01 00:00:00 PRT-002
Ms. Vets Inc. Cassens 2016-09-08 00:00:00 PRT-004
Mr. Vacation Car Rentals Strehle 2016-09-10 00:00:00 PRT-
006
Ms. Conner National Jacobs 2016-09-16 00:00:00 PRT-003
Mr. Regency Hospital Gasper 2016-09-17 00:00:00 PRT-003
Ms. Cottingham Plastics Cottingham 2016-09-20 00:00:00 PRT-
001
Mr. Powerful Employment Sokeland 2016-09-28 00:00:00 PRT-
006
Mrs. Rydell High School Franks 2016-10-05 00:00:00 PRT-
004
Mr. Cleaning Supply Schofield 2016-10-06 00:00:00 PRT-003
Mrs. Family Medical Center Strong 2016-10-12 00:00:00 PRT-
001
Mr. Bankruptcy Help Lichty 2016-10-14 00:00:00 PRT-001
Mr. Karate Made Easy Scott 2016-10-22 00:00:00 PRT-002
Mr. Realty Specialties Tarter 2016-10-27 00:00:00 PRT-
006
Mr. Pools For You Ezra 2016-10-31 00:00:00 PRT-004
Mr. Gards Auto Repair Sammons 2016-11-10 00:00:00 PRT-003
Mr. Karate Made Easy Scott 2016-11-11 00:00:00 PRT-001
Mr. Wadake Critters Gillespie 2016-11-17 00:00:00 PRT-001
Mr. Security Installation Gray 2016-12-01 00:00:00 PRT-004
Mrs. Sampson Home Mortgages Kyger 2016-12-06 00:00:00 PRT-003
Dr. Investment Specialties Valle 2016-12-06 00:00:00 PRT-001
Mr. Ceramic Supply Talauage 2016-12-07 00:00:00 PRT-006
Mr. Phone Corporation Katpally 2016-12-10 00:00:00 PRT-006
Mr. Security Installation Gray 2016-12-10 00:00:00 PRT-001
Ms. Automart Nakamura 2016-12-15 00:00:00 PRT-002
Ms. Automart Nakamura 2016-12-15 00:00:00 PRT-004
Mrs. Bryant Accounting Xiao 2016-12-16 00:00:00 PRT-004
Mrs. Sharons Shamrock Rouls 2016-12-16 00:00:00 PRT-003
Mr. South Street Rehabilitation Meers 2016-12-16 00:00:00 PRT-
004
Mr. North Street Church Elston 2017-01-11 00:00:00 PRT-
001
Dr. Investment Specialties Valle 2017-01-18 00:00:00 PRT-004
Mr. First National Bank Barrick 2017-01-27 00:00:00 PRT-
002
Ms. Conner National Jacobs 2017-02-16 00:00:00 PRT-004
Mr. BMA Advertising Design Purcell 2017-02-17 00:00:00 PRT-
006
Mr. Bankruptcy Help Lichty 2017-02-28 00:00:00 PRT-003
Dr. Main St. Bar and Grill Kluth 2017-03-23 00:00:00 PRT-001
Chapter 4 Query Lab
Mr. City Bus Transport Osmanova 2017-03-30 00:00:00 PRT-
006

------------------------------------------------

6. Write a SELECT statement that returns three columns

LastName From the Employee table


FirstName From the Employee table
SalaryWage From the Employee table

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.

Hint: Use a self-join.

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

Albregts Nicholas 13.50


Hettinger Gregory 13.50
Ortman Austin 16.50
Underwood Patricha 16.50
Nairn Michelle 16.75
Boden John 16.75
Voltare John 145000.00
Cochran Steve 145000.00

------------------------------------------------
7. Write a SELECT statement that returns three columns:

EmployeeID From the Employee table


LastName From the Employee table
ShippedDate From the PackingSlip table

The result set should have at least one row for each employee regardless
of whether or not the employee has ever packed an order.

Hint: Use an outer join to the PackingSlip table

Paste below the code you wrote and type the number of rows
returned for exercise 7:

SELECT Employee .EmployeeID, Employee .LastName,


PackingSlip .ShippedDate
FROM Employee
Chapter 4 Query Lab
FULL OUTER JOIN PackingSlip ON Employee .EmployeeID =
PackingSlip .EmployeeID

Rows Returned: 3533

------------------------------------------------
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

Hint: Use UNION or UNION ALL

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.

Hint: You will most likely have to join three tables

Paste below the code you wrote and the run results you
obtained for exercise 10:
Chapter 4 Query Lab

SELECT DISTINCT o.OrderID, o.OrderDate, c.CustFirstName, c.CustLastName,


c.Phone
FROM CustOrder o
JOIN CustOrderLine COL ON o.OrderID = COL.OrderID
JOIN Customer c ON o.CustomerID = c.CustomerID
WHERE COL.Status = 'PENDING' AND o.OrderDate > '2017-03-30'

2000000805 2017-03-31 00:00:00 Marjorie Vandermay 308-489-1137


2000000806 2017-03-31 00:00:00 Daniel Rodkey 719-748-3205
2000000807 2017-03-31 00:00:00 Cecil Scheetz 207-679-9822
2000000808 2017-03-31 00:00:00 Andy Huegel 302-620-1366
2000000809 2019-04-30 00:00:00 Jim Blough 610-261-4677
2000000810 2020-01-30 00:00:00 Jim Baggins 419-376-9228

------------------------------------------------

You might also like