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

Lab Answer Key - Module 2 - Introduction to T-SQL Querying

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)
7 views

Lab Answer Key - Module 2 - Introduction to T-SQL Querying

Uploaded by

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

09/03/2021 Lab Answer Key: Module 2: Introduction to T-SQL Querying

Destaque Anotação
Lab Answer Key: Module 2: Introduction to T-SQL Querying

Lab: Introduction to T-SQL Querying

Exercise 1: Executing Basic 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\Lab02\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.

Es the T-SQL Script


Task 2: Execute se
do
Nã cu
oé me
pe ron nto
a
rm machine, pe
1. On the virtual itid ldoo on rtethe Taskbar, click SQL Server Management Studio.
or tto nce
ea n aR
liza i@g
2. In the Connect to Server ma onina the Server name text box, type MIA-SQL.
r c window,
óp il ld
ias .com 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. 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,

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!

Task 3: Execute a Part of the T-SQL Script

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
!

1. Close SQL Server Management Studio.

2. If prompted to save the files, click No.


Es
se
3. On the virtual domachine, on the Taskbar, click SQL Server 2014 Management Studio.
Nã cu
oé me
r o nto
4. p
In the Connect
erm to na Server pe window, type MIA-SQL in the Server name text box.
itid l d o rte
o r otto nce
Click the Optionsebutton. ali n i Under a RConnection Properties, select <Browse server> in the Connect to
5. za @ o
r c gmai nald
database list. Choose Yes óp when l.cprompted for the connection to the database. Under System Databases,
ias om o Ot
select the master database. ão n t o ni.
au
tor
iza
6. Click OK and click Connect. da
s!
7. On the File menu, click Open and click Project / Solution. In the OpenProject window, select project
D:\Labfiles\Lab02\Starter\Project\Project.ssmssln.

8. In Solution Explorer, double-click 61 - Lab Exercise 2.sql.


Es
se
9. When the query do window opens, click Execute.
Nã cu
oé me
r o nto
p
10. Notice thateryou
mi get na the
l d o
perror
ert message:
e
tid
o r otto nce
ea ni@ aR
liza o
r c gmai nald
óp l.c
ias om o Ot
Invalid object name 'HR.Employees'.: nã ton
oa i.
uto
riz
ad
as
!
Why do you think this happened? This error is very common when you are beginning to learn T-SQL.

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
!

Task 1: Execute the T-SQL Script

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

Task 2: Uncomment the Needed T-SQL Statements and Execute Them

1. Locate the line:

--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.

5. Highlight the statement:

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

You might also like