|
| 1 | +/* |
| 2 | +In the accounts table, there is a column holding the website for each |
| 3 | +company. The last three digits specify what type of web address they |
| 4 | +are using. Pull these extensions and provide how many of each website |
| 5 | +type exist in the accounts table. |
| 6 | +*/ |
| 7 | + |
| 8 | +SELECT RIGHT(website, 3), |
| 9 | + COUNT(*) |
| 10 | +FROM accounts |
| 11 | +GROUP BY 1; |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +/* |
| 18 | +Use the accounts table to pull the first letter of each company name |
| 19 | +to see the distribution of company names that begin with each letter |
| 20 | +(or number). |
| 21 | +*/ |
| 22 | + |
| 23 | +SELECT LEFT(name, 1), |
| 24 | + COUNT(*) |
| 25 | +FROM accounts |
| 26 | +GROUP BY 1 |
| 27 | +ORDER BY 2 DESC; |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +/* |
| 34 | +Use the accounts table and a CASE statement to create two groups: one |
| 35 | +group of company names that start with a number and a second group of |
| 36 | +those company names that start with a letter. What proportion of |
| 37 | +company names start with a letter? |
| 38 | +*/ |
| 39 | + |
| 40 | +SELECT CASE WHEN LEFT(name, 1) IN ('0','1','2','3','4','5','6','7','8','9') |
| 41 | + THEN 'number' |
| 42 | + ELSE 'letter' |
| 43 | + END |
| 44 | + AS type, |
| 45 | + COUNT(*) |
| 46 | +FROM accounts |
| 47 | +GROUP BY 1 |
| 48 | +ORDER BY 2 DESC; |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +/* |
| 55 | +Consider vowels as a, e, i, o, and u. What proportion of company names |
| 56 | +start with a vowel, and what percent start with anything else? |
| 57 | +*/ |
| 58 | + |
| 59 | +SELECT CASE WHEN LEFT(UPPER(name), 1) IN ('A','E','I','O','U') |
| 60 | + THEN 'vowel' |
| 61 | + ELSE 'else' |
| 62 | + END |
| 63 | + AS type, |
| 64 | + COUNT(*) |
| 65 | +FROM accounts |
| 66 | +GROUP BY 1 |
| 67 | +ORDER BY 2 DESC; |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +/* |
| 74 | +Use the accounts table to create first and last name columns that hold the |
| 75 | +first and last names for the primary_poc. |
| 76 | +*/ |
| 77 | + |
| 78 | +SELECT LEFT(primary_poc, STRPOS(primary_poc, ' ')-1) AS firstname, |
| 79 | + RIGHT(primary_poc, LENGTH(primary_poc) - STRPOS(primary_poc, ' ')) AS lastname |
| 80 | +FROM accounts; |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +/* |
| 87 | +Now see if you can do the same thing for every rep name in the sales_reps |
| 88 | +table. Again provide first and last name columns. |
| 89 | +*/ |
| 90 | + |
| 91 | +SELECT LEFT(name, STRPOS(name, ' ')-1) AS firstname, |
| 92 | +RIGHT(name, LENGTH(name) - STRPOS(name, ' ')) AS lname |
| 93 | +FROM sales_reps; |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +/* |
| 100 | +Each company in the accounts table wants to create an email address for each |
| 101 | +primary_poc. The email address should be the first name of the primary_poc . |
| 102 | +last name primary_poc @ company name .com. |
| 103 | +*/ |
| 104 | + |
| 105 | +SELECT primary_poc, name, |
| 106 | + LOWER( |
| 107 | + LEFT(primary_poc, STRPOS(primary_poc, ' ')-1) || |
| 108 | + '.' || |
| 109 | + RIGHT(primary_poc, LENGTH(primary_poc) - STRPOS(primary_poc, ' ')) || |
| 110 | + '@' || |
| 111 | + name || |
| 112 | + '.com') |
| 113 | + AS email |
| 114 | +FROM accounts; |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +/* |
| 123 | +You may have noticed that in the previous solution some of the company names |
| 124 | +include spaces, which will certainly not work in an email address. See if |
| 125 | +you can create an email address that will work by removing all of the spaces |
| 126 | +in the account name, but otherwise your solution should be just as in |
| 127 | +question above. |
| 128 | +*/ |
| 129 | + |
| 130 | +SELECT primary_poc, name, |
| 131 | + LOWER( |
| 132 | + LEFT(primary_poc, STRPOS(primary_poc, ' ')-1) || |
| 133 | + '.' || |
| 134 | + RIGHT(primary_poc, LENGTH(primary_poc) - STRPOS(primary_poc, ' ')) || |
| 135 | + '@' || |
| 136 | + TRANSLATE(name, ' ', '') || |
| 137 | + '.com') |
| 138 | + AS email |
| 139 | +FROM accounts; |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +/* |
| 146 | +We would also like to create an initial password, which they will change after |
| 147 | +their first log in. The first password will be the first letter of the |
| 148 | +primary_poc's first name (lowercase), then the last letter of their first name |
| 149 | +(lowercase), the first letter of their last name (lowercase), the last letter of |
| 150 | +their last name (lowercase), the number of letters in their first name, the number |
| 151 | +of letters in their last name, and then the name of the company they are working |
| 152 | +with, all capitalized with no spaces. |
| 153 | +*/ |
| 154 | + |
| 155 | +WITH sub AS ( |
| 156 | + SELECT LEFT(primary_poc, STRPOS(primary_poc, ' ')-1) AS firstname, |
| 157 | + RIGHT(primary_poc, LENGTH(primary_poc) - STRPOS(primary_poc, ' ')) AS lastname, |
| 158 | + id |
| 159 | + FROM accounts) |
| 160 | + |
| 161 | +SELECT LEFT(LOWER(sub.firstname), 1) || |
| 162 | + RIGHT(LOWER(sub.firstname), 1) || |
| 163 | + LEFT(LOWER(sub.lastname), 1) || |
| 164 | + RIGHT(LOWER(sub.lastname), 1) || |
| 165 | + LENGTH(sub.firstname) || |
| 166 | + LENGTH(sub.lastname) || |
| 167 | + TRANSLATE(UPPER(a.name), ' ', '') |
| 168 | + AS pwd, |
| 169 | + a.primary_poc, |
| 170 | + a.name |
| 171 | +FROM accounts a |
| 172 | +JOIN sub |
| 173 | +ON sub.id = a.id; |
| 174 | + |
| 175 | + |
0 commit comments