├── 10.1 Importing Data into MySQL.sql ├── 10.2 Exporting Data to CSV.sql ├── 11.2 Removing Duplicates.sql ├── 11.3Standardizing Data.sql ├── 11.4 Breaking out Column into Multiple Columns.sql ├── 11.5 Working with NULL Values.sql ├── 11.6 Deleting Unused Columns.sql ├── 13.1 World Life Expectancy Data Cleaning.sql ├── 13.2 World Life Expectancy Exploratory Data Analysis.sql ├── 13.3 US Income Data Cleaning.sql ├── 13.4 US Income Exploratory Data Analysis.sql ├── 2.5 create-bakery-db.sql ├── 3.10 LIMIT Clause.sql ├── 3.11 Order BY Clause.sql ├── 3.12 Aliasing.sql ├── 3.2 Querying Basics.sql ├── 3.3 Select Statement.sql ├── 3.4 Where Clause.sql ├── 3.5 Comparison Operators.sql ├── 3.6 Logical Operators.sql ├── 3.7 IN Operator.sql ├── 3.8 Between Operator.sql ├── 3.9 Like Operator.sql ├── 4.2 Numeric Functions.sql ├── 4.3 String Functions.sql ├── 4.4 Date Functions.sql ├── 4.5 IF Function.sql ├── 4.6 Case Statement.sql ├── 4.7Cast and Convert Functions.sql ├── 5.2 Group By Basics.sql ├── 5.3 Aggregate Functions.sql ├── 5.4 Having vs Where Clause.sql ├── 5.5 Rollup.sql ├── 6.10 Unions.sql ├── 6.11 Join Use Cases.sql ├── 6.2 Inner Join.sql ├── 6.3 Joining Multiple Tables.sql ├── 6.4 Joining on Multiple Conditions.sql ├── 6.5 Outer Joins.sql ├── 6.6 Self Joins.sql ├── 6.7 Cross Joins.sql ├── 6.8 Natural Joins.sql ├── 6.9 Using Keyword.sql ├── 7.2 Subqueries.sql ├── 7.3 Any and All Statements.sql ├── 7.4 Exists Operator.sql ├── 7.5 Subqueries in Select and From Statements.sql ├── 8.2 Over + Partition By.sql ├── 8.3 Row_Number.sql ├── 8.4 Rank and Dense_rank.sql ├── 8.5 Lag and Lead.sql ├── 9.2 Regular Expression Methods.sql ├── 9.3 Regular Expression Metacharacters.sql └── README.md /10.1 Importing Data into MySQL.sql: -------------------------------------------------------------------------------- 1 | #Importing product_suggestions using Wizard 2 | 3 | # Option 1 4 | 5 | #Use import Wizard to import CSV 6 | 7 | 8 | # Option 2: 9 | 10 | # Create Table in Database, then import CSV into Table 11 | 12 | use bakery; 13 | 14 | CREATE TABLE product_suggestions2 ( 15 | product_suggestion_id INT, 16 | product_suggestion_name TEXT, 17 | date_received TEXT, 18 | PRIMARY KEY (product_suggestion_id) 19 | ); -------------------------------------------------------------------------------- /10.2 Exporting Data to CSV.sql: -------------------------------------------------------------------------------- 1 | # Exporting Data to CSV 2 | 3 | # Option 1 - exporting at query level 4 | # Option 2 - exporting at table level using Wizard 5 | 6 | 7 | SELECT * 8 | FROM ( 9 | SELECT c.customer_id, 10 | first_name, 11 | order_total, 12 | ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY order_total DESC) AS row_num 13 | FROM customers c 14 | JOIN customer_orders co 15 | ON c.customer_id = co.customer_id 16 | ) AS row_table 17 | WHERE row_num < 3 18 | ; -------------------------------------------------------------------------------- /11.2 Removing Duplicates.sql: -------------------------------------------------------------------------------- 1 | # ROW_ NUMBER 2 | #Row number is just going to assign a number to each row - simple, but pretty powerful too. 3 | 4 | #Okay so this is the query that we finished with in our last lesson. Let's reuse this to look at row_number 5 | SELECT c.customer_id, first_name, order_total, max(order_total) over(PARTITION BY first_name) as Max_order_total 6 | FROM customers c 7 | JOIN customer_orders co 8 | ON c.customer_id = co.customer_id; 9 | 10 | 11 | #We just need to say.... 12 | 13 | SELECT c.customer_id, first_name, order_total, ROW_NUMBER() OVER() 14 | FROM customers c 15 | JOIN customer_orders co 16 | ON c.customer_id = co.customer_id; 17 | 18 | #Now Let's use the partition by on first name like we did in the query above. 19 | 20 | SELECT c.customer_id, first_name, order_total, ROW_NUMBER() OVER(PARTITION BY first_name) 21 | FROM customers c 22 | JOIN custom -------------------------------------------------------------------------------- /11.3Standardizing Data.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AnalystBuilder/MySQL-for-Data-Analytics/c59cd755da64fcd83047d84f2ff01f3fefe72644/11.3Standardizing Data.sql -------------------------------------------------------------------------------- /11.4 Breaking out Column into Multiple Columns.sql: -------------------------------------------------------------------------------- 1 | # Breaking Column into Multiple Columns 2 | 3 | 4 | SELECT * 5 | FROM customer_sweepstakes 6 | ; 7 | 8 | SELECT address, SUBSTRING_INDEX(address,',',1) 9 | FROM customer_sweepstakes 10 | ; 11 | 12 | 13 | SELECT address, SUBSTRING_INDEX(address,',',1),SUBSTRING_INDEX(address,',',-1) 14 | FROM customer_sweepstakes 15 | ; 16 | 17 | SELECT address, 18 | SUBSTRING_INDEX(address,',',1) AS Street, 19 | SUBSTRING_INDEX(SUBSTRING_INDEX(address,',',2),',',-1) AS City, 20 | SUBSTRING_INDEX(address,',',-1) AS State 21 | FROM customer_sweepstakes 22 | ; 23 | 24 | SELECT * 25 | FROM customer_sweepstakes 26 | ; 27 | 28 | 29 | ALTER TABLE customer_sweepstakes 30 | ADD COLUMN street VARCHAR(50) AFTER address 31 | ; 32 | 33 | ALTER TABLE customer_sweepstakes 34 | ADD COLUMN city VARCHAR(50) AFTER street, 35 | ADD COLUMN state VARCHAR(50) AFTER city 36 | ; 37 | 38 | 39 | SELECT address, 40 | SUBSTRING_INDEX(address,',',1) AS Street, 41 | SUBSTRING_INDEX(SUBSTRING_INDEX(address,',',2),',',-1) AS City, 42 | SUBSTRING_INDEX(address,',',-1) AS State 43 | FROM customer_sweepstakes 44 | ; 45 | UPDATE customer_sweepstakes 46 | SET street = SUBSTRING_INDEX(address,',',1) 47 | ; 48 | UPDATE customer_sweepstakes 49 | SET city = SUBSTRING_INDEX(SUBSTRING_INDEX(address,',',2),',',-1) 50 | ; 51 | UPDATE customer_sweepstakes 52 | SET state = SUBSTRING_INDEX(address,',',-1) 53 | ; 54 | 55 | 56 | 57 | 58 | SELECT state, TRIM(state) 59 | FROM customer_sweepstakes 60 | ; 61 | 62 | 63 | UPDATE customer_sweepstakes 64 | SET state = UPPER(state) 65 | ; 66 | 67 | UPDATE customer_sweepstakes 68 | SET city = TRIM(city) 69 | ; 70 | 71 | UPDATE customer_sweepstakes 72 | SET state = TRIM(state) 73 | ; 74 | 75 | 76 | 77 | 78 | 79 | SELECT * 80 | FROM customer_sweepstakes 81 | ; 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | -------------------------------------------------------------------------------- /11.5 Working with NULL Values.sql: -------------------------------------------------------------------------------- 1 | # Working with NULL Values 2 | 3 | 4 | SELECT * 5 | FROM customer_sweepstakes 6 | ; 7 | 8 | UPDATE customer_sweepstakes 9 | SET phone = NULL 10 | WHERE phone = '' 11 | ; 12 | 13 | UPDATE customer_sweepstakes 14 | SET income = NULL 15 | WHERE income = '' 16 | ; 17 | 18 | SELECT AVG(income) 19 | FROM customer_sweepstakes 20 | ; 21 | 22 | SELECT AVG(income) 23 | FROM customer_sweepstakes 24 | WHERE income IS NOT NULL 25 | ; 26 | 27 | SELECT AVG(COALESCE(income,0)) 28 | FROM customer_sweepstakes 29 | ; 30 | 31 | 32 | SELECT birth_date, `Are you over 18?` 33 | FROM customer_sweepstakes 34 | WHERE (YEAR(NOW()) - 18) > YEAR(birth_date) 35 | ; 36 | 37 | UPDATE customer_sweepstakes 38 | SET `Are you over 18?` = 'N' 39 | WHERE (YEAR(NOW()) - 18) < YEAR(birth_date) 40 | ; 41 | 42 | UPDATE customer_sweepstakes 43 | SET `Are you over 18?` = 'Y' 44 | WHERE (YEAR(NOW()) - 18) > YEAR(birth_date) 45 | ; 46 | 47 | 48 | SELECT * 49 | FROM customer_sweepstakes 50 | ; 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /11.6 Deleting Unused Columns.sql: -------------------------------------------------------------------------------- 1 | # Deleting Unused Columns 2 | 3 | 4 | SELECT * 5 | FROM customer_sweepstakes 6 | ; 7 | 8 | ALTER TABLE customer_sweepstakes 9 | DROP COLUMN address 10 | ; 11 | 12 | ALTER TABLE customer_sweepstakes 13 | DROP COLUMN favorite_color 14 | ; 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /13.1 World Life Expectancy Data Cleaning.sql: -------------------------------------------------------------------------------- 1 | # World Life Expectancy Project (Data Cleaning) 2 | 3 | 4 | SELECT * 5 | FROM world_life_expectancy 6 | ; 7 | 8 | 9 | SELECT Country, Year, CONCAT(Country, Year), COUNT(CONCAT(Country, Year)) 10 | FROM world_life_expectancy 11 | GROUP BY Country, Year, CONCAT(Country, Year) 12 | HAVING COUNT(CONCAT(Country, Year)) > 1 13 | ; 14 | 15 | SELECT * 16 | FROM ( 17 | SELECT Row_ID, 18 | CONCAT(Country, Year), 19 | ROW_NUMBER() OVER( PARTITION BY CONCAT(Country, Year) ORDER BY CONCAT(Country, Year)) as Row_Num 20 | FROM world_life_expectancy 21 | ) AS Row_table 22 | WHERE Row_Num > 1 23 | ; 24 | 25 | DELETE FROM world_life_expectancy 26 | WHERE 27 | Row_ID IN ( 28 | SELECT Row_ID 29 | FROM ( 30 | SELECT Row_ID, 31 | CONCAT(Country, Year), 32 | ROW_NUMBER() OVER( PARTITION BY CONCAT(Country, Year) ORDER BY CONCAT(Country, Year)) as Row_Num 33 | FROM world_life_expectancy 34 | ) AS Row_table 35 | WHERE Row_Num > 1 36 | ) 37 | ; 38 | 39 | 40 | 41 | 42 | 43 | SELECT * 44 | FROM world_life_expectancy 45 | WHERE Status IS NULL 46 | ; 47 | 48 | SELECT DISTINCT(Status) 49 | FROM world_life_expectancy 50 | WHERE Status <> '' 51 | ; 52 | 53 | SELECT DISTINCT(Country) 54 | FROM world_life_expectancy 55 | WHERE Status = 'Developing'; 56 | 57 | UPDATE world_life_expectancy 58 | SET Status = 'Developing' 59 | WHERE Country IN (SELECT DISTINCT(Country) 60 | FROM world_life_expectancy 61 | WHERE Status = 'Developing'); 62 | 63 | 64 | UPDATE world_life_expectancy t1 65 | JOIN world_life_expectancy t2 66 | ON t1.Country = t2.Country 67 | SET t1.Status = 'Developing' 68 | WHERE t1.Status = '' 69 | AND t2.Status <> '' 70 | AND t2.Status = 'Developing' 71 | ; 72 | 73 | SELECT * 74 | FROM world_life_expectancy 75 | WHERE Country = 'United States of America' 76 | ; 77 | 78 | UPDATE world_life_expectancy t1 79 | JOIN world_life_expectancy t2 80 | ON t1.Country = t2.Country 81 | SET t1.Status = 'Developed' 82 | WHERE t1.Status = '' 83 | AND t2.Status <> '' 84 | AND t2.Status = 'Developed' 85 | ; 86 | 87 | 88 | SELECT * 89 | FROM world_life_expectancy 90 | #WHERE `Life expectancy` = '' 91 | ; 92 | 93 | 94 | 95 | 96 | SELECT Country, Year, `Life expectancy` 97 | FROM world_life_expectancy 98 | WHERE `Life expectancy` = '' 99 | ; 100 | 101 | 102 | 103 | SELECT t1.Country, t1.Year, t1.`Life expectancy`, 104 | t2.Country, t2.Year, t2.`Life expectancy`, 105 | t3.Country, t3.Year, t3.`Life expectancy`, 106 | ROUND((t2.`Life expectancy` + t3. `Life expectancy`)/2,1) 107 | FROM world_life_expectancy t1 108 | JOIN world_life_expectancy t2 109 | ON t1.Country = t2.Country 110 | AND t1.Year = t2.Year - 1 111 | JOIN world_life_expectancy t3 112 | ON t1.Country = t3.Country 113 | AND t1.Year = t3.Year + 1 114 | WHERE t1.`Life expectancy` = '' 115 | ; 116 | 117 | 118 | UPDATE world_life_expectancy t1 119 | JOIN world_life_expectancy t2 120 | ON t1.Country = t2.Country 121 | AND t1.Year = t2.Year - 1 122 | JOIN world_life_expectancy t3 123 | ON t1.Country = t3.Country 124 | AND t1.Year = t3.Year + 1 125 | SET t1.`Life expectancy` = ROUND((t2.`Life expectancy` + t3. `Life expectancy`)/2,1) 126 | WHERE t1.`Life expectancy` = '' 127 | ; 128 | 129 | 130 | SELECT * 131 | FROM world_life_expectancy 132 | #WHERE `Life expectancy` = '' 133 | ; 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | -------------------------------------------------------------------------------- /13.2 World Life Expectancy Exploratory Data Analysis.sql: -------------------------------------------------------------------------------- 1 | # World Life Expectancy Project (Exploratory Data Analysis) 2 | 3 | SELECT * 4 | FROM world_life_expectancy 5 | ; 6 | 7 | 8 | SELECT Country, 9 | MIN(`Life expectancy`), 10 | MAX(`Life expectancy`), 11 | ROUND(MAX(`Life expectancy`) - MIN(`Life expectancy`),1) AS Life_Increase_15_Years 12 | FROM world_life_expectancy 13 | GROUP BY Country 14 | HAVING MIN(`Life expectancy`) <> 0 15 | AND MAX(`Life expectancy`) <> 0 16 | ORDER BY Life_Increase_15_Years ASC 17 | ; 18 | 19 | 20 | 21 | SELECT Year, ROUND(AVG(`Life expectancy`),2) 22 | FROM world_life_expectancy 23 | WHERE `Life expectancy` <> 0 24 | AND `Life expectancy` <> 0 25 | GROUP BY Year 26 | ORDER BY Year 27 | ; 28 | 29 | SELECT * 30 | FROM world_life_expectancy 31 | ; 32 | 33 | SELECT Country, ROUND(AVG(`Life expectancy`),1) AS Life_Exp, ROUND(AVG(GDP),1) AS GDP 34 | FROM world_life_expectancy 35 | GROUP BY Country 36 | HAVING Life_Exp > 0 37 | AND GDP > 0 38 | ORDER BY GDP DESC 39 | ; 40 | 41 | 42 | 43 | SELECT 44 | SUM(CASE WHEN GDP >= 1500 THEN 1 ELSE 0 END) High_GDP_Count, 45 | AVG(CASE WHEN GDP >= 1500 THEN `Life expectancy` ELSE NULL END) High_GDP_Life_Expectancy, 46 | SUM(CASE WHEN GDP <= 1500 THEN 1 ELSE 0 END) Low_GDP_Count, 47 | AVG(CASE WHEN GDP <= 1500 THEN `Life expectancy` ELSE NULL END) Low_GDP_Life_Expectancy 48 | FROM world_life_expectancy 49 | ; 50 | 51 | SELECT * 52 | FROM world_life_expectancy 53 | ; 54 | 55 | SELECT Status, ROUND(AVG(`Life expectancy`),1) 56 | FROM world_life_expectancy 57 | GROUP BY Status 58 | ; 59 | 60 | 61 | SELECT Status, COUNT(DISTINCT Country), ROUND(AVG(`Life expectancy`),1) 62 | FROM world_life_expectancy 63 | GROUP BY Status 64 | ; 65 | 66 | 67 | 68 | SELECT Country, ROUND(AVG(`Life expectancy`),1) AS Life_Exp, ROUND(AVG(BMI),1) AS BMI 69 | FROM world_life_expectancy 70 | GROUP BY Country 71 | HAVING Life_Exp > 0 72 | AND BMI > 0 73 | ORDER BY BMI ASC 74 | ; 75 | 76 | 77 | SELECT Country, 78 | Year, 79 | `Life expectancy`, 80 | `Adult Mortality`, 81 | SUM(`Adult Mortality`) OVER(PARTITION BY Country ORDER BY Year) AS Rolling_Total 82 | FROM world_life_expectancy 83 | WHERE Country LIKE '%United%' 84 | ; 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | -------------------------------------------------------------------------------- /13.3 US Income Data Cleaning.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AnalystBuilder/MySQL-for-Data-Analytics/c59cd755da64fcd83047d84f2ff01f3fefe72644/13.3 US Income Data Cleaning.sql -------------------------------------------------------------------------------- /13.4 US Income Exploratory Data Analysis.sql: -------------------------------------------------------------------------------- 1 | # US Household Data Exploration 2 | 3 | 4 | SELECT * 5 | FROM household_income.ushouseholdincome; 6 | 7 | SELECT * 8 | FROM household_income.ushouseholdincome_statistics; 9 | 10 | SELECT State_Name, ALand, AWater 11 | FROM household_income.ushouseholdincome; 12 | 13 | SELECT State_Name, SUM(ALand), SUM(AWater) 14 | FROM household_income.ushouseholdincome 15 | GROUP BY State_Name 16 | ORDER BY 2; 17 | 18 | 19 | SELECT State_Name, SUM(ALand), SUM(AWater) 20 | FROM household_income.ushouseholdincome 21 | GROUP BY State_Name 22 | ORDER BY 2 DESC 23 | LIMIT 10; 24 | 25 | SELECT State_Name, SUM(ALand), SUM(AWater) 26 | FROM household_income.ushouseholdincome 27 | GROUP BY State_Name 28 | ORDER BY 3 DESC 29 | LIMIT 10; 30 | 31 | 32 | SELECT * 33 | FROM household_income.ushouseholdincome u 34 | JOIN household_income.ushouseholdincome_statistics us 35 | ON u.id = us.id; 36 | 37 | 38 | SELECT u.State_Name, County, `Type`, `Primary`, Mean, Median 39 | FROM household_income.ushouseholdincome u 40 | JOIN household_income.ushouseholdincome_statistics us 41 | ON u.id = us.id; 42 | 43 | 44 | SELECT u.State_Name, AVG(Mean), AVG(Median) 45 | FROM household_income.ushouseholdincome u 46 | JOIN household_income.ushouseholdincome_statistics us 47 | ON u.id = us.id 48 | GROUP BY u.State_Name 49 | order by 3; 50 | 51 | 52 | SELECT u.State_Name, AVG(Mean), AVG(Median) 53 | FROM household_income.ushouseholdincome u 54 | JOIN household_income.ushouseholdincome_statistics us 55 | ON u.id = us.id 56 | GROUP BY u.State_Name 57 | order by 3; 58 | 59 | 60 | SELECT `Type`, `Primary`, AVG(Mean), AVG(Median) 61 | FROM household_income.ushouseholdincome u 62 | JOIN household_income.ushouseholdincome_statistics us 63 | ON u.id = us.id 64 | GROUP BY `Type`, `Primary` 65 | order by 3; 66 | 67 | 68 | SELECT `Type`, AVG(Mean), AVG(Median) 69 | FROM household_income.ushouseholdincome u 70 | JOIN household_income.ushouseholdincome_statistics us 71 | ON u.id = us.id 72 | GROUP BY `Type` 73 | order by 3; 74 | 75 | 76 | SELECT `Type`,COUNT(`Type`), AVG(Mean), AVG(Median) 77 | FROM household_income.ushouseholdincome u 78 | JOIN household_income.ushouseholdincome_statistics us 79 | ON u.id = us.id 80 | GROUP BY `Type` 81 | order by 4; 82 | 83 | 84 | SELECT `Type`,COUNT(`Type`), AVG(Mean), AVG(Median) 85 | FROM household_income.ushouseholdincome u 86 | JOIN household_income.ushouseholdincome_statistics us 87 | ON u.id = us.id 88 | GROUP BY `Type` 89 | HAVING COUNT(`Type`) > 100 90 | order by 4; 91 | 92 | 93 | SELECT * 94 | FROM household_income.ushouseholdincome u 95 | JOIN household_income.ushouseholdincome_statistics us 96 | ON u.id = us.id; 97 | 98 | SELECT u.State_Name, City, AVG(Mean), AVG(Median) 99 | FROM household_income.ushouseholdincome u 100 | JOIN household_income.ushouseholdincome_statistics us 101 | ON u.id = us.id 102 | GROUP BY u.State_Name, City 103 | HAVING AVG(Mean) IS NOT NULL 104 | ORDER BY 3 ASC 105 | LIMIT 10; 106 | 107 | 108 | 109 | #Highest 110 | SELECT u.State_Name, City, AVG(Mean), AVG(Median) 111 | FROM household_income.ushouseholdincome u 112 | JOIN household_income.ushouseholdincome_statistics us 113 | ON u.id = us.id 114 | GROUP BY u.State_Name, City 115 | ORDER BY 3 DESC 116 | LIMIT 10; 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /2.5 create-bakery-db.sql: -------------------------------------------------------------------------------- 1 | 2 | DROP DATABASE IF EXISTS `bakery`; 3 | CREATE DATABASE `bakery`; 4 | USE `bakery`; 5 | 6 | CREATE TABLE `products` ( 7 | `product_id` int(11) NOT NULL AUTO_INCREMENT, 8 | `product_name` varchar(50) NOT NULL, 9 | `units_in_stock` int(11) NOT NULL, 10 | `sale_price` decimal(4,2) NOT NULL, 11 | PRIMARY KEY (`product_id`) 12 | ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 13 | INSERT INTO `products` VALUES (1001,'Chocolate Chip Cookie',200,1.50); 14 | INSERT INTO `products` VALUES (1002,'Banana Nut Muffin',180,2.50); 15 | INSERT INTO `products` VALUES (1003,'Croissant',70,1.75); 16 | INSERT INTO `products` VALUES (1004,'Cheese Danish',55,1.85); 17 | INSERT INTO `products` VALUES (1005,'Cannoli',112,2.25); 18 | INSERT INTO `products` VALUES (1006,'Sweet Bread Loaf',32,15.50); 19 | INSERT INTO `products` VALUES (1007,'Strawberry Macaron',98,2.00); 20 | INSERT INTO `products` VALUES (1008,'Coffee Cake',25,13.00); 21 | INSERT INTO `products` VALUES (1009,'Carrot Cake',15,14.50); 22 | INSERT INTO `products` VALUES (1010,'Chocolate Covered Doughnut',80,1.00); 23 | 24 | 25 | CREATE TABLE `suppliers` ( 26 | `supplier_id` smallint(6) NOT NULL AUTO_INCREMENT, 27 | `name` varchar(50) NOT NULL, 28 | PRIMARY KEY (`supplier_id`) 29 | ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 30 | INSERT INTO `suppliers` VALUES (1,'Bakery LLC'); 31 | INSERT INTO `suppliers` VALUES (2,'Goods 4 U'); 32 | INSERT INTO `suppliers` VALUES (3,'Savory Loaf Delivery Co.'); 33 | INSERT INTO `suppliers` VALUES (4,'Mrs. Yums'); 34 | INSERT INTO `suppliers` VALUES (5,'Grain to Table LLC'); 35 | 36 | 37 | CREATE TABLE `supplier_delivery_status` ( 38 | `order_status_id` tinyint(4) NOT NULL, 39 | `name` varchar(50) NOT NULL, 40 | PRIMARY KEY (`order_status_id`) 41 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 42 | INSERT INTO `supplier_delivery_status` VALUES (1,'Processed'); 43 | INSERT INTO `supplier_delivery_status` VALUES (2,'Shipped'); 44 | INSERT INTO `supplier_delivery_status` VALUES (3,'Delivered'); 45 | 46 | 47 | CREATE TABLE `ordered_items` ( 48 | `order_id` int(11) NOT NULL AUTO_INCREMENT, 49 | `product_id` int(11) NOT NULL, 50 | `status` tinyint(4) NOT NULL DEFAULT '1', 51 | `quantity` int(11) NOT NULL, 52 | `unit_price` decimal(4,2) NOT NULL, 53 | `shipped_date` date DEFAULT NULL, 54 | `shipper_id` smallint(6) DEFAULT NULL, 55 | PRIMARY KEY (`order_id`,`product_id`), 56 | KEY `fk_orders_items_order_idx` (`order_id`), 57 | KEY `fk_order_items_products_idx` (`product_id`), 58 | KEY `fk_orders_order_statuses_idx` (`status`), 59 | CONSTRAINT `fk_order_items_orders` FOREIGN KEY (`order_id`) REFERENCES `ordered_items` (`order_id`) ON UPDATE CASCADE, 60 | CONSTRAINT `fk_orders_order_statuses` FOREIGN KEY (`status`) REFERENCES `supplier_delivery_status` (`order_status_id`) ON UPDATE CASCADE, 61 | CONSTRAINT `fk_order_items_products` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`) ON UPDATE CASCADE, 62 | CONSTRAINT `fk_orders_shippers` FOREIGN KEY (`shipper_id`) REFERENCES `suppliers` (`supplier_id`) ON UPDATE CASCADE 63 | 64 | ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 65 | INSERT INTO `ordered_items` VALUES (1,1004,1,53,0.35,'2021-08-15',1); 66 | INSERT INTO `ordered_items` VALUES (2,1001,2,73,0.29,'2022-03-21',2); 67 | INSERT INTO `ordered_items` VALUES (2,1004,3,10,0.35,'2022-02-07',5); 68 | INSERT INTO `ordered_items` VALUES (2,1006,2,63,5.28,'2021-06-09',4); 69 | INSERT INTO `ordered_items` VALUES (3,1003,1,21,0.50,'2021-09-06',1); 70 | INSERT INTO `ordered_items` VALUES (4,1003,2,85,0.50,'2022-06-22',3); 71 | INSERT INTO `ordered_items` VALUES (4,1010,3,42,0.39,'2021-05-13',4); 72 | INSERT INTO `ordered_items` VALUES (5,1002,1,100,1.89,'2022-02-03',2); 73 | INSERT INTO `ordered_items` VALUES (6,1001,2,35,0.29,'2021-11-06',3); 74 | INSERT INTO `ordered_items` VALUES (6,1002,2,54,1.89,'2022-12-23',5); 75 | INSERT INTO `ordered_items` VALUES (6,1003,3,10,0.50,'2022-04-05',1); 76 | INSERT INTO `ordered_items` VALUES (6,1005,3,55,0.47,'2021-05-22',2); 77 | INSERT INTO `ordered_items` VALUES (7,1003,3,12,0.50,'2022-06-26',1); 78 | INSERT INTO `ordered_items` VALUES (8,1005,2,70,0.47,'2021-09-21',5); 79 | INSERT INTO `ordered_items` VALUES (8,1008,2,96,8.59,'2022-11-10',3); 80 | INSERT INTO `ordered_items` VALUES (9,1006,3,43,5.28,'2022-10-15',1); 81 | INSERT INTO `ordered_items` VALUES (10,1001,1,33,0.29,'2022-01-06',1); 82 | INSERT INTO `ordered_items` VALUES (10,1009,3,23,4.28,'2022-07-23',1); 83 | 84 | 85 | CREATE TABLE `customers` ( 86 | `customer_id` int(11) NOT NULL AUTO_INCREMENT, 87 | `first_name` varchar(50) NOT NULL, 88 | `last_name` varchar(50) NOT NULL, 89 | `birth_date` date DEFAULT NULL, 90 | `phone` varchar(50) DEFAULT NULL, 91 | `address` varchar(50) NOT NULL, 92 | `city` varchar(50) NOT NULL, 93 | `state` char(2) NOT NULL, 94 | `total_money_spent` int(11) NOT NULL DEFAULT '0', 95 | PRIMARY KEY (`customer_id`) 96 | ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 97 | INSERT INTO `customers` VALUES (100101,'Kevin','Malone','1989-04-28','635-573-9754','1229 Main Street','Scranton','PA',11000); 98 | INSERT INTO `customers` VALUES (100102,'Charles','Xavier','1965-04-11','729-287-9456','123 North Hill Drive','Dallas','TX',947); 99 | INSERT INTO `customers` VALUES (100103,'Finley','Danish','1999-02-07','126-583-7856','432 Hilly Road','Austin','TX',534); 100 | INSERT INTO `customers` VALUES (100104,'Obi','Kenobi','1921-04-22','975-357-7663','101 Alpine Avenue','New York','NY',3567); 101 | INSERT INTO `customers` VALUES (100105,'Don','Draper','1948-11-07',NULL,'12 South Main Lane','San Francisco','CA',195); 102 | INSERT INTO `customers` VALUES (100106,'Frodo','Baggins','2001-09-04',NULL,'1 Pastery Lane','Chicago','IL',56); 103 | INSERT INTO `customers` VALUES (100107,'Michael','Scott','1978-08-20','235-357-3464','987 Croissant Street','Scranton','PA',2536); 104 | INSERT INTO `customers` VALUES (100108,'Maggie','Muffin','2001-07-06','906-485-1542','701 North Street','Sarasota','FL',1009); 105 | INSERT INTO `customers` VALUES (100109,'Kelly','Kapoor','1987-05-30','674-357-9151','62810 Julip Lane','Scranton','PA',540); 106 | INSERT INTO `customers` VALUES (100110,'Anakin','Skywalker','1934-10-15','346-458-3370','122 South Street','Charleston','SC',36); 107 | 108 | 109 | CREATE TABLE `customer_orders` ( 110 | `order_id` int(11) NOT NULL AUTO_INCREMENT, 111 | `customer_id` int(11) NOT NULL, 112 | `product_id` int(11) NOT NULL, 113 | `order_date` date NOT NULL, 114 | `order_total` decimal(4,2) NOT NULL, 115 | `tip` varchar(2000) DEFAULT NULL, 116 | PRIMARY KEY (`order_id`), 117 | KEY `fk_customers_orders__idx` (`customer_id`), 118 | KEY `fk_orders_shippers_idx` (`product_id`), 119 | CONSTRAINT `fk_orders_customers` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`) ON UPDATE CASCADE, 120 | CONSTRAINT `fk_customer_orders` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`) ON UPDATE CASCADE 121 | ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 122 | INSERT INTO `customer_orders` VALUES (1,100101,1001,'2020-01-30',26.24,2); 123 | INSERT INTO `customer_orders` VALUES (2,100110,1002,'2021-08-25',6.19,1); 124 | INSERT INTO `customer_orders` VALUES (3,100106,1005,'2022-12-12',3.87,0); 125 | INSERT INTO `customer_orders` VALUES (4,100103,1007,'2018-03-22',4.00,5); 126 | INSERT INTO `customer_orders` VALUES (5,100102,1003,'2017-08-25',9.97,10); 127 | INSERT INTO `customer_orders` VALUES (6,100108,1009,'2018-11-18',87.01,1); 128 | INSERT INTO `customer_orders` VALUES (7,100101,1001,'2022-09-20',2.45,5); 129 | INSERT INTO `customer_orders` VALUES (8,100104,1008,'2018-06-08',16.42,0); 130 | INSERT INTO `customer_orders` VALUES (9,100105,1007,'2019-07-05',8.11,1); 131 | INSERT INTO `customer_orders` VALUES (10,100106,1006,'2018-04-22',53.12,3); 132 | INSERT INTO `customer_orders` VALUES (11,100103,1001,'2019-11-18',27.01,1); 133 | INSERT INTO `customer_orders` VALUES (12,100101,1003,'2018-09-20',10.45,5); 134 | INSERT INTO `customer_orders` VALUES (13,100106,1008,'2020-06-08',90.42,0); 135 | INSERT INTO `customer_orders` VALUES (14,100102,1009,'2022-07-05',11.11,1); 136 | INSERT INTO `customer_orders` VALUES (15,100104,1006,'2020-04-22',24.12,3); 137 | 138 | 139 | 140 | CREATE TABLE `customer_orders_review` ( 141 | `order_id` int(11) NOT NULL AUTO_INCREMENT, 142 | `customer_id` int(11) NOT NULL, 143 | `product_id` int(11) NOT NULL, 144 | `order_date` date NOT NULL, 145 | `Rating 1-10` int(11) NOT NULL, 146 | PRIMARY KEY (`order_id`) 147 | ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 148 | INSERT INTO `customer_orders_review` VALUES (1,100101,1001,'2020-01-30',8); 149 | INSERT INTO `customer_orders_review` VALUES (2,100110,1002,'2021-08-25',5); 150 | INSERT INTO `customer_orders_review` VALUES (3,100111,1005,'2022-12-12',10); 151 | INSERT INTO `customer_orders_review` VALUES (4,100103,1007,'2081-03-22',7); 152 | INSERT INTO `customer_orders_review` VALUES (5,100102,1003,'2017-08-25',6); 153 | INSERT INTO `customer_orders_review` VALUES (7,100101,1001,'2022-09-20',8); 154 | INSERT INTO `customer_orders_review` VALUES (8,100104,1008,'2018-06-08',9); 155 | INSERT INTO `customer_orders_review` VALUES (9,100105,1007,'2019-07-05',6); 156 | INSERT INTO `customer_orders_review` VALUES (10,100106,1006,'2018-04-22',8); 157 | INSERT INTO `customer_orders_review` VALUES (11,100103,1001,'2019-11-18',6); 158 | INSERT INTO `customer_orders_review` VALUES (12,1001001,1003,'2018-09-20',9); 159 | INSERT INTO `customer_orders_review` VALUES (13,100106,1008,'2020-06-08',10); 160 | INSERT INTO `customer_orders_review` VALUES (14,100102,1009,'2023-07-05',8); 161 | INSERT INTO `customer_orders_review` VALUES (15,100104,1006,'2020-04-22',7); 162 | 163 | 164 | 165 | 166 | 167 | CREATE TABLE `employees` ( 168 | `employee_id` int(11) NOT NULL AUTO_INCREMENT, 169 | `first_name` varchar(50) NOT NULL, 170 | `last_name` varchar(50) NOT NULL, 171 | `department` varchar(50) NOT NULL, 172 | `title` varchar(50) NOT NULL, 173 | `salary` int(11) NOT NULL, 174 | PRIMARY KEY (`employee_id`) 175 | ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 176 | INSERT INTO `employees` VALUES (1,'Christine','Freberg','Bakery','Lead Baker', 70000); 177 | INSERT INTO `employees` VALUES (2,'Dwight','Schrute','Bakery','Assistant to the Lead Baker', 45000); 178 | INSERT INTO `employees` VALUES (3,'Tom','Haveford','Bakery','Chocolatier', 45000); 179 | INSERT INTO `employees` VALUES (4,'Ann','Perkins','Bakery','Bakery Clerk', 30000); 180 | INSERT INTO `employees` VALUES (5,'Carl','Lorthner','Bakery','Dough Maker', 40000); 181 | INSERT INTO `employees` VALUES (6,'Ron','Swanson','Marketing','Director of Marketing', 75000); 182 | INSERT INTO `employees` VALUES (7,'Troy','Barnes','Marketing','Lead Marketer', 60000); 183 | INSERT INTO `employees` VALUES (8,'Jeff','Winger','Marketing','Marketing Analyst', 60000); 184 | INSERT INTO `employees` VALUES (9,'Annie','Edison','Marketing','Social Media Marketer', 65000); 185 | 186 | 187 | 188 | 189 | -------------------------------------------------------------------------------- /3.10 LIMIT Clause.sql: -------------------------------------------------------------------------------- 1 | # LIMIT Clause 2 | 3 | 4 | SELECT * 5 | FROM customers 6 | -- WHERE total_money_spent > 10000 7 | ORDER BY total_money_spent DESC 8 | LIMIT 5 9 | ; 10 | 11 | 12 | SELECT * 13 | FROM customers 14 | -- WHERE total_money_spent > 10000 15 | ORDER BY total_money_spent DESC 16 | LIMIT 2, 5 17 | ; -------------------------------------------------------------------------------- /3.11 Order BY Clause.sql: -------------------------------------------------------------------------------- 1 | # Order By 2 | 3 | 4 | SELECT * 5 | FROM customers 6 | ORDER BY first_name DESC 7 | ; 8 | 9 | SELECT * 10 | FROM customers 11 | ORDER BY state DESC, total_money_spent DESC 12 | ; 13 | 14 | SELECT * 15 | FROM customers 16 | ORDER BY 8 DESC, 9 ASC 17 | ; 18 | 19 | -------------------------------------------------------------------------------- /3.12 Aliasing.sql: -------------------------------------------------------------------------------- 1 | # Aliasing 2 | 3 | 4 | SELECT product_name AS 'Goodie Name', units_in_stock 'uis' 5 | FROM products 6 | ; 7 | 8 | SELECT units_in_stock * sale_price AS Potential_Revenue 9 | FROM products 10 | ; 11 | 12 | 13 | SELECT p.units_in_stock * p.sale_price AS Potential_Revenue, c.colum_name 14 | FROM products p 15 | JOIN customer c 16 | ; 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /3.2 Querying Basics.sql: -------------------------------------------------------------------------------- 1 | #This is the bakery database 2 | # This is also a note 3 | -- This is a note 4 | USE bakery; 5 | 6 | 7 | SELECT customer_id, first_name 8 | FROM customers; 9 | 10 | 11 | SELECT * 12 | FROM customer_orders 13 | WHERE product_id = 1001 14 | ; -------------------------------------------------------------------------------- /3.3 Select Statement.sql: -------------------------------------------------------------------------------- 1 | SELECT last_name, 2 | first_name, 3 | birth_date, 4 | phone, 5 | city, 6 | state, 7 | total_money_spent, 8 | (total_money_spent + 100) * 10 9 | FROM customers; 10 | 11 | # PEMDAS 12 | 13 | SELECT * -- state 14 | FROM customers; 15 | 16 | SELECT DISTINCT city, state 17 | FROM customers; 18 | 19 | 20 | -------------------------------------------------------------------------------- /3.4 Where Clause.sql: -------------------------------------------------------------------------------- 1 | # WHERE 2 | 3 | 4 | SELECT * 5 | FROM customers 6 | WHERE total_money_spent > 3000 7 | ; 8 | 9 | 10 | SELECT * 11 | FROM customers 12 | WHERE city = 'Scranton' 13 | ; 14 | 15 | 16 | SELECT * 17 | FROM customers 18 | WHERE birth_date > '1985-01-01' 19 | ; 20 | 21 | 22 | SELECT * 23 | FROM products 24 | WHERE units_in_stock < 30 25 | ; 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /3.5 Comparison Operators.sql: -------------------------------------------------------------------------------- 1 | # Comparison Operators 2 | 3 | # =, <>, >,< 4 | 5 | 6 | SELECT * 7 | FROM bakery.customer_orders 8 | WHERE tip = 1 9 | ; 10 | 11 | 12 | SELECT * 13 | FROM bakery.customer_orders 14 | WHERE tip != 1 15 | ; 16 | 17 | 18 | SELECT * 19 | FROM bakery.customer_orders 20 | WHERE tip > 5 21 | ; 22 | 23 | 24 | SELECT * 25 | FROM bakery.customer_orders 26 | WHERE tip >= 5 27 | ; 28 | 29 | 30 | 31 | SELECT * 32 | FROM bakery.customer_orders 33 | WHERE tip <= 5 34 | ; 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /3.6 Logical Operators.sql: -------------------------------------------------------------------------------- 1 | 2 | SELECT co.order_id, first_name, c.customer_id 3 | FROM bakery.customer_orders as co 4 | RIGHT OUTER JOIN bakery.customers as c 5 | ON co.customer_id = c.customer_id 6 | ORDER BY c.customer_id, co.order_id ; 7 | 8 | 9 | #Since the column names are the same we can use USING. Let's try it out 10 | SELECT co.order_id, first_name, c.customer_id 11 | FROM bakery.customer_orders as co 12 | RIGHT OUTER JOIN bakery.customers as c 13 | USING(customer_id) 14 | ORDER BY c.customer_id, co.order_id ; 15 | 16 | #There's absolutely nothing wrong with doing this. It gives the exact same output and performs the same job. 17 | #It may be cleaner as well. It's more of a personal preference. I don't personally use them si -------------------------------------------------------------------------------- /3.7 IN Operator.sql: -------------------------------------------------------------------------------- 1 | # IN Operator 2 | 3 | 4 | 5 | SELECT * 6 | FROM customers 7 | WHERE state = 'PA' OR city = 'Dallas' OR address = 'IL' 8 | ; 9 | 10 | SELECT * 11 | FROM customers 12 | WHERE state IN ('PA','TX', 'IL') 13 | ; 14 | 15 | 16 | 17 | SELECT * 18 | FROM customers 19 | WHERE first_name NOT IN ('Kevin','Kelly','Frodo') 20 | ; 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /3.8 Between Operator.sql: -------------------------------------------------------------------------------- 1 | # Between Operator 2 | 3 | 4 | SELECT * 5 | FROM customers 6 | WHERE total_money_spent BETWEEN 534 AND 1009 7 | ; 8 | 9 | SELECT * 10 | FROM customers 11 | WHERE total_money_spent >= 534 AND total_money_spent <= 1009 12 | ; 13 | 14 | SELECT * 15 | FROM customers 16 | WHERE birth_date BETWEEN '1990-01-01' AND '2020-01-01' 17 | ; 18 | 19 | SELECT * 20 | FROM customers 21 | WHERE city BETWEEN 'A' AND 'Z' 22 | ; 23 | 24 | #Dall 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /3.9 Like Operator.sql: -------------------------------------------------------------------------------- 1 | # Like Operator 2 | 3 | # % - zero, one, or multiple characters 4 | # _ - single character 5 | 6 | SELECT * 7 | FROM customers 8 | WHERE first_name LIKE 'k%' 9 | ; 10 | 11 | SELECT * 12 | FROM customers 13 | WHERE first_name LIKE '%n%' 14 | ; 15 | 16 | 17 | SELECT * 18 | FROM customers 19 | WHERE first_name LIKE '%kin' 20 | ; 21 | 22 | 23 | SELECT * 24 | FROM customers 25 | WHERE last_name LIKE 's_____%' 26 | ; 27 | 28 | SELECT * 29 | FROM customers 30 | WHERE phone LIKE '975%' 31 | ; 32 | 33 | 34 | -------------------------------------------------------------------------------- /4.2 Numeric Functions.sql: -------------------------------------------------------------------------------- 1 | # Numeric Functions 2 | 3 | 4 | SELECT * 5 | FROM products 6 | ; 7 | 8 | 9 | 10 | SELECT ROUND(123.456789,2); 11 | 12 | 13 | SELECT sale_price, ROUND(sale_price,1); 14 | 15 | 16 | 17 | SELECT CEILING(5.3); 18 | 19 | SELECT FLOOR(5.7); 20 | 21 | SELECT sale_price, CEILING(sale_price), FLOOR(sale_price); 22 | 23 | 24 | 25 | SELECT ABS(-4.6); 26 | 27 | 28 | 29 | FROM products; 30 | 31 | 32 | -------------------------------------------------------------------------------- /4.3 String Functions.sql: -------------------------------------------------------------------------------- 1 | # String Functions 2 | 3 | 4 | SELECT * 5 | FROM customers 6 | ; 7 | 8 | 9 | SELECT LENGTH('sky'); 10 | 11 | SELECT first_name, LENGTH(first_name) as Len_first 12 | FROM customers 13 | ORDER BY Len_first 14 | ; 15 | 16 | SELECT first_name, UPPER(first_name), LOWER(first_name) 17 | FROM customers; 18 | 19 | 20 | SELECT * 21 | FROM customers; 22 | 23 | SELECT ' sky ',TRIM(' sky '), LTRIM(' sky '), RTRIM(' sky '); 24 | 25 | 26 | SELECT 'I Love SQL', TRIM('I Love SQL'); 27 | 28 | 29 | SELECT LEFT('Alexander',4); 30 | 31 | SELECT first_name, LEFT(first_name,3), RIGHT(first_name,3) 32 | FROM customers 33 | ; 34 | 35 | 36 | SELECT SUBSTRING('Alexander',2,3); 37 | 38 | SELECT phone, 39 | SUBSTRING(phone,1,3), 40 | SUBSTRING(phone,5,3), 41 | SUBSTRING(phone,9,4), 42 | CONCAT(SUBSTRING(phone,1,3),SUBSTRING(phone,5,3),SUBSTRING(phone,9,4)) 43 | FROM customers 44 | ; 45 | 46 | 47 | SELECT REPLACE(first_name,'a','z') 48 | FROM customers 49 | ; 50 | 51 | SELECT REPLACE(phone,'-','') 52 | FROM customers 53 | ; 54 | 55 | 56 | SELECT LOCATE('x','Alexander'); 57 | 58 | SELECT first_name, LOCATE('Mic',first_name) 59 | FROM customers 60 | ; 61 | 62 | 63 | SELECT CONCAT('Alex',' Freberg'); 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /4.4 Date Functions.sql: -------------------------------------------------------------------------------- 1 | this column which was named poorly 2 | 3 | SELECT Are you over 18? 4 | FROM customer_sweepstakes; 5 | #This doesn't work 6 | 7 | #Let's try quotes nd double quotes 8 | SELECT 'Are you over 18?' 9 | FROM customer_sweepstakes; 10 | 11 | 12 | #What you need to use is what's called a backtik. On my computer it's right next to the 1 above my keyboard, above the tab on my right side 13 | SELECT `Are you over 18?` 14 | FROM customer_sweepstakes; 15 | #Now it works properly. Best thing to do is not name columns like this 16 | 17 | 18 | SELECT `Are you over 18?`, 19 | CASE 20 | WHEN `Are you over 18?` = 'Yes' THEN 'Y' 21 | WHEN `Are you over 18?` = 'No' THEN 'N' 22 | ELSE `Are you -------------------------------------------------------------------------------- /4.5 IF Function.sql: -------------------------------------------------------------------------------- 1 | # IF Function 2 | 3 | # IF(condition,condition is true do this, condition is false do this) 4 | 5 | SELECT tip, 6 | IF(tip > 1, 'Amazing!','Cheap...') 7 | FROM customer_orders 8 | ; 9 | 10 | 11 | SELECT order_total, 12 | tip, 13 | IF(tip > 2, order_total * .75,order_total * 1.1) as new_total 14 | FROM customer_orders 15 | ; 16 | 17 | SELECT * 18 | FROM customer_orders 19 | ; 20 | 21 | -------------------------------------------------------------------------------- /4.6 Case Statement.sql: -------------------------------------------------------------------------------- 1 | # Standardize Data 2 | 3 | SELECT * 4 | FROM customer_sweepstakes; 5 | 6 | #First let's standardize the phone numbers 7 | #We should remove all of the special characters, then we can use substring to format how we want 8 | 9 | SELECT * 10 | FROM customer_sweepstakes; 11 | 12 | #Here we can use Regular Expression to remove all special characters 13 | SELECT phone, REGEXP_REPLACE(phone, ('[\]\\[!@#$%.&*`~^_{}:;<>/\\|()-]+'),'') 14 | FROM customer_sweepstakes; 15 | 16 | #Now we can update and 17 | UPDATE customer_sweepstakes 18 | SET phone = REGEXP_REPLACE(phone, ('[\]\\[!@#$%.&*`~^_{}:;<>/\\|()-]+'),''); 19 | 20 | SELECT * 21 | FROM customer_sweepstakes; 22 | 23 | #Now we can set our own standardized format like this 24 | 25 | SELECT -------------------------------------------------------------------------------- /4.7Cast and Convert Functions.sql: -------------------------------------------------------------------------------- 1 | # Cast and Convert Functions 2 | 3 | 4 | 5 | SELECT CAST("2022-01-01" AS DATETIME); 6 | 7 | 8 | 9 | SELECT birth_date, 10 | CAST(birth_date AS DATETIME), 11 | CONVERT(birth_date, DATETIME) 12 | FROM customers 13 | ; 14 | 15 | SELECT first_name, 16 | CAST(first_name AS DECIMAL) 17 | FROM customers 18 | ; 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /5.2 Group By Basics.sql: -------------------------------------------------------------------------------- 1 | # Group By 2 | 3 | 4 | SELECT customer_id, SUM(tip) 5 | FROM customer_orders 6 | GROUP BY customer_id 7 | ; 8 | 9 | SELECT product_id, AVG(order_total) 10 | FROM customer_orders 11 | GROUP BY product_id 12 | ORDER BY AVG(order_total) DESC 13 | ; 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /5.3 Aggregate Functions.sql: -------------------------------------------------------------------------------- 1 | # Aggregate Functions 2 | 3 | 4 | SELECT customer_id, SUM(tip) AS total_tips 5 | FROM customer_orders 6 | GROUP BY customer_id 7 | ; 8 | 9 | SELECT customer_id, AVG(tip) AS average_tips 10 | FROM customer_orders 11 | GROUP BY customer_id 12 | ORDER BY average_tips 13 | ; 14 | 15 | 16 | SELECT customer_id, MAX(tip) AS biggest_tips 17 | FROM customer_orders 18 | GROUP BY customer_id 19 | ORDER BY biggest_tips 20 | ; 21 | 22 | SELECT customer_id, MIN(tip) AS smallest_tips 23 | FROM customer_orders 24 | GROUP BY customer_id 25 | ORDER BY smallest_tips 26 | ; 27 | 28 | 29 | SELECT customer_id, COUNT(tip) AS count_of_tips 30 | FROM customer_orders 31 | GROUP BY customer_id 32 | ORDER BY count_of_tips 33 | ; 34 | 35 | SELECT * 36 | FROM customer_orders; 37 | 38 | 39 | 40 | SELECT first_name, last_name, COUNT(phone) 41 | FROM customers 42 | GROUP BY first_name, last_name 43 | ; 44 | 45 | 46 | SELECT product_id, tip, COUNT(tip), COUNT(DISTINCT tip) 47 | FROM customer_orders 48 | GROUP BY product_id, tip 49 | ORDER BY product_id; 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /5.4 Having vs Where Clause.sql: -------------------------------------------------------------------------------- 1 | # Having vs Where Clause 2 | 3 | 4 | SELECT customer_id, SUM(tip) as total_tips 5 | FROM customer_orders 6 | WHERE total_tips > 5 7 | GROUP BY customer_id 8 | ; 9 | 10 | SELECT customer_id, SUM(tip) as total_tips 11 | FROM customer_orders 12 | GROUP BY customer_id 13 | HAVING total_tips > 5 14 | ; 15 | 16 | 17 | 18 | SELECT customer_id, SUM(order_total) as total 19 | FROM customer_orders 20 | GROUP BY customer_id 21 | HAVING SUM(order_total) > 40 22 | ORDER BY 2 23 | ; 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /5.5 Rollup.sql: -------------------------------------------------------------------------------- 1 | # Rollup 2 | 3 | 4 | SELECT customer_id, SUM(tip) as total_tips 5 | FROM customer_orders 6 | GROUP BY customer_id WITH ROLLUP 7 | ; 8 | 9 | SELECT customer_id, COUNT(tip) as count_tips 10 | FROM customer_orders 11 | GROUP BY customer_id WITH ROLLUP 12 | ; 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /6.10 Unions.sql: -------------------------------------------------------------------------------- 1 | #7 - The LIKE Operator: 2 | ---------------------- 3 | 4 | #The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. 5 | 6 | #There are two wildcards often used in conjunction with the LIKE operator: 7 | 8 | #% - The percent sign represents zero, one, or multiple characters 9 | #_ - The underscore represents a single character 10 | 11 | #The percent sign and the underscore can also be used in combinations which we will look at in this lesson.alter 12 | 13 | 14 | #So let's try out some different LIKE Statements to see how this works: 15 | 16 | #Let's write k with a % - this means start with "K" 17 | SELECT * 18 | FROM customers 19 | WHERE first_name LIKE 'K%'; 20 | 21 | #We could do the opposite and say a value that ends in 'n' 22 | SELECT * 23 | FROM customers 24 | WHERE first_name LIKE '%n'; 25 | 26 | #Now it's not cas -------------------------------------------------------------------------------- /6.11 Join Use Cases.sql: -------------------------------------------------------------------------------- 1 | # Join Use Cases 2 | 3 | 4 | SELECT DISTINCT p.product_name, 5 | oi.unit_price, 6 | p.sale_price, 7 | p.units_in_stock, 8 | p.sale_price - oi.unit_price AS profit, 9 | (p.sale_price - oi.unit_price) * p.units_in_stock AS potential_profit 10 | FROM ordered_items oi 11 | JOIN products p 12 | USING(product_id) 13 | ORDER BY potential_profit DESC 14 | ; 15 | SELECT * 16 | FROM products; 17 | 18 | 19 | 20 | 21 | 22 | SELECT * 23 | FROM supplier_delivery_status; 24 | SELECT * 25 | FROM ordered_items; 26 | SELECT * 27 | FROM suppliers; 28 | 29 | SELECT * 30 | FROM ordered_items oi 31 | JOIN supplier_delivery_status sds 32 | ON oi.status = sds.order_status_id 33 | JOIN suppliers s 34 | ON oi.shipper_id = s.supplier_id; 35 | 36 | 37 | 38 | SELECT oi.order_id, sds.name, oi.status, oi.shipped_date, s.name 39 | FROM ordered_items oi 40 | JOIN supplier_delivery_status sds 41 | ON oi.status = sds.order_status_id 42 | JOIN suppliers s 43 | ON oi.shipper_id = s.supplier_id 44 | WHERE sds.name <> 'Delivered' 45 | AND YEAR(shipped_date) < YEAR(NOW()) - 1 46 | ; 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /6.2 Inner Join.sql: -------------------------------------------------------------------------------- 1 | # Inner Join 2 | 3 | 4 | SELECT * 5 | FROM customers; 6 | 7 | SELECT * 8 | FROM customer_orders 9 | ORDER BY customer_id; 10 | 11 | 12 | SELECT * 13 | FROM customers c 14 | INNER JOIN customer_orders co 15 | ON c.customer_id = co.customer_id 16 | ORDER BY c.customer_id 17 | ; 18 | 19 | 20 | 21 | SELECT * 22 | FROM products; 23 | 24 | SELECT * 25 | FROM customer_orders; 26 | 27 | 28 | SELECT product_name, SUM(order_total) as Total 29 | FROM products p 30 | JOIN customer_orders co 31 | ON p.product_id = co.product_id 32 | GROUP BY product_name 33 | ORDER BY 2 34 | ; 35 | 36 | 37 | 38 | 39 | SELECT * 40 | FROM suppliers; 41 | 42 | SELECT * 43 | FROM ordered_items; 44 | 45 | 46 | SELECT * 47 | FROM suppliers s 48 | INNER JOIN ordered_items oi 49 | ON s.supplier_id = oi.shipper_id 50 | ; 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /6.3 Joining Multiple Tables.sql: -------------------------------------------------------------------------------- 1 | # Joining Multiple Tables 2 | 3 | 4 | SELECT product_name, order_total, first_name 5 | FROM products p 6 | JOIN customer_orders co 7 | ON p.product_id = co.product_id 8 | JOIN customers c 9 | ON co.customer_id = c.customer_id 10 | ; 11 | 12 | 13 | SELECT p.product_id, co.product_id, co.customer_id, c.customer_id 14 | FROM products p 15 | JOIN customer_orders co 16 | ON p.product_id = co.product_id 17 | JOIN customers c 18 | ON co.customer_id = c.customer_id 19 | ; 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /6.4 Joining on Multiple Conditions.sql: -------------------------------------------------------------------------------- 1 | # Joining on Multiple Conditions 2 | 3 | 4 | SELECT * 5 | FROM customer_orders 6 | ; 7 | SELECT * 8 | FROM customer_orders_review 9 | ; 10 | 11 | SELECT * 12 | FROM customer_orders co 13 | JOIN customer_orders_review cor 14 | ON co.order_id = cor.order_id 15 | AND co.customer_id = cor.customer_id 16 | AND co.order_date = cor.order_date 17 | ; 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /6.5 Outer Joins.sql: -------------------------------------------------------------------------------- 1 | # Outer Joins 2 | 3 | 4 | SELECT c.customer_id, first_name, co.order_id 5 | FROM customers c 6 | JOIN customer_orders co 7 | ON c.customer_id = co.customer_id 8 | ORDER BY c.customer_id, co.order_id 9 | ; 10 | 11 | 12 | 13 | SELECT c.customer_id, first_name, co.order_id 14 | FROM customers c 15 | LEFT OUTER JOIN customer_orders co 16 | ON c.customer_id = co.customer_id 17 | ORDER BY c.customer_id, co.order_id 18 | ; 19 | 20 | 21 | 22 | SELECT c.customer_id, first_name, co.order_id 23 | FROM customers c 24 | RIGHT OUTER JOIN customer_orders co 25 | ON c.customer_id = co.customer_id 26 | ORDER BY c.customer_id, co.order_id 27 | ; 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /6.6 Self Joins.sql: -------------------------------------------------------------------------------- 1 | # Self Joins 2 | 3 | 4 | 5 | SELECT * 6 | FROM customers c 7 | JOIN customers ss 8 | ON c.first_name = ss.first_name 9 | ; 10 | 11 | 12 | SELECT c.customer_id, c.first_name, c.last_name, ss.customer_id, ss.first_name, ss.last_name 13 | FROM customers c 14 | JOIN customers ss 15 | ON c.customer_id + 1 = ss.customer_id 16 | ; 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /6.7 Cross Joins.sql: -------------------------------------------------------------------------------- 1 | # Cross Joins 2 | 3 | 4 | SELECT * 5 | FROM customers 6 | ; 7 | 8 | SELECT * 9 | FROM customer_orders 10 | ; 11 | 12 | 13 | 14 | SELECT c.customer_id,c.first_name,co.customer_id, order_id 15 | FROM customers c 16 | CROSS JOIN customer_orders co 17 | ORDER BY c.customer_id 18 | ; 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /6.8 Natural Joins.sql: -------------------------------------------------------------------------------- 1 | # Natural Joins 2 | 3 | 4 | SELECT * 5 | FROM products p 6 | JOIN customer_orders co 7 | ON p.product_id = co.product_id 8 | ORDER BY p.product_id 9 | ; 10 | 11 | 12 | SELECT * 13 | FROM products p 14 | NATURAL JOIN customer_orders co 15 | ORDER BY p.product_id 16 | ; 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /6.9 Using Keyword.sql: -------------------------------------------------------------------------------- 1 | # "Using" in Joins 2 | 3 | 4 | SELECT c.customer_id, first_name, co.order_id 5 | FROM customers c 6 | LEFT OUTER JOIN customer_orders co 7 | ON c.customer_id = co.customer_id 8 | ORDER BY c.customer_id, co.order_id 9 | ; 10 | 11 | 12 | SELECT c.customer_id, first_name, co.order_id 13 | FROM customers c 14 | LEFT OUTER JOIN customer_orders co 15 | USING(customer_id) 16 | ORDER BY c.customer_id, co.order_id 17 | ; 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /7.2 Subqueries.sql: -------------------------------------------------------------------------------- 1 | # Subqueries 2 | 3 | 4 | SELECT * 5 | FROM customers 6 | ; 7 | 8 | SELECT * 9 | FROM customers 10 | WHERE customer_id IN 11 | (SELECT customer_id 12 | FROM customer_orders) 13 | ; 14 | 15 | 16 | SELECT * 17 | FROM customers 18 | WHERE customer_id IN 19 | (SELECT customer_id 20 | FROM customer_orders 21 | WHERE tip > 1) 22 | ; 23 | 24 | 25 | 26 | SELECT * 27 | FROM customers 28 | WHERE total_money_spent > (SELECT AVG(total_money_spent) 29 | FROM customers) 30 | ; 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /7.3 Any and All Statements.sql: -------------------------------------------------------------------------------- 1 | # ALL and ANY Statements 2 | 3 | 4 | SELECT * 5 | FROM ordered_items 6 | ; 7 | 8 | 9 | SELECT MAX(quantity * unit_price) as total_order_price 10 | FROM ordered_items 11 | WHERE shipper_id = 1 12 | ; 13 | 14 | 15 | 16 | SELECT shipper_id, order_id, quantity, unit_price, (quantity * unit_price) AS total_order_price 17 | FROM ordered_items 18 | WHERE (quantity * unit_price) > (SELECT MAX(quantity * unit_price) AS total_order_price 19 | FROM ordered_items 20 | WHERE shipper_id = 1) 21 | ; 22 | 23 | 24 | SELECT shipper_id, order_id, quantity, unit_price, (quantity * unit_price) AS total_order_price 25 | FROM ordered_items 26 | WHERE (quantity * unit_price) > ALL (SELECT (quantity * unit_price) AS total_order_price 27 | FROM ordered_items 28 | WHERE shipper_id = 1) 29 | ; 30 | 31 | 32 | 33 | 34 | SELECT shipper_id, order_id, quantity, unit_price, (quantity * unit_price) AS total_order_price 35 | FROM ordered_items 36 | WHERE (quantity * unit_price) >= ANY (SELECT (quantity * unit_price) AS total_order_price 37 | FROM ordered_items 38 | WHERE shipper_id = 1) 39 | ; 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /7.4 Exists Operator.sql: -------------------------------------------------------------------------------- 1 | # Exists 2 | 3 | 4 | SELECT * 5 | FROM customers 6 | WHERE customer_id IN 7 | (SELECT customer_id 8 | FROM customer_orders) 9 | ; 10 | 11 | 12 | 13 | 14 | SELECT * 15 | FROM customers c 16 | WHERE EXISTS 17 | (SELECT customer_id 18 | FROM customer_orders 19 | WHERE customer_id = c.customer_id) 20 | ; 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /7.5 Subqueries in Select and From Statements.sql: -------------------------------------------------------------------------------- 1 | #WHERE Clause: 2 | #------------- 3 | #The WHERE clause is used to filter records (rows of data) 4 | 5 | #It's going to extract only those records that fulfill a specified condition. 6 | 7 | # So basically if we say "Where name is = 'Alex' - only rows were the name = 'Alex' will return 8 | # So this is only effecting the rows, not the columns 9 | 10 | 11 | #Let's take a look at how this looks 12 | SELECT * 13 | FROM customers 14 | WHERE total_money_spent > 3000; 15 | 16 | SELECT * 17 | FROM customers 18 | WHERE city = 'Scranton'; 19 | 20 | 21 | #We can also return rows that do have not "Scranton" 22 | SELECT * 23 | FROM customers 24 | WHERE city != 'Scr -------------------------------------------------------------------------------- /8.2 Over + Partition By.sql: -------------------------------------------------------------------------------- 1 | 2 | #We can also select a specefic number of column based on our requirement. 3 | 4 | #Now remember we can just select everything by saying: 5 | SELECT * 6 | FROM customers; 7 | 8 | # If our table contains a large number of records it may create a negative 9 | #impact in the database server and the network also. 10 | #It could take a long time to retrieve the output so often it's best to select only the 11 | #columns you actually want to see or need 12 | 13 | 14 | 15 | 16 | #Let's try selecting a specific column 17 | SELECT first_name 18 | FROM customers; 19 | 20 | #As you can see from the output, we only have the one column here now and don't see the others 21 | 22 | #Now let's add some more columns, we just need to separate the columns with columns 23 | SELECT first_name, last_name 24 | FROM c -------------------------------------------------------------------------------- /8.3 Row_Number.sql: -------------------------------------------------------------------------------- 1 | # Row_Number 2 | 3 | SELECT c.customer_id, 4 | first_name, 5 | order_total, 6 | ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY order_total DESC) AS row_num 7 | FROM customers c 8 | JOIN customer_orders co 9 | ON c.customer_id = co.customer_id 10 | WHERE row_num < 3 11 | ; 12 | 13 | SELECT * 14 | FROM ( 15 | SELECT c.customer_id, 16 | first_name, 17 | order_total, 18 | ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY order_total DESC) AS row_num 19 | FROM customers c 20 | JOIN customer_orders co 21 | ON c.customer_id = co.customer_id 22 | ) AS row_table 23 | WHERE row_num < 3 24 | ; 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /8.4 Rank and Dense_rank.sql: -------------------------------------------------------------------------------- 1 | # Rank and Dense_Rank 2 | 3 | 4 | 5 | SELECT *, 6 | RANK() OVER(PARTITION BY department ORDER BY salary DESC) 7 | FROM Employees 8 | ; 9 | 10 | SELECT * 11 | FROM 12 | (SELECT *, 13 | RANK() OVER(PARTITION BY department ORDER BY salary DESC) as rank_row 14 | FROM Employees) AS ranked 15 | WHERE rank_row < 3 16 | ; 17 | 18 | 19 | SELECT *, 20 | RANK() OVER(PARTITION BY department ORDER BY salary DESC) as rank_, 21 | DENSE_RANK() OVER(PARTITION BY department ORDER BY salary DESC) as dense_ 22 | FROM Employees 23 | ; 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /8.5 Lag and Lead.sql: -------------------------------------------------------------------------------- 1 | # Lag and Lead 2 | 3 | 4 | SELECT *, 5 | LAG(salary) OVER(PARTITION BY department ORDER BY employee_id) 6 | FROM employees 7 | ; 8 | 9 | SELECT *, lag_col - salary AS pay_discrepancy 10 | FROM 11 | (SELECT *, 12 | LEAD(salary) OVER(PARTITION BY department ORDER BY employee_id) AS lag_col 13 | FROM employees) AS lag_table 14 | ; 15 | 16 | 17 | SELECT *, IF(salary > lag_col, 'More','Less') 18 | FROM 19 | (SELECT *, 20 | LEAD(salary) OVER(PARTITION BY department ORDER BY employee_id) AS lag_col 21 | FROM employees) AS lag_table 22 | ; 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /9.2 Regular Expression Methods.sql: -------------------------------------------------------------------------------- 1 | # Regular Expression Methods 2 | 3 | 4 | 5 | SELECT * 6 | FROM customers 7 | WHERE first_name LIKE '%k%' 8 | ; 9 | 10 | SELECT * 11 | FROM customers 12 | WHERE first_name REGEXP 'k' 13 | ; 14 | 15 | SELECT first_name, REGEXP_REPLACE(first_name,'a','b') 16 | FROM customers 17 | ; 18 | 19 | SELECT first_name, REGEXP_LIKE(first_name,'a') 20 | FROM customers 21 | ; 22 | 23 | SELECT first_name, REGEXP_INSTR(first_name,'a') 24 | FROM customers 25 | ; 26 | 27 | SELECT first_name, REGEXP_SUBSTR(first_name,'char') 28 | FROM customers 29 | ; 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /9.3 Regular Expression Metacharacters.sql: -------------------------------------------------------------------------------- 1 | # Exists 2 | 3 | #Exists kind of works like the IN statement - it checks to see if something exists and it it does it will give an output 4 | 5 | #Here is something we did in an earlier lesson. Let's try this out and then rewrite it using the Exists statement 6 | 7 | SELECT * 8 | FROM bakery.customers 9 | WHERE customer_id IN (SELECT customer_id 10 | FROM bakery.customer_orders); 11 | 12 | 13 | 14 | #This is the same thing 15 | 16 | SELECT * 17 | FROM bakery.customers c 18 | WHERE EXISTS (SELECT customer_id 19 | FROM bakery.customer_orders 20 | WHERE customer_id = c.customer_id); 21 | 22 | #Now why would you want to use this? Mostly performance. I personally don't use this a lot unless the dataset I'm working with is huge and I 23 | #actually need to improve performance -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MySQL-for-Data-Analytics 2 | --------------------------------------------------------------------------------