Lab Answer Key - Module 5 - Sorting and Filtering Data
Lab Answer Key - Module 5 - Sorting and Filtering Data
Destaque Anotação
Lab Answer Key: Module 5: Sorting and Filtering Data
5. When the query window opens, highlight the statement USE TSQL; and click Execute on the toolbar (or
press F5 on the keyboard).
Es
s
6. In the querye pane,
do type the following query after the task 1 description:
Nã cu
oé me
ro nto
pe
rm nald pert
itid o e
SELECT o r otto nce
ea ni@ aR
liza o
r c gmai nald
custid, companyname, contactname, address, city, country, phone
FROM Sales.Customers pia ó l .c o
s n om Ot
ton
WHERE ã o i.
au
tor
country = N'Brazil'; iza
da
s!
7. Notice the use of the N prefix for the character literal. This prefix is used because the country column is a
Unicode data type. When expressing a Unicode character literal, you need to specify the character N (for
National) as a prefix. You will learn more about data types in the next module.
Es
se
8. Highlight the dwritten
oc
Nã um query and click Execute.
oé en
pe r o to
rm nald pert
itid o e
o r otto nce
ea ni@ aR
Task 3: Write a SELECTliStatement
za
r c gmai that
on Uses an IN Predicate in the WHERE Clause
ald
óp l.c
ias om o Ot
t
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 1/7
09/03/2021 Lab Answer Key: Module 5: Sorting and Filtering Data
SELECT
custid, companyname, contactname, address, city, country, phone
FROM Sales.Customers
WHERE
country IN (N'Brazil', N'UK', N'USA');
Es
2. se written query and click Execute.
Highlight the do
Nã cu
oé me
pe ron nto
rm a pe
itid ldoo rten
or tto ce
Task 4: Write a SELECT n
ea Statement a that
liza i@g R Uses a LIKE Predicate in the WHERE Clause
rc ma ona
óp il ld
ias .com o Ot
nã ton
oa i.
1. In the query pane, type the following utoquery after the task 3 description:
riz
ad
as
!
SELECT
custid, companyname, contactname, address, city, country, phone
FROM Sales.Customers
WHERE
Es
se
contactnamed LIKE N'A%';
oc
Nã um
o ép en
r t
erm na o pe
o
itid ldoo rten
2. Remember thatothe tto
reapercent ce (%) wildcard represents a string of any size (including an empty string),
ni@ sign
aR
l i z
whereas the underscore ma ona represents a single character.
ar (_)gwildcard
có l
pia il.co do O
sn m tto
3. Highlight the written query and ãclick o a Execute. ni.
uto
riz
ad
as
!
1. Highlight the T-SQL statement provided under the task 4 description and click Execute.
Es
se
do
2. Highlight the cu
provided
Nã
oé me T-SQL statement. On the toolbar, click Edit and then Copy.
pe ron nto
a pe
3. In the queryrmwindow,
itid ldooclickrtethe line after the task 4 description. On the toolbar, click Edit and then Paste. You
or tto nce
have now copied the n
ea previous query
a R to the same query window after the task 4 description.
liza i@g
rc ma ona
ó il c ldo
4. Modify the query so that it plooksias .like om this: Ot
nã ton
oa i.
uto
riz
ad
SELECT as
!
c.custid, c.companyname, o.orderid
FROM Sales.Customers AS c
LEFT OUTER JOIN Sales.Orders AS o ON c.custid = o.custid
WHERE
E
c.city s=seN'Paris';
do
Nã cu
o me
ép r o nto
erm n ald per
5. Highlight the imodified
tid o ten and click Execute.
o r otquery c
ea toni@ e a R
liza g ma ona
6. Observe the result. Is itr the có same
pia il.coas that
ldo of the first SQL statement? The result is not the same. When you
s m O
specify the predicate in the ON clause, thett left outer join preserves all the rows from the left table
(Sales.Customers) and adds only the matching rows from the right table (Sales.Orders), based on the
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 5: Sorting and Filtering Data
predicate in the ON clause. This means that all the customers will show up in the output, but only the ones
from Paris will have matching orders. When you specify the predicate in the WHERE clause, the query will
filter only the Paris customers. So, be aware that, when you use an outer join, the result of a query in which
the predicate is specified in the ON clause can differ from the result of a query in which the predicate is
specified in the WHERE clause. (When using an inner join, the results are always the same.) This is because
the ON predicate is matching—it defines which rows from the non-preserved side to match to those from the
preserved side. The WHERE predicate is a filtering predicate—if a row from either side doesn’t satisfy the
WHERE predicate, the row is filtered out.
Es
se
do
Nã cu
oé me
pe ron nto
a pe
Task 6: Write a rSELECT
mi
tid ldoStatement rte to Retrieve those Customers Without Orders
o r otto nce
ea n aR
liza i@g
rc ma ona
óp il ld
i s .com o Ot
1. In the query pane, type the afollowing nã querytoafter
ni. the task 5 description:
oa
uto
riz
ad
as
SELECT !
c.custid, c.companyname
FROM Sales.Customers AS c
LEFT OUTER JOIN Sales.Orders AS o ON c.custid = o.custid
WHERE o.custid IS NULL;
Es
sed
oc
Nã um
oé en
2. p
It is important r o to
erm tonnote
ald that, pe when you are looking for a NULL, you should use the IS NULL, not the equality
itid oo rten
operator. The equality
or c will always return UNKNOWN when you compare something to a NULL. It will
ttoperator
ea oni@ e a R
even return UNKNOWN l i zar when gm youoncompare two NULLs.
có a al
pia il.co do O
sn m tto
3. The choice of which attribute toãfilter o a from the ni. non-preserved side of the join is also important. You should
uto
choose an attribute that can only haveriza NULL when the row is an outer row (for example, a NULL originating
ad
from the base table). For this purpose, three as cases are safe to consider:
!
o A primary key column. A primary key column cannot be NULL. Therefore, a NULL in such a column can
Esmean that the row is an outer row.
only
se
do
o N A join column. cu If a row has a NULL in the join column, it is filtered out by the second phase of the join.
ão me
Soé pa NULL r nt
in such a column can only mean that it is an outer row.
erm na o pe
o
l d
itiddefined o rte
o A column o r ottoas NOT nc NULL. A NULL in a column that is defined as NOT NULL can only mean that
ea n ea
the row is an outer i
liza row.@ Ro
rc g m na
óp ail.c ld
ias om o Ot
nã ton
oa i.
4. Highlight the written query and clickutExecute. ori
za
da
s!
Result: After this exercise, you should be able to filter rows of data from one or more tables by using WHERE
predicates with logical operators.
Es
se
Exercise 2: Writing Queries That Sort Data Using an ORDER BY Clause
do
Nã cu
o me
ép r o nto
erm n ald per
itid
Task 1: Write a SELECT oo ten
o r Statement
t c that Uses an ORDER BY Clause
ea toni@ e a R
liza o
r c gmai nald
óp l.c
ias om o Ot
t
1. In Solution Explorer, double-click the query 61 - Lab Exercise 2.sql.
2. When the query window opens, highlight the statement USE TSQL; and click Execute.
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 5: Sorting and Filtering Data
3. In the query pane, type the following query after the task 1 description:
SELECT
c.custid, c.contactname, o.orderid, o.orderdate
FROM Sales.Customers AS c
INNER JOIN Sales.Orders AS o ON c.custid = o.custid
WHERE E
ss
e d >= '20080401'
o.orderdate o cu
N
ão BY me
ORDER é pe ron nto
rm DESC, a pe
o.orderdate itid ldoo c.custidrte ASC;
or tto nce
ea n aR
liza i@g
rc ma ona
óp il.c ldo
Notice the date filter. It usesiasa nliteral om (constant)
Ot
ton of a date. SQL Server recognizes “20080401” as a character
ão i.
string literal and not as a date and time a uto literal, but because the expression involves two operands of different
riz
types, one needs to be implicitly converted ad to the other’s type. In this example, the character string literal is
as
!
converted to the column’s data type (DATETIME) because character strings are considered lower in terms of
data type precedence, with respect to date and time data types.
Also notice that the character string literal follows the format “yyyymmdd”. Using this format is a best practice
because SQL Server knows how to convert it to the correct date, regardless of the language settings.
Es
se
4. 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
l i z on Execute the T-SQL Statement
Task 2: Apply the NeededaChanges r c gmai and ald
óp l.c
ias om o Ot
nã ton
oa i.
uto
1. Highlight the written query under the task riz 2 description and click Execute.
ad
as
!
2. Observe the error message:
3. This error occurred because the WHERE clause is evaluated before the SELECT clause and, at that time, the
Es not have an alias. To fix this problem, you must use the source column name with the appropriate
column did se
do
table
Nã alias. Modifycu the T-SQL statement to look like this:
oé me
pe ron nto
rm a pe
itid ldoo rten
SELECT
o rea oni@ ce a
t t
liza gm Ron
r c e.firstname,ald
e.empid, e.lastname, óp ail.c e.title, e.mgrid,
ias om o Ot
m.lastname AS mgrlastname, nã m.firstnamet on AS mgrfirstname
oa i.
FROM HR.Employees AS e u tor
iza
da
INNER JOIN HR.Employees AS m ON e.mgrid s! = m.empid
WHERE
m.lastname = N'Buck';
4. HighlightEsthe
se written query and click Execute.
do
Nã cu
oé me
pe r on nto p
rm a e
itid ldoo rten
Task 3: Order the Result or tto the cfirstname
by ea Column
ea n
liza i@g Ro
rc m na
óp ail.c ld
ias om o Ot
t
1. Highlight the previous query. On the toolbar, click Edit and then Copy.
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 5: Sorting and Filtering Data
2. In the query window, click the line after the task 3 description. On the toolbar, click Edit and then Paste. You
have now copied the previous query to the same query window after the task 3 description.
3. Modify the T-SQL statement to include an ORDER BY clause that uses the source column name of
m.firstname. Your query should look like this:
SELECT
e.empid, e.lastname, e.firstname, e.title, e.mgrid,
Es
s
e d AS mgrlastname, m.firstname AS mgrfirstname
m.lastname o
N cu
me
ão HR.Employees
FROM é n AS e
ron to
pe p
INNER JOIN ald
rm HR.Employees
ert
i o
tid e AS m ON e.mgrid = m.empid
ott nc
or
ORDER BY ea oni@ e a R
liza o
m.firstname; r c gmai nald
óp l
ias .com o Ot
nã ton
oa i.
uto
riz
4. Highlight the written query and click Execute. ad
as
!
5. Modify the ORDER BY clause so that it uses the alias for the same column (mgrfirstname). Your query should
look like this:
SELECTEs
se
e.empid, oc d
e.lastname, e.firstname, e.title, e.mgrid,
N ão u me
ép nto
erm rAS
m.lastname on mgrlastname,
ald per
m.firstname AS mgrfirstname
itid
FROM HR.Employees oo ASteen
o t re c ton ea
ali i
za @gm ASRomn ON e.mgrid = m.empid
INNER JOIN HR.Employees
rc ail a
ORDER BY óp
ias .co ldo O
m tto
nã ni.
mgrfirstname; oa
uto
riz
ad
as
!
6. Highlight the written query and click Execute.
7. Observe the result. Why were you able to use a source column or alias column name? You can use either
one because the ORDER BY clause is evaluated after the SELECT clause and the alias for the column name
is known.Es
se
do
Nã cu
oé me
pe ron nto
rm al pe
rte should know how to use an ORDER BY clause.
Result: After this ido doott you
itexercise, n
rea oni@ ce a
liza Ro
r c gmai nald
óp l.c
ias om o Ot
Exercise 3: Writing Queries That
nã FiltertoData ni. Using the TOP Option
oa
uto
riz
ad
as
Task 1: Writing Queries That Filter Data Using! the TOP Clause
2. When the Esquery window opens, highlight the statement USE TSQL; and click Execute.
se
do
N cu
3. In theãoquery pane, metype the following query after the task 1 description:
ép r o nto
erm n ald per
itid o te
o r otto nce
ea ni@ aR
SELECT TOP (20) liza o
r c gmai nald
ó l .
orderid, orderdate p ias c om o Ot
t
FROM Sales.Orders
ORDER BY orderdate DESC;
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 5: Sorting and Filtering Data
2. Remember that the OFFSET-FETCH clause was a new functionality in SQL Server 2012. Unlike the TOP
clause, the OFFSET-FETCH clause must be used with the ORDER BY clause.
Es
s
ed
oc
2. Nã
Implementing thisumtask with the OFFSET-FETCH clause is possible but not easy because, unlike TOP,
oé en
p
OFFSET-FETCH r o to not support a PERCENT option.
erm nadoes pe
itid l d o rte
o r otto nce
ea query n aclick
3. Highlight the written liza i@and Ro Execute.
r c gmai nald
óp l.c
ias om o Ot
nã ton
oa i.
Result: After this exercise, you should uhave tor an understanding of how to apply the TOP option in the SELECT
iza
clause of a T-SQL statement. da
s!
Exercise 4: Writing Queries That Filter Data Using the OFFSET-FETCH Clause
Es
se
Task 1: OFFSET-FETCH Clause to Fetch the First 20 Rows
do
Nã cu
oé m en
pe ro to
rm nald pert
itid o en
1. In Solution Explorer,o r odouble-click
t c the query 81 - Lab Exercise 4.sql.
ea toni@ e a R
liza g on
m
r copens, ald the statement USE TSQL; and click Execute.
2. When the query window óp ail.highlight
ias c om o Ot
t
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 6/7
09/03/2021 Lab Answer Key: Module 5: Sorting and Filtering Data
SELECT
custid, orderid, orderdate
FROM Sales.Orders
ORDER BY orderdate, orderid
OFFSET 0 ROWS FETCH FIRST 20 ROWS ONLY;
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