|
| 1 | +/* |
| 2 | +Write a query that limits the response to only the first 15 rows |
| 3 | +and includes the occurred_at, account_id, and channel fields in |
| 4 | +the web_events table. |
| 5 | +*/ |
| 6 | + |
| 7 | +SELECT occurred_at, account_id, channel |
| 8 | +FROM web_events |
| 9 | +LIMIT 15; |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +/* |
| 16 | +In order to gain some practice using ORDER BY: |
| 17 | +
|
| 18 | +1. Write a query to return the 10 earliest orders in the orders table. |
| 19 | +Include the id, occurred_at, and total_amt_usd. |
| 20 | +
|
| 21 | +2. Write a query to return the top 5 orders in terms of largest |
| 22 | +total_amt_usd. Include the id, account_id, and total_amt_usd. |
| 23 | +
|
| 24 | +3. Write a query to return the bottom 20 orders in terms of least total. |
| 25 | +Include the id, account_id, and total. |
| 26 | +*/ |
| 27 | + |
| 28 | + |
| 29 | +SELECT id, occurred_at, total_amt_usd |
| 30 | +FROM orders |
| 31 | +ORDER BY occurred_at |
| 32 | +LIMIT 10; |
| 33 | + |
| 34 | +SELECT id, account_id, total_amt_usd |
| 35 | +FROM orders |
| 36 | +ORDER BY total_amt_usd DESC |
| 37 | +LIMIT 5; |
| 38 | + |
| 39 | +SELECT id, account_id, total |
| 40 | +FROM orders |
| 41 | +ORDER BY total |
| 42 | +LIMIT 20; |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +/* |
| 49 | +1. Write a query that returns the top 5 rows from orders ordered according |
| 50 | +to newest to oldest, but with the largest total_amt_usd for each date |
| 51 | +listed first for each date. |
| 52 | +
|
| 53 | +2. Write a query that returns the top 10 rows from orders ordered according |
| 54 | +to oldest to newest, but with the smallest total_amt_usd for each date |
| 55 | +listed first for each date. |
| 56 | +*/ |
| 57 | + |
| 58 | +SELECT * |
| 59 | +FROM orders |
| 60 | +ORDER BY occurred_at DESC, total_amt_usd DESC |
| 61 | +LIMIT 5; |
| 62 | + |
| 63 | +SELECT * |
| 64 | +FROM orders |
| 65 | +ORDER BY occurred_at, total_amt_usd |
| 66 | +LIMIT 10; |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +/* |
| 73 | +Write a query that |
| 74 | +
|
| 75 | +1. Pull the first 5 rows and all columns from the orders table that have a |
| 76 | +dollar amount of gloss_amt_usd greater than or equal to 1000. |
| 77 | +
|
| 78 | +2. Pull the first 10 rows and all columns from the orders table that have a |
| 79 | +total_amt_usd less than 500. |
| 80 | +*/ |
| 81 | + |
| 82 | +SELECT * |
| 83 | +FROM orders |
| 84 | +WHERE gloss_amt_usd >= 1000 |
| 85 | +LIMIT 5; |
| 86 | + |
| 87 | +SELECT * |
| 88 | +FROM orders |
| 89 | +WHERE total_amt_usd < 500 |
| 90 | +LIMIT 10; |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +/* |
| 97 | +Filter the accounts table to include the company name, website, and the primary |
| 98 | +point of contact (primary_poc) for Exxon Mobil in the accounts table. |
| 99 | +*/ |
| 100 | + |
| 101 | +SELECT name, website, primary_poc |
| 102 | +FROM accounts |
| 103 | +WHERE name = 'Exxon Mobil'; |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +/* |
| 110 | +Using the orders table: |
| 111 | +
|
| 112 | +1. Create a column that divides the standard_amt_usd by the standard_qty to find |
| 113 | +the unit price for standard paper for each order. Limit the results to the first |
| 114 | +10 orders, and include the id and account_id fields. |
| 115 | +
|
| 116 | +2. Write a query that finds the percentage of revenue that comes from poster paper |
| 117 | +for each order. You will need to use only the columns that end with _usd. (Try to do |
| 118 | +this without using the total column). Include the id and account_id fields. |
| 119 | +*/ |
| 120 | + |
| 121 | +# DERIVED COLUMNS |
| 122 | +SELECT id, account_id, |
| 123 | + standard_amt_usd/standard_qty as unit_price |
| 124 | +FROM orders |
| 125 | +LIMIT 10; |
| 126 | + |
| 127 | +SELECT id, account_id, |
| 128 | + poster_amt_usd/(poster_amt_usd + gloss_amt_usd + standard_amt_usd + 1e-10) as poster_paper_revenue |
| 129 | +FROM orders; |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +/* |
| 136 | +Use the accounts table to find |
| 137 | +
|
| 138 | +1. All the companies whose names start with 'C'. |
| 139 | +
|
| 140 | +2. All companies whose names contain the string 'one' somewhere in the name. |
| 141 | +
|
| 142 | +3. All companies whose names end with 's'. |
| 143 | +*/ |
| 144 | + |
| 145 | +# LIKE with % |
| 146 | +SELECT * |
| 147 | +FROM accounts |
| 148 | +WHERE name LIKE 'C%'; |
| 149 | + |
| 150 | +SELECT * |
| 151 | +FROM accounts |
| 152 | +WHERE name LIKE '%one%'; |
| 153 | + |
| 154 | +SELECT * |
| 155 | +FROM accounts |
| 156 | +WHERE name LIKE '%s'; |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +/* |
| 163 | +1. Use the accounts table to find the account name, primary_poc, and sales_rep_id |
| 164 | +for Walmart, Target, and Nordstrom. |
| 165 | +
|
| 166 | +2. Use the web_events table to find all information regarding individuals who were |
| 167 | +contacted via the channel of organic or adwords. |
| 168 | +*/ |
| 169 | + |
| 170 | +# IN clause |
| 171 | +SELECT name, primary_poc, sales_rep_id |
| 172 | +FROM accounts |
| 173 | +WHERE name IN ('Walmart', 'Target', 'Nordstrom'); |
| 174 | + |
| 175 | +SELECT * |
| 176 | +FROM web_events |
| 177 | +WHERE channel IN ('organic', 'adwords'); |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +/* |
| 184 | +We can pull all of the rows that were excluded from the queries in the previous two |
| 185 | +concepts with our new operator. |
| 186 | +
|
| 187 | +1. Use the accounts table to find the account name, primary poc, and sales rep id for |
| 188 | +all stores except Walmart, Target, and Nordstrom. |
| 189 | +
|
| 190 | +2. Use the web_events table to find all information regarding individuals who were |
| 191 | +contacted via any method except using organic or adwords methods. |
| 192 | +
|
| 193 | +Use the accounts table to find: |
| 194 | +
|
| 195 | +3. All the companies whose names do not start with 'C'. |
| 196 | +
|
| 197 | +4. All companies whose names do not contain the string 'one' somewhere in the name. |
| 198 | +
|
| 199 | +5. All companies whose names do not end with 's'. |
| 200 | +*/ |
| 201 | + |
| 202 | +# NOT clause |
| 203 | +SELECT name, primary_poc, sales_rep_id |
| 204 | +FROM accounts |
| 205 | +WHERE name NOT IN ('Walmart', 'Target', 'Nordstrom'); |
| 206 | + |
| 207 | +SELECT * |
| 208 | +FROM web_events |
| 209 | +WHERE channel NOT IN ('organic', 'adwords'); |
| 210 | + |
| 211 | +SELECT * |
| 212 | +FROM accounts |
| 213 | +WHERE name NOT LIKE 'C%'; |
| 214 | + |
| 215 | +SELECT * |
| 216 | +FROM accounts |
| 217 | +WHERE name NOT LIKE '%one%'; |
| 218 | + |
| 219 | +SELECT * |
| 220 | +FROM accounts |
| 221 | +WHERE name NOT LIKE '%s'; |
| 222 | + |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | +/* |
| 228 | +1. Write a query that returns all the orders where the standard_qty is over 1000, the |
| 229 | +poster_qty is 0, and the gloss_qty is 0. |
| 230 | +
|
| 231 | +2. Using the accounts table find all the companies whose names do not start with 'C' and |
| 232 | +end with 's'. |
| 233 | +
|
| 234 | +3. Use the web_events table to find all information regarding individuals who were |
| 235 | +contacted via organic or adwords and started their account at any point in 2016 sorted |
| 236 | +from newest to oldest. |
| 237 | +*/ |
| 238 | + |
| 239 | +# AND and BETWEEN |
| 240 | +SELECT * |
| 241 | +FROM orders |
| 242 | +WHERE standard_qty > 1000 and |
| 243 | + poster_qty = 0 and |
| 244 | + gloss_qty = 0; |
| 245 | + |
| 246 | + |
| 247 | +SELECT * |
| 248 | +FROM accounts |
| 249 | +WHERE name NOT LIKE 'C%' and |
| 250 | + name NOT LIKE '%s' ; |
| 251 | + |
| 252 | + |
| 253 | +SELECT * |
| 254 | +FROM web_events |
| 255 | +WHERE channel IN ('organic', 'adwords') |
| 256 | + AND occurred_at BETWEEN '2016-01-01' AND '2017-01-01' |
| 257 | +ORDER BY occurred_at DESC; |
| 258 | + |
| 259 | + |
| 260 | + |
| 261 | + |
| 262 | + |
| 263 | +/* |
| 264 | +1. Find list of orders ids where either gloss_qty or poster_qty is greater than 4000. Only |
| 265 | +include the id field in the resulting table. |
| 266 | +
|
| 267 | +2. Write a query that returns a list of orders where the standard_qty is zero and either |
| 268 | +the gloss_qty or poster_qty is over 1000. |
| 269 | +
|
| 270 | +3. Find all the company names that start with a 'C' or 'W', and the primary contact contains |
| 271 | +'ana' or 'Ana', but it doesn't contain 'eana'. |
| 272 | +
|
| 273 | +*/ |
| 274 | + |
| 275 | +# OR clause |
| 276 | +SELECT id |
| 277 | +FROM orders |
| 278 | +WHERE gloss_qty > 4000 |
| 279 | + OR poster_qty > 4000; |
| 280 | + |
| 281 | + |
| 282 | +SELECT * |
| 283 | +FROM orders |
| 284 | +WHERE (gloss_qty > 1000 OR poster_qty > 1000) |
| 285 | + AND standard_qty = 0; |
| 286 | + |
| 287 | + |
| 288 | + |
| 289 | +SELECT * |
| 290 | +FROM accounts |
| 291 | +WHERE (name LIKE 'C%' OR name LIKE 'W%') |
| 292 | + AND ((primary_poc LIKE '%ana%' OR primary_poc LIKE '%Ana%') |
| 293 | + AND primary_poc NOT LIKE '%eana%'); |
| 294 | + |
| 295 | + |
| 296 | + |
| 297 | + |
| 298 | + |
| 299 | + |
| 300 | + |
| 301 | + |
| 302 | + |
| 303 | + |
| 304 | + |
| 305 | + |
| 306 | + |
| 307 | + |
| 308 | + |
| 309 | + |
| 310 | + |
| 311 | + |
| 312 | + |
| 313 | + |
| 314 | + |
| 315 | + |
| 316 | + |
| 317 | + |
| 318 | + |
| 319 | + |
| 320 | + |
| 321 | + |
0 commit comments