├── README.md ├── 4.3 Viewing and Deleting Triggers.sql ├── 2.5 Temp Table vs CTE.sql ├── 5.3 Prefix Indexes.sql ├── 2.4 Temporary Tables.sql ├── 3.5 Output Paramaters.sql ├── 5.2 Creating an Index.sql ├── 4.4 Creating and Updating Events.sql ├── 5.5 Understanding Indexes Better + Best Practices.sql ├── 3.2 Stored Procedure Basics.sql ├── 2.3 Recursion in CTEs.sql ├── 5.4 Composite Indexes.sql ├── 4.2 Creating Triggers.sql ├── 3.3 Paramaters in Stored Procedures.sql ├── 3.6 Session vs Local Variables.sql ├── 3.4 Default Values as Stored Procedures.sql ├── 2.2 Using CTEs.sql ├── 6.1 Automated_Data_Cleaning.sql └── 1. Create Database.sql /README.md: -------------------------------------------------------------------------------- 1 | # Advanced-MySQL-Course -------------------------------------------------------------------------------- /4.3 Viewing and Deleting Triggers.sql: -------------------------------------------------------------------------------- 1 | -- Viewing and Deleting Triggers 2 | 3 | SHOW TRIGGERS; 4 | 5 | SHOW TRIGGERS 6 | WHERE Event = 'INSERT'; 7 | 8 | DROP TRIGGER IF EXISTS update_invoices_with_payments; 9 | 10 | 11 | DROP TRIGGER IF EXISTS update_invoices_with_payments; 12 | CREATE TRIGGER 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 | -------------------------------------------------------------------------------- /2.5 Temp Table vs CTE.sql: -------------------------------------------------------------------------------- 1 | -- Temp Tables vs CTEs 2 | 3 | -- 1. How they're stored in Memory 4 | -- 2. How they handle complex queries 5 | 6 | WITH CTE_EXAMPLE AS( 7 | SELECT *, 8 | ROW_NUMBER() OVER(PARTITION BY department ORDER BY salary desc) row_num 9 | FROM employees) 10 | SELECT * 11 | FROM CTE_EXAMPLE 12 | WHERE row_num > 2; 13 | 14 | CREATE TEMPORARY TABLE temp_table 15 | SELECT *, 16 | ROW_NUMBER() OVER(PARTITION BY department ORDER BY salary desc) row_num 17 | FROM employees; 18 | 19 | SELECT * 20 | FROM temp_table; 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /5.3 Prefix Indexes.sql: -------------------------------------------------------------------------------- 1 | -- Prefix Indexes 2 | 3 | 4 | SELECT * 5 | FROM bakery.us_household_income; 6 | 7 | 8 | SELECT DISTINCT State_Name 9 | FROM bakery.us_household_income; 10 | 11 | CREATE INDEX idx_state_name 12 | ON bakery.us_household_income(State_Name(8)); 13 | 14 | EXPLAIN SELECT * 15 | FROM bakery.us_household_income 16 | WHERE State_Name = 'Rhode Island'; 17 | 18 | CREATE INDEX idx_state_name2 19 | ON bakery.us_household_income(State_Name(13)); 20 | 21 | 22 | SHOW INDEXES IN bakery.us_household_income; 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 | -------------------------------------------------------------------------------- /2.4 Temporary Tables.sql: -------------------------------------------------------------------------------- 1 | -- Using Temporary Tables 2 | 3 | 4 | CREATE TEMPORARY TABLE temp_table 5 | (first_name varchar(50), 6 | last_name varchar(50), 7 | favorite_movie varchar(100) 8 | ); 9 | 10 | SELECT * 11 | FROM temp_table; 12 | 13 | INSERT INTO temp_table 14 | VALUES ('Alex','Freberg','Lord of the Rings: Two Towers'); 15 | 16 | 17 | 18 | SELECT * 19 | FROM temp_table; 20 | 21 | SELECT * 22 | FROM temp_table; 23 | 24 | 25 | 26 | CREATE TEMPORARY TABLE temp_table2 27 | SELECT * 28 | FROM employees 29 | WHERE salary > 50000; 30 | 31 | 32 | SELECT * 33 | FROM temp_table2; 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 | -------------------------------------------------------------------------------- /3.5 Output Paramaters.sql: -------------------------------------------------------------------------------- 1 | -- Output Paramaters 2 | 3 | DROP PROCEDURE IF EXISTS sum_of_orders; 4 | DELIMITER $$ 5 | CREATE PROCEDURE sum_of_orders(p_product_id INT, OUT sum_totals DECIMAL(9,2)) 6 | BEGIN 7 | SELECT SUM(order_total) 8 | INTO sum_totals 9 | FROM bakery.customer_orders 10 | WHERE product_id = p_product_id; 11 | END $$ 12 | 13 | DELIMITER ; 14 | 15 | set @sum_totals = 0; 16 | call bakery.sum_of_orders(1001, @sum_totals); 17 | select @sum_totals; 18 | 19 | 20 | SELECT ROUND(@sum_totals/SUM(order_total) * 100,2) AS Percentage_or_orders 21 | FROM bakery.customer_orders; 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 | -------------------------------------------------------------------------------- /5.2 Creating an Index.sql: -------------------------------------------------------------------------------- 1 | -- Indexing 2 | 3 | 4 | SELECT * 5 | FROM bakery.us_household_income 6 | WHERE Area_Code = 203; 7 | 8 | EXPLAIN SELECT * 9 | FROM bakery.us_household_income 10 | WHERE Area_Code = 203; 11 | 12 | CREATE INDEX idx_area_code 13 | ON bakery.us_household_income (Area_Code); 14 | 15 | SHOW INDEXES IN bakery.us_household_income; 16 | 17 | 18 | EXPLAIN SELECT * 19 | FROM bakery.us_household_income 20 | WHERE Area_Code = 203; 21 | 22 | 23 | DROP INDEX idx_area_code ON bakery.us_household_income; 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 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /4.4 Creating and Updating Events.sql: -------------------------------------------------------------------------------- 1 | -- Events 2 | 3 | SHOW VARIABLES LIKE 'event%'; 4 | 5 | SET event_scheduler = 'ON'; 6 | 7 | 8 | SELECT * 9 | FROM bakery.customer_orders 10 | ORDER BY order_date; 11 | 12 | DELIMITER $$ 13 | CREATE EVENT delete_old_customer_orders 14 | ON SCHEDULE EVERY 30 SECOND 15 | DO 16 | BEGIN 17 | DELETE 18 | FROM bakery.customer_orders 19 | WHERE order_date < NOW() - INTERVAL 5 YEAR; 20 | END $$ 21 | DELIMITER ; 22 | 23 | SHOW EVENTS; 24 | 25 | 26 | SELECT * 27 | FROM bakery.customer_orders 28 | ORDER BY order_date; 29 | 30 | 31 | DELIMITER $$ 32 | ALTER EVENT delete_old_customer_orders 33 | ON SCHEDULE EVERY 30 DAY 34 | DO 35 | BEGIN 36 | DELETE 37 | FROM bakery.customer_orders 38 | WHERE order_date < NOW() - INTERVAL 5 YEAR; 39 | END $$ 40 | DELIMITER ; 41 | 42 | DROP EVENT IF EXISTS delete_old_customer_orders; 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 | -------------------------------------------------------------------------------- /5.5 Understanding Indexes Better + Best Practices.sql: -------------------------------------------------------------------------------- 1 | -- Understanding Indexes Better + Best Practices 2 | 3 | SHOW INDEXES IN bakery.us_household_income; 4 | 5 | EXPLAIN SELECT * 6 | FROM bakery.us_household_income 7 | WHERE Area_Code = '203' 8 | OR City LIKE 'A%'; 9 | 10 | EXPLAIN SELECT * 11 | FROM bakery.us_household_income 12 | WHERE State_ab = 'NC' 13 | OR City LIKE 'A%'; 14 | 15 | 16 | EXPLAIN SELECT * 17 | FROM bakery.us_household_income 18 | WHERE State_Name = 'Rhode_Island' 19 | AND ALand > 500000; 20 | 21 | EXPLAIN SELECT ALand 22 | FROM bakery.us_household_income 23 | WHERE ALand + 10 > 100000; 24 | 25 | 26 | -- BEST PRACTICES 27 | 28 | -- Composite indexes are good 29 | -- Order matters 30 | -- Avoid using Select * 31 | -- Looking at your Where Clause 32 | -- Full table vs indexes 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 | -------------------------------------------------------------------------------- /3.2 Stored Procedure Basics.sql: -------------------------------------------------------------------------------- 1 | -- Stored Procedures 2 | 3 | 4 | 5 | SELECT * 6 | FROM bakery.customer_orders 7 | WHERE order_total > 20; 8 | 9 | 10 | 11 | CREATE PROCEDURE large_order_totals() 12 | SELECT * 13 | FROM bakery.customer_orders 14 | WHERE order_total > 20; 15 | 16 | 17 | CALL large_order_totals(); 18 | 19 | 20 | DELIMITER $$ 21 | CREATE PROCEDURE large_order_totals2() 22 | BEGIN 23 | SELECT * 24 | FROM bakery.customer_orders 25 | WHERE order_total > 20; 26 | SELECT * 27 | FROM bakery.customer_orders 28 | WHERE order_total > 5; 29 | END $$ 30 | 31 | DELIMITER ; 32 | 33 | CALL large_order_totals2(); 34 | 35 | CALL large_order_totals3(); 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 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /2.3 Recursion in CTEs.sql: -------------------------------------------------------------------------------- 1 | -- Recursion in CTEs 2 | 3 | 4 | SELECT * 5 | FROM employees; 6 | 7 | 8 | WITH RECURSIVE CTE_Example AS 9 | ( SELECT 1 AS x 10 | 11 | UNION ALL 12 | 13 | SELECT x + 1 14 | FROM CTE_Example 15 | WHERE x < 10 16 | ) 17 | SELECT x 18 | FROM CTE_Example; 19 | 20 | 21 | 22 | SELECT * 23 | FROM employees; 24 | 25 | 26 | WITH RECURSIVE company_hierarchy AS 27 | ( 28 | SELECT employee_id, first_name, last_name, boss_id, 29 | 0 AS hierarchy_level 30 | FROM employees 31 | WHERE boss_id IS NULL 32 | 33 | UNION ALL 34 | 35 | SELECT e.employee_id, e.first_name, e.last_name, e.boss_id, 36 | hierarchy_level + 1 37 | FROM employees e, company_hierarchy ch 38 | WHERE e.boss_id = ch.employee_id 39 | ) 40 | SELECT ch.first_name Employee_First_Name, ch.last_name Employee_Last_Name, 41 | e.first_name Boss_First_Name, e.last_name Boss_Last_Name, 42 | hierarchy_level 43 | FROM company_hierarchy ch 44 | JOIN employees e 45 | ON ch.boss_id = e.employee_id 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 | -------------------------------------------------------------------------------- /5.4 Composite Indexes.sql: -------------------------------------------------------------------------------- 1 | -- Composite Indexes 2 | EXPLAIN SELECT * 3 | FROM bakery.us_household_income 4 | WHERE State_Name = 'Rhode Island' 5 | AND ALand > 500000; 6 | 7 | 8 | 9 | CREATE INDEX idx_state_Aland 10 | ON bakery.us_household_income(State_Name(8),ALand); 11 | 12 | EXPLAIN SELECT * 13 | FROM bakery.us_household_income 14 | WHERE State_Name = 'Rhode Island' 15 | AND ALand > 10000000; 16 | -- ORDER OF COLUMNS IN INDEX 17 | 18 | SHOW INDEXES IN bakery.us_household_income; 19 | 20 | 21 | 22 | SELECT * 23 | FROM bakery.us_household_income 24 | WHERE State_ab = 'NC' 25 | AND City LIKE 'A%'; 26 | 27 | SELECT COUNT(DISTINCT State_ab), COUNT(DISTINCT City) 28 | FROM bakery.us_household_income; 29 | 30 | CREATE INDEX idx_city_stateab 31 | ON bakery.us_household_income(City(10), State_ab(2)); 32 | 33 | SHOW INDEXES IN bakery.us_household_income; 34 | 35 | EXPLAIN SELECT * 36 | FROM bakery.us_household_income 37 | WHERE State_ab = 'NC' 38 | AND City LIKE 'A%'; 39 | 40 | CREATE INDEX idx_stateab_city 41 | ON bakery.us_household_income(State_ab(2),City(10)); 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 | -------------------------------------------------------------------------------- /4.2 Creating Triggers.sql: -------------------------------------------------------------------------------- 1 | -- Triggers 2 | 3 | SELECT * 4 | FROM bakery.client_invoices; 5 | 6 | SELECT * 7 | FROM bakery.client_payments; 8 | 9 | DELIMITER $$ 10 | CREATE TRIGGER update_invoices_with_payments 11 | AFTER INSERT ON bakery.client_payments 12 | FOR EACH ROW 13 | BEGIN 14 | UPDATE bakery.client_invoices 15 | SET total_paid = total_paid + NEW.amount_paid 16 | WHERE invoice_id = NEW.invoice_id; 17 | END $$ 18 | DELIMITER ; 19 | 20 | 21 | INSERT INTO bakery.client_payments 22 | VALUES(11, 1001, 3, '2023-02-27', 1003.87); 23 | 24 | 25 | 26 | DELIMITER $$ 27 | CREATE TRIGGER update_invoices_when_deleted 28 | AFTER DELETE ON bakery.client_payments 29 | FOR EACH ROW 30 | BEGIN 31 | UPDATE bakery.client_invoices 32 | SET total_paid = total_paid - OLD.amount_paid 33 | WHERE invoice_id = OLD.invoice_id; 34 | END $$ 35 | DELIMITER ; 36 | 37 | SELECT * 38 | FROM bakery.client_invoices; 39 | 40 | SELECT * 41 | FROM bakery.client_payments; 42 | 43 | 44 | DELETE FROM client_payments 45 | WHERE payment_id = 11; 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 | -------------------------------------------------------------------------------- /3.3 Paramaters in Stored Procedures.sql: -------------------------------------------------------------------------------- 1 | -- Paramaters in Stored Procedures 2 | 3 | DROP PROCEDURE IF EXISTS orders_by_product; 4 | DELIMITER $$ 5 | CREATE PROCEDURE orders_by_product(p_product_id INT) 6 | BEGIN 7 | SELECT * 8 | FROM bakery.customer_orders 9 | WHERE product_id = p_product_id; 10 | END $$ 11 | 12 | CALL orders_by_product(1001); 13 | 14 | CALL orders_by_product(); 15 | 16 | CALL `bakery`.`orders_by_product`(<{p_product_id INT}>); 17 | 18 | 19 | 20 | 21 | DROP PROCEDURE IF EXISTS orders_by_product2; 22 | DELIMITER $$ 23 | CREATE PROCEDURE orders_by_product2(p_product_id INT, p_order_date DATE) 24 | BEGIN 25 | SELECT * 26 | FROM bakery.customer_orders 27 | WHERE product_id = p_product_id 28 | AND order_date = p_order_date; 29 | END $$ 30 | 31 | CALL orders_by_product2(1001,'2020-01-30'); 32 | 33 | CALL orders_by_product(1001); 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 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /3.6 Session vs Local Variables.sql: -------------------------------------------------------------------------------- 1 | -- Session vs Local Variable in Stored Procedures 2 | 3 | DROP PROCEDURE IF EXISTS percentage_of_orders; 4 | DELIMITER $$ 5 | CREATE PROCEDURE percentage_of_orders(p_product_id INT, OUT sum_totals DECIMAL(9,2)) 6 | BEGIN 7 | DECLARE all_totals DECIMAL(9,2); 8 | DECLARE percentage_total DECIMAL(9,2); 9 | 10 | SELECT SUM(order_total) 11 | INTO sum_totals 12 | FROM bakery.customer_orders 13 | WHERE product_id = p_product_id; 14 | 15 | SELECT SUM(order_total) 16 | INTO all_totals 17 | FROM bakery.customer_orders; 18 | 19 | SET percentage_total = (sum_totals / all_totals * 100); 20 | 21 | SELECT sum_totals; 22 | SELECT all_totals; 23 | SELECT percentage_total; 24 | END $$ 25 | 26 | DELIMITER ; 27 | 28 | set @sum_totals = 0; 29 | call bakery.percentage_of_orders(1001, @sum_totals); 30 | 31 | -- Session Variable 32 | select @sum_totals; 33 | 34 | -- Local Variable 35 | SELECT @all_totals; 36 | SELECT @percentage_total; 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 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | -------------------------------------------------------------------------------- /3.4 Default Values as Stored Procedures.sql: -------------------------------------------------------------------------------- 1 | -- Default Values as Paramaters 2 | 3 | DROP PROCEDURE IF EXISTS orders_by_product; 4 | DELIMITER $$ 5 | CREATE PROCEDURE orders_by_product(p_product_id INT) 6 | BEGIN 7 | IF p_product_id IS NULL 8 | THEN SET p_product_id = 1001; 9 | END IF; 10 | SELECT * 11 | FROM bakery.customer_orders 12 | WHERE product_id = p_product_id; 13 | END $$ 14 | 15 | DELIMITER ; 16 | 17 | CALL orders_by_product(1003); 18 | 19 | CALL orders_by_product(NULL); 20 | 21 | 22 | DROP PROCEDURE IF EXISTS orders_by_product2; 23 | DELIMITER $$ 24 | CREATE PROCEDURE orders_by_product2(p_product_id INT) 25 | BEGIN 26 | IF p_product_id IS NULL 27 | THEN SELECT * 28 | FROM bakery.customer_orders; 29 | ELSE 30 | SELECT * 31 | FROM bakery.customer_orders 32 | WHERE product_id = p_product_id; 33 | END IF; 34 | END $$ 35 | 36 | DELIMITER ; 37 | 38 | CALL orders_by_product2(1001); 39 | 40 | CALL orders_by_product2(NULL); 41 | 42 | 43 | 44 | DROP PROCEDURE IF EXISTS orders_by_product3; 45 | DELIMITER $$ 46 | CREATE PROCEDURE orders_by_product3(p_product_id INT) 47 | BEGIN 48 | SELECT * 49 | FROM bakery.customer_orders 50 | WHERE product_id = IFNULL(p_product_id,1001); 51 | END $$ 52 | 53 | DELIMITER ; 54 | 55 | CALL orders_by_product3(1006); 56 | 57 | CALL orders_by_product3(NULL); 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 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /2.2 Using CTEs.sql: -------------------------------------------------------------------------------- 1 | -- Using Common Table Expressions (CTEs) 2 | 3 | SELECT * 4 | FROM bakery.customer_orders; 5 | 6 | 7 | SELECT product_id, SUM(order_total), SUM(tip), COUNT(tip) 8 | FROM bakery.customer_orders 9 | WHERE tip != 0 10 | GROUP BY product_id; 11 | 12 | 13 | WITH CTE_Example AS 14 | ( 15 | SELECT product_id, SUM(order_total), SUM(tip), COUNT(tip) 16 | FROM bakery.customer_orders 17 | WHERE tip != 0 18 | GROUP BY product_id 19 | ) 20 | 21 | 22 | 23 | 24 | SELECT * 25 | FROM CTE_Example 26 | WHERE `SUM(tip)` > 3; 27 | 28 | SELECT * 29 | FROM CTE_Example 30 | WHERE `SUM(tip)` > 3; 31 | 32 | 33 | WITH CTE_Example(product_id, sum_order_total,sum_tip,count_tip) AS 34 | ( 35 | SELECT product_id, SUM(order_total), SUM(tip), COUNT(tip) 36 | FROM bakery.customer_orders 37 | WHERE tip != 0 38 | GROUP BY product_id 39 | ) 40 | 41 | SELECT product_id, ROUND(sum_tip/count_tip,2) 42 | FROM CTE_Example; 43 | 44 | SELECT product_id, ROUND(`SUM(tip)`/`COUNT(tip)`,2) 45 | FROM (SELECT product_id, SUM(order_total), SUM(tip), COUNT(tip) 46 | FROM bakery.customer_orders 47 | WHERE tip != 0 48 | GROUP BY product_id) Subquery_Example 49 | GROUP BY product_id; 50 | 51 | 52 | 53 | WITH CTE_Example(product_id, sum_order_total,sum_tip,count_tip) AS 54 | ( 55 | SELECT product_id, SUM(order_total), SUM(tip), COUNT(tip) 56 | FROM bakery.customer_orders 57 | WHERE tip != 0 58 | GROUP BY product_id 59 | ), 60 | CTE_Example2 AS ( 61 | SELECT product_id, SUM(order_total), SUM(tip), COUNT(tip) 62 | FROM bakery.customer_orders 63 | GROUP BY product_id 64 | ) 65 | SELECT * 66 | FROM CTE_Example cte1 67 | RIGHT JOIN CTE_Example2 cte2 68 | ON cte1.product_id = cte2.product_id; 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 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | -------------------------------------------------------------------------------- /6.1 Automated_Data_Cleaning.sql: -------------------------------------------------------------------------------- 1 | -- Automated Data Cleaning 2 | 3 | 4 | SELECT * 5 | FROM bakery.us_household_income; 6 | 7 | SELECT * 8 | FROM bakery.us_household_income_cleaned; 9 | 10 | 11 | DELIMITER $$ 12 | DROP PROCEDURE IF EXISTS Copy_and_Clean_Data; 13 | CREATE PROCEDURE Copy_and_Clean_Data() 14 | BEGIN 15 | -- CREATING OUR TABLE 16 | CREATE TABLE IF NOT EXISTS `us_household_income_Cleaned` ( 17 | `row_id` int DEFAULT NULL, 18 | `id` int DEFAULT NULL, 19 | `State_Code` int DEFAULT NULL, 20 | `State_Name` text, 21 | `State_ab` text, 22 | `County` text, 23 | `City` text, 24 | `Place` text, 25 | `Type` text, 26 | `Primary` text, 27 | `Zip_Code` int DEFAULT NULL, 28 | `Area_Code` int DEFAULT NULL, 29 | `ALand` int DEFAULT NULL, 30 | `AWater` int DEFAULT NULL, 31 | `Lat` double DEFAULT NULL, 32 | `Lon` double DEFAULT NULL, 33 | `TimeStamp` TIMESTAMP DEFAULT NULL 34 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 35 | 36 | -- COPY DATA TO NEW TABLE 37 | INSERT INTO us_household_income_Cleaned 38 | SELECT *, CURRENT_TIMESTAMP 39 | FROM bakery.us_household_income; 40 | 41 | -- Data Cleaning Steps 42 | 43 | -- 1. Remove Duplicates 44 | DELETE FROM us_household_income_Cleaned 45 | WHERE 46 | row_id IN ( 47 | SELECT row_id 48 | FROM ( 49 | SELECT row_id, id, 50 | ROW_NUMBER() OVER ( 51 | PARTITION BY id, `TimeStamp` 52 | ORDER BY id, `TimeStamp`) AS row_num 53 | FROM 54 | us_household_income_Cleaned 55 | ) duplicates 56 | WHERE 57 | row_num > 1 58 | ); 59 | 60 | -- 2. Standardization 61 | UPDATE us_household_income_Cleaned 62 | SET State_Name = 'Georgia' 63 | WHERE State_Name = 'georia'; 64 | 65 | UPDATE us_household_income_Cleaned 66 | SET County = UPPER(County); 67 | 68 | UPDATE us_household_income_Cleaned 69 | SET City = UPPER(City); 70 | 71 | UPDATE us_household_income_Cleaned 72 | SET Place = UPPER(Place); 73 | 74 | UPDATE us_household_income_Cleaned 75 | SET State_Name = UPPER(State_Name); 76 | 77 | UPDATE us_household_income_Cleaned 78 | SET `Type` = 'CDP' 79 | WHERE `Type` = 'CPD'; 80 | 81 | UPDATE us_household_income_Cleaned 82 | SET `Type` = 'Borough' 83 | WHERE `Type` = 'Boroughs'; 84 | 85 | END $$ 86 | DELIMITER ; 87 | 88 | 89 | CALL Copy_and_Clean_Data(); 90 | 91 | 92 | -- CREATE EVENT 93 | DROP EVENT run_data_cleaning; 94 | CREATE EVENT run_data_cleaning 95 | ON SCHEDULE EVERY 30 DAY 96 | DO CALL Copy_and_Clean_Data(); 97 | 98 | 99 | 100 | 101 | 102 | 103 | -- CREATE TRIGGER 104 | DELIMITER $$ 105 | CREATE TRIGGER Transfer_clean_data 106 | AFTER INSERT ON bakery.us_household_income 107 | FOR EACH ROW 108 | BEGIN 109 | CALL Copy_and_Clean_Data(); 110 | END $$ 111 | DELIMITER ; 112 | 113 | 114 | INSERT INTO bakery.us_household_income 115 | (`row_id`,`id`,`State_Code`,`State_Name`,`State_ab`,`County`,`City`,`Place`,`Type`,`Primary`,`Zip_Code`,`Area_Code`,`ALand`,`AWater`,`Lat`,`Lon`) 116 | VALUES 117 | (121671,37025904,37,'North Carolina','NC','Alamance County','Charlotte','Alamance','Track','Track',28215,980,24011255,98062070,35.2661197,-80.6865346); 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 | -- DEBUGGING OR CHECKING SP WORKS 147 | SELECT row_id, id, row_num 148 | FROM ( 149 | SELECT row_id, id, 150 | ROW_NUMBER() OVER ( 151 | PARTITION BY id 152 | ORDER BY id) AS row_num 153 | FROM 154 | us_household_income_Cleaned 155 | ) duplicates 156 | WHERE 157 | row_num > 1; 158 | 159 | 160 | SELECT COUNT(row_id) 161 | FROM us_household_income_Cleaned; 162 | 163 | SELECT State_Name, COUNT(State_Name) 164 | FROM us_household_income_Cleaned 165 | GROUP BY State_Name; 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | -------------------------------------------------------------------------------- /1. Create Database.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 (11,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 | CONSTRAINT `fk_orders_customers` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`) ON UPDATE CASCADE 119 | ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 120 | INSERT INTO `customer_orders` VALUES (1,100101,1001,'2020-01-30',26.24,2); 121 | INSERT INTO `customer_orders` VALUES (2,100110,1002,'2021-08-25',6.19,1); 122 | INSERT INTO `customer_orders` VALUES (3,100106,1005,'2022-12-12',3.87,0); 123 | INSERT INTO `customer_orders` VALUES (4,100103,1007,'2018-03-22',4.00,5); 124 | INSERT INTO `customer_orders` VALUES (5,100102,1003,'2017-08-25',9.97,10); 125 | INSERT INTO `customer_orders` VALUES (6,100108,1009,'2018-11-18',87.01,1); 126 | INSERT INTO `customer_orders` VALUES (7,100101,1001,'2022-09-20',2.45,5); 127 | INSERT INTO `customer_orders` VALUES (8,100104,1008,'2018-06-08',16.42,0); 128 | INSERT INTO `customer_orders` VALUES (9,100105,1007,'2019-07-05',8.11,1); 129 | INSERT INTO `customer_orders` VALUES (10,100106,1006,'2018-04-22',53.12,3); 130 | INSERT INTO `customer_orders` VALUES (11,100103,1001,'2019-11-18',27.01,1); 131 | INSERT INTO `customer_orders` VALUES (12,100101,1003,'2018-09-20',10.45,5); 132 | INSERT INTO `customer_orders` VALUES (13,100106,1008,'2020-06-08',90.42,0); 133 | INSERT INTO `customer_orders` VALUES (14,100102,1009,'2022-07-05',11.11,1); 134 | INSERT INTO `customer_orders` VALUES (15,100104,1006,'2020-04-22',24.12,3); 135 | 136 | 137 | 138 | CREATE TABLE `customer_orders_review` ( 139 | `order_id` int(11) NOT NULL AUTO_INCREMENT, 140 | `customer_id` int(11) NOT NULL, 141 | `product_id` int(11) NOT NULL, 142 | `order_date` date NOT NULL, 143 | `Rating 1-10` int(11) NOT NULL, 144 | PRIMARY KEY (`order_id`) 145 | ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 146 | INSERT INTO `customer_orders_review` VALUES (1,100101,1001,'2020-01-30',8); 147 | INSERT INTO `customer_orders_review` VALUES (2,100110,1002,'2021-08-25',5); 148 | INSERT INTO `customer_orders_review` VALUES (3,100111,1005,'2022-12-12',10); 149 | INSERT INTO `customer_orders_review` VALUES (4,100103,1007,'2081-03-22',7); 150 | INSERT INTO `customer_orders_review` VALUES (5,100102,1003,'2017-08-25',6); 151 | INSERT INTO `customer_orders_review` VALUES (7,100101,1001,'2022-09-20',8); 152 | INSERT INTO `customer_orders_review` VALUES (8,100104,1008,'2018-06-08',9); 153 | INSERT INTO `customer_orders_review` VALUES (9,100105,1007,'2019-07-05',6); 154 | INSERT INTO `customer_orders_review` VALUES (10,100106,1006,'2018-04-22',8); 155 | INSERT INTO `customer_orders_review` VALUES (11,100103,1001,'2019-11-18',6); 156 | INSERT INTO `customer_orders_review` VALUES (12,1001001,1003,'2018-09-20',9); 157 | INSERT INTO `customer_orders_review` VALUES (13,100106,1008,'2020-06-08',10); 158 | INSERT INTO `customer_orders_review` VALUES (14,100102,1009,'2023-07-05',8); 159 | INSERT INTO `customer_orders_review` VALUES (15,100104,1006,'2020-04-22',7); 160 | 161 | 162 | 163 | 164 | 165 | CREATE TABLE `employees` ( 166 | `employee_id` int(11) NOT NULL AUTO_INCREMENT, 167 | `first_name` varchar(50) NOT NULL, 168 | `last_name` varchar(50) NOT NULL, 169 | `department` varchar(50) NOT NULL, 170 | `title` varchar(50) NOT NULL, 171 | `salary` int(11) NOT NULL, 172 | `boss_id` int(11), 173 | PRIMARY KEY (`employee_id`) 174 | ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 175 | INSERT INTO `employees` VALUES (1,'Christine','Freberg','Bakery','Lead Baker', 70000, NULL); 176 | INSERT INTO `employees` VALUES (2,'Dwight','Schrute','Bakery','Assistant to the Lead Baker', 45000, 1); 177 | INSERT INTO `employees` VALUES (3,'Tom','Haveford','Bakery','Chocolatier', 45000, 1); 178 | INSERT INTO `employees` VALUES (4,'Ann','Perkins','Bakery','Bakery Clerk', 30000, 1); 179 | INSERT INTO `employees` VALUES (5,'Carl','Lorthner','Bakery','Dough Maker', 40000, 1); 180 | INSERT INTO `employees` VALUES (6,'Ron','Swanson','Marketing','Director of Marketing', 75000, NULL); 181 | INSERT INTO `employees` VALUES (7,'Troy','Barnes','Marketing','Lead Marketer', 60000, 6); 182 | INSERT INTO `employees` VALUES (8,'Jeff','Winger','Marketing','Marketing Analyst', 60000, 7); 183 | INSERT INTO `employees` VALUES (9,'Annie','Edison','Marketing','Social Media Marketer', 65000, 7); 184 | 185 | CREATE TABLE `client` ( 186 | `client_id` smallint(6) NOT NULL AUTO_INCREMENT, 187 | `name` varchar(50) NOT NULL, 188 | PRIMARY KEY (`client_id`) 189 | ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 190 | INSERT INTO `client` VALUES (1001,'Very Good Hotel'); 191 | INSERT INTO `client` VALUES (1002,'Wedding Vendor 4 U'); 192 | INSERT INTO `client` VALUES (1003,'Catering Is Us'); 193 | INSERT INTO `client` VALUES (1004,"Bradley's Law Firm"); 194 | 195 | CREATE TABLE `client_invoices` ( 196 | `invoice_id` int(11) NOT NULL AUTO_INCREMENT, 197 | `client_id` int(11) NOT NULL, 198 | `product_id` int(11) NOT NULL, 199 | `order_date` date NOT NULL, 200 | `order_total` decimal(6,2) NOT NULL, 201 | `total_paid` decimal(6,2) DEFAULT NULL, 202 | PRIMARY KEY (`invoice_id`) 203 | ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 204 | INSERT INTO `client_invoices` VALUES (1,1001,1001,'2020-01-30',126.24,126.24); 205 | INSERT INTO `client_invoices` VALUES (2,1002,1002,'2021-08-25',196.19,100); 206 | INSERT INTO `client_invoices` VALUES (3,1001,1005,'2022-12-12',1003.87,0); 207 | INSERT INTO `client_invoices` VALUES (4,1003,1007,'2018-03-22',404.00,404.00); 208 | INSERT INTO `client_invoices` VALUES (5,1004,1003,'2017-08-25',309.97,0); 209 | INSERT INTO `client_invoices` VALUES (6,1002,1009,'2018-11-18',187.01,120); 210 | INSERT INTO `client_invoices` VALUES (7,1003,1001,'2022-09-20',92.45,92.45); 211 | INSERT INTO `client_invoices` VALUES (8,1001,1008,'2018-06-08',116.42,0); 212 | INSERT INTO `client_invoices` VALUES (9,1002,1007,'2019-07-05',98.11,98.11); 213 | INSERT INTO `client_invoices` VALUES (10,1004,1006,'2018-04-22',153.12,0); 214 | INSERT INTO `client_invoices` VALUES (11,1003,1001,'2019-11-18',127.01,0); 215 | INSERT INTO `client_invoices` VALUES (12,1001,1003,'2018-09-20',1010.45,575); 216 | INSERT INTO `client_invoices` VALUES (13,1003,1008,'2020-06-08',490.42,490.42); 217 | INSERT INTO `client_invoices` VALUES (14,1001,1009,'2022-07-05',211.11,100); 218 | INSERT INTO `client_invoices` VALUES (15,1002,1006,'2020-04-22',324.12,300); 219 | 220 | 221 | 222 | 223 | CREATE TABLE `client_payments` ( 224 | `payment_id` int(11) NOT NULL AUTO_INCREMENT, 225 | `client_id` int(11) NOT NULL, 226 | `invoice_id` int(11) NOT NULL, 227 | `payment_date` date NOT NULL, 228 | `amount_paid` decimal(6,2) NOT NULL, 229 | PRIMARY KEY (`payment_id`) 230 | ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 231 | INSERT INTO `client_payments` VALUES (1,1001,1,'2020-01-1',126.24); 232 | INSERT INTO `client_payments` VALUES (2,1002,2,'2021-08-26',100); 233 | INSERT INTO `client_payments` VALUES (3,1003,4,'2022-12-13',404.00); 234 | INSERT INTO `client_payments` VALUES (4,1002,6,'2018-03-23',120); 235 | INSERT INTO `client_payments` VALUES (5,1003,7,'2017-08-26',92.45); 236 | INSERT INTO `client_payments` VALUES (6,1002,9,'2018-11-21',98.11); 237 | INSERT INTO `client_payments` VALUES (7,1001,12,'2022-09-24',575); 238 | INSERT INTO `client_payments` VALUES (8,1003,13,'2018-06-10',490.42); 239 | INSERT INTO `client_payments` VALUES (9,1001,14,'2019-07-15',100); 240 | INSERT INTO `client_payments` VALUES (10,1002,15,'2018-04-24',300); 241 | 242 | --------------------------------------------------------------------------------