|
| 1 | +/* |
| 2 | +Write a query with FULL OUTER JOIN to see: |
| 3 | +- each account who has a sales rep and each sales rep that |
| 4 | +has an account (all of the columns in these returned rows |
| 5 | +will be full) |
| 6 | +- but also each account that does not have a sales rep and |
| 7 | +each sales rep that does not have an account (some of the |
| 8 | +columns in these returned rows will be empty) |
| 9 | +*/ |
| 10 | + |
| 11 | +SELECT a.*, |
| 12 | + s.id AS s_id, |
| 13 | + s.name AS s_name, |
| 14 | + s.region_id AS r_id |
| 15 | +FROM accounts a |
| 16 | +FULL OUTER JOIN sales_reps s |
| 17 | +ON a.sales_rep_id = s.id; |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +/* |
| 24 | +Write a query that left joins the accounts table and the |
| 25 | +sales_reps tables on each sale rep's ID number and joins it |
| 26 | +using the < comparison operator on accounts.primary_poc and |
| 27 | +sales_reps.name, like so: |
| 28 | + `accounts.primary_poc < sales_reps.name` |
| 29 | +The query results should be a table with three columns: the |
| 30 | +account name (e.g. Johnson Controls), the primary contact |
| 31 | +name (e.g. Cammy Sosnowski), and the sales representative's |
| 32 | +name (e.g. Samuel Racine). |
| 33 | +*/ |
| 34 | + |
| 35 | +SELECT a.name, |
| 36 | + a.primary_poc, |
| 37 | + s.name AS sales_rep |
| 38 | +FROM accounts a |
| 39 | +LEFT JOIN sales_reps s |
| 40 | +ON a.sales_rep_id = s.id |
| 41 | + AND a.primary_poc < s.name; |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +/* |
| 48 | +Use SELF JOIN to find we events that occur one after another |
| 49 | +within 1 day for each account. |
| 50 | +*/ |
| 51 | + |
| 52 | +SELECT w1.channel w1_channel, |
| 53 | + w1.id w1_id, |
| 54 | + w1.account_id w1_aid, |
| 55 | + w1.occurred_at w1_time, |
| 56 | + w2.channel w2_channel, |
| 57 | + w2.id w2_id, |
| 58 | + w2.account_id w2_aid, |
| 59 | + w2.occurred_at w2_time |
| 60 | +FROM web_events w1 |
| 61 | +LEFT JOIN web_events w2 |
| 62 | +ON w1.account_id = w2.account_id |
| 63 | + AND w1.occurred_at < w2.occurred_at |
| 64 | + AND w1.occurred_at + INTERVAL '1 day' >= w2.occurred_at |
| 65 | +ORDER BY 1, 2; |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +/* |
| 72 | +Write a query that uses UNION ALL on two instances (and selecting |
| 73 | +all columns) of the accounts table. |
| 74 | +*/ |
| 75 | + |
| 76 | +SELECT * |
| 77 | +FROM accounts |
| 78 | + |
| 79 | +UNION ALL |
| 80 | + |
| 81 | +SELECT * |
| 82 | +FROM accounts; |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +/* |
| 89 | +Add a WHERE clause to each of the tables that you unioned in the |
| 90 | +query above, filtering the first table where name equals Walmart |
| 91 | +and filtering the second table where name equals Disney. |
| 92 | +*/ |
| 93 | + |
| 94 | +SELECT * |
| 95 | +FROM accounts |
| 96 | +WHERE name = 'Walmart' |
| 97 | + |
| 98 | +UNION ALL |
| 99 | + |
| 100 | +SELECT * |
| 101 | +FROM accounts |
| 102 | +WHERE name = 'Disney'; |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +/* |
| 109 | +Perform the union in your first query (under the Appending Data via |
| 110 | +UNION header) in a common table expression and name it double_accounts. |
| 111 | +Then do a COUNT the number of times a name appears in the |
| 112 | +double_accounts table. If you do this correctly, your query results |
| 113 | +should have a count of 2 for each name. |
| 114 | +*/ |
| 115 | + |
| 116 | +WITH double_accounts AS ( |
| 117 | + SELECT * |
| 118 | + FROM accounts |
| 119 | + |
| 120 | + UNION ALL |
| 121 | + |
| 122 | + SELECT * |
| 123 | + FROM accounts |
| 124 | +) |
| 125 | + |
| 126 | +SELECT name, COUNT(*) |
| 127 | +FROM double_accounts |
| 128 | +GROUP BY name; |
| 129 | + |
0 commit comments