Lab Answer Key - Module 3 - Writing SELECT Queries
Lab Answer Key - Module 3 - Writing SELECT Queries
Destaque Anotação
Lab Answer Key: Module 3: Writing SELECT Queries
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!
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
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.
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
!
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; !
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
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
!
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
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
!
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.
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
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
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