Skip to content

Commit 67291ed

Browse files
committed
Updated answer to 1.3 and fixed some markdown formatting.
1 parent 575de27 commit 67291ed

1 file changed

Lines changed: 46 additions & 46 deletions

File tree

Instructions/Labs/01-get-started-with-tsql.md

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,38 @@ Now that you've had a chance to explore the **AdventureWorks** database, it's ti
3232
2. In the new **SQLQuery_...** pane, ensure that the **AdventureWorks** database is selected at the top of the query pane. If not, use the **Connect** button to connect the query to the **AdventureWorks** saved connection.
3333
3. In the query editor, enter the following code:
3434

35-
```
35+
```sql
3636
SELECT * FROM SalesLT.Product;
3737
```
3838

3939
4. Use the **⏵Run** button to run the query, and and after a few seconds, review the results, which includes all columns for all products.
4040
5. In the query editor, modify the query as follows:
4141

42-
```
42+
```sql
4343
SELECT Name, StandardCost, ListPrice
4444
FROM SalesLT.Product;
4545
```
4646

4747
6. Use the **⏵Run** button to re-run the query, and and after a few seconds, review the results, which this time include only the **Name**, **StandardCost**, and **ListPrice** columns for all products.
4848
7. Modify the query as shown below to include an expression that results in a calculated column, and then re-run the query:
4949

50-
```
50+
```sql
5151
SELECT Name, ListPrice - StandardCost
5252
FROM SalesLT.Product;
5353
```
5454

5555
8. Note that the results this time include the **Name** column and an unnamed column containing the result of subtracting the **StandardCost** from the **ListPrice**.
5656
9. Modify the query as shown below to assign names to the columns in the results, and then re-run the query.
5757

58-
```
58+
```sql
5959
SELECT Name AS ProductName, ListPrice - StandardCost AS Markup
6060
FROM SalesLT.Product;
6161
```
6262

6363
10. Note that the results now include columns named **ProductName** and **Markup**. The **AS** keyword has been used to assign an *alias* for each column in the results.
6464
11. Replace the existing query with the following code, which also includes an expression that produces a calculated column in the results:
6565

66-
```
66+
```sql
6767
SELECT ProductNumber, Color, Size, Color + ', ' + Size AS ProductDetails
6868
FROM SalesLT.Product;
6969
```
@@ -76,15 +76,15 @@ As you just saw, columns in a table are defined as specific data types, which af
7676
7777
1. Replace the existing query with the following code, and run it:
7878
79-
```
79+
```sql
8080
SELECT ProductID + ': ' + Name AS ProductName
8181
FROM SalesLT.Product;
8282
```
8383
8484
2. Note that this query returns an error. The **+** operator can be used to *concatenate* text-based values, or *add* numeric values; but in this case there's one numeric value (**ProductID**) and one text-based value (**Name**), so it's unclear how the operator should be applied.
8585
3. Modify the query as follows, and re-run it:
8686
87-
```
87+
```sql
8888
SELECT CAST(ProductID AS varchar(5)) + ': ' + Name AS ProductName
8989
FROM SalesLT.Product;
9090
```
@@ -93,7 +93,7 @@ As you just saw, columns in a table are defined as specific data types, which af
9393
9494
5. Modify the query to replace the **CAST** function with a **CONVERT** function as shown below, and then re-run it:
9595
96-
```
96+
```sql
9797
SELECT CONVERT(varchar(5), ProductID) + ': ' + Name AS ProductName
9898
FROM SalesLT.Product;
9999
```
@@ -102,16 +102,16 @@ As you just saw, columns in a table are defined as specific data types, which af
102102
103103
7. Another key difference between the two functions is that **CONVERT** includes an additional parameter that can be useful for formatting date and time values when converting them to text-based data. For example, replace the existing query with the following code and run it.
104104
105-
```
105+
```sql
106106
SELECT SellStartDate,
107107
CONVERT(nvarchar(30), SellStartDate) AS ConvertedDate,
108-
CONVERT(nvarchar(30), SellStartDate, 126) AS ISO8601FormatDate
108+
CONVERT(nvarchar(30), SellStartDate, 126) AS ISO8601FormatDate
109109
FROM SalesLT.Product;
110110
```
111111
112112
8. Replace the existing query with the following code, and run it.
113113
114-
```
114+
```sql
115115
SELECT Name, CAST(Size AS Integer) AS NumericSize
116116
FROM SalesLT.Product;
117117
```
@@ -120,7 +120,7 @@ As you just saw, columns in a table are defined as specific data types, which af
120120
121121
10. Modify the query to use a **TRY_CAST** function, as shown here.
122122
123-
```
123+
```sql
124124
SELECT Name, TRY_CAST(Size AS Integer) AS NumericSize
125125
FROM SalesLT.Product;
126126
```
@@ -133,7 +133,7 @@ We've seen some examples of queries that return *NULL* values. *NULL* is general
133133

134134
1. Modify the existing query as shown here:
135135

136-
```
136+
```sql
137137
SELECT Name, ISNULL(TRY_CAST(Size AS Integer),0) AS NumericSize
138138
FROM SalesLT.Product;
139139
```
@@ -144,7 +144,7 @@ We've seen some examples of queries that return *NULL* values. *NULL* is general
144144
145145
3. Replace the query with the following code to handle *NULL* values for **Color** and **Size** values in the source table:
146146
147-
```
147+
```sql
148148
SELECT ProductNumber, ISNULL(Color, '') + ', ' + ISNULL(Size, '') AS ProductDetails
149149
FROM SalesLT.Product;
150150
```
@@ -153,7 +153,7 @@ We've seen some examples of queries that return *NULL* values. *NULL* is general
153153
154154
4. Try the following query, which replaces the **Color** value "Multi" to *NULL*.
155155
156-
```
156+
```sql
157157
SELECT Name, NULLIF(Color, 'Multi') AS SingleColor
158158
FROM SalesLT.Product;
159159
```
@@ -162,7 +162,7 @@ We've seen some examples of queries that return *NULL* values. *NULL* is general
162162
163163
5. Use the following query to find the first non-*NULL* date for product selling status.
164164
165-
```
165+
```sql
166166
SELECT Name, COALESCE(SellEndDate, SellStartDate) AS StatusLastUpdated
167167
FROM SalesLT.Product;
168168
```
@@ -173,30 +173,30 @@ We've seen some examples of queries that return *NULL* values. *NULL* is general
173173

174174
6. Run the following query, which includes *searched* **CASE** that uses an **IS NULL** expression to check for *NULL* **SellEndDate** values.
175175

176-
```
176+
```sql
177177
SELECT Name,
178-
CASE
179-
WHEN SellEndDate IS NULL THEN 'Currently for sale'
180-
ELSE 'No longer available'
181-
END AS SalesStatus
178+
CASE
179+
WHEN SellEndDate IS NULL THEN 'Currently for sale'
180+
ELSE 'No longer available'
181+
END AS SalesStatus
182182
FROM SalesLT.Product;
183183
```
184184

185185
The previous query used a *searched* **CASE** expression, which begins with a **CASE** keyword, and includes one or more **WHEN...THEN** expressions with the values and predicates to be checked. An **ELSE** expression provides a value to use if none of the **WHEN** conditions are matched, and the **END** keyword denotes the end of the **CASE** expression, which is aliased to a column name for the result using an **AS** expression.
186-
186+
187187
In some queries, it's more appropriate to use a *simple* **CASE** expression that applies multiple **WHERE...THEN** predictes to the same value.
188188
189189
7. Run the following query to see an example of a *simple* **CASE** expression that produced different results depending on the **Size** column value.
190190
191-
```
191+
```sql
192192
SELECT Name,
193-
CASE Size
194-
WHEN 'S' THEN 'Small'
195-
WHEN 'M' THEN 'Medium'
196-
WHEN 'L' THEN 'Large'
197-
WHEN 'XL' THEN 'Extra-Large'
198-
ELSE ISNULL(Size, 'n/a')
199-
END AS ProductSize
193+
CASE Size
194+
WHEN 'S' THEN 'Small'
195+
WHEN 'M' THEN 'Medium'
196+
WHEN 'L' THEN 'Large'
197+
WHEN 'XL' THEN 'Extra-Large'
198+
ELSE ISNULL(Size, 'n/a')
199+
END AS ProductSize
200200
FROM SalesLT.Product;
201201
```
202202
@@ -241,10 +241,10 @@ Some records in the database include missing or unknown values that are returned
241241
- You have been asked to write a query that returns a list of customer names. The list must consist of a single column in the format *first last* (for example *Keith Harris*) if the middle name is unknown, or *first middle last* (for example *Jane M. Gates*) if a middle name is known.
242242
2. Retrieve primary contact details
243243
- Customers may provide Adventure Works with an email address, a phone number, or both. If an email address is available, then it should be used as the primary contact method; if not, then the phone number should be used. You must write a query that returns a list of customer IDs in one column, and a second column named **PrimaryContact** that contains the email address if known, and otherwise the phone number.
244-
244+
245245
**IMPORTANT**: In the sample data provided, there are no customer records without an email address. Therefore, to verify that your query works as expected, run the following **UPDATE** statement to remove some existing email addresses before creating your query:
246-
247-
```
246+
247+
```sql
248248
UPDATE SalesLT.Customer
249249
SET EmailAddress = NULL
250250
WHERE CustomerID % 7 = 1;
@@ -254,8 +254,8 @@ Some records in the database include missing or unknown values that are returned
254254
- You have been asked to create a query that returns a list of sales order IDs and order dates with a column named **ShippingStatus** that contains the text *Shipped* for orders with a known ship date, and *Awaiting Shipment* for orders with no ship date.
255255
256256
**IMPORTANT**: In the sample data provided, there are no sales order header records without a ship date. Therefore, to verify that your query works as expected, run the following UPDATE statement to remove some existing ship dates before creating your query.
257-
258-
```
257+
258+
```sql
259259
UPDATE SalesLT.SalesOrderHeader
260260
SET ShipDate = NULL
261261
WHERE SalesOrderID > 71899;
@@ -269,60 +269,60 @@ This section contains suggested solutions for the challenge queries.
269269
270270
1. Retrieve customer details:
271271
272-
```
272+
```sql
273273
SELECT * FROM SalesLT.Customer;
274274
```
275275
276276
2. Retrieve customer name data:
277277
278-
```
278+
```sql
279279
SELECT Title, FirstName, MiddleName, LastName, Suffix
280280
FROM SalesLT.Customer;
281281
```
282282
283283
3. Retrieve customer names and phone numbers:
284284
285-
```
286-
SELECT Salesperson, Title + ' ' + LastName AS CustomerName, Phone
285+
```sql
286+
SELECT Salesperson, ISNULL(Title,'') + ' ' + LastName AS CustomerName, Phone
287287
FROM SalesLT.Customer;
288288
```
289289
290290
### Challenge 2
291291
292292
1. Retrieve a list of customer companies:
293293
294-
```
294+
```sql
295295
SELECT CAST(CustomerID AS varchar) + ': ' + CompanyName AS CustomerCompany
296296
FROM SalesLT.Customer;
297297
```
298298
299299
2. Retrieve a list of sales order revisions:
300300
301-
```
301+
```sql
302302
SELECT SalesOrderNumber + ' (' + STR(RevisionNumber, 1) + ')' AS OrderRevision,
303-
CONVERT(nvarchar(30), OrderDate, 102) AS OrderDate
303+
CONVERT(nvarchar(30), OrderDate, 102) AS OrderDate
304304
FROM SalesLT.SalesOrderHeader;
305305
```
306306
307-
### Challenge 3:
307+
### Challenge 3
308308
309309
1. Retrieve customer contact names with middle names if known:
310310
311-
```
311+
```sql
312312
SELECT FirstName + ' ' + ISNULL(MiddleName + ' ', '') + LastName AS CustomerName
313313
FROM SalesLT.Customer;
314314
```
315315
316316
2. Retrieve primary contact details:
317317
318-
```
318+
```sql
319319
SELECT CustomerID, COALESCE(EmailAddress, Phone) AS PrimaryContact
320320
FROM SalesLT.Customer;
321321
```
322322
323323
3. Retrieve shipping status:
324324
325-
```
325+
```sql
326326
SELECT SalesOrderID, OrderDate,
327327
CASE
328328
WHEN ShipDate IS NULL THEN 'Awaiting Shipment'

0 commit comments

Comments
 (0)