SQL Server More Clause Part 2
SQL Server More Clause Part 2
1. The HumanResources.Employee table does not contain the employee names. Join that table to the
Person.Person table on the BusinessEntityID column. Display the job title, birth date, first name,
FROM HumanResources.Employee AS E
2. The customer names also appear in the Person.Person table. Join the Sales.Customer table to the
Person.Person table. The BusinessEntityID column in the Person.Person table matches the
PersonID column in the Sales.Customer table. Display the CustomerID, StoreID, and TerritoryID
FROM Sales.Customer AS C
3. Extend the query written in question 2 to include the Sales.SalesOrderHeader table. Display the
SalesOrderID column along with the columns already specified. The Sales.SalesOrderHeader
LastName, SalesOrderID
FROM Sales.Customer AS C
SalesPersonID column in the Sales.SalesOrderHeader table. Display the SalesOrderID along with
FROM Sales.SalesOrderHeader AS S
ON S.SalesPersonID = SP.BusinessEntityID;
5. Add the name columns to the query written in question 4 by joining on the Person.Person table.
See whether you can figure out which columns will be used to write the join.
You can join the Person.Person table on the SalesOrderHeader table or the Sales.SalesPerson
table.
FROM Sales.SalesOrderHeader AS S
FROM Sales.SalesOrderHeader AS S
6. The catalog description for each product is stored in the Production.ProductModel table. Display
the columns that describe the product from the Production.Product table, such as the color and
FROM Production.Product AS P
have purchased. Hint: Five tables will be required to write this query!
FROM Sales.Customer AS C
ON SOH.SalesOrderID = SOD.SalesOrderID
1. Write a query that displays all the products along with the SalesOrderID even if an order has never
been placed for that product. Join to the Sales.SalesOrderDetail table using the ProductID
column.
FROM Production.Product AS P
2. Change the query written in question 1 so that only products that have not been ordered show up
in the query.
FROM Production.Product AS P
Sales.SalesOrderHeader table along with the SalesOrderID column even if no orders match.
FROM Sales.SalesPerson AS SP
ON SP.BusinessEntityID = SOH.SalesPersonID;
4. Change the query written in question 3 so that the salesperson’s name also displays from the
Person.Person table.
MiddleName, LastName
FROM Sales.SalesPerson AS SP
ON SP.BusinessEntityID = SOH.SalesPersonID
ON P.BusinessEntityID = SP.BusinessEntityID;
Purchasing.ShipMethod tables. Write a query joining all three tables, making sure it contains all
ShipBase columns.
ON SOH.CurrencyRateID = CR.CurrencyRateID
ON SOH.ShipMethodID = SM.ShipMethodID;
6. Write a query that returns the BusinessEntityID column from the Sales.SalesPerson table along
7. Starting with the query written in Listing 4-13, join the table a to the Person.Contact table to
display the employee’s name. The EmployeeID column joins the ContactID column.
USE AdventureWorks;
GO
a.Title AS EmployeeTitle,
b.EmployeeID AS ManagerID,
b.Title AS ManagerTitle,
FROM HumanResources.Employee AS a
ON a.ManagerID = b.EmployeeID
Writing Subqueries
1. Using a subquery, display the product names and product ID numbers from the
FROM Production.Product
FROM Production.Product
3. If the Production.ProductColor table is not part of the AdventureWorks2008 database, run the
code in Listing 4-11 to create it. Write a query using a subquery that returns the rows from the
Production.ProductColor table that are not being used in the Production.Product table.
SELECT Color
FROM Production.ProductColor
4. Write a query that displays the colors used in the Production.Product table that are not listed in
the Production.ProductColor table using a subquery. Use the keyword DISTINCT before the
FROM Production.Product
5. Write a UNION query that combines the ModifiedDate from Person.Person and the HireDate from
HumanResources.Employee.
SELECT ModifiedDate
FROM Person.Person
UNION
SELECT HireDate
FROM HumanResources.Employee;
Exploring Derived Tables and Common
Table Expressions
1. Using a derived table, join the Sales.SalesOrderHeader table to the Sales.SalesOrderDetail table.
Display the SalesOrderID, OrderDate, and ProductID columns in the results. The
INNER JOIN (
ON SOH.SalesOrderID = SOD.SalesOrderID;
WITH SOD AS (
FROM Sales.SalesOrderDetail
3. Write a query that displays all customers along with the orders placed in 2001. Use a common
table expression to write the query and include the CustomerID, SalesOrderID, and OrderDate
WITH SOH AS (
SELECT SalesOrderID, OrderDate, CustomerID
FROM Sales.SalesOrderHeader
WHERE OrderDate BETWEEN '1/1/2001' AND '12/31/2001'
)
SELECT C.CustomerID, SalesOrderID, OrderDate
FROM Sales.Customer AS C
LEFT OUTER JOIN SOH ON C.CustomerID = SOH.CustomerID;
Thinking About Performance
Run the following code to add and populate a new column, OrderID, to the
Sales.SalesOrderDetail table. After running the code, the new column will contain the same data as the
SalesOrderID column.
USE AdventureWorks;
GO
GO
1. Make sure that the Include Actual Execution Plan is turned on before running the following code.
View the execution plans, and explain why one query performs better than the other.
--1
SELECT o.SalesOrderID,d.SalesOrderDetailID
FROM Sales.SalesOrderHeader AS o
--2
SELECT o.SalesOrderID,d.SalesOrderDetailID
FROM Sales.SalesOrderHeader AS o
ON o.SalesOrderID = d.OrderID;
SalesOrderID column, performs better because there is a nonclustered index defined on the
SalesOrderID column. There is not an index on the new OrderID column, so a clustered index
2. Compare the execution plans of the derived table example and the CTE example
. Explain why the query performance is the same or why one query performs better
SELECT *
select
from HumanResources.Employee