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

Lab Answer Key - Module 3 - Writing SELECT Queries

Uploaded by

ronaldoottoni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab Answer Key - Module 3 - Writing SELECT Queries

Uploaded by

ronaldoottoni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

09/03/2021 Lab Answer Key: Module 3: Writing SELECT Queries

Destaque Anotação
Lab Answer Key: Module 3: Writing SELECT Queries

Lab: Writing Basic SELECT Statements

Exercise 1: Writing Simple SELECT Statements


Es
s
ed
oc
Nã um

Task 1: Prepare the Lab e Environment
pe ron nto
rm a pe
itid ldoo rten
or tto ce
ea n aR
liza i@g on 20461C-MIA-SQL virtual machines are both running, and then log on to
1. Ensure that the 20461C-MIA-DC rc ma and ald
óp il
ias .com o Ot
nã ton
20461C-MIA-SQL as ADVENTUREWORKS\Student oa i. with the password Pa$$w0rd.
uto
riz
ad
2. In the D:\Labfiles\Lab03\Starter folder, right-click as Setup.cmd, and then click Run as administrator.
!
3. In the User Account Control dialog box, click Yes, and then wait for the script to finish.

Task 2: View Eall


ss the Tables in the TSQL Database in Object Explorer
ed
oc
Nã um
oé e
pe ron nto
r a
mi machine, pe
1. On the virtual tid ldoot on rtethe Taskbar, click SQL Server 2014 Management Studio.
nc
or to ea
ea n i @
liza g Ro
2. In the Connect to Server rc window,
m nathe Server name text box, type MIA-SQL.
in
óp ail.c ld
ias om o Ot
nã Connection ton
3. Click the Options button. Under oa i. Properties, select <Browse server> in the Connect to
uto
database list. Choose Yes when prompted riz
ad for the connection to the database. Under User Databases,
select the TSQL database. Click OK. as
!

4. Click the Login tab, select Windows Authentication in the Authentication list, and click Connect.

5. In Object Explorer, expand the server MIA-SQL, expand Databases, expand the database TSQL, and
expand Tables.
Es
se
do
6. Under
Nã Tables, cnotice
um that there are four table objects in the Sales schema:
oé en
pe ro to
rm nald pert
itid o e
o r otto nce
ea ni@ aR
liza o
r c gmai nald
o Sales.Customers óp l.c
ias om o Ot
n ton
o Sales.OrderDetails ão i.
au
tor
o Sales.Orders iza
da
o Sales.Shippers s!

Task 3: Write a Simple SELECT Statement


Es
s ed
oc
Nã um
1. On the o éFile menu, eclick
nto Open and click Project/Solution.
pe r o
rm nald pert
itid o en
2. In the Open Project o r owindow,
t c open the project D:\Labfiles\Lab03\Starter\Project\Project.ssmssln.
ea toni@ e a R
liza g on
rc m ad
3. In Solution Explorer, double-clickóp ail.cthe lquery
ias om o Ot 51 - Lab Exercise 1.sql. (If Solution Explorer is not visible, select
t
Solution Explorer on the View menu or press Ctrl+Alt+L on the keyboard.)

4. When the query window opens, highlight the statement USE TSQL; and click Execute on the toolbar (or
https://www.skillpipe.com/#/reader/urn:uuid:91ddbb8d-bb35-443c-9b77-1e5f0f71fa17@2020-12-11T07:01:36Z/content 1/7
09/03/2021 Lab Answer Key: Module 3: Writing SELECT Queries

press F5 on the keyboard).

5. In the query pane, type the following query after the task 2 description:

SELECT*
FROM Sales.Customers;

Es
6. se query you typed in step 5 and click Execute.
Highlight the do
Nã cu
oé me
7. In the query nto the following code after the first query:
ron type
pe pane,
rm ald per
itid o te
o r otto nce
ea ni@ aR
liza o
SELECT* r c gmai nald
óp l
ias .com o Ot
FROM nã ton
oa i.
uto
riz
ad
as
!
8. In Object Explorer, select the Sales.Customers table under MIA-SQL, TSQL, Tables. Using the mouse, drag
the selected table into the query pane, after the FROM clause. Then add a semicolon to the end of the
SELECT statement.

Your finished query should look like this:


Es
se
do
Nã cu

SELECT*
me
pe ron nto
rm a pe
itid ldoo rten
FROM [Sales].[Customers];
or tto ce
ea n aR
liza i@g
rc ma ona
óp il ld
ias .com o Ot
9. Highlight the written query andnãclick Execute. ton
oa i.
uto
riz
ad
as
!
Task 4: Write a SELECT Statement that Includes Specific Columns

1. In Object Explorer, expand the Sales.Customers table under MIA-SQL, TSQL, and Tables.
Es
se
2. Expand Columns do
Nã cu and observe all the columns in the Sales.Customers table.
oé me
r o nto
3. p
In the queryermpane, na type pthe
l d ert following query after the task 3 description:
itid o e
o r otto nce
ea ni@ a
liza
r gm Ron
c a il ald
SELECT óp
ias .com o Ot
contactname, address, postalcode, nã tocity,
ni. country
oa
uto
FROM Sales.Customers; riz
ad
as
!

4. Highlight the written query and click Execute.

5. Observe the result. How many rows are affected by the last query? There are multiple ways to answer this
questionEusing SQL Server Management Studio. One way is to select the previous query and click Execute.
ss
ed
The total numberoc of rows affected by the executed query is written in the Results pane under the Messages
Nã um
tab: o é en
pe r o to
rm nald pert
itid o e
o r otto nce
ea ni@ a
(91 row(s) affected) izar
l gm Ron
có a al
pia il.co do O
s m tt

https://www.skillpipe.com/#/reader/urn:uuid:91ddbb8d-bb35-443c-9b77-1e5f0f71fa17@2020-12-11T07:01:36Z/content 2/7
09/03/2021 Lab Answer Key: Module 3: Writing SELECT Queries

Another way is to look at the status bar displayed below the Results pane. On the left side of the status bar, there
is a message stating: “Query executed successfully.” On the right side, the total number of rows affected by the
current query is displayed (91 rows).

Result: After this exercise, you should know how to create simple SELECT statements to analyze existing
tables.

Es
s e
Exercise do
2: Eliminating Duplicates Using DISTINCT
N cu
ão me
ép r nt
erm na o pe
o
itid l d oo rten
t
o r Statement c
Task 1: Write a SELECT ea toni@ e a that Ro
Includes a Specific Column
liza g
rc m n ald
óp ail.c
ias om o Ot
nã ton
1. In Solution Explorer, double-clicko the i
au query 61. - Lab Exercise 2.sql.
tor
iza
da
2. When the query window opens, highlight the s! statement USE TSQL; and click Execute.

3. In the query pane, type the following query after the task 1 description:

SELECTcountry
ss E
FROM Sales.Customers;
e do
Nã cu
o me
ép r nt
erm na o pe
o
l d oo rteand
4. Highlight the iwritten
tid
o r query t nc click Execute.
ea toni@ e a R
liza gm on
5. Observe that you havermultiple có
pia
ailrows
.
ald
co with o O the same values. This occurs because the Sales.Customers table
s m
has multiple rows with the same nã value for ttheton country column.
i.
oa
uto
riz
ad
as
!
Task 2: Write a SELECT Statement that Uses the DISTINCT Clause

1. Highlight the previous query. On the toolbar, click Edit and then Copy.
Es
se
2. do
In the query window, click the line after the task 2 description. On the toolbar, click Edit and then Paste. You
Nã cu
o me
have now é p copied thent previous query to the same query window after the task 2 description.
erm rona o pe
itid ldoo rten
t
o rby typing ce
3. Modify the query ea toni@DISTINCT aR after the SELECT clause. Your query should look like this:
liza g on
rc m ald
óp ail.c
ias om o Ot
nã ton
SELECT DISTINCT oa i.
uto
country riz
ad
as
FROM Sales.Customers; !

4. Highlight the written query and click Execute.

5. ObserveEthess result and answer these questions:


ed
oc
Nã um
oé en
How many rows r
pe did theo to in task 1 return?
rm naldquery pe
rte
itid o
o r otto nce
ea ni@ a
liza
r gm Ron
To answer this question, you can a
có highlight althe query written under the task 1 description, click Execute, and read
pia il.co do O
the Results pane. (If you forgot how m
s to access tthis
t pane, look at task 4 in exercise 1.) The number of rows affected
by the query is 91.

https://www.skillpipe.com/#/reader/urn:uuid:91ddbb8d-bb35-443c-9b77-1e5f0f71fa17@2020-12-11T07:01:36Z/content 3/7
09/03/2021 Lab Answer Key: Module 3: Writing SELECT Queries

How many rows did the query in Task 2 return?

To answer this question, you can highlight the query written under the task 2 description, click Execute, and read
the Results pane. The number of rows affected by the query is 21. This means that there are 21 distinct values for
the country column in the Sales.Customers table.

Under which circumstances do the following queries against the Sales.Customers table return the same result?
Es
se
do
Nã cu
oé me
r o nto
p
mi naldoFROM
SELECT city,erregion pe Sales.Customers;
r
tid
o ott tenc
SELECT DISTINCT city, on
rea region e aFROM Sales.Customers;
liza i@g Ro
rc m na
óp ail.c ld
ias om o Ot
nã ton
Both queries would return the same number oa i.
uto of rows if all combinations of values in the city and region columns in
the Sales.Customers table are unique. If they riz are not unique, the first query would return more rows than the
ad
as
second one with the DISTINCT clause. !

Is the DISTINCT clause applied to all columns specified in the query or just the first column?

E
The DISTINCTsclause
se is always applied to all columns specified in the SELECT list. It is very important to
do
remember c
Nãthat the DISTINCTum clause does not apply to just the first column in the list.
oé e
pe ron nto
rm a pe
itid ldoo rten
or tto c
ea ni@ e a
Result: After this exercise, liza you gm Ronhave an understanding of how to return only the different (distinct) rows in
should
the result set of a query. r cóp ail.c aldo
ias om Ot
nã ton
oa i.
uto
riz
Exercise 3: Using Table and ColumnadAliases as
!

Task 1: Write a SELECT Statement that Uses a Table Alias

Es
1. In Solution seExplorer, double-click the query 71 - Lab Exercise 3.sql.
do
Nã cu
oé me
2. When thepequery nto opens, highlight the statement USE TSQL; and click Execute.
ronwindow
rm ald per
itid oo te
o tto nce
3. In the query pane,retype ali n
the
i following
a query after the task 1 description:
za @gm Ron
rc ald
óp ail.c
ias om o Ot
nã ton
SELECTc.contactname, c.contacttitle oa i.
uto
FROM Sales.Customers AS c; riza
da
s!

Tip: To use the IntelliSense feature when entering column names in a SELECT statement, you can use
keyboard shortcuts. To enable IntelliSense, press Ctrl+Q+I. To list all the alias members, position your pointer
after the alias and dot (for example, after “c.”) and press Ctrl+J.
Es
se
4. Highlight the dwritten
oc query and click Execute.
Nã um
oé en
pe ro to
rm nald pert
itid o e
o r otto nce
ea ni@ a
Task 2: Write A SELECTlizStatement
ar gm R on Uses Column Aliases
That
có a al
pia il.co do O
s m tt

1. In the query pane, type the following query after the task 2 description:

https://www.skillpipe.com/#/reader/urn:uuid:91ddbb8d-bb35-443c-9b77-1e5f0f71fa17@2020-12-11T07:01:36Z/content 4/7
09/03/2021 Lab Answer Key: Module 3: Writing SELECT Queries

SELECTc.contactname AS Name, c.contacttitle AS Title, c.companyname AS [Company Name]


FROM Sales.Customers AS c;

Observe that the column alias [Company Name] is enclosed in square brackets. Column names and aliases
that have embedded spaces or reserved keywords must be delimited. This example uses square brackets as
the delimiter, but you can also use the ANSI SQL standard delimiter of double quotes, as in “Company
Name”.
Es
s
ed
2. Highlight oc
the written
Nã um query and click Execute.
oé e
pe ron nto
rm a pe
itid ldoo rten
or tto ce
ea n aR
liza i@g on
Task 3: Write a SELECT Statement rc ma that aUses a Table Alias and a Column Alias
óp il ld
ias .com o Ot
nã ton
oa i.
uto
riz
1. In the query pane, type the following query ad after the task 3 description:
as
!

SELECT p.productname AS [Product Name]


FROM Production.Products AS p;

Es
se
2. Highlight the dwritten
oc
Nã um query and click Execute.
oé en
pe ro to
rm nald pert
itid oo en
or t c
ea toni@ e a R
Task 4: Analyze and Correct l i zar the gmQuery o
có ail nald
pia .c oO
s n om tto
ão ni.
au
t r
1. Highlight the written query under theotask iza 4 description and click Execute.
da
s!
2. Observe the result. Note that only one column is retrieved. The problem is that the developer forgot to add a
comma after the first column name, so SQL Server treated the second word after the first column name as an
alias. For this reason, it is a best practice to always use AS when specifying aliases. That way, it is easier to
spot such errors.
Es
s
3. Correct the equery
do by adding a comma after the first column name. The corrected query should look like this:
Nã cu
oé me
pe ron nto
rm a pe
itid ldoo rten
or tto ce
ea n aR
SELECT city, country liza i@g
rc ma ona
óp il ld
FROM Sales.Customers; ias .com o Ot
nã ton
oa i.
uto
riz
ad
as
Result: After this exercise, you will know how to! use aliases for table and column names.

Exercise 4: Using a Simple CASE Expression


Es
se
do
Task 1: Write
Nã a SELECTcu Statement
oé me
r o n to
pe
rm nald pert
itid o e
o r otto nce
1. In Solution Explorer, ni@
ea double-click a Rthe query 81 - Lab Exercise 4.sql.
liza o
r c gmai nald
óp l.c
2. When the query window opens, om o Othe
ias highlight tt statement USE TSQL; and click Execute.

3. In the query pane, type the following query after the task 1 description:

https://www.skillpipe.com/#/reader/urn:uuid:91ddbb8d-bb35-443c-9b77-1e5f0f71fa17@2020-12-11T07:01:36Z/content 5/7
09/03/2021 Lab Answer Key: Module 3: Writing SELECT Queries

SELECT p.categoryid, p.productname


FROM Production.Products AS p;

4. Highlight the written query and click Execute.

Task 2: WriteEassSELECT Statement that Uses a CASE Expression


ed
oc
Nã um
oé e
pe ron nto
a pe
1. In the queryrmpane,
itid ldtype
o the rte following after the task 2 description:
o r otto nce
ea n aR
liza i@g
rc ma ona
ó il ld
ias .com o Ot
SELECT p.categoryid, pp.productname,
nã ton
CASE oa i.
uto
riz
WHEN p.categoryid = 1 THEN 'Beverages' ad
as
!
WHEN p.categoryid = 2 THEN 'Condiments'
WHEN p.categoryid = 3 THEN 'Confections'
WHEN p.categoryid = 4 THEN 'Dairy Products'
WHEN p.categoryid = 5 THEN 'Grains/Cereals'
Es
WHEN p.categoryid = 6 THEN 'Meat/Poultry'
se
do
WHEN p.categoryid = 7 THEN 'Produce'
Nã c um
WHENo p.categoryid
ép ro
en = 8 THEN 'Seafood'
to
ermna
itid
ELSE 'Other' ldo perte
o r otto nce
ea n aR
END AS categoryname liza i@g
rc ma ona
FROM Production.Products óp il.c p;ldo
ias AS om Ot
nã ton
oa i.
uto
riz
ad
This query uses a CASE expression to add as a new column. Note that, when you have a dynamic list of
!
possible values, you usually store them in a separate table. However, for this example, a static list of values is
being supplied.

2. Highlight the written query and click Execute.


Es
s
ed
oc
Nã um
oé e
Task 3: Write paeSELECTron nStatement
to that Uses a CASE Expression to differentiate Campaign-Focused
rm ald per
Products itid oo ten
or t c
ea toni@ e a R
liza g on
rc m ald
óp ail.c oO
1. Highlight the previous query. om toolbar,
iasOn the tto click Edit and then Copy.
nã ni.
oa
uto
2. In the query window, click the line after rizthe task 3 description. On the toolbar, click Edit and then Paste. You
ad
have now copied the previous query to theassame ! query window after the task 3 description.

3. Add a new column using an additional CASE expression. Your query should look like this:

SELECTp.categoryid, p.productname,
Es
CASE s ed
oc
WHEN um
Nã p.categoryid = 1 THEN 'Beverages'
o é e nt
pe o r o
rm nald p=er2
WHEN p.categoryid
tenTHEN 'Condiments'
itid oo
o
WHEN p.categoryid re t t ce
o=n 3 THEN
al i @ Ra 'Confections'
iz gm on
WHEN p.categoryidar=có4 THEN al
a 'Dairy Products'
pia il.co do O
s
WHEN p.categoryid = 5 THEN m tt
'Grains/Cereals'
WHEN p.categoryid = 6 THEN 'Meat/Poultry'

https://www.skillpipe.com/#/reader/urn:uuid:91ddbb8d-bb35-443c-9b77-1e5f0f71fa17@2020-12-11T07:01:36Z/content 6/7
09/03/2021 Lab Answer Key: Module 3: Writing SELECT Queries

WHEN p.categoryid = 7 THEN 'Produce'


WHEN p.categoryid = 8 THEN 'Seafood'
ELSE 'Other'
END AS categoryname,
CASE
WHEN p.categoryid IN (1, 7, 8) THEN 'Campaign Products'
ELSE 'Non-Campaign Products'
END ASE iscampaign
ss
do e
FROM Production.Products AS p;
Nã cu
o me
ép r nt
erm na o pe
o
itid l d oo rten
o r query t ce
4. Highlight the written ea toni@and aclick
Ro
Execute.
liza g
rc m ail n ald
ó .coCASE o Oexpression uses the simple form, whereas the second uses the
5. In the result, observe that pthe ias first m tto
nã ni.
searched form. oa
uto
riz
ad
as
!
Result: After this exercise, you should know how to use CASE expressions to write simple conditional logic.

Es
s ed
oc
Nã um
o ép en
r t
erm na o pe
o
itid ldoo rten
or t c
ea toni@ e a R
liza o
r c gmai nald
óp l.c
ias om o Ot
nã ton
oa i.
uto
riz
ad
as
!

Es
s ed
oc
Nã um
o ép en
r t
erm na o pe
o
itid l d o rte
o r otto nce
ea n aR
liza i@g
rc ma ona
óp il ld
ias .com o Ot
nã ton
oa i.
uto
riz
ad
as
!

Es
s ed
oc
Nã um
o ép en
r t
erm ona o pe
itid l d o rte
o r otto nce
ea ni@ aR
liza o
r c gmai nald
óp l.c
ias om o Ot
t

https://www.skillpipe.com/#/reader/urn:uuid:91ddbb8d-bb35-443c-9b77-1e5f0f71fa17@2020-12-11T07:01:36Z/content 7/7

You might also like