SQL vs R Exercise
Use the Adventure Works dataset to create the following reports. The dataset is availablt for download in it's original format at https://msdn.microsoft.com/en-us/library/hh403424.aspx (follow instructions to download).
Note
- We only use base functions in R. (With the exception of loading the Excel spreadsheets.)
- NULL values may be converted to '' (empty strings).
- Dates are loaded as factors and need to be converted to date values. We perform the conversion when it's needed.
Task
- write the R expression to produce a table as described in the problem statements.
- The SQL expression may give you a hint. It also allows you to see both systems side-by-side.
- If you don't know SQL just ignore the SQL code.
Loading required package: rJava
Loading required package: xlsxjars
Out[5]:
user system elapsed
4.914 0.022 4.750
Out[13]:
user system elapsed
0.075 0.001 0.074
Out[15]:
user system elapsed
0.099 0.000 0.093
Out[17]:
user system elapsed
0.123 0.001 0.122
1. Provide a list of employees that are married.
SELECT e.EmployeeID, e.FirstName, e.LastName FROM dbo.Employees AS e
WHERE e.MaritalStatus = 'M';
Out[22]:
| EmployeeID | FirstName | LastName | MaritalStatus |
1 | 259 | Ben | Miller | M |
2 | 278 | Garrett | Vargas | M |
3 | 204 | Gabe | Mares | M |
4 | 78 | Reuben | D'sa | M |
5 | 255 | Gordon | Hee | M |
11 | 10 | Michael | Raheem | M |
13 | 175 | Reed | Koch | M |
15 | 144 | Paul | Singh | M |
16 | 192 | Brenda | Diaz | M |
19 | 203 | Ken | Myer | M |
20 | 245 | Barbara | Moreland | M |
25 | 163 | Alex | Nayberg | M |
26 | 118 | Don | Hall | M |
27 | 176 | David | Lawrence | M |
28 | 147 | Sandra | Reátegui Alayo | M |
29 | 138 | Samantha | Smith | M |
30 | 95 | Jim | Scardelis | M |
31 | 231 | Jo | Berry | M |
32 | 86 | Ryan | Cornelsen | M |
34 | 214 | Andreas | Berglund | M |
35 | 72 | Steven | Selikoff | M |
37 | 107 | Christopher | Hill | M |
38 | 269 | Dan | Bacon | M |
39 | 187 | Yvonne | McKay | M |
41 | 12 | Thierry | D'Hers | M |
43 | 30 | Britta | Simon | M |
44 | 198 | Lorraine | Nay | M |
46 | 31 | Margie | Shoop | M |
49 | 3 | Roberto | Tamburello | M |
50 | 199 | Paula | Nartker | M |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
228 | 136 | Sylvester | Valdez | M |
232 | 35 | Brandon | Heidepriem | M |
233 | 228 | Christian | Kleinerman | M |
234 | 223 | Sairaj | Uddin | M |
235 | 100 | Lolan | Song | M |
240 | 200 | Frank | Lee | M |
241 | 279 | Tsvi | Reiter | M |
244 | 73 | Carole | Poland | M |
250 | 21 | Terry | Eminhizer | M |
253 | 254 | Fukiko | Ogisu | M |
256 | 53 | Diane | Glimp | M |
257 | 282 | José | Saraiva | M |
259 | 209 | Kathie | Flood | M |
260 | 50 | Sidney | Higa | M |
262 | 70 | David | Ortiz | M |
265 | 140 | Prasanna | Samarawickrama | M |
267 | 5 | Gail | Erickson | M |
268 | 252 | Arvind | Rao | M |
270 | 233 | Magnus | Hedlund | M |
271 | 83 | Patrick | Cook | M |
273 | 241 | David | Liu | M |
276 | 239 | Mindy | Martin | M |
279 | 38 | Kim | Abercrombie | M |
280 | 154 | Raymond | Sam | M |
281 | 114 | Mindaugas | Krapauskas | M |
282 | 123 | Vamsi | Kuppa | M |
284 | 101 | Houman | Pournasseh | M |
285 | 221 | Chris | Norred | M |
289 | 289 | Jae | Pak | M |
291 | 999 | Chadwick | Smith | M |
2a. Show me a list of employees that have a lastname that begins with "R".
SELECT e.EmployeeID, e.FirstName, e.LastName FROM dbo.Employees AS e
WHERE e.LastName LIKE 'R%';
Out[28]:
| EmployeeID | FirstName | LastName |
10 | 124 | Kim | Ralls |
11 | 10 | Michael | Raheem |
17 | 166 | Jack | Richins |
28 | 147 | Sandra | Reátegui Alayo |
45 | 133 | Michael | Rothkugel |
96 | 44 | Simon | Rapier |
100 | 65 | Randy | Reeves |
129 | 145 | Cynthia | Randall |
132 | 149 | Andy | Ruth |
167 | 74 | Bjorn | Rettig |
181 | 71 | Michael | Ray |
189 | 190 | Robert | Rounthwaite |
220 | 157 | Linda | Randall |
241 | 279 | Tsvi | Reiter |
268 | 252 | Arvind | Rao |
2b. Show me a list of employees that have a lastname that ends with "r"
SELECT e.EmployeeID, e.FirstName, e.LastName FROM dbo.Employees AS e
WHERE e.LastName LIKE '%r';
Out[29]:
| EmployeeID | FirstName | LastName |
1 | 259 | Ben | Miller |
9 | 161 | Kirk | Koenigsbauer |
19 | 203 | Ken | Myer |
50 | 199 | Paula | Nartker |
54 | 41 | Bryan | Baker |
57 | 104 | Mary | Baker |
65 | 225 | Alan | Brewer |
76 | 156 | Lane | Sacksteder |
96 | 44 | Simon | Rapier |
98 | 96 | Elizabeth | Keyser |
99 | 229 | Lori | Penor |
128 | 39 | Ed | Dudenhoefer |
130 | 42 | James | Kramer |
166 | 117 | Chad | Niswonger |
169 | 57 | Frank | Miller |
173 | 105 | Kevin | Homer |
174 | 253 | Linda | Meisner |
197 | 7 | Dylan | Miller |
201 | 183 | Barbara | Decker |
204 | 142 | Olinda | Turner |
217 | 94 | Russell | Hunter |
223 | 273 | Brian | Welcker |
230 | 216 | Sean | Alexander |
241 | 279 | Tsvi | Reiter |
250 | 21 | Terry | Eminhizer |
255 | 262 | David | Barber |
283 | 29 | Mark | McArthur |
2c. Provide a list of employees that have a hyphenated lastname.
SELECT e.EmployeeID, e.FirstName, e.LastName FROM dbo.Employees AS e
WHERE e.LastName LIKE '%-%';
Out[32]:
| EmployeeID | FirstName | LastName |
115 | 284 | Tete | Mensa-Annan |
135 | 180 | Katie | McAskill-White |
177 | 280 | Pamela | Ansman-Wolfe |
3a. Provide a list of employees that are on salary and have more than 35 vacation hours left.
SELECT e.EmployeeID
,e.FirstName
,e.LastName
,e.VacationHours
,e.SalariedFlag
FROM dbo.Employees AS e
WHERE (e.SalariedFlag = 1) AND (e.VacationHours > 35);
Out[36]:
| EmployeeID | FirstName | LastName | SalariedFlag | VacationHours |
7 | 270 | François | Ajenstat | 1 | 67 |
12 | 248 | Mike | Seamans | 1 | 59 |
20 | 245 | Barbara | Moreland | 1 | 58 |
38 | 269 | Dan | Bacon | 1 | 72 |
58 | 263 | Jean | Trenary | 1 | 65 |
66 | 212 | Peng | Wu | 1 | 81 |
67 | 227 | Gary | Altman | 1 | 86 |
94 | 286 | Lynn | Tsoflias | 1 | 36 |
102 | 26 | Peter | Krebs | 1 | 43 |
107 | 16 | David | Bradley | 1 | 40 |
109 | 268 | Ramesh | Meyyappan | 1 | 73 |
115 | 284 | Tete | Mensa-Annan | 1 | 39 |
122 | 235 | Paula | Barreto de Mattos | 1 | 54 |
124 | 1 | Ken | Sánchez | 1 | 99 |
126 | 249 | Wendy | Kahn | 1 | 55 |
131 | 8 | Diane | Margheim | 1 | 62 |
141 | 211 | Hazem | Abolrous | 1 | 80 |
146 | 267 | Karen | Berg | 1 | 74 |
148 | 250 | Sheela | Word | 1 | 49 |
152 | 121 | Pilar | Ackerman | 1 | 93 |
168 | 271 | Dan | Wilson | 1 | 66 |
170 | 272 | Janaina | Bueno | 1 | 71 |
197 | 7 | Dylan | Miller | 1 | 61 |
211 | 264 | Stephanie | Conroy | 1 | 68 |
224 | 9 | Gigi | Matthew | 1 | 63 |
233 | 228 | Christian | Kleinerman | 1 | 92 |
236 | 275 | Michael | Blythe | 1 | 38 |
273 | 241 | David | Liu | 1 | 57 |
274 | 25 | James | Hamilton | 1 | 64 |
289 | 289 | Jae | Pak | 1 | 37 |
3b. Show the same as above but limit it to American employees.
SELECT DISTINCT CountryName FROM dbo.Employees;
SELECT e.EmployeeID
,e.FirstName
,e.LastName
,e.VacationHours
,e.SalariedFlag
,e.CountryName
FROM dbo.Employees AS e
WHERE (e.SalariedFlag = 1) AND (e.VacationHours > 5) AND e.CountryName = 'United States';
Out[37]:
| EmployeeID | FirstName | LastName | SalariedFlag | VacationHours |
7 | 270 | François | Ajenstat | 1 | 67 |
12 | 248 | Mike | Seamans | 1 | 59 |
20 | 245 | Barbara | Moreland | 1 | 58 |
38 | 269 | Dan | Bacon | 1 | 72 |
58 | 263 | Jean | Trenary | 1 | 65 |
66 | 212 | Peng | Wu | 1 | 81 |
67 | 227 | Gary | Altman | 1 | 86 |
102 | 26 | Peter | Krebs | 1 | 43 |
107 | 16 | David | Bradley | 1 | 40 |
109 | 268 | Ramesh | Meyyappan | 1 | 73 |
115 | 284 | Tete | Mensa-Annan | 1 | 39 |
122 | 235 | Paula | Barreto de Mattos | 1 | 54 |
124 | 1 | Ken | Sánchez | 1 | 99 |
126 | 249 | Wendy | Kahn | 1 | 55 |
131 | 8 | Diane | Margheim | 1 | 62 |
141 | 211 | Hazem | Abolrous | 1 | 80 |
146 | 267 | Karen | Berg | 1 | 74 |
148 | 250 | Sheela | Word | 1 | 49 |
152 | 121 | Pilar | Ackerman | 1 | 93 |
168 | 271 | Dan | Wilson | 1 | 66 |
170 | 272 | Janaina | Bueno | 1 | 71 |
197 | 7 | Dylan | Miller | 1 | 61 |
211 | 264 | Stephanie | Conroy | 1 | 68 |
224 | 9 | Gigi | Matthew | 1 | 63 |
233 | 228 | Christian | Kleinerman | 1 | 92 |
236 | 275 | Michael | Blythe | 1 | 38 |
273 | 241 | David | Liu | 1 | 57 |
274 | 25 | James | Hamilton | 1 | 64 |
3c. Show the same as above but limit it to non-American employees.
SELECT e.EmployeeID
,e.FirstName
,e.LastName
,e.VacationHours
,e.SalariedFlag
,e.CountryName
FROM dbo.Employees AS e
WHERE (e.SalariedFlag = 1) AND (e.VacationHours > 5) AND e.CountryName != 'United States';
Out[38]:
| EmployeeID | FirstName | LastName | SalariedFlag | VacationHours |
94 | 286 | Lynn | Tsoflias | 1 | 36 |
289 | 289 | Jae | Pak | 1 | 37 |
4a. List the married employees with more than 35 vacation hours, only ones living in Washington state.
SELECT e.EmployeeID
,e.FirstName
,e.LastName
,e.VacationHours
,e.SalariedFlag
,e.StateProvinceName
,e.CountryName
FROM dbo.Employees AS e
WHERE e.MaritalStatus = 'M' AND e.VacationHours > 35 AND e.StateProvinceName = 'Washington'
;
Out[39]:
| EmployeeID | FirstName | LastName | MaritalStatus | VacationHours | StateProvinceName |
1 | 259 | Ben | Miller | M | 55 | Washington |
3 | 204 | Gabe | Mares | M | 57 | Washington |
4 | 78 | Reuben | D'sa | M | 72 | Washington |
5 | 255 | Gordon | Hee | M | 52 | Washington |
16 | 192 | Brenda | Diaz | M | 71 | Washington |
20 | 245 | Barbara | Moreland | M | 58 | Washington |
25 | 163 | Alex | Nayberg | M | 77 | Washington |
26 | 118 | Don | Hall | M | 88 | Washington |
28 | 147 | Sandra | Reátegui Alayo | M | 37 | Washington |
31 | 231 | Jo | Berry | M | 91 | Washington |
32 | 86 | Ryan | Cornelsen | M | 59 | Washington |
34 | 214 | Andreas | Berglund | M | 84 | Washington |
35 | 72 | Steven | Selikoff | M | 41 | Washington |
37 | 107 | Christopher | Hill | M | 91 | Washington |
38 | 269 | Dan | Bacon | M | 72 | Washington |
39 | 187 | Yvonne | McKay | M | 79 | Washington |
44 | 198 | Lorraine | Nay | M | 49 | Washington |
50 | 199 | Paula | Nartker | M | 54 | Washington |
51 | 48 | Ruth | Ellerbrock | M | 83 | Washington |
52 | 126 | Jimmy | Bischoff | M | 96 | Washington |
53 | 230 | Stuart | Macrae | M | 88 | Washington |
56 | 226 | Brian | LaMee | M | 48 | Washington |
57 | 104 | Mary | Baker | M | 94 | Washington |
65 | 225 | Alan | Brewer | M | 47 | Washington |
66 | 212 | Peng | Wu | M | 81 | Washington |
67 | 227 | Gary | Altman | M | 86 | Washington |
70 | 62 | John | Campbell | M | 81 | Washington |
83 | 220 | Karen | Berge | M | 79 | Washington |
85 | 24 | Jill | Williams | M | 47 | Washington |
86 | 132 | Nicole | Holliday | M | 67 | Washington |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
175 | 261 | Reinout | Hillmann | M | 51 | Washington |
178 | 260 | Annette | Hill | M | 50 | Washington |
186 | 165 | Chris | Preston | M | 76 | Washington |
188 | 224 | William | Vong | M | 45 | Washington |
191 | 247 | Janet | Sheperdigian | M | 64 | Washington |
192 | 76 | Carol | Philips | M | 45 | Washington |
195 | 20 | Wanida | Benshoof | M | 41 | Washington |
197 | 7 | Dylan | Miller | M | 61 | Washington |
203 | 87 | Cristian | Petculescu | M | 67 | Washington |
208 | 45 | Thomas | Michaels | M | 37 | Washington |
210 | 208 | Scott | Gode | M | 86 | Washington |
218 | 242 | Deborah | Poe | M | 60 | Washington |
224 | 9 | Gigi | Matthew | M | 63 | Washington |
233 | 228 | Christian | Kleinerman | M | 92 | Washington |
234 | 223 | Sairaj | Uddin | M | 46 | Washington |
240 | 200 | Frank | Lee | M | 56 | Washington |
244 | 73 | Carole | Poland | M | 42 | Washington |
250 | 21 | Terry | Eminhizer | M | 44 | Washington |
253 | 254 | Fukiko | Ogisu | M | 57 | Washington |
256 | 53 | Diane | Glimp | M | 87 | Washington |
259 | 209 | Kathie | Flood | M | 87 | Washington |
260 | 50 | Sidney | Higa | M | 84 | Washington |
268 | 252 | Arvind | Rao | M | 60 | Washington |
270 | 233 | Magnus | Hedlund | M | 87 | Washington |
271 | 83 | Patrick | Cook | M | 61 | Washington |
273 | 241 | David | Liu | M | 57 | Washington |
276 | 239 | Mindy | Martin | M | 51 | Washington |
281 | 114 | Mindaugas | Krapauskas | M | 97 | Washington |
282 | 123 | Vamsi | Kuppa | M | 95 | Washington |
285 | 221 | Chris | Norred | M | 75 | Washington |
4b. Change the logic to include anyone who meets any of the 3 conditions (i.e., people who are either married, live in Washington state, or have more than 35 vacation hours left)
SELECT e.EmployeeID
,e.FirstName
,e.LastName
,e.MaritalStatus
,e.VacationHours
,e.SalariedFlag
,e.StateProvinceName
,e.CountryName
FROM dbo.Employees AS e
WHERE e.MaritalStatus = 'M' OR e.VacationHours > 35 OR e.StateProvinceName = 'Washington'
;
Out[40]:
| EmployeeID | FirstName | LastName | MaritalStatus | VacationHours | StateProvinceName |
1 | 259 | Ben | Miller | M | 55 | Washington |
2 | 278 | Garrett | Vargas | M | 33 | Alberta |
3 | 204 | Gabe | Mares | M | 57 | Washington |
4 | 78 | Reuben | D'sa | M | 72 | Washington |
5 | 255 | Gordon | Hee | M | 52 | Washington |
6 | 66 | Karan | Khanna | S | 28 | Washington |
7 | 270 | François | Ajenstat | S | 67 | Washington |
8 | 22 | Sariya | Harnpadoungsataya | S | 45 | Washington |
9 | 161 | Kirk | Koenigsbauer | S | 74 | Washington |
10 | 124 | Kim | Ralls | S | 98 | Washington |
11 | 10 | Michael | Raheem | M | 16 | California |
12 | 248 | Mike | Seamans | S | 59 | Washington |
13 | 175 | Reed | Koch | M | 23 | Washington |
14 | 155 | Fadi | Fakhouri | S | 16 | Washington |
15 | 144 | Paul | Singh | M | 6 | Washington |
16 | 192 | Brenda | Diaz | M | 71 | Washington |
17 | 166 | Jack | Richins | S | 68 | Washington |
18 | 112 | John | Evans | S | 91 | Washington |
19 | 203 | Ken | Myer | M | 51 | Alberta |
20 | 245 | Barbara | Moreland | M | 58 | Washington |
21 | 257 | Eric | Kurjan | S | 54 | Washington |
22 | 244 | Bryan | Walton | S | 62 | Alberta |
23 | 69 | Steve | Masters | S | 31 | Washington |
24 | 210 | Belinda | Newman | S | 83 | Washington |
25 | 163 | Alex | Nayberg | M | 77 | Washington |
26 | 118 | Don | Hall | M | 88 | Washington |
27 | 176 | David | Lawrence | M | 29 | Washington |
28 | 147 | Sandra | Reátegui Alayo | M | 37 | Washington |
29 | 138 | Samantha | Smith | M | 7 | Washington |
30 | 95 | Jim | Scardelis | M | 1 | Washington |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
260 | 50 | Sidney | Higa | M | 84 | Washington |
261 | 222 | A. Scott | Wright | S | 44 | Washington |
262 | 70 | David | Ortiz | M | 33 | Washington |
263 | 219 | Sean | Chai | S | 78 | Washington |
264 | 148 | Jason | Watters | S | 38 | Washington |
265 | 140 | Prasanna | Samarawickrama | M | 5 | Washington |
267 | 5 | Gail | Erickson | M | 5 | Washington |
268 | 252 | Arvind | Rao | M | 60 | Washington |
269 | 266 | Peter | Connelly | S | 69 | Washington |
270 | 233 | Magnus | Hedlund | M | 87 | Washington |
271 | 83 | Patrick | Cook | M | 61 | Washington |
272 | 251 | Mikael | Sandberg | S | 59 | Washington |
273 | 241 | David | Liu | M | 57 | Washington |
274 | 25 | James | Hamilton | S | 64 | Washington |
275 | 108 | Jinghao | Liu | S | 77 | Washington |
276 | 239 | Mindy | Martin | M | 51 | Washington |
277 | 27 | Jo | Brown | S | 80 | Washington |
278 | 186 | Shane | Kim | S | 75 | Washington |
279 | 38 | Kim | Abercrombie | M | 24 | Washington |
280 | 154 | Raymond | Sam | M | 10 | Washington |
281 | 114 | Mindaugas | Krapauskas | M | 97 | Washington |
282 | 123 | Vamsi | Kuppa | M | 95 | Washington |
283 | 29 | Mark | McArthur | S | 19 | Washington |
284 | 101 | Houman | Pournasseh | M | 3 | Washington |
285 | 221 | Chris | Norred | M | 75 | Washington |
286 | 18 | John | Wood | S | 48 | Washington |
287 | 46 | Eugene | Kogan | S | 36 | Washington |
288 | 202 | Tawana | Nusbaum | S | 50 | Washington |
289 | 289 | Jae | Pak | M | 37 | England |
291 | 999 | Chadwick | Smith | M | 55 | Georgia |
4c. Show the same as above, but only for Production Technicians
SELECT DISTINCT JobTitle FROM dbo.Employees; --- look at job titles
SELECT e.EmployeeID
,e.FirstName
,e.LastName
,e.MaritalStatus
,e.JobTitle
,e.VacationHours
,e.SalariedFlag
,e.StateProvinceName
,e.CountryName
FROM dbo.Employees AS e
WHERE e.JobTitle LIKE 'Production Technician%'
AND (e.MaritalStatus = 'M' OR e.VacationHours > 35 OR e.StateProvinceName = 'Washington')
;
Out[53]:
| EmployeeID | FirstName | LastName | JobTitle | MaritalStatus | VacationHours | StateProvinceName |
3 | 204 | Gabe | Mares | Production Technician - WC40 | M | 57 | Washington |
6 | 66 | Karan | Khanna | Production Technician - WC60 | S | 28 | Washington |
9 | 161 | Kirk | Koenigsbauer | Production Technician - WC45 | S | 74 | Washington |
13 | 175 | Reed | Koch | Production Technician - WC30 | M | 23 | Washington |
14 | 155 | Fadi | Fakhouri | Production Technician - WC20 | S | 16 | Washington |
15 | 144 | Paul | Singh | Production Technician - WC20 | M | 6 | Washington |
18 | 112 | John | Evans | Production Technician - WC50 | S | 91 | Washington |
19 | 203 | Ken | Myer | Production Technician - WC40 | M | 51 | Alberta |
23 | 69 | Steve | Masters | Production Technician - WC60 | S | 31 | Washington |
24 | 210 | Belinda | Newman | Production Technician - WC45 | S | 83 | Washington |
25 | 163 | Alex | Nayberg | Production Technician - WC45 | M | 77 | Washington |
26 | 118 | Don | Hall | Production Technician - WC50 | M | 88 | Washington |
27 | 176 | David | Lawrence | Production Technician - WC30 | M | 29 | Washington |
28 | 147 | Sandra | Reátegui Alayo | Production Technician - WC30 | M | 37 | Washington |
29 | 138 | Samantha | Smith | Production Technician - WC20 | M | 7 | Washington |
30 | 95 | Jim | Scardelis | Production Technician - WC50 | M | 1 | Washington |
32 | 86 | Ryan | Cornelsen | Production Technician - WC40 | M | 59 | Washington |
35 | 72 | Steven | Selikoff | Production Technician - WC30 | M | 41 | Washington |
36 | 162 | Laura | Steele | Production Technician - WC45 | S | 75 | Washington |
37 | 107 | Christopher | Hill | Production Technician - WC10 | M | 91 | Washington |
39 | 187 | Yvonne | McKay | Production Technician - WC45 | M | 79 | Washington |
40 | 75 | Michiko | Osada | Production Technician - WC30 | S | 44 | Washington |
42 | 116 | Michael | Patten | Production Technician - WC50 | S | 98 | Washington |
43 | 30 | Britta | Simon | Production Technician - WC60 | M | 14 | Washington |
44 | 198 | Lorraine | Nay | Production Technician - WC40 | M | 49 | Washington |
45 | 133 | Michael | Rothkugel | Production Technician - WC40 | S | 70 | Washington |
46 | 31 | Margie | Shoop | Production Technician - WC60 | M | 18 | Washington |
47 | 168 | Garrett | Young | Production Technician - WC30 | S | 34 | Washington |
50 | 199 | Paula | Nartker | Production Technician - WC40 | M | 54 | Washington |
51 | 48 | Ruth | Ellerbrock | Production Technician - WC10 | M | 83 | Washington |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
235 | 100 | Lolan | Song | Production Technician - WC50 | M | 7 | Washington |
237 | 85 | Brian | Goldstein | Production Technician - WC40 | S | 63 | Washington |
238 | 61 | Diane | Tibbott | Production Technician - WC50 | S | 8 | Washington |
239 | 106 | John | Kane | Production Technician - WC10 | S | 92 | Washington |
240 | 200 | Frank | Lee | Production Technician - WC40 | M | 56 | Washington |
242 | 146 | Jian Shuo | Wang | Production Technician - WC30 | S | 36 | Washington |
243 | 103 | Ebru | Ersan | Production Technician - WC10 | S | 93 | Washington |
244 | 73 | Carole | Poland | Production Technician - WC30 | M | 42 | Washington |
245 | 185 | Stefen | Hesse | Production Technician - WC20 | S | 19 | Washington |
246 | 37 | Chris | Okelberry | Production Technician - WC60 | S | 16 | Washington |
248 | 179 | Jan | Miksovsky | Production Technician - WC30 | S | 30 | Washington |
249 | 91 | Kimberly | Zimmerman | Production Technician - WC10 | S | 95 | Washington |
251 | 207 | Greg | Alderson | Production Technician - WC45 | S | 85 | Washington |
252 | 82 | Jack | Creasey | Production Technician - WC40 | S | 62 | Washington |
254 | 67 | Jay | Adams | Production Technician - WC60 | S | 32 | Washington |
256 | 53 | Diane | Glimp | Production Technician - WC10 | M | 87 | Washington |
258 | 169 | Susan | Metters | Production Technician - WC30 | S | 31 | Washington |
259 | 209 | Kathie | Flood | Production Technician - WC45 | M | 87 | Washington |
260 | 50 | Sidney | Higa | Production Technician - WC10 | M | 84 | Washington |
262 | 70 | David | Ortiz | Production Technician - WC60 | M | 33 | Washington |
264 | 148 | Jason | Watters | Production Technician - WC30 | S | 38 | Washington |
265 | 140 | Prasanna | Samarawickrama | Production Technician - WC20 | M | 5 | Washington |
271 | 83 | Patrick | Cook | Production Technician - WC40 | M | 61 | Washington |
279 | 38 | Kim | Abercrombie | Production Technician - WC60 | M | 24 | Washington |
280 | 154 | Raymond | Sam | Production Technician - WC20 | M | 10 | Washington |
281 | 114 | Mindaugas | Krapauskas | Production Technician - WC50 | M | 97 | Washington |
283 | 29 | Mark | McArthur | Production Technician - WC60 | S | 19 | Washington |
284 | 101 | Houman | Pournasseh | Production Technician - WC50 | M | 3 | Washington |
287 | 46 | Eugene | Kogan | Production Technician - WC60 | S | 36 | Washington |
288 | 202 | Tawana | Nusbaum | Production Technician - WC40 | S | 50 | Washington |
5a. List all employees living in Redmond, Seattle, and Bellevue, showing EmployeeID, FirstName, LastName, and City. Sort the list alphabetically by city.
SELECT e.EmployeeID, e.FirstName, e.LastName, e.City
FROM dbo.Employees AS e
WHERE e.City in ('Redmond', 'Seattle', 'Bellevue')
ORDER BY e.City
;
Out[65]:
| EmployeeID |
1 | 259 |
2 | 278 |
3 | 204 |
4 | 78 |
5 | 255 |
6 | 66 |
7 | 270 |
8 | 22 |
9 | 161 |
10 | 124 |
11 | 10 |
12 | 248 |
13 | 175 |
14 | 155 |
15 | 144 |
16 | 192 |
17 | 166 |
18 | 112 |
19 | 203 |
20 | 245 |
21 | 257 |
22 | 244 |
23 | 69 |
24 | 210 |
25 | 163 |
26 | 118 |
27 | 176 |
28 | 147 |
29 | 138 |
30 | 95 |
⋮ | ⋮ |
262 | 70 |
263 | 219 |
264 | 148 |
265 | 140 |
266 | 290 |
267 | 5 |
268 | 252 |
269 | 266 |
270 | 233 |
271 | 83 |
272 | 251 |
273 | 241 |
274 | 25 |
275 | 108 |
276 | 239 |
277 | 27 |
278 | 186 |
279 | 38 |
280 | 154 |
281 | 114 |
282 | 123 |
283 | 29 |
284 | 101 |
285 | 221 |
286 | 18 |
287 | 46 |
288 | 202 |
289 | 289 |
290 | 288 |
291 | 999 |
Out[84]:
| EmployeeID | FirstName | LastName | City | StateProvinceName |
5 | 255 | Gordon | Hee | Bellevue | Washington |
15 | 144 | Paul | Singh | Bellevue | Washington |
29 | 138 | Samantha | Smith | Bellevue | Washington |
55 | 283 | David | Campbell | Bellevue | Washington |
74 | 258 | Erin | Hagens | Bellevue | Washington |
79 | 89 | Patrick | Wedge | Bellevue | Washington |
84 | 32 | Rebecca | Laszlo | Bellevue | Washington |
89 | 113 | Linda | Moschell | Bellevue | Washington |
90 | 196 | Shammi | Mohamed | Bellevue | Washington |
97 | 99 | Nuan | Yu | Bellevue | Washington |
102 | 26 | Peter | Krebs | Bellevue | Washington |
103 | 93 | Kok-Ho | Loh | Bellevue | Washington |
105 | 151 | Rostislav | Shabalin | Bellevue | Washington |
112 | 177 | Russell | King | Bellevue | Washington |
122 | 235 | Paula | Barreto de Mattos | Bellevue | Washington |
132 | 149 | Andy | Ruth | Bellevue | Washington |
161 | 34 | Suchitra | Mohan | Bellevue | Washington |
168 | 271 | Dan | Wilson | Bellevue | Washington |
173 | 105 | Kevin | Homer | Bellevue | Washington |
174 | 253 | Linda | Meisner | Bellevue | Washington |
175 | 261 | Reinout | Hillmann | Bellevue | Washington |
176 | 19 | Mary | Dempsey | Bellevue | Washington |
180 | 206 | Stuart | Munson | Bellevue | Washington |
188 | 224 | William | Vong | Bellevue | Washington |
196 | 159 | Terrence | Earls | Bellevue | Washington |
204 | 142 | Olinda | Turner | Bellevue | Washington |
206 | 128 | Paul | Komosinski | Bellevue | Washington |
209 | 141 | Min | Su | Bellevue | Washington |
224 | 9 | Gigi | Matthew | Bellevue | Washington |
228 | 136 | Sylvester | Valdez | Bellevue | Washington |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
128 | 39 | Ed | Dudenhoefer | Seattle | Washington |
135 | 180 | Katie | McAskill-White | Seattle | Washington |
137 | 152 | Yuhong | Li | Seattle | Washington |
138 | 171 | David | Yalovsky | Seattle | Washington |
143 | 36 | Jose | Lugo | Seattle | Washington |
149 | 60 | Pete | Male | Seattle | Washington |
150 | 120 | Kitti | Lertpiriyasuwat | Seattle | Washington |
152 | 121 | Pilar | Ackerman | Seattle | Washington |
157 | 77 | Merav | Netz | Seattle | Washington |
167 | 74 | Bjorn | Rettig | Seattle | Washington |
181 | 71 | Michael | Ray | Seattle | Washington |
183 | 102 | Zheng | Mu | Seattle | Washington |
186 | 165 | Chris | Preston | Seattle | Washington |
189 | 190 | Robert | Rounthwaite | Seattle | Washington |
199 | 40 | JoLynn | Dobney | Seattle | Washington |
200 | 193 | Alejandro | McGuel | Seattle | Washington |
203 | 87 | Cristian | Petculescu | Seattle | Washington |
210 | 208 | Scott | Gode | Seattle | Washington |
212 | 58 | Kendall | Keil | Seattle | Washington |
216 | 195 | Kevin | Liu | Seattle | Washington |
217 | 94 | Russell | Hunter | Seattle | Washington |
220 | 157 | Linda | Randall | Seattle | Washington |
222 | 191 | Lionel | Penuchot | Seattle | Washington |
226 | 33 | Annik | Stahl | Seattle | Washington |
240 | 200 | Frank | Lee | Seattle | Washington |
249 | 91 | Kimberly | Zimmerman | Seattle | Washington |
252 | 82 | Jack | Creasey | Seattle | Washington |
262 | 70 | David | Ortiz | Seattle | Washington |
271 | 83 | Patrick | Cook | Seattle | Washington |
288 | 202 | Tawana | Nusbaum | Seattle | Washington |
5b. For the list above, make sure these are only in the state of Washington, just to be careful.
SELECT DISTINCT e.City, e.StateProvinceName, e.CountryName
FROM dbo.Employees AS e
WHERE e.City in ('Redmond', 'Seattle', 'Bellevue')
;
SELECT e.EmployeeID, e.FirstName, e.LastName, e.City, e.StateProvinceName
FROM dbo.Employees AS e
WHERE e.City in ('Redmond', 'Seattle', 'Bellevue') AND e.StateProvinceName = 'Washington'
ORDER BY e.City
;
Out[85]:
| EmployeeID | FirstName | LastName | City | StateProvinceName |
5 | 255 | Gordon | Hee | Bellevue | Washington |
15 | 144 | Paul | Singh | Bellevue | Washington |
29 | 138 | Samantha | Smith | Bellevue | Washington |
55 | 283 | David | Campbell | Bellevue | Washington |
74 | 258 | Erin | Hagens | Bellevue | Washington |
79 | 89 | Patrick | Wedge | Bellevue | Washington |
84 | 32 | Rebecca | Laszlo | Bellevue | Washington |
89 | 113 | Linda | Moschell | Bellevue | Washington |
90 | 196 | Shammi | Mohamed | Bellevue | Washington |
97 | 99 | Nuan | Yu | Bellevue | Washington |
102 | 26 | Peter | Krebs | Bellevue | Washington |
103 | 93 | Kok-Ho | Loh | Bellevue | Washington |
105 | 151 | Rostislav | Shabalin | Bellevue | Washington |
112 | 177 | Russell | King | Bellevue | Washington |
122 | 235 | Paula | Barreto de Mattos | Bellevue | Washington |
132 | 149 | Andy | Ruth | Bellevue | Washington |
161 | 34 | Suchitra | Mohan | Bellevue | Washington |
168 | 271 | Dan | Wilson | Bellevue | Washington |
173 | 105 | Kevin | Homer | Bellevue | Washington |
174 | 253 | Linda | Meisner | Bellevue | Washington |
175 | 261 | Reinout | Hillmann | Bellevue | Washington |
176 | 19 | Mary | Dempsey | Bellevue | Washington |
180 | 206 | Stuart | Munson | Bellevue | Washington |
188 | 224 | William | Vong | Bellevue | Washington |
196 | 159 | Terrence | Earls | Bellevue | Washington |
204 | 142 | Olinda | Turner | Bellevue | Washington |
206 | 128 | Paul | Komosinski | Bellevue | Washington |
209 | 141 | Min | Su | Bellevue | Washington |
224 | 9 | Gigi | Matthew | Bellevue | Washington |
228 | 136 | Sylvester | Valdez | Bellevue | Washington |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
128 | 39 | Ed | Dudenhoefer | Seattle | Washington |
135 | 180 | Katie | McAskill-White | Seattle | Washington |
137 | 152 | Yuhong | Li | Seattle | Washington |
138 | 171 | David | Yalovsky | Seattle | Washington |
143 | 36 | Jose | Lugo | Seattle | Washington |
149 | 60 | Pete | Male | Seattle | Washington |
150 | 120 | Kitti | Lertpiriyasuwat | Seattle | Washington |
152 | 121 | Pilar | Ackerman | Seattle | Washington |
157 | 77 | Merav | Netz | Seattle | Washington |
167 | 74 | Bjorn | Rettig | Seattle | Washington |
181 | 71 | Michael | Ray | Seattle | Washington |
183 | 102 | Zheng | Mu | Seattle | Washington |
186 | 165 | Chris | Preston | Seattle | Washington |
189 | 190 | Robert | Rounthwaite | Seattle | Washington |
199 | 40 | JoLynn | Dobney | Seattle | Washington |
200 | 193 | Alejandro | McGuel | Seattle | Washington |
203 | 87 | Cristian | Petculescu | Seattle | Washington |
210 | 208 | Scott | Gode | Seattle | Washington |
212 | 58 | Kendall | Keil | Seattle | Washington |
216 | 195 | Kevin | Liu | Seattle | Washington |
217 | 94 | Russell | Hunter | Seattle | Washington |
220 | 157 | Linda | Randall | Seattle | Washington |
222 | 191 | Lionel | Penuchot | Seattle | Washington |
226 | 33 | Annik | Stahl | Seattle | Washington |
240 | 200 | Frank | Lee | Seattle | Washington |
249 | 91 | Kimberly | Zimmerman | Seattle | Washington |
252 | 82 | Jack | Creasey | Seattle | Washington |
262 | 70 | David | Ortiz | Seattle | Washington |
271 | 83 | Patrick | Cook | Seattle | Washington |
288 | 202 | Tawana | Nusbaum | Seattle | Washington |
6. Provide a list of employees who have no title, whether it's a NULL or empty string.
SELECT e.EmployeeID, e.Title, e.FirstName, e.LastName
FROM dbo.Employees AS e
WHERE e.Title IS NULL OR e.Title = ''
;
Out[95]:
| EmployeeID | Title | FirstName | LastName |
85 | 24 | Ms. | Jill | Williams |
93 | 13 | Ms. | Janice | Galvin |
115 | 284 | Mr. | Tete | Mensa-Annan |
159 | 6 | Mr. | Jossef | Goldberg |
198 | 139 | Mr. | Hung-Fu | Ting |
213 | 285 | Mr. | Syed | Abbas |
223 | 273 | Mr. | Brian | Welcker |
267 | 5 | Ms. | Gail | Erickson |
7a. Provide a list of employees who have at least 60 vacation hours left.
SELECT e.EmployeeID, e.Title, e.FirstName, e.LastName, e.VacationHours, e.SickLeaveHours
FROM dbo.Employees AS e
WHERE e.VacationHours >= 60
;
Out[97]:
| EmployeeID | FirstName | LastName | VacationHours |
4 | 78 | Reuben | D'sa | 72 |
7 | 270 | François | Ajenstat | 67 |
9 | 161 | Kirk | Koenigsbauer | 74 |
10 | 124 | Kim | Ralls | 98 |
16 | 192 | Brenda | Diaz | 71 |
17 | 166 | Jack | Richins | 68 |
18 | 112 | John | Evans | 91 |
22 | 244 | Bryan | Walton | 62 |
24 | 210 | Belinda | Newman | 83 |
25 | 163 | Alex | Nayberg | 77 |
26 | 118 | Don | Hall | 88 |
31 | 231 | Jo | Berry | 91 |
34 | 214 | Andreas | Berglund | 84 |
36 | 162 | Laura | Steele | 75 |
37 | 107 | Christopher | Hill | 91 |
38 | 269 | Dan | Bacon | 72 |
39 | 187 | Yvonne | McKay | 79 |
42 | 116 | Michael | Patten | 98 |
45 | 133 | Michael | Rothkugel | 70 |
51 | 48 | Ruth | Ellerbrock | 83 |
52 | 126 | Jimmy | Bischoff | 96 |
53 | 230 | Stuart | Macrae | 88 |
57 | 104 | Mary | Baker | 94 |
58 | 263 | Jean | Trenary | 65 |
59 | 232 | Pat | Coleman | 89 |
61 | 81 | Mihail | Frintu | 64 |
63 | 119 | Michael | Entin | 94 |
66 | 212 | Peng | Wu | 81 |
67 | 227 | Gary | Altman | 86 |
70 | 62 | John | Campbell | 81 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
206 | 128 | Paul | Komosinski | 68 |
210 | 208 | Scott | Gode | 86 |
211 | 264 | Stephanie | Conroy | 68 |
218 | 242 | Deborah | Poe | 60 |
222 | 191 | Lionel | Penuchot | 78 |
224 | 9 | Gigi | Matthew | 63 |
230 | 216 | Sean | Alexander | 82 |
233 | 228 | Christian | Kleinerman | 92 |
237 | 85 | Brian | Goldstein | 63 |
239 | 106 | John | Kane | 92 |
243 | 103 | Ebru | Ersan | 93 |
247 | 215 | Mark | Harrington | 83 |
249 | 91 | Kimberly | Zimmerman | 95 |
251 | 207 | Greg | Alderson | 85 |
252 | 82 | Jack | Creasey | 62 |
256 | 53 | Diane | Glimp | 87 |
259 | 209 | Kathie | Flood | 87 |
260 | 50 | Sidney | Higa | 84 |
263 | 219 | Sean | Chai | 78 |
268 | 252 | Arvind | Rao | 60 |
269 | 266 | Peter | Connelly | 69 |
270 | 233 | Magnus | Hedlund | 87 |
271 | 83 | Patrick | Cook | 61 |
274 | 25 | James | Hamilton | 64 |
275 | 108 | Jinghao | Liu | 77 |
277 | 27 | Jo | Brown | 80 |
278 | 186 | Shane | Kim | 75 |
281 | 114 | Mindaugas | Krapauskas | 97 |
282 | 123 | Vamsi | Kuppa | 95 |
285 | 221 | Chris | Norred | 75 |
7b. Provide a list of employees who have less than 60 vacation hours left.
Out[98]:
| EmployeeID | FirstName | LastName | VacationHours |
1 | 259 | Ben | Miller | 55 |
2 | 278 | Garrett | Vargas | 33 |
3 | 204 | Gabe | Mares | 57 |
5 | 255 | Gordon | Hee | 52 |
6 | 66 | Karan | Khanna | 28 |
8 | 22 | Sariya | Harnpadoungsataya | 45 |
11 | 10 | Michael | Raheem | 16 |
12 | 248 | Mike | Seamans | 59 |
13 | 175 | Reed | Koch | 23 |
14 | 155 | Fadi | Fakhouri | 16 |
15 | 144 | Paul | Singh | 6 |
19 | 203 | Ken | Myer | 51 |
20 | 245 | Barbara | Moreland | 58 |
21 | 257 | Eric | Kurjan | 54 |
23 | 69 | Steve | Masters | 31 |
27 | 176 | David | Lawrence | 29 |
28 | 147 | Sandra | Reátegui Alayo | 37 |
29 | 138 | Samantha | Smith | 7 |
30 | 95 | Jim | Scardelis | 1 |
32 | 86 | Ryan | Cornelsen | 59 |
33 | 181 | Michael | Hines | 20 |
35 | 72 | Steven | Selikoff | 41 |
40 | 75 | Michiko | Osada | 44 |
41 | 12 | Thierry | D'Hers | 9 |
43 | 30 | Britta | Simon | 14 |
44 | 198 | Lorraine | Nay | 49 |
46 | 31 | Margie | Shoop | 18 |
47 | 168 | Garrett | Young | 34 |
48 | 236 | Grant | Culbertson | 53 |
49 | 3 | Roberto | Tamburello | 2 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
242 | 146 | Jian Shuo | Wang | 36 |
244 | 73 | Carole | Poland | 42 |
245 | 185 | Stefen | Hesse | 19 |
246 | 37 | Chris | Okelberry | 16 |
248 | 179 | Jan | Miksovsky | 30 |
250 | 21 | Terry | Eminhizer | 44 |
253 | 254 | Fukiko | Ogisu | 57 |
254 | 67 | Jay | Adams | 32 |
255 | 262 | David | Barber | 56 |
257 | 282 | José | Saraiva | 31 |
258 | 169 | Susan | Metters | 31 |
261 | 222 | A. Scott | Wright | 44 |
262 | 70 | David | Ortiz | 33 |
264 | 148 | Jason | Watters | 38 |
265 | 140 | Prasanna | Samarawickrama | 5 |
266 | 290 | Ranjit | Varkey Chudukatil | 34 |
267 | 5 | Gail | Erickson | 5 |
272 | 251 | Mikael | Sandberg | 59 |
273 | 241 | David | Liu | 57 |
276 | 239 | Mindy | Martin | 51 |
279 | 38 | Kim | Abercrombie | 24 |
280 | 154 | Raymond | Sam | 10 |
283 | 29 | Mark | McArthur | 19 |
284 | 101 | Houman | Pournasseh | 3 |
286 | 18 | John | Wood | 48 |
287 | 46 | Eugene | Kogan | 36 |
288 | 202 | Tawana | Nusbaum | 50 |
289 | 289 | Jae | Pak | 37 |
290 | 288 | Rachel | Valdez | 35 |
291 | 999 | Chadwick | Smith | 55 |
7c. Show me employees who have more than 20 and less than 60 vacation hours left.
SELECT e.EmployeeID, e.Title, e.FirstName, e.LastName, e.VacationHours, e.SickLeaveHours
FROM dbo.Employees AS e
WHERE e.VacationHours BETWEEN 21 AND 59
;
Out[100]:
| EmployeeID | FirstName | LastName | VacationHours |
1 | 259 | Ben | Miller | 55 |
2 | 278 | Garrett | Vargas | 33 |
3 | 204 | Gabe | Mares | 57 |
5 | 255 | Gordon | Hee | 52 |
6 | 66 | Karan | Khanna | 28 |
8 | 22 | Sariya | Harnpadoungsataya | 45 |
12 | 248 | Mike | Seamans | 59 |
13 | 175 | Reed | Koch | 23 |
19 | 203 | Ken | Myer | 51 |
20 | 245 | Barbara | Moreland | 58 |
21 | 257 | Eric | Kurjan | 54 |
23 | 69 | Steve | Masters | 31 |
27 | 176 | David | Lawrence | 29 |
28 | 147 | Sandra | Reátegui Alayo | 37 |
32 | 86 | Ryan | Cornelsen | 59 |
35 | 72 | Steven | Selikoff | 41 |
40 | 75 | Michiko | Osada | 44 |
44 | 198 | Lorraine | Nay | 49 |
47 | 168 | Garrett | Young | 34 |
48 | 236 | Grant | Culbertson | 53 |
50 | 199 | Paula | Nartker | 54 |
54 | 41 | Bryan | Baker | 35 |
55 | 283 | David | Campbell | 23 |
56 | 226 | Brian | LaMee | 48 |
62 | 172 | Marc | Ingle | 22 |
64 | 276 | Linda | Mitchell | 27 |
65 | 225 | Alan | Brewer | 47 |
69 | 194 | Fred | Northup | 47 |
73 | 150 | Michael | Vanderhyde | 35 |
74 | 258 | Erin | Hagens | 53 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
229 | 173 | Eugene | Zabokritski | 33 |
231 | 277 | Jillian | Carson | 24 |
232 | 35 | Brandon | Heidepriem | 22 |
234 | 223 | Sairaj | Uddin | 46 |
236 | 275 | Michael | Blythe | 38 |
240 | 200 | Frank | Lee | 56 |
241 | 279 | Tsvi | Reiter | 29 |
242 | 146 | Jian Shuo | Wang | 36 |
244 | 73 | Carole | Poland | 42 |
248 | 179 | Jan | Miksovsky | 30 |
250 | 21 | Terry | Eminhizer | 44 |
253 | 254 | Fukiko | Ogisu | 57 |
254 | 67 | Jay | Adams | 32 |
255 | 262 | David | Barber | 56 |
257 | 282 | José | Saraiva | 31 |
258 | 169 | Susan | Metters | 31 |
261 | 222 | A. Scott | Wright | 44 |
262 | 70 | David | Ortiz | 33 |
264 | 148 | Jason | Watters | 38 |
266 | 290 | Ranjit | Varkey Chudukatil | 34 |
272 | 251 | Mikael | Sandberg | 59 |
273 | 241 | David | Liu | 57 |
276 | 239 | Mindy | Martin | 51 |
279 | 38 | Kim | Abercrombie | 24 |
286 | 18 | John | Wood | 48 |
287 | 46 | Eugene | Kogan | 36 |
288 | 202 | Tawana | Nusbaum | 50 |
289 | 289 | Jae | Pak | 37 |
290 | 288 | Rachel | Valdez | 35 |
291 | 999 | Chadwick | Smith | 55 |
7d. If you did not use BETWEEN for 7c, do the same but use BETWEEN. If you did use BETWEEN for 7c, do it another way.
SELECT e.EmployeeID, e.Title, e.FirstName, e.LastName, e.VacationHours, e.SickLeaveHours
FROM dbo.Employees AS e
WHERE e.VacationHours > 20 AND e.VacationHours < 60
;
Out[102]:
| EmployeeID | FirstName | LastName | VacationHours |
1 | 259 | Ben | Miller | 55 |
2 | 278 | Garrett | Vargas | 33 |
3 | 204 | Gabe | Mares | 57 |
5 | 255 | Gordon | Hee | 52 |
6 | 66 | Karan | Khanna | 28 |
8 | 22 | Sariya | Harnpadoungsataya | 45 |
12 | 248 | Mike | Seamans | 59 |
13 | 175 | Reed | Koch | 23 |
19 | 203 | Ken | Myer | 51 |
20 | 245 | Barbara | Moreland | 58 |
21 | 257 | Eric | Kurjan | 54 |
23 | 69 | Steve | Masters | 31 |
27 | 176 | David | Lawrence | 29 |
28 | 147 | Sandra | Reátegui Alayo | 37 |
32 | 86 | Ryan | Cornelsen | 59 |
35 | 72 | Steven | Selikoff | 41 |
40 | 75 | Michiko | Osada | 44 |
44 | 198 | Lorraine | Nay | 49 |
47 | 168 | Garrett | Young | 34 |
48 | 236 | Grant | Culbertson | 53 |
50 | 199 | Paula | Nartker | 54 |
54 | 41 | Bryan | Baker | 35 |
55 | 283 | David | Campbell | 23 |
56 | 226 | Brian | LaMee | 48 |
62 | 172 | Marc | Ingle | 22 |
64 | 276 | Linda | Mitchell | 27 |
65 | 225 | Alan | Brewer | 47 |
69 | 194 | Fred | Northup | 47 |
73 | 150 | Michael | Vanderhyde | 35 |
74 | 258 | Erin | Hagens | 53 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
229 | 173 | Eugene | Zabokritski | 33 |
231 | 277 | Jillian | Carson | 24 |
232 | 35 | Brandon | Heidepriem | 22 |
234 | 223 | Sairaj | Uddin | 46 |
236 | 275 | Michael | Blythe | 38 |
240 | 200 | Frank | Lee | 56 |
241 | 279 | Tsvi | Reiter | 29 |
242 | 146 | Jian Shuo | Wang | 36 |
244 | 73 | Carole | Poland | 42 |
248 | 179 | Jan | Miksovsky | 30 |
250 | 21 | Terry | Eminhizer | 44 |
253 | 254 | Fukiko | Ogisu | 57 |
254 | 67 | Jay | Adams | 32 |
255 | 262 | David | Barber | 56 |
257 | 282 | José | Saraiva | 31 |
258 | 169 | Susan | Metters | 31 |
261 | 222 | A. Scott | Wright | 44 |
262 | 70 | David | Ortiz | 33 |
264 | 148 | Jason | Watters | 38 |
266 | 290 | Ranjit | Varkey Chudukatil | 34 |
272 | 251 | Mikael | Sandberg | 59 |
273 | 241 | David | Liu | 57 |
276 | 239 | Mindy | Martin | 51 |
279 | 38 | Kim | Abercrombie | 24 |
286 | 18 | John | Wood | 48 |
287 | 46 | Eugene | Kogan | 36 |
288 | 202 | Tawana | Nusbaum | 50 |
289 | 289 | Jae | Pak | 37 |
290 | 288 | Rachel | Valdez | 35 |
291 | 999 | Chadwick | Smith | 55 |
1a. What is the earliest birthdate for all employees?
SELECT MIN(e.BirthDate) FROM dbo.Employees AS e;
1b. Add to the above, the most recent birthdate for all employees
SELECT MIN(e.BirthDate) AS 'Earliest Birthday', MAX(e.BirthDate) AS 'Most Reecent Birthday'
FROM dbo.Employees AS e;
1c. Show the above results broken down by gender
SELECT e.Gender, MIN(e.BirthDate) AS 'Earliest Birthday', MAX(e.BirthDate) AS 'Most Reecent Birthday'
FROM dbo.Employees AS e
GROUP BY e.Gender
;
Out[174]:
| Gender | BD |
1 | F | 1946-10-29 | 1985-07-01 |
2 | M | 1945-11-17 | 1985-05-07 |
The above solution creates only two columns.
Out[189]:
| Gender | EarliestBirthdate | MostRecentBirthdate |
1 | F | 1946-10-29 | 1985-07-01 |
2 | M | 1945-11-17 | 1985-05-07 |
1d. Show the above results broken down by gender, and salaried/hourly
SELECT e.Gender, e.SalariedFlag, MIN(e.BirthDate) AS 'Earliest Birthday', MAX(e.BirthDate) AS 'Most Reecent Birthday'
FROM dbo.Employees AS e
GROUP BY e.Gender, e.SalariedFlag
;
Out[191]:
| Gender | SalariedFlag | BD |
1 | F | 0 | 1948-05-25 | 1985-07-01 |
2 | M | 0 | 1946-04-03 | 1985-05-07 |
3 | F | 1 | 1946-10-29 | 1980-07-06 |
4 | M | 1 | 1945-11-17 | 1982-04-14 |
2a. What are the average vacation hours for all employees?
SELECT AVG(e.VacationHours)
FROM dbo.Employees AS e
;
Out[192]:
50.6288659793814
2b. Add to the above, the minimum vacation hours for all employees
SELECT AVG(e.VacationHours) AS 'Average Vacation', MIN(e.VacationHours) AS 'Minumum Vacation'
FROM dbo.Employees AS e
;
2c. Show the above results broken down and ordered by job title
SELECT e.JobTitle, AVG(e.VacationHours) AS 'Average Vacation', MIN(e.VacationHours) AS 'Minimum Vacation'
FROM dbo.Employees AS e
GROUP BY e.JobTitle
;
Out[194]:
| JobTitle | VacationHours |
1 | Accountant | 58.5 | 58 |
2 | Accounts Manager | 57 | 57 |
3 | Accounts Payable Specialist | 63.5 | 63 |
4 | Accounts Receivable Specialist | 61 | 60 |
5 | Application Specialist | 72.5 | 71 |
6 | Assistant to the Chief Financial Officer | 56 | 56 |
7 | Benefits Specialist | 51 | 51 |
8 | BI Professor | 55 | 55 |
9 | Buyer | 56 | 52 |
10 | Chief Executive Officer | 99 | 99 |
11 | Chief Financial Officer | 0 | 0 |
12 | Control Specialist | 75.5 | 75 |
13 | Database Administrator | 66.5 | 66 |
14 | Design Engineer | 5 | 4 |
15 | Document Control Assistant | 78.5 | 78 |
16 | Document Control Manager | 77 | 77 |
17 | Engineering Manager | 2 | 2 |
18 | European Sales Manager | 21 | 21 |
19 | Facilities Administrative Assistant | 87 | 87 |
20 | Facilities Manager | 86 | 86 |
21 | Finance Manager | 55 | 55 |
22 | Human Resources Administrative Assistant | 52.5 | 52 |
23 | Human Resources Manager | 54 | 54 |
24 | Information Services Manager | 65 | 65 |
25 | Janitor | 89.5 | 88 |
26 | Maintenance Supervisor | 92 | 92 |
27 | Marketing Assistant | 42 | 41 |
28 | Marketing Manager | 40 | 40 |
29 | Marketing Specialist | 46 | 44 |
30 | Master Scheduler | 44 | 44 |
⋮ | ⋮ | ⋮ | ⋮ |
39 | Production Supervisor - WC40 | 72 | 71 |
40 | Production Supervisor - WC45 | 75 | 74 |
41 | Production Supervisor - WC50 | 78 | 77 |
42 | Production Supervisor - WC60 | 81 | 80 |
43 | Production Technician - WC10 | 91 | 83 |
44 | Production Technician - WC20 | 10.5 | 0 |
45 | Production Technician - WC30 | 34 | 22 |
46 | Production Technician - WC40 | 59.5 | 47 |
47 | Production Technician - WC45 | 80 | 73 |
48 | Production Technician - WC50 | 46.6538461538462 | 0 |
49 | Production Technician - WC60 | 26.5 | 14 |
50 | Purchasing Assistant | 50.5 | 50 |
51 | Purchasing Manager | 49 | 49 |
52 | Quality Assurance Manager | 80 | 80 |
53 | Quality Assurance Supervisor | 81 | 81 |
54 | Quality Assurance Technician | 83.5 | 82 |
55 | Recruiter | 49.5 | 49 |
56 | Research and Development Engineer | 62.5 | 62 |
57 | Research and Development Manager | 38.5 | 16 |
58 | Sales Representative | 31 | 22 |
59 | Scheduling Assistant | 46.5 | 45 |
60 | Senior Design Engineer | 3 | 3 |
61 | Senior Tool Designer | 27.5 | 7 |
62 | Shipping and Receiving Clerk | 94.5 | 94 |
63 | Shipping and Receiving Supervisor | 93 | 93 |
64 | Stocker | 97 | 96 |
65 | Tool Designer | 8.5 | 8 |
66 | Vice President of Engineering | 1 | 1 |
67 | Vice President of Production | 64 | 64 |
68 | Vice President of Sales | 10 | 10 |
2d. Show the above results broken down by job title, and married/single employees
SELECT e.JobTitle, e.MaritalStatus, AVG(e.VacationHours) AS 'Average Vacation', MIN(e.VacationHours) AS 'Minimum Vacation'
FROM dbo.Employees AS e
GROUP BY e.JobTitle, e.MaritalStatus
;
Out[196]:
| JobTitle | MaritalStatus | VacationHours |
1 | Accountant | M | 58 | 58 |
2 | Accounts Manager | M | 57 | 57 |
3 | Accounts Payable Specialist | M | 63.5 | 63 |
4 | Accounts Receivable Specialist | M | 60 | 60 |
5 | Application Specialist | M | 71.5 | 71 |
6 | Benefits Specialist | M | 51 | 51 |
7 | BI Professor | M | 55 | 55 |
8 | Buyer | M | 56.3333333333333 | 52 |
9 | Chief Financial Officer | M | 0 | 0 |
10 | Control Specialist | M | 75 | 75 |
11 | Database Administrator | M | 66 | 66 |
12 | Design Engineer | M | 5 | 4 |
13 | Document Control Assistant | M | 79 | 79 |
14 | Document Control Manager | M | 77 | 77 |
15 | Engineering Manager | M | 2 | 2 |
16 | European Sales Manager | M | 21 | 21 |
17 | Facilities Administrative Assistant | M | 87 | 87 |
18 | Facilities Manager | M | 86 | 86 |
19 | Human Resources Manager | M | 54 | 54 |
20 | Janitor | M | 89.6666666666667 | 88 |
21 | Maintenance Supervisor | M | 92 | 92 |
22 | Marketing Assistant | M | 41 | 41 |
23 | Marketing Specialist | M | 45.6666666666667 | 44 |
24 | North American Sales Manager | M | 14 | 14 |
25 | Pacific Sales Manager | M | 20 | 20 |
26 | Production Control Manager | M | 43 | 43 |
27 | Production Supervisor - WC10 | M | 67 | 67 |
28 | Production Supervisor - WC20 | M | 40.5 | 40 |
29 | Production Supervisor - WC40 | M | 71.5 | 71 |
30 | Production Supervisor - WC45 | M | 74 | 74 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
66 | Master Scheduler | S | 44 | 44 |
67 | Network Administrator | S | 69.5 | 69 |
68 | Network Manager | S | 68 | 68 |
69 | Production Supervisor - WC10 | S | 65.5 | 65 |
70 | Production Supervisor - WC20 | S | 42 | 42 |
71 | Production Supervisor - WC30 | S | 69 | 68 |
72 | Production Supervisor - WC40 | S | 73 | 73 |
73 | Production Supervisor - WC45 | S | 75.5 | 75 |
74 | Production Supervisor - WC50 | S | 77.5 | 77 |
75 | Production Supervisor - WC60 | S | 81 | 80 |
76 | Production Technician - WC10 | S | 92.7777777777778 | 85 |
77 | Production Technician - WC20 | S | 11.9166666666667 | 0 |
78 | Production Technician - WC30 | S | 33.4166666666667 | 25 |
79 | Production Technician - WC40 | S | 61.8571428571429 | 47 |
80 | Production Technician - WC45 | S | 79.4444444444444 | 73 |
81 | Production Technician - WC50 | S | 53.2307692307692 | 0 |
82 | Production Technician - WC60 | S | 28.5833333333333 | 16 |
83 | Purchasing Manager | S | 49 | 49 |
84 | Quality Assurance Manager | S | 80 | 80 |
85 | Quality Assurance Technician | S | 82.5 | 82 |
86 | Recruiter | S | 49.5 | 49 |
87 | Research and Development Engineer | S | 62 | 62 |
88 | Sales Representative | S | 30.2857142857143 | 22 |
89 | Senior Design Engineer | S | 3 | 3 |
90 | Senior Tool Designer | S | 27.5 | 7 |
91 | Shipping and Receiving Supervisor | S | 93 | 93 |
92 | Stocker | S | 97.5 | 97 |
93 | Vice President of Engineering | S | 1 | 1 |
94 | Vice President of Production | S | 64 | 64 |
95 | Vice President of Sales | S | 10 | 10 |
2e. Add to the above, the maximum vacation hours per group
SELECT e.JobTitle, e.MaritalStatus
, AVG(e.VacationHours) AS 'Average Vacation'
, MIN(e.VacationHours) AS 'Minimum Vacation'
, MAX(e.VacationHours) AS 'Maximum Vacation'
FROM dbo.Employees AS e
GROUP BY e.JobTitle, e.MaritalStatus
;
Out[197]:
| JobTitle | MaritalStatus | VacationHours |
1 | Accountant | M | 58 | 58 | 58 |
2 | Accounts Manager | M | 57 | 57 | 57 |
3 | Accounts Payable Specialist | M | 63.5 | 63 | 64 |
4 | Accounts Receivable Specialist | M | 60 | 60 | 60 |
5 | Application Specialist | M | 71.5 | 71 | 72 |
6 | Benefits Specialist | M | 51 | 51 | 51 |
7 | BI Professor | M | 55 | 55 | 55 |
8 | Buyer | M | 56.3333333333333 | 52 | 60 |
9 | Chief Financial Officer | M | 0 | 0 | 0 |
10 | Control Specialist | M | 75 | 75 | 75 |
11 | Database Administrator | M | 66 | 66 | 66 |
12 | Design Engineer | M | 5 | 4 | 6 |
13 | Document Control Assistant | M | 79 | 79 | 79 |
14 | Document Control Manager | M | 77 | 77 | 77 |
15 | Engineering Manager | M | 2 | 2 | 2 |
16 | European Sales Manager | M | 21 | 21 | 21 |
17 | Facilities Administrative Assistant | M | 87 | 87 | 87 |
18 | Facilities Manager | M | 86 | 86 | 86 |
19 | Human Resources Manager | M | 54 | 54 | 54 |
20 | Janitor | M | 89.6666666666667 | 88 | 91 |
21 | Maintenance Supervisor | M | 92 | 92 | 92 |
22 | Marketing Assistant | M | 41 | 41 | 41 |
23 | Marketing Specialist | M | 45.6666666666667 | 44 | 47 |
24 | North American Sales Manager | M | 14 | 14 | 14 |
25 | Pacific Sales Manager | M | 20 | 20 | 20 |
26 | Production Control Manager | M | 43 | 43 | 43 |
27 | Production Supervisor - WC10 | M | 67 | 67 | 67 |
28 | Production Supervisor - WC20 | M | 40.5 | 40 | 41 |
29 | Production Supervisor - WC40 | M | 71.5 | 71 | 72 |
30 | Production Supervisor - WC45 | M | 74 | 74 | 74 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
66 | Master Scheduler | S | 44 | 44 | 44 |
67 | Network Administrator | S | 69.5 | 69 | 70 |
68 | Network Manager | S | 68 | 68 | 68 |
69 | Production Supervisor - WC10 | S | 65.5 | 65 | 66 |
70 | Production Supervisor - WC20 | S | 42 | 42 | 42 |
71 | Production Supervisor - WC30 | S | 69 | 68 | 70 |
72 | Production Supervisor - WC40 | S | 73 | 73 | 73 |
73 | Production Supervisor - WC45 | S | 75.5 | 75 | 76 |
74 | Production Supervisor - WC50 | S | 77.5 | 77 | 78 |
75 | Production Supervisor - WC60 | S | 81 | 80 | 82 |
76 | Production Technician - WC10 | S | 92.7777777777778 | 85 | 99 |
77 | Production Technician - WC20 | S | 11.9166666666667 | 0 | 21 |
78 | Production Technician - WC30 | S | 33.4166666666667 | 25 | 44 |
79 | Production Technician - WC40 | S | 61.8571428571429 | 47 | 72 |
80 | Production Technician - WC45 | S | 79.4444444444444 | 73 | 85 |
81 | Production Technician - WC50 | S | 53.2307692307692 | 0 | 98 |
82 | Production Technician - WC60 | S | 28.5833333333333 | 16 | 38 |
83 | Purchasing Manager | S | 49 | 49 | 49 |
84 | Quality Assurance Manager | S | 80 | 80 | 80 |
85 | Quality Assurance Technician | S | 82.5 | 82 | 83 |
86 | Recruiter | S | 49.5 | 49 | 50 |
87 | Research and Development Engineer | S | 62 | 62 | 62 |
88 | Sales Representative | S | 30.2857142857143 | 22 | 38 |
89 | Senior Design Engineer | S | 3 | 3 | 3 |
90 | Senior Tool Designer | S | 27.5 | 7 | 48 |
91 | Shipping and Receiving Supervisor | S | 93 | 93 | 93 |
92 | Stocker | S | 97.5 | 97 | 98 |
93 | Vice President of Engineering | S | 1 | 1 | 1 |
94 | Vice President of Production | S | 64 | 64 | 64 |
95 | Vice President of Sales | S | 10 | 10 | 10 |
2f. Show the above results broken down by job title, married/single employees, and State
SELECT e.JobTitle, e.MaritalStatus, e.StateProvinceName
, AVG(e.VacationHours) AS 'Average Vacation'
, MIN(e.VacationHours) AS 'Minimum Vacation'
, MAX(e.VacationHours) AS 'Maximum Vacation'
FROM dbo.Employees AS e
GROUP BY e.JobTitle, e.MaritalStatus, e.StateProvinceName
;
Out[198]:
| JobTitle | MaritalStatus | StateProvinceName | VacationHours |
1 | Production Technician - WC20 | M | Alberta | 9 | 9 | 9 |
2 | Production Technician - WC40 | M | Alberta | 51 | 51 | 51 |
3 | Sales Representative | M | Alberta | 33 | 33 | 33 |
4 | Accounts Receivable Specialist | S | Alberta | 62 | 62 | 62 |
5 | Production Technician - WC20 | S | Alberta | 20 | 20 | 20 |
6 | Research and Development Manager | M | California | 16 | 16 | 16 |
7 | Sales Representative | M | California | 26 | 26 | 26 |
8 | Sales Representative | M | England | 37 | 37 | 37 |
9 | BI Professor | M | Georgia | 55 | 55 | 55 |
10 | Sales Representative | S | Gironde | 34 | 34 | 34 |
11 | Sales Representative | S | Hamburg | 35 | 35 | 35 |
12 | Sales Representative | M | Massachusetts | 39 | 39 | 39 |
13 | Sales Representative | S | Michigan | 38 | 38 | 38 |
14 | Sales Representative | S | Minnesota | 24 | 24 | 24 |
15 | Senior Tool Designer | S | Minnesota | 48 | 48 | 48 |
16 | Sales Representative | M | Ontario | 31 | 31 | 31 |
17 | Sales Representative | S | Oregon | 22 | 22 | 22 |
18 | Sales Representative | M | Tennessee | 29 | 29 | 29 |
19 | Sales Representative | M | Utah | 27 | 27 | 27 |
20 | Sales Representative | S | Victoria | 36 | 36 | 36 |
21 | Accountant | M | Washington | 58 | 58 | 58 |
22 | Accounts Manager | M | Washington | 57 | 57 | 57 |
23 | Accounts Payable Specialist | M | Washington | 63.5 | 63 | 64 |
24 | Accounts Receivable Specialist | M | Washington | 60 | 60 | 60 |
25 | Application Specialist | M | Washington | 71.5 | 71 | 72 |
26 | Benefits Specialist | M | Washington | 51 | 51 | 51 |
27 | Buyer | M | Washington | 56.3333333333333 | 52 | 60 |
28 | Chief Financial Officer | M | Washington | 0 | 0 | 0 |
29 | Control Specialist | M | Washington | 75 | 75 | 75 |
30 | Database Administrator | M | Washington | 66 | 66 | 66 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
84 | Master Scheduler | S | Washington | 44 | 44 | 44 |
85 | Network Administrator | S | Washington | 69.5 | 69 | 70 |
86 | Network Manager | S | Washington | 68 | 68 | 68 |
87 | Production Supervisor - WC10 | S | Washington | 65.5 | 65 | 66 |
88 | Production Supervisor - WC20 | S | Washington | 42 | 42 | 42 |
89 | Production Supervisor - WC30 | S | Washington | 69 | 68 | 70 |
90 | Production Supervisor - WC40 | S | Washington | 73 | 73 | 73 |
91 | Production Supervisor - WC45 | S | Washington | 75.5 | 75 | 76 |
92 | Production Supervisor - WC50 | S | Washington | 77.5 | 77 | 78 |
93 | Production Supervisor - WC60 | S | Washington | 81 | 80 | 82 |
94 | Production Technician - WC10 | S | Washington | 92.7777777777778 | 85 | 99 |
95 | Production Technician - WC20 | S | Washington | 11.1818181818182 | 0 | 21 |
96 | Production Technician - WC30 | S | Washington | 33.4166666666667 | 25 | 44 |
97 | Production Technician - WC40 | S | Washington | 61.8571428571429 | 47 | 72 |
98 | Production Technician - WC45 | S | Washington | 79.4444444444444 | 73 | 85 |
99 | Production Technician - WC50 | S | Washington | 53.2307692307692 | 0 | 98 |
100 | Production Technician - WC60 | S | Washington | 28.5833333333333 | 16 | 38 |
101 | Purchasing Manager | S | Washington | 49 | 49 | 49 |
102 | Quality Assurance Manager | S | Washington | 80 | 80 | 80 |
103 | Quality Assurance Technician | S | Washington | 82.5 | 82 | 83 |
104 | Recruiter | S | Washington | 49.5 | 49 | 50 |
105 | Research and Development Engineer | S | Washington | 62 | 62 | 62 |
106 | Sales Representative | S | Washington | 23 | 23 | 23 |
107 | Senior Design Engineer | S | Washington | 3 | 3 | 3 |
108 | Senior Tool Designer | S | Washington | 7 | 7 | 7 |
109 | Shipping and Receiving Supervisor | S | Washington | 93 | 93 | 93 |
110 | Stocker | S | Washington | 97.5 | 97 | 98 |
111 | Vice President of Engineering | S | Washington | 1 | 1 | 1 |
112 | Vice President of Production | S | Washington | 64 | 64 | 64 |
113 | Vice President of Sales | S | Washington | 10 | 10 | 10 |
2g. Show the above results but only for American employees
SELECT e.JobTitle, e.MaritalStatus, e.StateProvinceName
, AVG(e.VacationHours) AS 'Average Vacation'
, MIN(e.VacationHours) AS 'Minimum Vacation'
, MAX(e.VacationHours) AS 'Maximum Vacation'
FROM dbo.Employees AS e
WHERE e.CountryName = 'United States'
GROUP BY e.JobTitle, e.MaritalStatus, e.StateProvinceName
;
Out[200]:
| JobTitle | MaritalStatus | StateProvinceName | VacationHours |
1 | Research and Development Manager | M | California | 16 | 16 | 16 |
2 | Sales Representative | M | California | 26 | 26 | 26 |
3 | BI Professor | M | Georgia | 55 | 55 | 55 |
4 | Sales Representative | M | Massachusetts | 39 | 39 | 39 |
5 | Sales Representative | S | Michigan | 38 | 38 | 38 |
6 | Sales Representative | S | Minnesota | 24 | 24 | 24 |
7 | Senior Tool Designer | S | Minnesota | 48 | 48 | 48 |
8 | Sales Representative | S | Oregon | 22 | 22 | 22 |
9 | Sales Representative | M | Tennessee | 29 | 29 | 29 |
10 | Sales Representative | M | Utah | 27 | 27 | 27 |
11 | Accountant | M | Washington | 58 | 58 | 58 |
12 | Accounts Manager | M | Washington | 57 | 57 | 57 |
13 | Accounts Payable Specialist | M | Washington | 63.5 | 63 | 64 |
14 | Accounts Receivable Specialist | M | Washington | 60 | 60 | 60 |
15 | Application Specialist | M | Washington | 71.5 | 71 | 72 |
16 | Benefits Specialist | M | Washington | 51 | 51 | 51 |
17 | Buyer | M | Washington | 56.3333333333333 | 52 | 60 |
18 | Chief Financial Officer | M | Washington | 0 | 0 | 0 |
19 | Control Specialist | M | Washington | 75 | 75 | 75 |
20 | Database Administrator | M | Washington | 66 | 66 | 66 |
21 | Design Engineer | M | Washington | 5 | 4 | 6 |
22 | Document Control Assistant | M | Washington | 79 | 79 | 79 |
23 | Document Control Manager | M | Washington | 77 | 77 | 77 |
24 | Engineering Manager | M | Washington | 2 | 2 | 2 |
25 | European Sales Manager | M | Washington | 21 | 21 | 21 |
26 | Facilities Administrative Assistant | M | Washington | 87 | 87 | 87 |
27 | Facilities Manager | M | Washington | 86 | 86 | 86 |
28 | Human Resources Manager | M | Washington | 54 | 54 | 54 |
29 | Janitor | M | Washington | 89.6666666666667 | 88 | 91 |
30 | Maintenance Supervisor | M | Washington | 92 | 92 | 92 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
74 | Master Scheduler | S | Washington | 44 | 44 | 44 |
75 | Network Administrator | S | Washington | 69.5 | 69 | 70 |
76 | Network Manager | S | Washington | 68 | 68 | 68 |
77 | Production Supervisor - WC10 | S | Washington | 65.5 | 65 | 66 |
78 | Production Supervisor - WC20 | S | Washington | 42 | 42 | 42 |
79 | Production Supervisor - WC30 | S | Washington | 69 | 68 | 70 |
80 | Production Supervisor - WC40 | S | Washington | 73 | 73 | 73 |
81 | Production Supervisor - WC45 | S | Washington | 75.5 | 75 | 76 |
82 | Production Supervisor - WC50 | S | Washington | 77.5 | 77 | 78 |
83 | Production Supervisor - WC60 | S | Washington | 81 | 80 | 82 |
84 | Production Technician - WC10 | S | Washington | 92.7777777777778 | 85 | 99 |
85 | Production Technician - WC20 | S | Washington | 11.1818181818182 | 0 | 21 |
86 | Production Technician - WC30 | S | Washington | 33.4166666666667 | 25 | 44 |
87 | Production Technician - WC40 | S | Washington | 61.8571428571429 | 47 | 72 |
88 | Production Technician - WC45 | S | Washington | 79.4444444444444 | 73 | 85 |
89 | Production Technician - WC50 | S | Washington | 53.2307692307692 | 0 | 98 |
90 | Production Technician - WC60 | S | Washington | 28.5833333333333 | 16 | 38 |
91 | Purchasing Manager | S | Washington | 49 | 49 | 49 |
92 | Quality Assurance Manager | S | Washington | 80 | 80 | 80 |
93 | Quality Assurance Technician | S | Washington | 82.5 | 82 | 83 |
94 | Recruiter | S | Washington | 49.5 | 49 | 50 |
95 | Research and Development Engineer | S | Washington | 62 | 62 | 62 |
96 | Sales Representative | S | Washington | 23 | 23 | 23 |
97 | Senior Design Engineer | S | Washington | 3 | 3 | 3 |
98 | Senior Tool Designer | S | Washington | 7 | 7 | 7 |
99 | Shipping and Receiving Supervisor | S | Washington | 93 | 93 | 93 |
100 | Stocker | S | Washington | 97.5 | 97 | 98 |
101 | Vice President of Engineering | S | Washington | 1 | 1 | 1 |
102 | Vice President of Production | S | Washington | 64 | 64 | 64 |
103 | Vice President of Sales | S | Washington | 10 | 10 | 10 |
2h. Change the grouping above so it's broken down by married/single and State, no more job title
SELECT e.MaritalStatus, e.StateProvinceName
, AVG(e.VacationHours) AS 'Average Vacation'
, MIN(e.VacationHours) AS 'Minimum Vacation'
, MAX(e.VacationHours) AS 'Maximum Vacation'
FROM dbo.Employees AS e
WHERE e.CountryName = 'United States'
GROUP BY e.MaritalStatus, e.StateProvinceName
;
Out[201]:
| MaritalStatus | StateProvinceName | VacationHours |
1 | M | California | 21 | 16 | 26 |
2 | M | Georgia | 55 | 55 | 55 |
3 | M | Massachusetts | 39 | 39 | 39 |
4 | S | Michigan | 38 | 38 | 38 |
5 | S | Minnesota | 36 | 24 | 48 |
6 | S | Oregon | 22 | 22 | 22 |
7 | M | Tennessee | 29 | 29 | 29 |
8 | M | Utah | 27 | 27 | 27 |
9 | M | Washington | 49.4264705882353 | 0 | 99 |
10 | S | Washington | 54.362962962963 | 0 | 99 |
2i. Limit the results above to States where the average vacation hours is greater than 30
SELECT e.MaritalStatus, e.StateProvinceName
, AVG(e.VacationHours) AS 'Average Vacation'
, MIN(e.VacationHours) AS 'Minimum Vacation'
, MAX(e.VacationHours) AS 'Maximum Vacation'
FROM dbo.Employees AS e
WHERE e.CountryName = 'United States'
GROUP BY e.MaritalStatus, e.StateProvinceName
HAVING AVG(e.VacationHours) > 30
;
SELECT e.StateProvinceName
, AVG(e.VacationHours) AS 'Average Vacation'
, MIN(e.VacationHours) AS 'Minimum Vacation'
, MAX(e.VacationHours) AS 'Maximum Vacation'
FROM dbo.Employees AS e
WHERE e.CountryName = 'United States'
GROUP BY e.StateProvinceName
HAVING AVG(e.VacationHours) > 30
;
Out[206]:
| MaritalStatus | StateProvinceName | VacationHours |
2 | M | Georgia | 55 | 55 | 55 |
3 | M | Massachusetts | 39 | 39 | 39 |
4 | S | Michigan | 38 | 38 | 38 |
5 | S | Minnesota | 36 | 24 | 48 |
9 | M | Washington | 49.4264705882353 | 0 | 99 |
10 | S | Washington | 54.362962962963 | 0 | 99 |
2j. Limit the results above to States where the average vacation hours is greater than 30 and the maximum vacation hours is less than 50
SELECT e.StateProvinceName
, AVG(e.VacationHours) AS 'Average Vacation'
, MIN(e.VacationHours) AS 'Minimum Vacation'
, MAX(e.VacationHours) AS 'Maximum Vacation'
FROM dbo.Employees AS e
WHERE e.CountryName = 'United States'
GROUP BY e.StateProvinceName
HAVING AVG(e.VacationHours) BETWEEN 31 AND 49
;
Out[209]:
| MaritalStatus | StateProvinceName | VacationHours |
3 | M | Massachusetts | 39 | 39 | 39 |
4 | S | Michigan | 38 | 38 | 38 |
5 | S | Minnesota | 36 | 24 | 48 |
9 | M | Washington | 49.4264705882353 | 0 | 99 |
2k. Show the same results but only for non-American employees
SELECT e.StateProvinceName
, AVG(e.VacationHours) AS 'Average Vacation'
, MIN(e.VacationHours) AS 'Minimum Vacation'
, MAX(e.VacationHours) AS 'Maximum Vacation'
FROM dbo.Employees AS e
WHERE e.CountryName != 'United States'
GROUP BY e.StateProvinceName
HAVING AVG(e.VacationHours) BETWEEN 31 AND 49
;
Out[210]:
| MaritalStatus | StateProvinceName | VacationHours |
1 | M | Alberta | 31 | 9 | 51 |
2 | S | Alberta | 41 | 20 | 62 |
3 | M | England | 37 | 37 | 37 |
4 | S | Gironde | 34 | 34 | 34 |
5 | S | Hamburg | 35 | 35 | 35 |
6 | M | Ontario | 31 | 31 | 31 |
7 | S | Victoria | 36 | 36 | 36 |
3a. Report how many employees are in the company
SELECT COUNT(1) FROM dbo.Employees;
3b. For the above report, show the number of employees per manager (hint: use ManagerID)
SELECT e.ManagerID, COUNT(1) AS 'NumEmployees'
FROM dbo.Employees AS e
GROUP BY e.ManagerID
;
Out[213]:
| ManagerID | EmployeeID |
1 | | 1 |
2 | 1 | 8 |
3 | 16 | 8 |
4 | 227 | 6 |
5 | 234 | 3 |
6 | 235 | 5 |
7 | 249 | 9 |
8 | 25 | 30 |
9 | 250 | 17 |
10 | 26 | 178 |
11 | 263 | 9 |
12 | 273 | 3 |
13 | 274 | 10 |
14 | 285 | 1 |
15 | 287 | 3 |
3c. Remove any manager ID's that are NULL from the results above
SELECT e.ManagerID, COUNT(1) AS 'NumEmployees'
FROM dbo.Employees AS e
WHERE e.ManagerID IS NOT NULL
GROUP BY e.ManagerID
;
Out[236]:
| ManagerID | EmployeeID |
1 | 1 | 8 |
2 | 16 | 8 |
3 | 227 | 6 |
4 | 234 | 3 |
5 | 235 | 5 |
6 | 249 | 9 |
7 | 25 | 30 |
8 | 250 | 17 |
9 | 26 | 178 |
10 | 263 | 9 |
11 | 273 | 3 |
12 | 274 | 10 |
13 | 285 | 1 |
14 | 287 | 3 |
3d. Show the same results as above, but only for managers who have at least 5 employees
SELECT e.ManagerID, COUNT(1) AS 'NumEmployees'
FROM dbo.Employees AS e
WHERE e.ManagerID IS NOT NULL
GROUP BY e.ManagerID
HAVING COUNT(1) >= 5
;
Out[239]:
| ManagerID | NumEmployees |
1 | 1 | 8 |
2 | 16 | 8 |
3 | 227 | 6 |
5 | 235 | 5 |
6 | 249 | 9 |
7 | 25 | 30 |
8 | 250 | 17 |
9 | 26 | 178 |
10 | 263 | 9 |
12 | 274 | 10 |
4a. List the average vacation hours of all employees
SELECT AVG(e.VacationHours)
FROM dbo.Employees AS e
;
Out[240]:
50.6288659793814
4b. Break down the results by State
SELECT e.StateProvinceName, AVG(e.VacationHours) AS 'Average Vacation Hours'
FROM dbo.Employees AS e
GROUP BY e.StateProvinceName
;
Out[242]:
| StateProvinceName | VacationHours |
1 | Alberta | 35 |
2 | California | 21 |
3 | England | 37 |
4 | Georgia | 55 |
5 | Gironde | 34 |
6 | Hamburg | 35 |
7 | Massachusetts | 39 |
8 | Michigan | 38 |
9 | Minnesota | 36 |
10 | Ontario | 31 |
11 | Oregon | 22 |
12 | Tennessee | 29 |
13 | Utah | 27 |
14 | Victoria | 36 |
15 | Washington | 51.8856088560886 |
4c. Break down the results by city and State
SELECT e.StateProvinceName, e.City, AVG(e.VacationHours) AS 'Average Vacation Hours'
FROM dbo.Employees AS e
GROUP BY e.StateProvinceName, e.City
;
Out[243]:
| StateProvinceName | City | VacationHours |
1 | Georgia | Atlanta | 55 |
2 | Washington | Bellevue | 44.3055555555556 |
3 | Hamburg | Berlin | 35 |
4 | Gironde | Bordeaux | 34 |
5 | Washington | Bothell | 65.6923076923077 |
6 | Alberta | Calgary | 35 |
7 | England | Cambridge | 37 |
8 | Massachusetts | Cambridge | 39 |
9 | Washington | Carnation | 59 |
10 | Michigan | Detroit | 38 |
11 | Minnesota | Duluth | 24 |
12 | Washington | Duvall | 42.2 |
13 | Washington | Edmonds | 47.64 |
14 | Washington | Everett | 57.8235294117647 |
15 | Washington | Gold Bar | 70.8 |
16 | Washington | Index | 76 |
17 | Washington | Issaquah | 67.6666666666667 |
18 | Washington | Kenmore | 53.9166666666667 |
19 | Washington | Kent | 5 |
20 | Victoria | Melbourne | 36 |
21 | Tennessee | Memphis | 29 |
22 | Minnesota | Minneapolis | 48 |
23 | Washington | Monroe | 47.9166666666667 |
24 | Utah | Nevada | 27 |
25 | Washington | Newport Hills | 62.8333333333333 |
26 | Ontario | Ottawa | 31 |
27 | Oregon | Portland | 22 |
28 | Washington | Redmond | 37.4285714285714 |
29 | Washington | Renton | 54.1764705882353 |
30 | Washington | Sammamish | 42.8235294117647 |
31 | California | San Francisco | 21 |
32 | Washington | Seattle | 54.6590909090909 |
33 | Washington | Snohomish | 52.8 |
4d. Add something that shows the number of employees per city
SELECT e.StateProvinceName, e.City
, AVG(e.VacationHours) AS 'Average Vacation Hours'
, COUNT(1) AS 'NumEmployees'
FROM dbo.Employees AS e
GROUP BY e.StateProvinceName, e.City
;
Out[247]:
| StateProvinceName | City | VacationHours |
1 | Georgia | Atlanta | 55 | 1 |
2 | Washington | Bellevue | 44.3055555555556 | 36 |
3 | Hamburg | Berlin | 35 | 1 |
4 | Gironde | Bordeaux | 34 | 1 |
5 | Washington | Bothell | 65.6923076923077 | 13 |
6 | Alberta | Calgary | 35 | 5 |
7 | England | Cambridge | 37 | 1 |
8 | Massachusetts | Cambridge | 39 | 1 |
9 | Washington | Carnation | 59 | 5 |
10 | Michigan | Detroit | 38 | 1 |
11 | Minnesota | Duluth | 24 | 1 |
12 | Washington | Duvall | 42.2 | 10 |
13 | Washington | Edmonds | 47.64 | 25 |
14 | Washington | Everett | 57.8235294117647 | 17 |
15 | Washington | Gold Bar | 70.8 | 5 |
16 | Washington | Index | 76 | 5 |
17 | Washington | Issaquah | 67.6666666666667 | 15 |
18 | Washington | Kenmore | 53.9166666666667 | 12 |
19 | Washington | Kent | 5 | 1 |
20 | Victoria | Melbourne | 36 | 1 |
21 | Tennessee | Memphis | 29 | 1 |
22 | Minnesota | Minneapolis | 48 | 1 |
23 | Washington | Monroe | 47.9166666666667 | 12 |
24 | Utah | Nevada | 27 | 1 |
25 | Washington | Newport Hills | 62.8333333333333 | 6 |
26 | Ontario | Ottawa | 31 | 1 |
27 | Oregon | Portland | 22 | 1 |
28 | Washington | Redmond | 37.4285714285714 | 21 |
29 | Washington | Renton | 54.1764705882353 | 17 |
30 | Washington | Sammamish | 42.8235294117647 | 17 |
31 | California | San Francisco | 21 | 2 |
32 | Washington | Seattle | 54.6590909090909 | 44 |
33 | Washington | Snohomish | 52.8 | 10 |
4e. Sort the results by the city and state
SELECT e.StateProvinceName, e.City
, AVG(e.VacationHours) AS 'Average Vacation Hours'
, COUNT(1) AS 'NumEmployees'
FROM dbo.Employees AS e
GROUP BY e.StateProvinceName, e.City
ORDER BY e.StateProvinceName, e.City
;
Out[258]:
| StateProvinceName | City | VacationHours |
6 | Alberta | Calgary | 35 | 5 |
31 | California | San Francisco | 21 | 2 |
7 | England | Cambridge | 37 | 1 |
1 | Georgia | Atlanta | 55 | 1 |
4 | Gironde | Bordeaux | 34 | 1 |
3 | Hamburg | Berlin | 35 | 1 |
8 | Massachusetts | Cambridge | 39 | 1 |
10 | Michigan | Detroit | 38 | 1 |
11 | Minnesota | Duluth | 24 | 1 |
22 | Minnesota | Minneapolis | 48 | 1 |
26 | Ontario | Ottawa | 31 | 1 |
27 | Oregon | Portland | 22 | 1 |
21 | Tennessee | Memphis | 29 | 1 |
24 | Utah | Nevada | 27 | 1 |
20 | Victoria | Melbourne | 36 | 1 |
2 | Washington | Bellevue | 44.3055555555556 | 36 |
5 | Washington | Bothell | 65.6923076923077 | 13 |
9 | Washington | Carnation | 59 | 5 |
12 | Washington | Duvall | 42.2 | 10 |
13 | Washington | Edmonds | 47.64 | 25 |
14 | Washington | Everett | 57.8235294117647 | 17 |
15 | Washington | Gold Bar | 70.8 | 5 |
16 | Washington | Index | 76 | 5 |
17 | Washington | Issaquah | 67.6666666666667 | 15 |
18 | Washington | Kenmore | 53.9166666666667 | 12 |
19 | Washington | Kent | 5 | 1 |
23 | Washington | Monroe | 47.9166666666667 | 12 |
25 | Washington | Newport Hills | 62.8333333333333 | 6 |
28 | Washington | Redmond | 37.4285714285714 | 21 |
29 | Washington | Renton | 54.1764705882353 | 17 |
30 | Washington | Sammamish | 42.8235294117647 | 17 |
32 | Washington | Seattle | 54.6590909090909 | 44 |
33 | Washington | Snohomish | 52.8 | 10 |
SELECT CONCAT(e.StateProvinceName, ', ', e.City) AS 'Location'
, AVG(e.VacationHours) AS 'Average Vacation Hours'
, COUNT(1) AS 'NumEmployees'
FROM dbo.Employees AS e
GROUP BY e.StateProvinceName, e.City
ORDER BY e.StateProvinceName, e.City
;
Out[294]:
| StateProvinceName | City | CityState | AvgVacationHours | NumEmployees |
1 | Georgia | Atlanta | Atlanta, Georgia | 55 | 1 |
2 | Washington | Bellevue | Bellevue, Washington | 44.3055555555556 | 36 |
3 | Hamburg | Berlin | Berlin, Hamburg | 35 | 1 |
4 | Gironde | Bordeaux | Bordeaux, Gironde | 34 | 1 |
5 | Washington | Bothell | Bothell, Washington | 65.6923076923077 | 13 |
6 | Alberta | Calgary | Calgary, Alberta | 35 | 5 |
7 | England | Cambridge | Cambridge, England | 37 | 1 |
8 | Massachusetts | Cambridge | Cambridge, Massachusetts | 39 | 1 |
9 | Washington | Carnation | Carnation, Washington | 59 | 5 |
10 | Michigan | Detroit | Detroit, Michigan | 38 | 1 |
11 | Minnesota | Duluth | Duluth, Minnesota | 24 | 1 |
12 | Washington | Duvall | Duvall, Washington | 42.2 | 10 |
13 | Washington | Edmonds | Edmonds, Washington | 47.64 | 25 |
14 | Washington | Everett | Everett, Washington | 57.8235294117647 | 17 |
15 | Washington | Gold Bar | Gold Bar, Washington | 70.8 | 5 |
16 | Washington | Index | Index, Washington | 76 | 5 |
17 | Washington | Issaquah | Issaquah, Washington | 67.6666666666667 | 15 |
18 | Washington | Kenmore | Kenmore, Washington | 53.9166666666667 | 12 |
19 | Washington | Kent | Kent, Washington | 5 | 1 |
20 | Victoria | Melbourne | Melbourne, Victoria | 36 | 1 |
21 | Tennessee | Memphis | Memphis, Tennessee | 29 | 1 |
22 | Minnesota | Minneapolis | Minneapolis, Minnesota | 48 | 1 |
23 | Washington | Monroe | Monroe, Washington | 47.9166666666667 | 12 |
24 | Utah | Nevada | Nevada, Utah | 27 | 1 |
25 | Washington | Newport Hills | Newport Hills, Washington | 62.8333333333333 | 6 |
26 | Ontario | Ottawa | Ottawa, Ontario | 31 | 1 |
27 | Oregon | Portland | Portland, Oregon | 22 | 1 |
28 | Washington | Redmond | Redmond, Washington | 37.4285714285714 | 21 |
29 | Washington | Renton | Renton, Washington | 54.1764705882353 | 17 |
30 | Washington | Sammamish | Sammamish, Washington | 42.8235294117647 | 17 |
31 | California | San Francisco | San Francisco, California | 21 | 2 |
32 | Washington | Seattle | Seattle, Washington | 54.6590909090909 | 44 |
33 | Washington | Snohomish | Snohomish, Washington | 52.8 | 10 |
4g. Add a column that shows the difference between the maximum vacation hours and minimum vacation hours for each city
SELECT CONCAT(e.StateProvinceName, ', ', e.City) AS 'Location'
, AVG(e.VacationHours) AS 'Average Vacation Hours'
, COUNT(1) AS 'NumEmployees'
, MAX(e.VacationHours)-MIN(e.VacationHours) AS 'Difference Vacation Hours'
FROM dbo.Employees AS e
GROUP BY e.StateProvinceName, e.City
ORDER BY e.StateProvinceName, e.City
;
Out[295]:
| StateProvinceName | City | CityState | AvgVacationHours | NumEmployees | DifferenceVacHrs |
1 | Georgia | Atlanta | Atlanta, Georgia | 55 | 1 | 0 |
2 | Washington | Bellevue | Bellevue, Washington | 44.3055555555556 | 36 | 95 |
3 | Hamburg | Berlin | Berlin, Hamburg | 35 | 1 | 0 |
4 | Gironde | Bordeaux | Bordeaux, Gironde | 34 | 1 | 0 |
5 | Washington | Bothell | Bothell, Washington | 65.6923076923077 | 13 | 90 |
6 | Alberta | Calgary | Calgary, Alberta | 35 | 5 | 53 |
7 | England | Cambridge | Cambridge, England | 37 | 1 | 0 |
8 | Massachusetts | Cambridge | Cambridge, Massachusetts | 39 | 1 | 0 |
9 | Washington | Carnation | Carnation, Washington | 59 | 5 | 64 |
10 | Michigan | Detroit | Detroit, Michigan | 38 | 1 | 0 |
11 | Minnesota | Duluth | Duluth, Minnesota | 24 | 1 | 0 |
12 | Washington | Duvall | Duvall, Washington | 42.2 | 10 | 92 |
13 | Washington | Edmonds | Edmonds, Washington | 47.64 | 25 | 97 |
14 | Washington | Everett | Everett, Washington | 57.8235294117647 | 17 | 71 |
15 | Washington | Gold Bar | Gold Bar, Washington | 70.8 | 5 | 39 |
16 | Washington | Index | Index, Washington | 76 | 5 | 32 |
17 | Washington | Issaquah | Issaquah, Washington | 67.6666666666667 | 15 | 73 |
18 | Washington | Kenmore | Kenmore, Washington | 53.9166666666667 | 12 | 90 |
19 | Washington | Kent | Kent, Washington | 5 | 1 | 0 |
20 | Victoria | Melbourne | Melbourne, Victoria | 36 | 1 | 0 |
21 | Tennessee | Memphis | Memphis, Tennessee | 29 | 1 | 0 |
22 | Minnesota | Minneapolis | Minneapolis, Minnesota | 48 | 1 | 0 |
23 | Washington | Monroe | Monroe, Washington | 47.9166666666667 | 12 | 71 |
24 | Utah | Nevada | Nevada, Utah | 27 | 1 | 0 |
25 | Washington | Newport Hills | Newport Hills, Washington | 62.8333333333333 | 6 | 55 |
26 | Ontario | Ottawa | Ottawa, Ontario | 31 | 1 | 0 |
27 | Oregon | Portland | Portland, Oregon | 22 | 1 | 0 |
28 | Washington | Redmond | Redmond, Washington | 37.4285714285714 | 21 | 92 |
29 | Washington | Renton | Renton, Washington | 54.1764705882353 | 17 | 97 |
30 | Washington | Sammamish | Sammamish, Washington | 42.8235294117647 | 17 | 93 |
31 | California | San Francisco | San Francisco, California | 21 | 2 | 10 |
32 | Washington | Seattle | Seattle, Washington | 54.6590909090909 | 44 | 89 |
33 | Washington | Snohomish | Snohomish, Washington | 52.8 | 10 | 85 |
4h. Now sort the results by the new column created above
SELECT CONCAT(e.StateProvinceName, ', ', e.City) AS 'Location'
, AVG(e.VacationHours) AS 'Average Vacation Hours'
, COUNT(1) AS 'NumEmployees'
, MAX(e.VacationHours)-MIN(e.VacationHours) AS 'Difference Vacation Hours'
FROM dbo.Employees AS e
GROUP BY e.StateProvinceName, e.City
ORDER BY 'Difference Vacation Hours' DESC
;
Out[297]:
| StateProvinceName | City | CityState | AvgVacationHours | NumEmployees | DifferenceVacHrs |
13 | Washington | Edmonds | Edmonds, Washington | 47.64 | 25 | 97 |
29 | Washington | Renton | Renton, Washington | 54.1764705882353 | 17 | 97 |
2 | Washington | Bellevue | Bellevue, Washington | 44.3055555555556 | 36 | 95 |
30 | Washington | Sammamish | Sammamish, Washington | 42.8235294117647 | 17 | 93 |
12 | Washington | Duvall | Duvall, Washington | 42.2 | 10 | 92 |
28 | Washington | Redmond | Redmond, Washington | 37.4285714285714 | 21 | 92 |
5 | Washington | Bothell | Bothell, Washington | 65.6923076923077 | 13 | 90 |
18 | Washington | Kenmore | Kenmore, Washington | 53.9166666666667 | 12 | 90 |
32 | Washington | Seattle | Seattle, Washington | 54.6590909090909 | 44 | 89 |
33 | Washington | Snohomish | Snohomish, Washington | 52.8 | 10 | 85 |
17 | Washington | Issaquah | Issaquah, Washington | 67.6666666666667 | 15 | 73 |
14 | Washington | Everett | Everett, Washington | 57.8235294117647 | 17 | 71 |
23 | Washington | Monroe | Monroe, Washington | 47.9166666666667 | 12 | 71 |
9 | Washington | Carnation | Carnation, Washington | 59 | 5 | 64 |
25 | Washington | Newport Hills | Newport Hills, Washington | 62.8333333333333 | 6 | 55 |
6 | Alberta | Calgary | Calgary, Alberta | 35 | 5 | 53 |
15 | Washington | Gold Bar | Gold Bar, Washington | 70.8 | 5 | 39 |
16 | Washington | Index | Index, Washington | 76 | 5 | 32 |
31 | California | San Francisco | San Francisco, California | 21 | 2 | 10 |
1 | Georgia | Atlanta | Atlanta, Georgia | 55 | 1 | 0 |
3 | Hamburg | Berlin | Berlin, Hamburg | 35 | 1 | 0 |
4 | Gironde | Bordeaux | Bordeaux, Gironde | 34 | 1 | 0 |
7 | England | Cambridge | Cambridge, England | 37 | 1 | 0 |
8 | Massachusetts | Cambridge | Cambridge, Massachusetts | 39 | 1 | 0 |
10 | Michigan | Detroit | Detroit, Michigan | 38 | 1 | 0 |
11 | Minnesota | Duluth | Duluth, Minnesota | 24 | 1 | 0 |
19 | Washington | Kent | Kent, Washington | 5 | 1 | 0 |
20 | Victoria | Melbourne | Melbourne, Victoria | 36 | 1 | 0 |
21 | Tennessee | Memphis | Memphis, Tennessee | 29 | 1 | 0 |
22 | Minnesota | Minneapolis | Minneapolis, Minnesota | 48 | 1 | 0 |
24 | Utah | Nevada | Nevada, Utah | 27 | 1 | 0 |
26 | Ontario | Ottawa | Ottawa, Ontario | 31 | 1 | 0 |
27 | Oregon | Portland | Portland, Oregon | 22 | 1 | 0 |
4i. Limit the results to cities that have more than 1 employee
SELECT CONCAT(e.StateProvinceName, ', ', e.City) AS 'Location'
, AVG(e.VacationHours) AS 'Average Vacation Hours'
, COUNT(1) AS 'NumEmployees'
, MAX(e.VacationHours)-MIN(e.VacationHours) AS 'Difference Vacation Hours'
FROM dbo.Employees AS e
GROUP BY e.StateProvinceName, e.City
HAVING COUNT(1) > 1
ORDER BY 'Difference Vacation Hours' DESC
;
Out[309]:
| StateProvinceName | City | CityState | AvgVacationHours | NumEmployees | DifferenceVacHrs |
13 | Washington | Edmonds | Edmonds, Washington | 47.64 | 25 | 97 |
29 | Washington | Renton | Renton, Washington | 54.1764705882353 | 17 | 97 |
2 | Washington | Bellevue | Bellevue, Washington | 44.3055555555556 | 36 | 95 |
30 | Washington | Sammamish | Sammamish, Washington | 42.8235294117647 | 17 | 93 |
12 | Washington | Duvall | Duvall, Washington | 42.2 | 10 | 92 |
28 | Washington | Redmond | Redmond, Washington | 37.4285714285714 | 21 | 92 |
5 | Washington | Bothell | Bothell, Washington | 65.6923076923077 | 13 | 90 |
18 | Washington | Kenmore | Kenmore, Washington | 53.9166666666667 | 12 | 90 |
32 | Washington | Seattle | Seattle, Washington | 54.6590909090909 | 44 | 89 |
33 | Washington | Snohomish | Snohomish, Washington | 52.8 | 10 | 85 |
17 | Washington | Issaquah | Issaquah, Washington | 67.6666666666667 | 15 | 73 |
14 | Washington | Everett | Everett, Washington | 57.8235294117647 | 17 | 71 |
23 | Washington | Monroe | Monroe, Washington | 47.9166666666667 | 12 | 71 |
9 | Washington | Carnation | Carnation, Washington | 59 | 5 | 64 |
25 | Washington | Newport Hills | Newport Hills, Washington | 62.8333333333333 | 6 | 55 |
6 | Alberta | Calgary | Calgary, Alberta | 35 | 5 | 53 |
15 | Washington | Gold Bar | Gold Bar, Washington | 70.8 | 5 | 39 |
16 | Washington | Index | Index, Washington | 76 | 5 | 32 |
31 | California | San Francisco | San Francisco, California | 21 | 2 | 10 |
4j. Limit the results to non-U.S. cities
SELECT CONCAT(e.StateProvinceName, ', ', e.City) AS 'Location'
, AVG(e.VacationHours) AS 'Average Vacation Hours'
, COUNT(1) AS 'NumEmployees'
, MAX(e.VacationHours)-MIN(e.VacationHours) AS 'Difference Vacation Hours'
FROM dbo.Employees AS e
WHERE e.CountryName != 'United States'
GROUP BY e.StateProvinceName, e.City
HAVING COUNT(1) > 1
ORDER BY 'Difference Vacation Hours' DESC
;
Out[313]:
| StateProvinceName | City | CityState | AvgVacationHours | NumEmployees | DifferenceVacHrs |
3 | Alberta | Calgary | Calgary, Alberta | 35 | 5 | 53 |