0% found this document useful (0 votes)
93 views

SQL Assignment 2 Opt3

The document provides instructions for a series of 8 SQL queries to retrieve and filter data from the AdventureWorks2008 database. The queries select columns from the Production.Product table, apply various filters on columns like Color and ListPrice, concatenate columns, and customize the output format. The goal is to practice different SQL skills like querying, filtering, concatenating, and customizing result sets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

SQL Assignment 2 Opt3

The document provides instructions for a series of 8 SQL queries to retrieve and filter data from the AdventureWorks2008 database. The queries select columns from the Production.Product table, apply various filters on columns like Color and ListPrice, concatenate columns, and customize the output format. The goal is to practice different SQL skills like querying, filtering, concatenating, and customizing result sets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

BSQL Assignment 203

1. Exercise 1: Querying and Filtering Data


Barem: 50%
Objective: H4SD - SQL skills
Problem Description:
This exercise performs on AdventureWorks2008 database that included in the same folder
with the assignment.
Note: A series of dots (…………) in result sets indicates that rows have been excluded for
presentational reasons.
Assumptions:
Questions to answer:

Write a query that retrieves the columns ProductID, Name, Color and ListPrice from
the
Production.Product table, with no filter. Your result set should look something like the
following.

ProductID Name Color ListPrice


----------- ------------------------------ --------------- -----------
1 Adjustable Race NULL 0,00
2 Bearing Ball NULL 0,00
Query 1
3 BB Ball Bearing NULL 0,00
4 Headset Ball Bearings NULL 0,00
316 Blade NULL 0,00
…………
995 ML Bottom Bracket NULL 101,24
996 HL Bottom Bracket NULL 121,49
997 Road-750 Black, 44 Black 539,99
998 Road-750 Black, 48 Black 539,99
999 Road-750 Black, 52 Black 539,99
(504 row(s) affected)

Continue to work with the previous query and exclude those rows that are 0 for the
column ListPrice.
Your result set should look something like the following.
ProductID Name Color ListPrice
----------- ------------------------------ --------------- ----------
514 LL Mountain Seat Assembly NULL 133,34
515 ML Mountain Seat Assembly NULL 147,14
Query 2
516 HL Mountain Seat Assembly NULL 196,92
517 LL Road Seat Assembly NULL 133,34
518 ML Road Seat Assembly NULL 147,14
…………
997 Road-750 Black, 44 Black 539,99
998 Road-750 Black, 48 Black 539,99
999 Road-750 Black, 52 Black 539,99
(304 row(s) affected)

Use the same query, but this time you just want to see the rows that are NULL for
the Color column.
Your result set should look something like the following.
ProductID Name Color ListPrice
----------- ------------------------------ - -------------- -----------
1 Adjustable Race NULL 0,00
2 Bearing Ball NULL 0,00
Query 3
3 BB Ball Bearing NULL 0,00
4 Headset Ball Bearings NULL 0,00
316 Blade NULL 0,00
………
947 HL Touring Handlebars NULL 91,57
994 LL Bottom Bracket NULL 53,99
995 ML Bottom Bracket NULL 101,24
996 HL Bottom Bracket NULL 121,49
(248 row(s) affected)

Use the same query, but this time you just want to see the rows that are not NULL for
the Color column. Your result set should look something like the following.
ProductID Name Color ListPrice
----------- ------------------------------ --------------- -----------
317 LL Crankarm Black 0,00
318 ML Crankarm Black 0,00
319 HL Crankarm Black 0,00
Query 4
320 Chainring Bolts Silver 0,00
………
992 Mountain-500 Black, 48 Black 539,99
993 Mountain-500 Black, 52 Black 539,99
997 Road-750 Black, 44 Black 539,99
998 Road-750 Black, 48 Black 539,99
999 Road-750 Black, 52 Black 539,99
(256 row(s) affected)

Now, combine two search arguments in the query you have been working with. You
just want to see the rows that are not NULL for the column Color, and the column
ListPricehas a value greater than zero. Your result set should look something like the
following.
ProductID Name Color ListPrice
----------- ------------------------------ --------------- ----------
680 HL Road Frame - Black, 58 Black 1431,50
706 HL Road Frame - Red, 58 Red 1431,50
Query 5
707 Sport-100 Helmet, Red Red 34,99
708 Sport-100 Helmet, Black Black 34,99
………
993 Mountain-500 Black, 52 Black 539,99
997 Road-750 Black, 44 Black 539,99
998 Road-750 Black, 48 Black 539,99
999 Road-750 Black, 52 Black 539,99
(245 row(s) affected)
Now we want a report that concatenates the columns Name and Color from the
Production.Product table. Your result set should look something like the following.
Make sure you exclude rows that are NULL for the column Color. Also notice the
column name.
Name And Color
------------------------------------------
LL Crankarm : Black
ML Crankarm : Black
HL Crankarm : Black
Chainring Bolts : Silver
Query 6
Chainring Nut : Silver
Chainring : Black
………
Mountain-500 Black, 44 : Black
Mountain-500 Black, 48 : Black
Mountain-500 Black, 52 : Black
Road-750 Black, 44 : Black
Road-750 Black, 48 : Black
Road-750 Black, 52 : Black
(256 row(s) affected)

Customize the previous query so the answer looks like the following.
Name And Color
--------------------------------------------------
NAME: LL Crankarm -- COLOR: Black
NAME: ML Crankarm -- COLOR: Black
Query 7
NAME: HL Crankarm -- COLOR: Black
NAME: Chainring Bolts -- COLOR: Silver
NAME: Chainring Nut -- COLOR: Silver
NAME: Chainring -- COLOR: Black
………
NAME: Mountain-500 Black, 48 -- COLOR: Black
NAME: Mountain-500 Black, 52 -- COLOR: Black
NAME: Road-750 Black, 44 -- COLOR: Black
NAME: Road-750 Black, 48 -- COLOR: Black
NAME: Road-750 Black, 52 -- COLOR: Black
(256 row(s) affected)

Now we would like to see the columns ProductID and Name from the
Production.Product table filtered by ProductID from 400 to 500. Write a query that
makes your result set look something like the following. Try to make your WHERE
clause as simple and readable as possible.
ProductID Name
----------- -----------------------------
400 LL Hub
401 HL Hub
402 Keyed Washer
Query 8 403 External Lock Washer 3
404 External Lock Washer 4
405 External Lock Washer 9
406 External Lock Washer 5
………
494 Paint - Silver
495 Paint - Blue
496 Paint - Yellow
497 Pinch Bolt
(98 row(s) affected)

We would like to see the columns ProductID, Name and color from the
Production.Product table restricted to the colors black and blue. Write a query that
makes your result set look something like the following. Try to make your WHERE
clause as simple and readable as possible.
Query 9
ProductID Name Color
----------- ----------------------------------- --------
317 LL Crankarm Black
318 ML Crankarm Black
319 HL Crankarm Black
322 Chainring Black
680 HL Road Frame - Black, 58 Black
708 Sport-100 Helmet, Black Black
711 Sport-100 Helmet, Blue Blue
722 LL Road Frame - Black, 58 Black
………
992 Mountain-500 Black, 48 Black
993 Mountain-500 Black, 52 Black
997 Road-750 Black, 44 Black
998 Road-750 Black, 48 Black
999 Road-750 Black, 52 Black
(119 row(s) affected)

Wildcards (ký hiệu đại diện)


This exercise and the next three following will make use of wildcards in Transact-SQL.
To begin with, we would like a report on products that begins with the letter S.
Write a query that retrieves the columns Name and ListPrice from the
Production.Product table. Your result set should look something like the following.
Order the result set by the Name column.
Name ListPrice
-------------------------------------------------- -----------
Seat Lug 0,00
Query 10
Seat Post 0,00
Seat Stays 0,00
Seat Tube 0,00
Short-Sleeve Classic Jersey, L 53,99
Short-Sleeve Classic Jersey, M 53,99
………
Sport-100 Helmet, Blue 34,99
Sport-100 Helmet, Red 34,99
Steerer 0,00
Stem 0,00
(14 row(s) affected)

Now we would like a report on products that begins with the letters S or A. Write a
query that retrieves the columns Name and ListPrice from the Production.Product
table. Your result set should look something like the following. Order the result set by
the Name column.
Name ListPrice
-------------------------------------------------- ----------
Adjustable Race 0,00
All-Purpose Bike Stand 159,00
Query 11
AWC Logo Cap 8,99
Seat Lug 0,00
Seat Post 0,00
………
Sport-100 Helmet, Red 34,99
Steerer 0,00
Stem 0,00
(17 row(s) affected)

Adjust your query so you retrieve rows that have a Name that begins with the letters
SPO, but is then not followed by the letter K. After this zero or more letters can exists.
Order the result set by the Name column.
Name ListPrice
Query 12 -------------------------------------------------- -----------
Sport-100 Helmet, Black 34,99
Sport-100 Helmet, Blue 34,99
Sport-100 Helmet, Red 34,99
(3 row(s) affected)

Write a query that retrieves unique colors from the table Production.Product. We do
not want to see all the rows, just what colors that exist in the column Color. Your result
set should look something like the following.
Query 13
Color
---------------
NULL
Black
Blue
Grey
Multi
Red
Silver
Silver/Black
White
Yellow
(10 row(s) affected)

Write a query that retrieves the unique combination of columns ProductSubcategoryID


and Color from the Production.Product table. Format and sort so the result set
accordingly to the following. We do not want any rows that are NULL.in any of the two
columns in the result.
ProductSubcategoryID Color
-------------------- ---------------
1 Silver
1 Black
2 Yellow
Query 14 2 Red
2 Black
3 Yellow
…………
31 Red
31 Blue
31 Black
32 Silver
35 Grey
(34 row(s) affected)

Query 15
Something is “wrong” with the WHERE clause in the following query.
We do not want any Red or Black products from any SubCategory than those with the
value of 1 in column ProductSubCategoryID, unless they cost between 1000 and
2000.

SELECT ProductSubCategoryID
, LEFT([Name],35) AS [Name]
, Color, ListPrice
FROM Production.Product
WHERE Color IN ('Red','Black')
OR ListPrice BETWEEN 1000 AND 2000
AND ProductSubCategoryID = 1
ORDER BY ProductID

Write the query in the editor and execute it. Take a look at the result set and then
adjust the query so it delivers the following result set.
Tip:
Operator precedence is often a source of confusion.

ProductSubCategoryID Name Color ListPrice


-------------------- ----------------------------------- --------------- ---------
14 HL Road Frame - Black, 58 Black 1431,50
14 HL Road Frame - Red, 58 Red 1431,50
14 HL Road Frame - Red, 62 Red 1431,50
14 HL Road Frame - Red, 44 Red 1431,50
14 HL Road Frame - Red, 48 Red 1431,50
14 HL Road Frame - Red, 52 Red 1431,50
14 HL Road Frame - Red, 56 Red 1431,50
12 HL Mountain Frame - Silver, 42 Silver 1364,50
12 HL Mountain Frame - Silver, 44 Silver 1364,50
12 HL Mountain Frame - Silver, 48 Silver 1364,50
......
2 Road-350-W Yellow, 44 Yellow 1700,99
2 Road-350-W Yellow, 48 Yellow 1700,99
1 Mountain-500 Black, 40 Black 539,99
1 Mountain-500 Black, 42 Black 539,99
1 Mountain-500 Black, 44 Black 539,99
1 Mountain-500 Black, 48 Black 539,99
1 Mountain-500 Black, 52 Black 539,99
(63 row(s) affected)

Use the Production.Product table to return product name, color and list price for each
product. For the color column, where there is NULL, replace it with the string Unknown.
Name Color ListPrice
------------------------------ --------------- ----------
Adjustable Race Unknown 0,00
Bearing Ball Unknown 0,00
BB Ball Bearing Unknown 0,00
Query 16
Headset Ball Bearings Unknown 0,00
.....
HL Bottom Bracket Unknown 121,49
Road-750 Black, 44 Black 539,99
Road-750 Black, 48 Black 539,99
Road-750 Black, 52 Black 539,99
(504 row(s) affected)

Estimated Time to complete:90 mins.

You might also like