You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Instructions/Labs/01-get-started-with-tsql.md
+46-46Lines changed: 46 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,38 +32,38 @@ Now that you've had a chance to explore the **AdventureWorks** database, it's ti
32
32
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.
33
33
3. In the query editor, enter the following code:
34
34
35
-
```
35
+
```sql
36
36
SELECT*FROMSalesLT.Product;
37
37
```
38
38
39
39
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.
40
40
5. In the query editor, modify the query as follows:
41
41
42
-
```
42
+
```sql
43
43
SELECT Name, StandardCost, ListPrice
44
44
FROM SalesLT.Product;
45
45
```
46
46
47
47
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.
48
48
7. Modify the query as shown below to include an expression that results in a calculated column, and then re-run the query:
49
49
50
-
```
50
+
```sql
51
51
SELECT Name, ListPrice - StandardCost
52
52
FROM SalesLT.Product;
53
53
```
54
54
55
55
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**.
56
56
9. Modify the query as shown below to assign names to the columns in the results, and then re-run the query.
57
57
58
-
```
58
+
```sql
59
59
SELECT Name AS ProductName, ListPrice - StandardCost AS Markup
60
60
FROM SalesLT.Product;
61
61
```
62
62
63
63
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.
64
64
11. Replace the existing query with the following code, which also includes an expression that produces a calculated column in the results:
65
65
66
-
```
66
+
```sql
67
67
SELECT ProductNumber, Color, Size, Color + ', ' + Size AS ProductDetails
68
68
FROM SalesLT.Product;
69
69
```
@@ -76,15 +76,15 @@ As you just saw, columns in a table are defined as specific data types, which af
76
76
77
77
1. Replace the existing query with the following code, and run it:
78
78
79
-
```
79
+
```sql
80
80
SELECT ProductID + ': ' + Name AS ProductName
81
81
FROM SalesLT.Product;
82
82
```
83
83
84
84
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.
85
85
3. Modify the query as follows, and re-run it:
86
86
87
-
```
87
+
```sql
88
88
SELECT CAST(ProductID AS varchar(5)) + ': ' + Name AS ProductName
89
89
FROM SalesLT.Product;
90
90
```
@@ -93,7 +93,7 @@ As you just saw, columns in a table are defined as specific data types, which af
93
93
94
94
5. Modify the query to replace the **CAST** function with a **CONVERT** function as shown below, and then re-run it:
95
95
96
-
```
96
+
```sql
97
97
SELECT CONVERT(varchar(5), ProductID) + ': ' + Name AS ProductName
98
98
FROM SalesLT.Product;
99
99
```
@@ -102,16 +102,16 @@ As you just saw, columns in a table are defined as specific data types, which af
102
102
103
103
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.
104
104
105
-
```
105
+
```sql
106
106
SELECT SellStartDate,
107
107
CONVERT(nvarchar(30), SellStartDate) AS ConvertedDate,
108
-
CONVERT(nvarchar(30), SellStartDate, 126) AS ISO8601FormatDate
108
+
CONVERT(nvarchar(30), SellStartDate, 126) AS ISO8601FormatDate
109
109
FROM SalesLT.Product;
110
110
```
111
111
112
112
8. Replace the existing query with the following code, and run it.
113
113
114
-
```
114
+
```sql
115
115
SELECT Name, CAST(Size AS Integer) AS NumericSize
116
116
FROM SalesLT.Product;
117
117
```
@@ -120,7 +120,7 @@ As you just saw, columns in a table are defined as specific data types, which af
120
120
121
121
10. Modify the query to use a **TRY_CAST** function, as shown here.
122
122
123
-
```
123
+
```sql
124
124
SELECT Name, TRY_CAST(Size AS Integer) AS NumericSize
125
125
FROM SalesLT.Product;
126
126
```
@@ -133,7 +133,7 @@ We've seen some examples of queries that return *NULL* values. *NULL* is general
133
133
134
134
1. Modify the existing query as shown here:
135
135
136
-
```
136
+
```sql
137
137
SELECT Name, ISNULL(TRY_CAST(Size AS Integer),0) AS NumericSize
138
138
FROM SalesLT.Product;
139
139
```
@@ -144,7 +144,7 @@ We've seen some examples of queries that return *NULL* values. *NULL* is general
144
144
145
145
3. Replace the query with the following code to handle *NULL* values for **Color** and **Size** values in the source table:
@@ -153,7 +153,7 @@ We've seen some examples of queries that return *NULL* values. *NULL* is general
153
153
154
154
4. Try the following query, which replaces the **Color** value "Multi" to *NULL*.
155
155
156
-
```
156
+
```sql
157
157
SELECT Name, NULLIF(Color, 'Multi') AS SingleColor
158
158
FROM SalesLT.Product;
159
159
```
@@ -162,7 +162,7 @@ We've seen some examples of queries that return *NULL* values. *NULL* is general
162
162
163
163
5. Use the following query to find the first non-*NULL* date for product selling status.
164
164
165
-
```
165
+
```sql
166
166
SELECT Name, COALESCE(SellEndDate, SellStartDate) AS StatusLastUpdated
167
167
FROM SalesLT.Product;
168
168
```
@@ -173,30 +173,30 @@ We've seen some examples of queries that return *NULL* values. *NULL* is general
173
173
174
174
6. Run the following query, which includes *searched***CASE** that uses an **IS NULL** expression to check for *NULL***SellEndDate**values.
175
175
176
-
```
176
+
```sql
177
177
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
182
182
FROM SalesLT.Product;
183
183
```
184
184
185
185
The previous query used a *searched***CASE** expression, which begins with a **CASE** keyword, and includes one or more **WHEN...THEN** expressions with the valuesand 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
+
187
187
In some queries, it's more appropriate to use a *simple* **CASE** expression that applies multiple **WHERE...THEN** predictes to the same value.
188
188
189
189
7. Run the following query to see an example of a *simple* **CASE** expression that produced different results depending on the **Size** column value.
190
190
191
-
```
191
+
```sql
192
192
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
200
200
FROM SalesLT.Product;
201
201
```
202
202
@@ -241,10 +241,10 @@ Some records in the database include missing or unknown values that are returned
241
241
- 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.
242
242
2. Retrieve primary contact details
243
243
- 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
+
245
245
**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
248
248
UPDATE SalesLT.Customer
249
249
SET EmailAddress = NULL
250
250
WHERE CustomerID % 7 = 1;
@@ -254,8 +254,8 @@ Some records in the database include missing or unknown values that are returned
254
254
- 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.
255
255
256
256
**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
259
259
UPDATE SalesLT.SalesOrderHeader
260
260
SET ShipDate = NULL
261
261
WHERE SalesOrderID > 71899;
@@ -269,60 +269,60 @@ This section contains suggested solutions for the challenge queries.
0 commit comments