Lab Answer Key - Module 2 - Introduction to T-SQL Querying
Lab Answer Key - Module 2 - Introduction to T-SQL Querying
Destaque Anotação
Lab Answer Key: Module 2: Introduction to T-SQL Querying
4. Click the Login tab, select Windows Authentication in the Authentication list, and click Connect.
5. On the File menu, click Open and click Project / Solution. In the OpenProject window, select project
D:\Labfiles\Lab02\Starter\Project\Project.ssmssln.
Es
se
do
6. In Solution cu
Explorer,
Nã
oé me double-click 51 - Lab Exercise 1.sql. (If Solution Explorer is not visible, select Solution
ronViewnto
Explorerpon ermthe ald menupe or press Ctrl+Alt+L on the keyboard).
itid oo rten
or t ce
7. When the query window ea tonopens, a click the Execute button on the toolbar (or press F5 on the keyboard). You
liza i@g R
r ma ona
c
will notice that the TSQL ódatabase l
pia il.co is selected
do
Ot
in the Available Databases box. The Available Databases
sn m tunder
o
box displays the current database ão context n i. which the T-SQL script will run. This information is also
au
visible on the status bar. tor
iza
da
s!
E
1. Highlight sthe
se following statement under the task 2 description:
do
Nã cu
oé me
pe r on nto p
SELECT rmi a e
tid ldoot rtenc
or t e a country
firstname, lastname, ea oni@city,
liza
r gm Ron
FROM HR.Employees; 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 1/4
09/03/2021 Lab Answer Key: Module 2: Introduction to T-SQL Querying
To highlight it, you can move the pointer over the statement while pressing the left mouse button or use the
arrow keys to move the pointer while pressing the Shift key.
2. Click Execute (or press F5). It is very important to understand that you can highlight a specific part of the
code inside the T-SQL script and execute that part alone. If you click Execute without selecting any part of the
code, the whole T-SQL script will be executed. If you highlight a specific part of the code by mistake, the SQL
Server will attempt to run only that part.
Es
se
Result:NAfter thisdexercise,
oc
um you should know how to open the T-SQL script and execute the whole script or just a
ão en it.
specific statement
ép inside t
erm rona o pe
itid ldoo rten
or t c
ea toni@ e a R
liza on
Exercise 2: Executing Queries r c gmai That ald Filter Data Using Predicates
óp l
ias .com o Ot
nã ton
oa i.
uto
Task 1: Execute the T-SQL Script riz
ad
as
!
The message tells you that SQL Server could not find the object HR.Employees. This is because the current
database context is set to the master database (look at the Available Databases box where the current database is
Esthe IT department supplied T-SQL scripts to be run against the TSQL database. So, you need to
displayed), but se
change the databasedo context from master to TSQL. You will learn how to change the database context in the next
Nã cu
o me
task. ép r o nto
erm n ald per
itid o te
o r otto nce
ea ni@ aR
liza o
r c gmai nald
óp l.c
Task 2: Apply Needed Changesiasand Execute om o Ot the T-SQL Script
t
https://www.skillpipe.com/#/reader/urn:uuid:91ddbb8d-bb35-443c-9b77-1e5f0f71fa17@2020-12-11T07:01:36Z/content 2/4
09/03/2021 Lab Answer Key: Module 2: Introduction to T-SQL Querying
1. In the Available Databases box, select TSQL to change the database context.
2. Click Execute.
3. Notice that the result from the SELECT statement returns fewer rows than the one in exercise 1. That is
because it has a predicate in the WHERE clause to filter out all rows that do not have the value USA in the
column country. Only rows for which the logical expression evaluates to TRUE are returned by the WHERE
phase to the subsequent logical query processing phase.
Es
ed s
oc
Nã um
oé e
Task 3: Uncomment ronthenUSEto
pe
rm a pe Statement
itid ldoo rten
or t c
ea toni@ e a R
liza o
r c gmai nald
1. In the script 61 - Lab Exercise óp 2.sql,
l find the line:
ias .com o Ot
nã ton
oa i.
uto
riz
--USE TSQL; ad
as
!
2. Delete the first two characters, so that the line looks like this:
Es
USE TSQL;
se
do
Nã cu
o me
ép n
e r o na to pe
By deletingrm
these
itid l
two
d o characters,
rte you have removed the comment mark. Now the line will not be ignored by
o r otto nce
SQL Server. ea n aR
liza i@g
rc ma ona
óp il ldo
3. On the File menu, click Save ias 61.c-oLab Ot
m Exercise 2.sql.
nã ton
oa i.
uo
4. On the File menu, click Close. This twill riz close the T-SQL script.
ad
as
!
5. In Solution Explorer, double-click 61 - Lab Exercise 2.sql.
6. Click Execute.
7. Observe the results. Why did the script execute with no errors? The script now includes the uncommented
USE TSQL;Es statement. When you execute the whole T-SQL script, the USE statement applies the database
se
d
context
Nã to the oTSQL
cu
me database. The next statement in the T-SQL script then executes against the TSQL
oé nto
database. pe ro
rm nald pert
itid o e
o r otto nce
ea ni@ aR
liza o
r c gmai nald
óp should
l.c have
Result: After this exercise, you ias om o Oat basic understanding of database context and how to change it.
nã ton
oa i.
uto
riz
ad
Exercise 3: Executing Queries That Sort as Data Using ORDER BY
!
Es
s
1. In Solution eExplorer,
do double-click 71 - Lab Exercise 3.sql.
Nã cu
oé m en
2. pe
Click Execute. ro to
rm nald pert
itid o e
o r otto nce
3. Notice that the result ea windowni@ isaempty. All the statements inside the T-SQL script are commented out, so SQL
liza
r gm Ron
ald the T-SQL script.
c
Server ignores all the statementsóp ail.c inside
ias om o Ot
t
https://www.skillpipe.com/#/reader/urn:uuid:91ddbb8d-bb35-443c-9b77-1e5f0f71fa17@2020-12-11T07:01:36Z/content 3/4
09/03/2021 Lab Answer Key: Module 2: Introduction to T-SQL Querying
--USE TSQL;
Es
2. Delete thestwo
e d characters before the USE statement. The line should now look like this:
oc
Nã um
oé e
pe ron nto
rm a pe
USE TSQL; itid ldoo rte
or tto nce
ea n aR
liza i@g
rc ma ona
óp il ld
ias .com o Ot
3. Locate the block comment start nãelement /*toafter
ni. the task 1 description and delete it.
oa
uto
ri
4. Locate the block comment end elementza*/daand delete it. Any text residing within a block starting with /* and
s!
ending with */ is treated as a block comment and is ignored by SQL Server.
Es
s ed
USE TSQL; 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
o Click Execute. The database nã context tois
ni.now changed to the TSQL database.
oa
uto
riz
ad
as
!
6. Highlight the statement:
SELECT
firstname, lastname, city, country
ss E
FROM HR.Employees
e do
cu = 'USA'
Nã country
WHERE
oé m en
ro
pe lastname; to
ORDER BY rm nald pert
itid oo en
o tt c
rea ea on
liza Roi@
g
rc
ó
m ail nald
7. Click Execute. pia .c oO
s n om tto
ão ni.
au
8. Observe the result and notice that the tor rows are sorted by the lastname column in ascending order.
iza
da
s!
Result: After this exercise, you should have an understanding of how comments can be specified inside T-SQL
scripts.
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 4/4