Académique Documents
Professionnel Documents
Culture Documents
1
Introduction:
This lab manual is based on the SQL Server 2005 AdventureWorks sample database.
All assignments will use the objects from the AdventureWorks database, and the labs will use the
Microsoft SQL Server management Studio as a learning platform.
Each lab will feature a basic assignment and optionally a challenge assignment. You Instructor will
review each lab in class and provide example solutions to the challenge assignments.
Learning SQL is a hand-on experience, you are encouraged to explore beyond these basic assignments
and ask questions in class.
Before you proceed beyond a specific lab, please ensure you understand the topics being explored and
all your questions have been answered.
2
Lab 1: Learning to explore a Database Table in SQL Server Management Studio
For the Employee table in the AdventureWorks sample database, please answer the following
questions:
3. Are there any Foreign Keys within the Employee table, if so what tables do these keys
reference?
7. What is the Default Value for the SalariedFlag? And what does it mean?
Lab 1 Challenge:
1. Which field(s) will change value when the uEmployee Trigger is invoked?
3
Lab 2: Using the Graphical Query Designer
Using the Graphical Query Designer, add the following tables and fields:
Contact (Table)
FirstName (Field)
LastName
Employee
Title
SalesPerson
SalesYTD
SalesTerritory
Name
CountryRegion
Name
Lab 2 Challenge:
Product name, Name of the Vendor that supplies that product and the City that Vendor is located in?
4
Lab 3: The Languages of SQL
Please match the SQL command with the sub-language it belongs in:
Select
Alter
Grant
Update
Deny
Create
Drop
Delete
Deny
Insert
Lab 3 Challenge:
To add a column to an existing database table, which of the command listed above would be required
to:
5
Lab 4: The Select Command
For the Employee table in the AdventureWorks sample database, please create the following queries:
Lab 4 Challenge:
Create a query that lists: All Employees that have no manager, sorted by title and birth date
6
Lab 5: The Select Command Comparison Operators
For the Production.Product table in the AdventureWorks sample database, please create the following
queries:
2. All Products that have a Safety Stock Level less than 100.
5. Product Name, Color and Size where the Size is Not 'L' or Not 'M' or not null.
Lab 5 Challenge:
Create a query to list: Product Name, Number, Days to Manufacture, and List Price for all Products that
take between 3 and 6 days to manufacture, sorted by list price in descending order.
7
Lab 6: The Select Command Expanded Filtering
For the Sales.SalesPerson table in the AdventureWorks sample database, please create the following
queries:
1. All records that have a Bonus between 1000.00 and 5000.00 sort by Bonus.
3. First 10 records.
4. All record where this years sales is greater than last years sales.
5. All records with nulls in Territory ID sorted by Sales Person Id in descending order.
Lab 6 Challenge:
Create a query to list: The top 25 percent of sales where last years sales was not zero.
8
Lab 7: The SQL Functions
These question refer to several tables in the AdventureWorks sample database, please create the
following queries:
5. The Currency Code and the first 3 letters of the Currency Name as 'AB'.
Lab 7 Challenge:
Create query to list the number of days between the Product Sell Start Date and the Product Sell End
Date, where Sell End Date is a valid date.
9
Lab 8: The Aggregation and Grouping
These question refer to several tables in the AdventureWorks sample database, please create the
following queries:
1. List the Title, Gender, and Lowest Login Id for each group of Employees grouped by the
following titles: Buyer', 'Recruiter' or 'Stocker'.
2. List Product Sub Category IDs from the Product table, include only those subcategories that
occur more than 20 times. In addition to the ID also return the first product name in
alphabetical order and the highest price for products in this subcategory.
3. Provide a list of Employee Titles and Genders from the Employee table. For each title, include
the average vacation hours for all employees of each gender. Also provide an additional subtotal
for each title that includes the average vacation hours for all employees of that title.
Lab 8 Challenge:
10
Lab 9: The Multi-Table Queries
These question refer to several tables in the AdventureWorks sample database, please create the
following queries:
1. Create a list of Vendors and the subtotals amounts for their purchase orders, sorted by vendor
name. Please include vendor name, and the subtotal amount for all vendors who have purchase
orders recorded in the PurchaseOrderHeader table.
2. Provide a list of all Product subcategories and related products that do not have any sales order
detail records. Please provide two columns including the SubCategory Name and the Product
Name.
Lab 9 Challenge:
11
Student Lab Manual
Solutions
12
Lab 1: Learning to explore a Database Table in SQL Server Management Studio
For the Employee table in the AdventureWorks sample database, please answer the following
questions:
HumanResources
EmployeeID
3. Are there any Foreign Keys within the Employee table, if so what tables do these keys
reference?
ContactID Contact
ManagerID Employee
ManagerID
M = Married, S = Single
NationalIDNumber,
LoginID
Title
7. What is the Default Value for the SalariedFlag? And what does it mean?
13
Lab 2: Using the Graphical Query Designer
Using the Graphical Query Designer, add the following tables and fields:
Contact (Table)
FirstName (Field)
LastName
Employee
Title
SalesPerson
SalesYTD
SalesTerritory
Name
CountryRegion
Name
14
Lab 3: The Languages of SQL
Please match the SQL command with the sub-language it belongs in:
Select X
Alter X
Grant X
Update X
15
Deny X
Create X
Drop X
Delete X
Deny X
Insert X
16
Lab 4: The Select Command
For the Employee table in the AdventureWorks sample database, please create the following queries:
SELECT *
FROM HumanResources.Employee
WHERE MaritalStatus = 'S';
SELECT *
FROM HumanResources.Employee
WHERE MaritalStatus = 'M'
AND Gender = 'M';
SELECT *
FROM HumanResources.Employee
ORDER BY HireDate;
17
Lab 5: The Select Command Comparison Operators
For the Production.Product table in the AdventureWorks sample database, please create the following
queries:
SELECT *
FROM Production.Product
WHERE Color = 'Black';
2. All Products that have a Safety Stock Level less than 100.
SELECT *
FROM Production.Product
WHERE SafetyStockLevel < 100;
SELECT *
FROM Production.Product
WHERE ProductNumber LIKE 'BK%';
SELECT *
FROM Production.Product
WHERE Name LIKE '%Lock%';
5. Product Name, Color and Size where the Size is Not 'L' or Not 'M' or not null.
18
Lab 6: The Select Command Expanded Filtering
For the Sales.SalesPerson table in the AdventureWorks sample database, please create the following
queries:
1. All records that have a Bonus between 1000.00 and 5000.00 sort by Bonus.
SELECT *
FROM Sales.SalesPerson
WHERE Bonus BETWEEN 1000.00 AND 5000.00
ORDER BY Bonus;
SELECT *
FROM Sales.SalesPerson
WHERE TerritoryID IN (1,2,4);
3. First 10 records.
SELECT TOP 10 *
FROM Sales.SalesPerson;
4. All record where this years sales is greater than last years sales.
SELECT *
FROM Sales.SalesPerson
WHERE SalesYTD > SalesLastYear;
5. All records with nulls in Territory ID sorted by Sales Person Id in descending order.
SELECT *
FROM Sales.SalesPerson
WHERE TerritoryID IS NULL
ORDER BY SalesPersonID DESC;
19
Lab 7: The SQL Functions
These question refer to several tables in the AdventureWorks sample database, please create the
following queries:
SELECT AVG(CommissionPct)
FROM Sales.SalesPerson;
SELECT COUNT(*)
FROM HumanResources.Employee
WHERE Gender = 'M';
SELECT MAX(ListPrice)
FROM Production.Product;
SELECT LEN(Description)
FROM Production.ProductDescription;
5. The Currency Code and the first 3 letters of the Currency Name as 'AB'.
20
Lab 8: The Aggregation and Grouping
These question refer to several tables in the AdventureWorks sample database, please create the
following queries:
1. List the Title, Gender, and Lowest Login Id for each group of Employees grouped by the
following titles: Buyer', 'Recruiter' or 'Stocker'.
2. List Product Sub Category IDs from the Product table, include only those subcategories that
occur more than 20 times. In addition to the ID also return the first product name in
alphabetical order and the highest price for products in this subcategory.
3. Provide a list of Employee Titles and Genders from the Employee table. For each title, include
the average vacation hours for all employees of each gender. Also provide an additional subtotal
for each title that includes the average vacation hours for all employees of that title.
21
Lab 9: The Multi-Table Queries
These question refer to several tables in the AdventureWorks sample database, please create the
following queries:
1. Create a list of Vendors and the subtotals amounts for their purchase orders, sorted by vendor
name. Please include vendor name, and the subtotal amount for all vendors who have purchase
orders recorded in the PurchaseOrderHeader table.
2. Provide a list of all Product subcategories and related products that do not have any sales order
detail records. Please provide two columns including the SubCategory Name and the Product
Name.
22
Challenge
Lab 1:
UPDATE [HumanResources].[Employee]
SET [HumanResources].[Employee].[ModifiedDate] = GETDATE()
Lab 2:
Lab 3:
Lab 4:
SELECT *
FROM HumanResources.Employee
WHERE ManagerID IS NULL
ORDER BY Title, BirthDate
Lab 5:
23
Lab 6:
Lab 7:
Lab 7:
Lab 8:
Lab 9:
24