├── 01. Introduction to Databases └── SQL Intro.pdf ├── 02. Introduction to SQL & Setup └── SQL.pdf ├── 03. Data Retrieval with SQL ├── 01. create database.sql ├── 02. select.sql ├── 03. where.sql ├── 04. limit & offset.sql └── 05. order by.sql ├── 04. Cases in SQL ├── 01. create database.sql └── 02. cases.sql ├── 05. Introduction to DDL Commands ├── 01. create database.sql ├── 01. create.sql ├── 02. alter.sql └── 02. drop .sql ├── 07. Keys & Constraints └── 01. keys & constraints.sql ├── 08. Insert Data in a table └── 01. insert data in a table.sql ├── 09. Update data from a table └── 01. update data from a table.sql ├── 10. Delete data from a table └── 01. delete data from a table.sql ├── 11. SQL in Action ├── assignment.sql └── data.csv.zip ├── 12. Pattern Matching ├── 01. pattern matching.sql └── data.csv.zip ├── 13. Aggregate Functions └── 01. aggregate functions.sql ├── 14. Grouping and Filtering Data ├── 01. groupby.sql └── data.csv.zip ├── 15. Working with Subqueries └── 1. Subqueries.sql ├── 16. Joins in Action ├── 01. creating database.sql ├── 02. joins.sql └── Joins on Cricket Players │ ├── 01. data exploration.sql │ ├── final.csv │ ├── player_basic.csv │ ├── players_and_team.csv │ ├── players_full.csv │ └── teams.csv ├── 17. Set Operators in Action ├── 01. creating database.sql ├── 02. operators.sql └── 03. operators assignment.sql ├── 18. Restaurant Data Analysis with SQL ├── 01. assignment.sql ├── 02. solution.sql └── restaurants.csv ├── 19. Window Functions ├── 01. window.sql └── restaurants.csv ├── 20. Ranks in SQL ├── 01. rank.sql └── restaurants.csv ├── 21. Advance Window Functions ├── 01. practice.sql └── restaurants.csv ├── 22. Views in SQL ├── 01. views.sql └── restaurants.csv ├── 23. Table Aliases ├── 1. Exporting Tables.sql ├── 3. Temporary Tables.sql ├── 3. alias.sql └── restaurants.csv ├── 24. Functions & Procedures ├── 01. dataset creation.sql ├── 02. functions.sql └── 03. procedures.sql ├── 25. Transections ├── 01. dataset creation.sql └── 02. transections.sql ├── 26. Errors & Exceptions ├── Errors & Exceptions.sql └── Exception Handling.sql ├── 27. Introduction to Data Cleaning ├── 01. dataset creation.sql └── 02. Introduction to Data Cleaning.sql ├── 28. Handling Missing Values ├── 01. dataset creation.sql └── 02. Handling Missing Values.sql ├── 29. Handling Duplicate Values ├── 01. dataset creation.sql └── 02. handling duplicate values.sql ├── 30. Handling Outliers ├── 01. dataset creation.sql └── 02. handling outliers.sql ├── 31. Working with Dates ├── 01. dataset creation.sql └── 02. working with dates.sql ├── 32. Data Cleaning ├── 01. dataset creation.sql └── 02. data cleaning.sql ├── 33. Restaurant Data Cleaning ├── 01. data cleaning.sql └── restaurants.csv ├── 34. Analysing Query Execution ├── 01. dataset creation.sql └── 02. query execution plan.sql ├── 35. Improving Query Performance ├── 01. dataset creation.sql └── overall.sql ├── 36. Table Partitioning ├── 01. dataset creation.sql └── partition tables.sql ├── 37. Indexing in SQL ├── 01. dataset creation.sql └── Indexing.sql ├── 38. Common Table Expressions ├── 01. dataset creation.sql └── CTE.sql ├── 39. SQL with ChatGPT ├── SQLite.csv └── SQLite.sql ├── LICENSE └── README.md /01. Introduction to Databases/SQL Intro.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AshishJangra27/SQL-for-Data-Analysis/a40aa1b7015cead9cae2fbc5bd1874dcca68a2d3/01. Introduction to Databases/SQL Intro.pdf -------------------------------------------------------------------------------- /02. Introduction to SQL & Setup/SQL.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AshishJangra27/SQL-for-Data-Analysis/a40aa1b7015cead9cae2fbc5bd1874dcca68a2d3/02. Introduction to SQL & Setup/SQL.pdf -------------------------------------------------------------------------------- /03. Data Retrieval with SQL/01. create database.sql: -------------------------------------------------------------------------------- 1 | use employees; 2 | CREATE TABLE Employees ( 3 | employee_id INT PRIMARY KEY, 4 | name VARCHAR(50), 5 | age INT, 6 | department VARCHAR(50) 7 | ); 8 | 9 | INSERT INTO Employees (employee_id, name, age, department) VALUES 10 | (1, 'John', 35, 'Sales'), 11 | (2, 'Alice', 28, 'Marketing'), 12 | (3, 'Bob', 40, 'HR'), 13 | (4, 'Sarah', 33, 'Finance'), 14 | (5, 'Michael', 25, 'Operations'), 15 | (6, 'Emily', 29, 'Sales'), 16 | (7, 'David', 42, 'HR'), 17 | (8, 'Jessica', 31, 'Marketing'), 18 | (9, 'Richard', 37, NULL), 19 | (10, 'Michelle', 26, 'Finance'), 20 | (11, 'Daniel', 34, 'Operations'), 21 | (12, 'Laura', 38, 'Sales'), 22 | (13, 'Mark', 27, NULL), 23 | (14, 'Jennifer', 32, 'Marketing'), 24 | (15, 'Andrew', 39, 'HR'), 25 | (16, 'Elizabeth', 24, 'Finance'), 26 | (17, 'Matthew', 30, 'Operations'), 27 | (18, 'Samantha', 36, 'Sales'), 28 | (19, 'Christopher', NULL, 'Marketing'), 29 | (20, 'Stephanie', 41, 'HR'), 30 | (21, 'Kevin', 29, 'Finance'), 31 | (22, 'Amanda', 35, 'Operations'), 32 | (23, 'Jason', 33, 'Sales'), 33 | (24, 'Nicole', 28, 'Marketing'), 34 | (25, 'Ryan', 39, 'HR'), 35 | (26, 'Ashley', 27, NULL), 36 | (27, 'Justin', 36, 'Finance'), 37 | (28, 'Rachel', 31, 'Operations'), 38 | (29, 'Eric', 40, 'Sales'), 39 | (30, 'Megan', 25, 'Marketing'), 40 | (31, 'Brandon', 37, 'HR'), 41 | (32, 'Kimberly', 30, 'Finance'), 42 | (33, 'Tyler', 24, 'Operations'), 43 | (34, 'Heather', 38, 'Sales'), 44 | (35, 'Jeffrey', 31, 'Marketing'), 45 | (36, 'Melissa', 40, 'HR'), 46 | (37, 'Scott', 26, 'Finance'), 47 | (38, 'Tiffany', 35, 'Operations'), 48 | (39, 'Joshua', 33, 'Sales'), 49 | (40, 'Lauren', 29, 'Marketing'), 50 | (41, 'Brian', 42, 'HR'), 51 | (42, 'Christina', 27, 'Finance'), 52 | (43, 'Kyle', 34, 'Operations'), 53 | (44, 'Victoria', 39, 'Sales'), 54 | (45, 'Nathan', 28, 'Marketing'), 55 | (46, 'Amy', 36, 'HR'), 56 | (47, 'Patrick', 25, 'Finance'), 57 | (48, 'Kelly', 30, 'Operations'), 58 | (49, 'Adam', 37, 'Sales'), 59 | (50, 'Christine', 31, 'Marketing'), 60 | (51, 'Gregory', 40, 'HR'), 61 | (52, 'Maria', 26, 'Finance'), 62 | (53, 'Jonathan', 38, 'Operations'), 63 | (54, 'Lisa', 32, 'Sales'), 64 | (55, 'Shawn', 29, 'Marketing'), 65 | (56, 'Julie', 41, 'HR'), 66 | (57, 'Stephen', 27, 'Finance'), 67 | (58, 'Jamie', 34, 'Operations'), 68 | (59, 'Rebecca', 39, 'Sales'), 69 | (60, 'Aaron', NULL, 'Marketing'), 70 | (61, 'Jacqueline', 35, 'HR'), 71 | (62, 'Derek', 30, 'Finance'), 72 | (63, 'Hannah', 24, 'Operations'), 73 | (64, 'Edward', 37, 'Sales'), 74 | (65, 'Cassandra', 31, 'Marketing'), 75 | (66, 'Alexander', 40, 'HR'), 76 | (67, 'Kristen', 26, 'Finance'), 77 | (68, 'Zachary', 38, 'Operations'), 78 | (69, 'April', 32, 'Sales'), 79 | (70, 'Jeremy', 29, 'Marketing'), 80 | (71, 'Katherine', 41, 'HR'), 81 | (72, 'Dylan', 27, 'Finance'), 82 | (73, 'Lindsay', 34, 'Operations'), 83 | (74, 'Travis', 39, 'Sales'), 84 | (75, 'Allison', 25, 'Marketing'), 85 | (76, 'Phillip', 42, 'HR'), 86 | (77, 'Kelsey', 28, 'Finance'), 87 | (78, 'Cody', 36, 'Operations'), 88 | (79, 'Bethany', 33, 'Sales'), 89 | (80, 'Wesley', 30, 'Marketing'), 90 | (81, 'Monica', 41, 'HR'), 91 | (82, 'Brett', 26, 'Finance'), 92 | (83, 'Tara', 35, 'Operations'), 93 | (84, 'Gerald', 31, 'Sales'), 94 | (85, 'Erin', 40, 'Marketing'), 95 | (86, 'Donald', 27, 'HR'), 96 | (87, 'Pamela', 38, 'Finance'), 97 | (88, 'Logan', 32, 'Operations'), 98 | (89, 'Christine', 39, 'Sales'), 99 | (90, 'Justin', 25, 'Marketing'), 100 | (91, 'Anna', 42, 'HR'), 101 | (92, 'Bryan', 28, 'Finance'), 102 | (93, 'Leah', 36, 'Operations'), 103 | (94, 'Keith', 33, 'Sales'), 104 | (95, 'Caitlin', 29, 'Marketing'), 105 | (96, 'Alexander', 40, 'HR'), 106 | (97, 'Cassandra', 27, 'Finance'), 107 | (98, 'Jared', 34, 'Operations'), 108 | (99, 'Tiffany', 39, 'Sales'), 109 | (100, 'Joshua', 31, 'Marketing'); 110 | -------------------------------------------------------------------------------- /03. Data Retrieval with SQL/02. select.sql: -------------------------------------------------------------------------------- 1 | use employees; 2 | 3 | select * from Employees; 4 | 5 | select * from employees.Employees; 6 | 7 | select age,name from Employees; 8 | 9 | select name as employee_name, age as employee_age from Employees; -------------------------------------------------------------------------------- /03. Data Retrieval with SQL/03. where.sql: -------------------------------------------------------------------------------- 1 | use employees; 2 | select * from Employees; 3 | 4 | -- Employees with age more than 30 5 | select * from Employees where age > 30; 6 | 7 | -- Employees with age more than 30 from sales 8 | select * from Employees where age > 30 and department = 'sales'; 9 | 10 | -- Employees with age in range 30 - 40 years 11 | select * from Employees where age >= 30 and age <= 40; 12 | select * from Employees where age between 30 and 40; 13 | 14 | -- Employees from Sales or Marketing 15 | select * from Employees where department = 'Marketing' or department = 'Sales'; 16 | 17 | -- Searching for name starting with Jo 18 | select * from Employees where name like 'Jo%'; 19 | 20 | -- Getting only not null data of departments 21 | select * from Employees where department is not null; 22 | 23 | -- Getting only not null data throught the table 24 | select * from Employees where department is not null and age is not null; 25 | 26 | -- Getting data using IN command 27 | select * from Employees where department in ('Sales','Marketing'); 28 | 29 | -- Getting data using NOT IN command 30 | select * from Employees where department not in ('Sales','Marketing'); 31 | 32 | -- Marketing or Sales people witha ge more than 30 33 | select * from Employees where (department in ('Sales','Marketing')) and age > 30; -------------------------------------------------------------------------------- /03. Data Retrieval with SQL/04. limit & offset.sql: -------------------------------------------------------------------------------- 1 | use employees; 2 | select * from Employees; 3 | 4 | select * from Employees limit 5; 5 | 6 | select * from Employees limit 5 offset 10; 7 | 8 | select * from Employees where department = 'Sales' limit 5 offset 0; 9 | select * from Employees where department = 'Sales' limit 5; -------------------------------------------------------------------------------- /03. Data Retrieval with SQL/05. order by.sql: -------------------------------------------------------------------------------- 1 | use employees; 2 | select * from Employees; 3 | 4 | -- Sorting the data based on age 5 | select * from Employees order by age; 6 | select * from Employees order by age asc; 7 | select * from Employees order by age desc; 8 | 9 | -- Sorting the data based on multiple columns 10 | select * from Employees order by department asc, age asc; 11 | 12 | -- Sorting sales data based on age 13 | select * from Employees where department = 'sales' order by age; 14 | 15 | -- Sorted age data of not null values 16 | select * from Employees where age is not null order by age asc; 17 | 18 | -- Sorted age data of not null values with limit 5 19 | select * from Employees 20 | where age is not null 21 | order by age asc 22 | limit 5; 23 | 24 | -- Sorting the data based on the length fo the name 25 | select * from Employees order by length(name); -------------------------------------------------------------------------------- /04. Cases in SQL/01. create database.sql: -------------------------------------------------------------------------------- 1 | use employees; 2 | CREATE TABLE Employees ( 3 | employee_id INT PRIMARY KEY, 4 | name VARCHAR(50), 5 | age INT, 6 | department VARCHAR(50) 7 | ); 8 | 9 | INSERT INTO Employees (employee_id, name, age, department) VALUES 10 | (1, 'John', 35, 'Sales'), 11 | (2, 'Alice', 28, 'Marketing'), 12 | (3, 'Bob', 40, 'HR'), 13 | (4, 'Sarah', 33, 'Finance'), 14 | (5, 'Michael', 25, 'Operations'), 15 | (6, 'Emily', 29, 'Sales'), 16 | (7, 'David', 42, 'HR'), 17 | (8, 'Jessica', 31, 'Marketing'), 18 | (9, 'Richard', 37, NULL), 19 | (10, 'Michelle', 26, 'Finance'), 20 | (11, 'Daniel', 34, 'Operations'), 21 | (12, 'Laura', 38, 'Sales'), 22 | (13, 'Mark', 27, NULL), 23 | (14, 'Jennifer', 32, 'Marketing'), 24 | (15, 'Andrew', 39, 'HR'), 25 | (16, 'Elizabeth', 24, 'Finance'), 26 | (17, 'Matthew', 30, 'Operations'), 27 | (18, 'Samantha', 36, 'Sales'), 28 | (19, 'Christopher', NULL, 'Marketing'), 29 | (20, 'Stephanie', 41, 'HR'), 30 | (21, 'Kevin', 29, 'Finance'), 31 | (22, 'Amanda', 35, 'Operations'), 32 | (23, 'Jason', 33, 'Sales'), 33 | (24, 'Nicole', 28, 'Marketing'), 34 | (25, 'Ryan', 39, 'HR'), 35 | (26, 'Ashley', 27, NULL), 36 | (27, 'Justin', 36, 'Finance'), 37 | (28, 'Rachel', 31, 'Operations'), 38 | (29, 'Eric', 40, 'Sales'), 39 | (30, 'Megan', 25, 'Marketing'), 40 | (31, 'Brandon', 37, 'HR'), 41 | (32, 'Kimberly', 30, 'Finance'), 42 | (33, 'Tyler', 24, 'Operations'), 43 | (34, 'Heather', 38, 'Sales'), 44 | (35, 'Jeffrey', 31, 'Marketing'), 45 | (36, 'Melissa', 40, 'HR'), 46 | (37, 'Scott', 26, 'Finance'), 47 | (38, 'Tiffany', 35, 'Operations'), 48 | (39, 'Joshua', 33, 'Sales'), 49 | (40, 'Lauren', 29, 'Marketing'), 50 | (41, 'Brian', 42, 'HR'), 51 | (42, 'Christina', 27, 'Finance'), 52 | (43, 'Kyle', 34, 'Operations'), 53 | (44, 'Victoria', 39, 'Sales'), 54 | (45, 'Nathan', 28, 'Marketing'), 55 | (46, 'Amy', 36, 'HR'), 56 | (47, 'Patrick', 25, 'Finance'), 57 | (48, 'Kelly', 30, 'Operations'), 58 | (49, 'Adam', 37, 'Sales'), 59 | (50, 'Christine', 31, 'Marketing'), 60 | (51, 'Gregory', 40, 'HR'), 61 | (52, 'Maria', 26, 'Finance'), 62 | (53, 'Jonathan', 38, 'Operations'), 63 | (54, 'Lisa', 32, 'Sales'), 64 | (55, 'Shawn', 29, 'Marketing'), 65 | (56, 'Julie', 41, 'HR'), 66 | (57, 'Stephen', 27, 'Finance'), 67 | (58, 'Jamie', 34, 'Operations'), 68 | (59, 'Rebecca', 39, 'Sales'), 69 | (60, 'Aaron', NULL, 'Marketing'), 70 | (61, 'Jacqueline', 35, 'HR'), 71 | (62, 'Derek', 30, 'Finance'), 72 | (63, 'Hannah', 24, 'Operations'), 73 | (64, 'Edward', 37, 'Sales'), 74 | (65, 'Cassandra', 31, 'Marketing'), 75 | (66, 'Alexander', 40, 'HR'), 76 | (67, 'Kristen', 26, 'Finance'), 77 | (68, 'Zachary', 38, 'Operations'), 78 | (69, 'April', 32, 'Sales'), 79 | (70, 'Jeremy', 29, 'Marketing'), 80 | (71, 'Katherine', 41, 'HR'), 81 | (72, 'Dylan', 27, 'Finance'), 82 | (73, 'Lindsay', 34, 'Operations'), 83 | (74, 'Travis', 39, 'Sales'), 84 | (75, 'Allison', 25, 'Marketing'), 85 | (76, 'Phillip', 42, 'HR'), 86 | (77, 'Kelsey', 28, 'Finance'), 87 | (78, 'Cody', 36, 'Operations'), 88 | (79, 'Bethany', 33, 'Sales'), 89 | (80, 'Wesley', 30, 'Marketing'), 90 | (81, 'Monica', 41, 'HR'), 91 | (82, 'Brett', 26, 'Finance'), 92 | (83, 'Tara', 35, 'Operations'), 93 | (84, 'Gerald', 31, 'Sales'), 94 | (85, 'Erin', 40, 'Marketing'), 95 | (86, 'Donald', 27, 'HR'), 96 | (87, 'Pamela', 38, 'Finance'), 97 | (88, 'Logan', 32, 'Operations'), 98 | (89, 'Christine', 39, 'Sales'), 99 | (90, 'Justin', 25, 'Marketing'), 100 | (91, 'Anna', 42, 'HR'), 101 | (92, 'Bryan', 28, 'Finance'), 102 | (93, 'Leah', 36, 'Operations'), 103 | (94, 'Keith', 33, 'Sales'), 104 | (95, 'Caitlin', 29, 'Marketing'), 105 | (96, 'Alexander', 40, 'HR'), 106 | (97, 'Cassandra', 27, 'Finance'), 107 | (98, 'Jared', 34, 'Operations'), 108 | (99, 'Tiffany', 39, 'Sales'), 109 | (100, 'Joshua', 31, 'Marketing'); 110 | -------------------------------------------------------------------------------- /04. Cases in SQL/02. cases.sql: -------------------------------------------------------------------------------- 1 | use employees; 2 | 3 | select * from Employees; 4 | 5 | -- Basic case for sales team 6 | select name, 7 | case department 8 | when 'sales' then 'sales team' 9 | else 'other' 10 | end as employee_name from Employees; 11 | 12 | -- Basic case for sales & marketing team 13 | select name, 14 | case department 15 | when 'sales' then 'sales team' 16 | when 'marketing' then 'marketing team' 17 | else 'other' 18 | end as employee_name from Employees; 19 | 20 | -- Cases with respect to age 21 | select name, 22 | case 23 | when age < 30 then 'Young' 24 | when age <= 30 and age <= 40 then 'mid-aged' 25 | else 'Senior' 26 | end as employee_name from Employees; 27 | 28 | 29 | -- Nested Cases 30 | select name, 31 | case 32 | when age < 30 then 33 | case 34 | when department = 'Sales' then 'Jr Sales' 35 | else 'Junior' 36 | end 37 | when age >= 30 and age <= 40 then 'Middle' 38 | else 'Senior' 39 | end as employee_name from Employees; 40 | 41 | -- Dealing with null departments 42 | select name, 43 | case 44 | when department is null then 'No Department Assigned' 45 | else department 46 | end as department_status from Employees; 47 | -------------------------------------------------------------------------------- /05. Introduction to DDL Commands/01. create database.sql: -------------------------------------------------------------------------------- 1 | use employees; 2 | CREATE TABLE Employees ( 3 | employee_id INT PRIMARY KEY, 4 | name VARCHAR(50), 5 | age INT, 6 | department VARCHAR(50) 7 | ); 8 | 9 | INSERT INTO Employees (employee_id, name, age, department) VALUES 10 | (1, 'John', 35, 'Sales'), 11 | (2, 'Alice', 28, 'Marketing'), 12 | (3, 'Bob', 40, 'HR'), 13 | (4, 'Sarah', 33, 'Finance'), 14 | (5, 'Michael', 25, 'Operations'), 15 | (6, 'Emily', 29, 'Sales'), 16 | (7, 'David', 42, 'HR'), 17 | (8, 'Jessica', 31, 'Marketing'), 18 | (9, 'Richard', 37, NULL), 19 | (10, 'Michelle', 26, 'Finance'), 20 | (11, 'Daniel', 34, 'Operations'), 21 | (12, 'Laura', 38, 'Sales'), 22 | (13, 'Mark', 27, NULL), 23 | (14, 'Jennifer', 32, 'Marketing'), 24 | (15, 'Andrew', 39, 'HR'), 25 | (16, 'Elizabeth', 24, 'Finance'), 26 | (17, 'Matthew', 30, 'Operations'), 27 | (18, 'Samantha', 36, 'Sales'), 28 | (19, 'Christopher', NULL, 'Marketing'), 29 | (20, 'Stephanie', 41, 'HR'), 30 | (21, 'Kevin', 29, 'Finance'), 31 | (22, 'Amanda', 35, 'Operations'), 32 | (23, 'Jason', 33, 'Sales'), 33 | (24, 'Nicole', 28, 'Marketing'), 34 | (25, 'Ryan', 39, 'HR'), 35 | (26, 'Ashley', 27, NULL), 36 | (27, 'Justin', 36, 'Finance'), 37 | (28, 'Rachel', 31, 'Operations'), 38 | (29, 'Eric', 40, 'Sales'), 39 | (30, 'Megan', 25, 'Marketing'), 40 | (31, 'Brandon', 37, 'HR'), 41 | (32, 'Kimberly', 30, 'Finance'), 42 | (33, 'Tyler', 24, 'Operations'), 43 | (34, 'Heather', 38, 'Sales'), 44 | (35, 'Jeffrey', 31, 'Marketing'), 45 | (36, 'Melissa', 40, 'HR'), 46 | (37, 'Scott', 26, 'Finance'), 47 | (38, 'Tiffany', 35, 'Operations'), 48 | (39, 'Joshua', 33, 'Sales'), 49 | (40, 'Lauren', 29, 'Marketing'), 50 | (41, 'Brian', 42, 'HR'), 51 | (42, 'Christina', 27, 'Finance'), 52 | (43, 'Kyle', 34, 'Operations'), 53 | (44, 'Victoria', 39, 'Sales'), 54 | (45, 'Nathan', 28, 'Marketing'), 55 | (46, 'Amy', 36, 'HR'), 56 | (47, 'Patrick', 25, 'Finance'), 57 | (48, 'Kelly', 30, 'Operations'), 58 | (49, 'Adam', 37, 'Sales'), 59 | (50, 'Christine', 31, 'Marketing'), 60 | (51, 'Gregory', 40, 'HR'), 61 | (52, 'Maria', 26, 'Finance'), 62 | (53, 'Jonathan', 38, 'Operations'), 63 | (54, 'Lisa', 32, 'Sales'), 64 | (55, 'Shawn', 29, 'Marketing'), 65 | (56, 'Julie', 41, 'HR'), 66 | (57, 'Stephen', 27, 'Finance'), 67 | (58, 'Jamie', 34, 'Operations'), 68 | (59, 'Rebecca', 39, 'Sales'), 69 | (60, 'Aaron', NULL, 'Marketing'), 70 | (61, 'Jacqueline', 35, 'HR'), 71 | (62, 'Derek', 30, 'Finance'), 72 | (63, 'Hannah', 24, 'Operations'), 73 | (64, 'Edward', 37, 'Sales'), 74 | (65, 'Cassandra', 31, 'Marketing'), 75 | (66, 'Alexander', 40, 'HR'), 76 | (67, 'Kristen', 26, 'Finance'), 77 | (68, 'Zachary', 38, 'Operations'), 78 | (69, 'April', 32, 'Sales'), 79 | (70, 'Jeremy', 29, 'Marketing'), 80 | (71, 'Katherine', 41, 'HR'), 81 | (72, 'Dylan', 27, 'Finance'), 82 | (73, 'Lindsay', 34, 'Operations'), 83 | (74, 'Travis', 39, 'Sales'), 84 | (75, 'Allison', 25, 'Marketing'), 85 | (76, 'Phillip', 42, 'HR'), 86 | (77, 'Kelsey', 28, 'Finance'), 87 | (78, 'Cody', 36, 'Operations'), 88 | (79, 'Bethany', 33, 'Sales'), 89 | (80, 'Wesley', 30, 'Marketing'), 90 | (81, 'Monica', 41, 'HR'), 91 | (82, 'Brett', 26, 'Finance'), 92 | (83, 'Tara', 35, 'Operations'), 93 | (84, 'Gerald', 31, 'Sales'), 94 | (85, 'Erin', 40, 'Marketing'), 95 | (86, 'Donald', 27, 'HR'), 96 | (87, 'Pamela', 38, 'Finance'), 97 | (88, 'Logan', 32, 'Operations'), 98 | (89, 'Christine', 39, 'Sales'), 99 | (90, 'Justin', 25, 'Marketing'), 100 | (91, 'Anna', 42, 'HR'), 101 | (92, 'Bryan', 28, 'Finance'), 102 | (93, 'Leah', 36, 'Operations'), 103 | (94, 'Keith', 33, 'Sales'), 104 | (95, 'Caitlin', 29, 'Marketing'), 105 | (96, 'Alexander', 40, 'HR'), 106 | (97, 'Cassandra', 27, 'Finance'), 107 | (98, 'Jared', 34, 'Operations'), 108 | (99, 'Tiffany', 39, 'Sales'), 109 | (100, 'Joshua', 31, 'Marketing'); 110 | -------------------------------------------------------------------------------- /05. Introduction to DDL Commands/01. create.sql: -------------------------------------------------------------------------------- 1 | -- Creating first table 2 | drop table if exists employee; 3 | create table employee (employee_id INT, name VARCHAR(50), age INT, department VARCHAR(50)); 4 | select * from employee; 5 | 6 | -- Table with constraints 7 | drop table if exists employee; 8 | create table employee ( 9 | employee_id INT, 10 | name VARCHAR(50) not null, 11 | age INT check (age >= 18), 12 | department VARCHAR(50)); 13 | select * from employee; 14 | 15 | 16 | drop table if exists employee; 17 | create table employee ( 18 | employee_id SERIAL, 19 | name VARCHAR(50) not null, 20 | age INT check (age >= 18), 21 | department VARCHAR(50), 22 | hire_date date); 23 | select * from employee; -------------------------------------------------------------------------------- /05. Introduction to DDL Commands/02. alter.sql: -------------------------------------------------------------------------------- 1 | drop table if exists employee; 2 | create table employee ( 3 | employee_id INT, 4 | name VARCHAR(50) not null, 5 | age INT check (age >= 18), 6 | department VARCHAR(50)); 7 | select * from employee; 8 | 9 | -- Adding new column in a table 10 | alter table employee add column mail varchar(50); 11 | select * from employee; 12 | 13 | -- Renaming the column 14 | alter table employee rename column employee_id to id; 15 | select * from employee; 16 | 17 | -- Drop the column 18 | alter table employee drop column mail; 19 | select * from employee; -------------------------------------------------------------------------------- /05. Introduction to DDL Commands/02. drop .sql: -------------------------------------------------------------------------------- 1 | drop table if exists employee; 2 | create table employee (employee_id INT, name VARCHAR(50), age INT, department VARCHAR(50)); 3 | select * from employee; 4 | drop table if exists employee; 5 | 6 | select * from employee; -------------------------------------------------------------------------------- /07. Keys & Constraints/01. keys & constraints.sql: -------------------------------------------------------------------------------- 1 | -- Primary Key 2 | DROP Table if EXISTS Employees; 3 | CREATE TABLE if not exists Employees ( 4 | employee_id SERIAL PRIMARY KEY, 5 | name VARCHAR(50), 6 | department VARCHAR(50)); 7 | Select * from Employees; 8 | 9 | -- Composite Key 10 | DROP Table if EXISTS Orders; 11 | CREATE TABLE Orders ( 12 | order_id SERIAL, 13 | customer_id INT, 14 | order_date DATE, 15 | PRIMARY KEY (order_id, customer_id)); 16 | Select * from Orders; 17 | DROP Table if EXISTS Orders; 18 | 19 | -- Foreign Key 20 | CREATE TABLE Orders ( 21 | order_id SERIAL PRIMARY KEY, 22 | customer_id INT, 23 | order_date DATE, 24 | FOREIGN KEY (customer_id) REFERENCES Employees (employee_id)); 25 | Select * from Orders; 26 | 27 | -- Unique Constrains 28 | DROP Table if EXISTS Employees; 29 | CREATE TABLE if not exists Employees ( 30 | employee_id SERIAL PRIMARY KEY, 31 | email VARCHAR(50) UNIQUE, 32 | name VARCHAR(50), 33 | department VARCHAR(50)); 34 | Select * from Employees; 35 | 36 | -- Unique Constrains as Composite Key 37 | DROP Table if EXISTS Employees; 38 | CREATE TABLE if not exists Employees ( 39 | employee_id SERIAL PRIMARY KEY, 40 | email VARCHAR(50) UNIQUE, 41 | phone INT UNIQUE, 42 | name VARCHAR(50), 43 | department VARCHAR(50), 44 | UNIQUE (email,phone)); 45 | Select * from Employees; 46 | 47 | -- Check Constraint 48 | DROP Table if EXISTS Employees; 49 | CREATE TABLE if not exists Employees ( 50 | employee_id SERIAL PRIMARY KEY, 51 | email VARCHAR(50) UNIQUE, 52 | age INT CHECK (age >= 18), 53 | phone INT UNIQUE, 54 | name VARCHAR(50), 55 | department VARCHAR(50), 56 | UNIQUE (email,phone)); 57 | Select * from Employees; 58 | 59 | 60 | -------------------------------------------------------------------------------- /08. Insert Data in a table/01. insert data in a table.sql: -------------------------------------------------------------------------------- 1 | use employees; 2 | 3 | DROP Table if EXISTS Employees; 4 | CREATE TABLE if not exists Employees ( 5 | employee_id SERIAL PRIMARY KEY, 6 | first_name VARCHAR(50), 7 | last_name VARCHAR(50), 8 | department_id INT, 9 | hire_date DATE); 10 | Select * from Employees; 11 | 12 | -- Insert values in single row 13 | insert into Employees(employee_id,first_name, last_name, department_id, hire_date) 14 | values 15 | (1,'Ashish','Jangra',1,'2021-07-16'); 16 | select * from Employees; 17 | 18 | -- Insert multiple rows 19 | insert into Employees(employee_id,first_name, last_name, department_id, hire_date) 20 | values 21 | (2,'Manish','Kumar',2,'2021-10-16'), 22 | (3,'Sakshi','Awasthi',2,'2021-10-16'), 23 | (4,'Avneet','Kaur',3,'2021-10-16'); 24 | select * from Employees; 25 | 26 | -- Add partial data 27 | insert into Employees(first_name, last_name) 28 | values 29 | ('Ashish','Jangra'); 30 | select * from Employees; 31 | 32 | -- Inserting data in different order 33 | insert into Employees(department_id,hire_date, last_name, first_name) 34 | values 35 | (1,'2022-10-10','Sakari','Prakash'); 36 | select * from Employees; 37 | 38 | -- Adding data without order 39 | insert into Employees() 40 | values 41 | (11,'Ashish','Jangra',5,'2024-10-10'); 42 | select * from Employees; 43 | 44 | -- Adding current date & default values 45 | DROP Table if EXISTS ExampleTable; 46 | CREATE TABLE if not exists ExampleTable ( 47 | employee_id SERIAL PRIMARY KEY, 48 | name VARCHAR(50) NOT NULL, 49 | hire_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, 50 | status VARCHAR(20) DEFAULT 'Active'); 51 | Select * from ExampleTable; 52 | 53 | insert into ExampleTable(name) values ('Ashish'); 54 | Select * from ExampleTable; 55 | 56 | insert into ExampleTable(name) values ('Ashish'),('Manish'),('Parag'); 57 | Select * from ExampleTable; 58 | 59 | insert into ExampleTable(name,status) values ('Ashish','Inactive'),('Manish','Inactive'),('Parag','Inactive'); 60 | Select * from ExampleTable; -------------------------------------------------------------------------------- /09. Update data from a table/01. update data from a table.sql: -------------------------------------------------------------------------------- 1 | use employees; 2 | DROP TABLE IF EXISTS People; 3 | 4 | CREATE TABLE People ( 5 | person_id SERIAL PRIMARY KEY, 6 | first_name VARCHAR(50), 7 | last_name VARCHAR(50), 8 | age INT, 9 | city VARCHAR(50)); 10 | 11 | -- Insert sample data into the table 12 | INSERT INTO People (first_name, last_name, age, city) 13 | VALUES 14 | ('John', 'Doe', 30, 'New York'), 15 | ('Jane', 'Smith', 25, 'Los Angeles'), 16 | ('Michael', 'Johnson', 40, 'Chicago'), 17 | ('Emily', 'Brown', 35, 'Houston'), 18 | ('David', 'Jones', 28, 'San Francisco'), 19 | ('Sarah', 'Davis', 32, 'Seattle'), 20 | ('Robert', 'Wilson', 45, 'Boston'), 21 | ('Jennifer', 'Martinez', 27, 'Miami'), 22 | ('William', 'Taylor', 38, 'Atlanta'), 23 | ('Jessica', 'Anderson', 33, 'Dallas'), 24 | ('Daniel', 'Thomas', 29, 'Philadelphia'), 25 | ('Maria', 'Jackson', 42, 'Phoenix'), 26 | ('James', 'White', 31, 'Denver'), 27 | ('Elizabeth', 'Harris', 36, 'Austin'), 28 | ('Christopher', 'Clark', 39, 'San Diego'), 29 | ('Amanda', 'Lewis', 26, 'Portland'), 30 | ('Matthew', 'Walker', 34, 'Detroit'), 31 | ('Ashley', 'Allen', 37, 'Las Vegas'), 32 | ('Joseph', 'Young', 41, 'Nashville'), 33 | ('Stephanie', 'Scott', 24, 'Orlando'); 34 | select * from People; 35 | 36 | -- Update the data based on age 37 | update People 38 | set age = 40 39 | where person_id = 1; 40 | select * from People; 41 | 42 | -- Update the city as per last name 43 | update People 44 | set city = 'Austin' 45 | where last_name = 'Smith'; 46 | select * from People; 47 | 48 | -- Update age of all people under age 30 to 30 49 | update People 50 | set age = 30 51 | where age < 30; 52 | select * from People; 53 | 54 | -- Update age of all the people in a specific city 55 | update People 56 | set age = age + 1 57 | where city = 'New York'; 58 | select * from People; 59 | 60 | -- Update the first name of people whose age is greater than 35 61 | update People 62 | set first_name = 'Senior' 63 | where age > 35; 64 | select * from People; 65 | 66 | -- Update multiples values in one go 67 | update People 68 | set first_name = 'Ashish', last_name = 'Jangra', age = 25, city = 'India' 69 | where person_id = 1; 70 | select * from People; -------------------------------------------------------------------------------- /10. Delete data from a table/01. delete data from a table.sql: -------------------------------------------------------------------------------- 1 | -- Drop the table if it already exists 2 | DROP TABLE IF EXISTS People; 3 | 4 | -- Create the table 5 | CREATE TABLE People ( 6 | person_id SERIAL PRIMARY KEY, 7 | first_name VARCHAR(50), 8 | last_name VARCHAR(50), 9 | age INT, 10 | city VARCHAR(50), 11 | email VARCHAR(100) 12 | ); 13 | 14 | -- Insert sample data into the table 15 | INSERT INTO People (first_name, last_name, age, city, email) 16 | VALUES 17 | ('John', 'Doe', 30, 'New York', 'john.doe@example.com'), 18 | ('Jane', 'Smith', 25, 'Los Angeles', 'jane.smith@example.com'), 19 | ('Michael', 'Johnson', 40, 'Chicago', 'michael.johnson@example.com'), 20 | ('Emily', 'Brown', 35, 'Houston', 'emily.brown@example.com'), 21 | ('David', 'Jones', 28, 'San Francisco', 'david.jones@example.com'), 22 | ('Sarah', 'Davis', 32, 'Seattle', 'sarah.davis@example.com'), 23 | ('Robert', 'Wilson', 45, 'Boston', 'robert.wilson@example.com'), 24 | ('Jennifer', 'Martinez', 27, 'Miami', 'jennifer.martinez@example.com'), 25 | ('William', 'Taylor', 38, 'Atlanta', 'william.taylor@example.com'), 26 | ('Jessica', 'Anderson', 33, 'Dallas', 'jessica.anderson@example.com'), 27 | ('Daniel', 'Thomas', 29, 'Philadelphia', 'daniel.thomas@example.com'), 28 | ('Maria', 'Jackson', 42, 'Phoenix', 'maria.jackson@example.com'), 29 | ('James', 'White', 31, 'Denver', 'james.white@example.com'), 30 | ('Elizabeth', 'Harris', 36, 'Austin', 'elizabeth.harris@example.com'), 31 | ('Christopher', 'Clark', 39, 'San Diego', 'christopher.clark@example.com'), 32 | ('Amanda', 'Lewis', 26, 'Portland', 'amanda.lewis@example.com'), 33 | ('Matthew', 'Walker', 34, 'Detroit', 'matthew.walker@example.com'), 34 | ('Ashley', 'Allen', 37, 'Las Vegas', 'ashley.allen@example.com'), 35 | ('Joseph', 'Young', 41, 'Nashville', 'joseph.young@example.com'), 36 | ('Stephanie', 'Scott', 24, 'Orlando', 'stephanie.scott@example.com'); 37 | select * from People; 38 | 39 | -- Delete the person based on person_id 40 | delete from People 41 | where person_id = 1; 42 | select * from People; 43 | 44 | -- Delete all the people under age 30 45 | delete from People 46 | where age < 30; 47 | select * from People; 48 | 49 | -- Delete all people in a specific city 50 | delete from People 51 | where city = 'Las Vegas'; 52 | select * from People; 53 | 54 | -- Delete the data whose last name starts with W 55 | delete from People 56 | where last_name Like 'W%'; 57 | select * from People; 58 | 59 | -- Delete all the rows of a table 60 | delete from People; 61 | select * from People; 62 | -------------------------------------------------------------------------------- /11. SQL in Action/assignment.sql: -------------------------------------------------------------------------------- 1 | -- create database gfg; 2 | use gfg; 3 | -- showing the Dataset 4 | select * from products; 5 | 6 | -- showing specific columns 7 | select product_name, brand_name from products; 8 | 9 | -- showing specific column in specific order 10 | select brand_name, product_name from products; 11 | 12 | -- create new column with mathemtical functions | Making Discounted Amount 13 | select product_name, brand_name, marked_price, discounted_price, marked_price - discounted_price AS discounted_amount from products ; 14 | 15 | -- create new column with mathemtical functions | Making Rating Count * Rating 16 | select product_name, brand_name, rating, rating_count, rating*rating_count AS rating_filter from products ; 17 | 18 | -- create new column with mathemtical functions | Making Discount Percentage 19 | select product_name, brand_name, marked_price, discounted_price, ((marked_price - discounted_price)/marked_price)*100 AS discounted_percent from products ; 20 | 21 | -- Finding UNIQUE values 22 | select distinct(brand_name) as unique_brands from products; 23 | 24 | -- Adding Where Clause 25 | select product_name, brand_name, marked_price, discounted_price from products where brand_tag = 'Adidas'; 26 | 27 | -- Adding Distinct with Where | Unique Products served by Adidas 28 | select distinct(product_tag), brand_name from products where brand_tag = 'Adidas'; 29 | 30 | -- Adding Distinct with Where | Unique Products served by Adidas 31 | select count(distinct(product_tag)) from products where brand_tag = 'Adidas'; 32 | 33 | -- Products with Multiple Where Clause with AND 34 | select product_name, brand_name, marked_price, discounted_price from products where brand_tag = 'Adidas' and discounted_price > 5000; 35 | select product_name, brand_name, marked_price, discounted_price from products where discounted_price > 5000 and discounted_price < 8000; 36 | 37 | -- Products with BETWEEN 38 | select product_name, brand_name, marked_price, discounted_price from products where discounted_price between 5000 and 8000; 39 | 40 | -- Adding more filters 41 | select product_name, brand_tag, marked_price, discounted_price from products 42 | where (discounted_price between 5000 and 8000) and brand_tag = 'Adidas' and rating > 4; 43 | 44 | -- Lets go one steps ahead 45 | select product_name, brand_tag, marked_price, discounted_price from products 46 | where (discounted_price between 3000 and 8000) and 47 | brand_tag = 'Adidas' 48 | and rating > 4 49 | and rating_count > 10; 50 | 51 | -- One More 52 | select count(distinct (product_tag)) from products where brand_tag = 'adidas'; 53 | 54 | -- Using OR 55 | select product_name, product_tag,brand_tag, discounted_price from products 56 | where (brand_tag = 'Adidas' or brand_tag = 'Puma') and discounted_price between 3000 and 5000; 57 | 58 | -- Using NOT 59 | select product_name, product_tag,brand_tag, discounted_price from products 60 | where not(brand_tag = 'Adidas' or brand_tag = 'Puma') and discounted_price between 3000 and 5000; 61 | 62 | -- Using IN 63 | select product_name, product_tag,brand_tag, discounted_price from products 64 | where brand_tag in ('Adidas', 'Puma') and discounted_price between 3000 and 5000; 65 | 66 | -- Using NOT IN 67 | select product_name, product_tag,brand_tag, discounted_price from products 68 | where brand_tag not in ('Adidas', 'Puma') and discounted_price between 3000 and 5000; 69 | -------------------------------------------------------------------------------- /11. SQL in Action/data.csv.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AshishJangra27/SQL-for-Data-Analysis/a40aa1b7015cead9cae2fbc5bd1874dcca68a2d3/11. SQL in Action/data.csv.zip -------------------------------------------------------------------------------- /12. Pattern Matching/01. pattern matching.sql: -------------------------------------------------------------------------------- 1 | use gfg; 2 | select * from products; 3 | 4 | -- Find the products where the product name ends with s 5 | select * from products where product_name like '%s'; 6 | 7 | -- Find the products where the brand name contains 'ad' 8 | select * from products where brand_name like '%ad%'; 9 | 10 | -- Find the products where name stands with P and ends with S 11 | select * from products where brand_name like 'p%s'; 12 | 13 | -- Find the products where the product name starting with s 14 | select * from products where product_name like 'S%'; 15 | 16 | -- Find the products where the product name contains with 'sho' 17 | select * from products where product_name like '%SHO%'; 18 | 19 | -- Find the products where the brans name is exactly of 6 cahracters long 20 | select * from products where brand_name like '______'; 21 | 22 | -- Find the products where the second character of the brans name is s 23 | select * from products where brand_name like '_s%'; 24 | -------------------------------------------------------------------------------- /12. Pattern Matching/data.csv.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AshishJangra27/SQL-for-Data-Analysis/a40aa1b7015cead9cae2fbc5bd1874dcca68a2d3/12. Pattern Matching/data.csv.zip -------------------------------------------------------------------------------- /13. Aggregate Functions/01. aggregate functions.sql: -------------------------------------------------------------------------------- 1 | use gfg; 2 | SELECT * FROM products; 3 | -- Finding the number of products in the dataset 4 | 5 | SELECT count(*) as total_products FROM products ; 6 | SELECT count (product_name) as total_products FROM products; 7 | 8 | -- Finding the average discounted price of products in the dataset 9 | SELECT avg(discounted_price) as average_price FROM products ; 10 | SELECT avg(marked_price) as average_price FROM products; 11 | 12 | -- Finding the most expensive product on myntra 13 | SELECT max(discounted_price) as average_price FROM products; 14 | SELECT max(marked_price) as average_price FROM products; 15 | 16 | -- Finding the least expensive product on myntra 17 | SELECT min (discounted_price) as average_price FROM products ; 18 | SELECT min (marked_price) as average_price FROM products ; 19 | 20 | -- Find the total rating count of all products 21 | SELECT sum (rating_count) as total_rating FROM products ; 22 | 23 | -- Find the total unique brands 24 | SELECT DISTINCT (brand_name) as brands FROM products ; 25 | 26 | -- Find the number of unique brands 27 | SELECT count(DISTINCT(brand_name)) as total_brands FROM products ; 28 | -------------------------------------------------------------------------------- /14. Grouping and Filtering Data/01. groupby.sql: -------------------------------------------------------------------------------- 1 | use gfg; 2 | select * from products; 3 | 4 | -- 1. Finding the names of unique brands 5 | select distinct brand_name from products; 6 | 7 | -- 2. Finding the number of unique brands 8 | select count(distinct brand_name) from products; 9 | 10 | -- 3. Finding the number of products in each brands 11 | select brand_tag, count(product_tag) from products group by brand_tag; 12 | 13 | -- 4. Finding the top 5 brand who has the most number of products | different product in their inventory 14 | select brand_tag, count(product_tag) as 'products' 15 | from products 16 | group by brand_tag 17 | order by products desc limit 5; 18 | 19 | -- 5. Finding the top 5 brand who sold the most number of products 20 | select brand_tag, sum(rating_count) as 'products_sold' from products 21 | group by brand_tag 22 | order by products_sold desc limit 5; 23 | 24 | -- 6. Finding the top 5 most expensive brands based on their discounted price 25 | select brand_tag, round(avg(discounted_price)) as 'average_price' from products 26 | group by brand_tag 27 | order by average_price desc limit 5; 28 | 29 | -- 7. Finding the top 5 least expensive brands based on their discounted price 30 | select brand_tag, round(avg(discounted_price)) as 'average_price' from products 31 | group by brand_tag 32 | order by average_price asc limit 5; 33 | 34 | -- 8. Finding the top 10 best-selling product categories 35 | select product_tag, sum(rating_count) as 'product_sold' from products 36 | group by product_tag 37 | order by product_sold desc limit 5; 38 | 39 | -- 9. Finding the top 10 brands who gives maximum discount 40 | select brand_tag, avg(discount_percent) as 'avg_discount' from products 41 | group by brand_tag 42 | order by avg_discount desc limit 5; 43 | 44 | -- 10. Finding the top 5 most expensive product categories 45 | select product_tag, avg(discounted_price) as 'avg_price' from products 46 | group by product_tag 47 | order by avg_price desc limit 5; 48 | 49 | -- 11. Brand Report Card 50 | select brand_tag, 51 | sum(rating_count) as 'people_rated', 52 | min(marked_price) as 'min_mar_price', 53 | avg(marked_price) as 'avg_mar_price', 54 | max(marked_price) as 'max_mar_price' 55 | from products group by brand_tag; 56 | 57 | -- 12. Which product_category of any brand is sold the most? 58 | select brand_tag, product_tag, sum(rating_count) as 'people_rated' from products 59 | group by brand_tag, product_tag 60 | order by people_rated desc limit 10; 61 | 62 | -- 13. List top 5 brands which has sold most number of tshirts 63 | select brand_tag, product_tag, sum(rating_count) as 'orders', avg(discounted_price) as 'avg_price' 64 | from products 65 | where product_tag = 'tshirts' 66 | group by brand_tag 67 | order by orders desc limit 5; 68 | 69 | -- 14. List top 5 brands which has sold most number of shirts 70 | select brand_tag, product_tag, sum(rating_count) as 'orders', avg(discounted_price) as 'avg_price' 71 | from products 72 | where product_tag = 'shirts' 73 | group by brand_tag 74 | order by orders desc limit 5; 75 | 76 | -- 15. List top 5 brands which has sold most number of jeans 77 | select brand_tag, product_tag, sum(rating_count) as 'orders', avg(discounted_price) as 'avg_price' 78 | from products 79 | where product_tag = 'jeans' 80 | group by brand_tag 81 | order by orders desc limit 5; 82 | 83 | -- 16. List top 5 brands which has sold most number of dresses 84 | select brand_tag, product_tag, sum(rating_count) as 'orders', avg(discounted_price) as 'avg_price' 85 | from products 86 | where product_tag = 'dresses' 87 | group by brand_tag 88 | order by orders desc limit 5; 89 | 90 | -- 17. Most popular product name listed in Myntra 91 | select product_name ,count(product_name) as 'name_count' from products 92 | group by product_name 93 | order by name_count desc limit 10; 94 | 95 | -- 18. Number of products sold for every rating (0 - 5) 96 | select rating ,count(rating) as 'freq' from products 97 | group by rating 98 | order by rating asc; 99 | 100 | -- 19. Number of products sold for every rating by nike 101 | select rating ,count(rating) as 'freq' from products 102 | where brand_tag = 'nike' 103 | group by rating 104 | order by rating asc; 105 | 106 | -- 20. Number of products sold for every rating in tshirt category 107 | select rating ,count(rating_count) as 'freq' from products 108 | where product_tag = 'tshirts' 109 | group by rating 110 | order by rating asc; 111 | 112 | -- 21. Relation between price of the thisrt and its rating wrt to people rated 113 | select product_tag, rating, round(avg(rating_count)) as 'product_count', round(avg(discounted_price)) 114 | from products 115 | where product_tag = 'tshirts' 116 | group by rating 117 | order by rating asc; 118 | 119 | -- 22. Find the average rating for each product category (product tag) along with the number of products and total rating count 120 | select product_tag, avg(rating) as 'avg_rating', count(*) as 'total_products', sum(rating_count) as 'total_rating_count' 121 | from products 122 | group by product_tag 123 | order by avg_rating asc; 124 | 125 | -- 23. Find the brand with the highest average rating among products with a discounted price greater than 5000 126 | select brand_tag, avg(rating) as 'avg_rating', sum(rating_count) 127 | from products 128 | where discounted_price > 5000 129 | group by brand_tag 130 | order by avg_rating desc; -------------------------------------------------------------------------------- /14. Grouping and Filtering Data/data.csv.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AshishJangra27/SQL-for-Data-Analysis/a40aa1b7015cead9cae2fbc5bd1874dcca68a2d3/14. Grouping and Filtering Data/data.csv.zip -------------------------------------------------------------------------------- /15. Working with Subqueries/1. Subqueries.sql: -------------------------------------------------------------------------------- 1 | 2 | -- Basic Query: Find the average rating of products 3 | select avg(rating) as 'avg_rating' from gfg.products; 4 | 5 | -- Subquery in WHERE Clause: Find the brand names with a rating higher than the average rating 6 | select brand_name from gfg.products where rating > (select avg(rating) as 'avg_rating' from gfg.products); 7 | 8 | -- Subquery in SELECT Clause: Retrieve the product name along with the average rating of products 9 | select product_name , rating, 10 | (select avg(rating) from gfg.products) as 'avg_rating' 11 | from gfg.products; 12 | 13 | -- Subquery with Multiple Results: Find the total rating count of products for each brand compared to the overall average rating count 14 | select brand_name, 15 | (select sum(rating_count) from gfg.products where brand_name = p.brand_name) as total_rating_count 16 | from (select distinct brand_name from gfg.products) as p; 17 | -------------------------------------------------------------------------------- /16. Joins in Action/01. creating database.sql: -------------------------------------------------------------------------------- 1 | DROP DATABASE IF EXISTS users; 2 | CREATE DATABASE users; 3 | 4 | CREATE TABLE IF NOT EXISTS users.users_2021 (UserID INT PRIMARY KEY, Name VARCHAR(50)); 5 | CREATE TABLE IF NOT EXISTS users.users_2022 (UserID INT PRIMARY KEY, Name VARCHAR(50)); 6 | CREATE TABLE IF NOT EXISTS users.users_2023 (UserID INT PRIMARY KEY, Name VARCHAR(50)); 7 | 8 | INSERT INTO users.users_2021 (UserID, Name) VALUES (1, 'Ashish'), (2, 'Laura'), (7, 'Prakash'); 9 | INSERT INTO users.users_2022 (UserID, Name) VALUES (1, 'Ashish'), (2, 'Laura'), (3, 'Charlie'), (4, 'Grace'); 10 | INSERT INTO users.users_2023 (UserID, Name) VALUES (1, 'Ashish'), (2, 'Laura'), (3, 'Charlie'), (4, 'Grace'), (5, 'Henry'); 11 | 12 | select * from users.users_2021; 13 | select * from users.users_2022; 14 | select * from users.users_2023; -------------------------------------------------------------------------------- /16. Joins in Action/02. joins.sql: -------------------------------------------------------------------------------- 1 | select * from users.users_2021; 2 | select * from users.users_2022; 3 | select * from users.users_2023; 4 | 5 | -- User 2021 & 2022 6 | select * from users.users_2021 as t_2021 inner join users.users_2022 as t_2022 on t_2021.UserID = t_2022.UserID; 7 | select * from users.users_2022 as t_2022 inner join users.users_2021 as t_2021 on t_2021.UserID = t_2022.UserID; 8 | 9 | -- User 2022 & 2023 10 | select * from users.users_2022 as t_2022 inner join users.users_2023 as t_2023 on t_2022.UserID = t_2023.UserID; 11 | select * from users.users_2023 as t_2023 inner join users.users_2022 as t_2022 on t_2022.UserID = t_2023.UserID; 12 | 13 | -- User 2021 & 2023 14 | select * from users.users_2021 as t_2021 inner join users.users_2023 as t_2023 on t_2021.UserID = t_2023.UserID; 15 | select * from users.users_2023 as t_2023 inner join users.users_2021 as t_2021 on t_2021.UserID = t_2023.UserID; 16 | 17 | -- User 2021,2022 & 2023 18 | select * from users.users_2021 as t_2021 inner join users.users_2022 as t_2022 on t_2021.UserID = t_2022.UserID 19 | inner join users.users_2023 as t_2023 on t_2023.UserID = t_2021.UserID; 20 | 21 | -- Left Join | 2021 & 2022 22 | select * from users.users_2021 as t_2021 left join users.users_2022 as t_2022 on t_2021.UserID = t_2022.UserID; 23 | select * from users.users_2023 as t_2023 left join users.users_2021 as t_2021 on t_2021.UserID = t_2023.UserID; 24 | 25 | -- Right Join 26 | select * from users.users_2021 as t_2021 right join users.users_2022 as t_2022 on t_2021.UserID = t_2022.UserID; 27 | select * from users.users_2023 as t_2023 right join users.users_2021 as t_2021 on t_2021.UserID = t_2023.UserID; 28 | 29 | -- Cross Inner Join 30 | select * from users.users_2021 as t_2021 left join users.users_2022 as t_2022 on t_2021.UserID = t_2022.UserID; 31 | select * from users.users_2021 as t_2021 right join users.users_2022 as t_2022 on t_2021.UserID = t_2022.UserID; 32 | 33 | select * from users.users_2021 as t_2021 left join users.users_2022 as t_2022 on t_2021.UserID = t_2022.UserID 34 | union 35 | select * from users.users_2021 as t_2021 right join users.users_2022 as t_2022 on t_2021.UserID = t_2022.UserID; -------------------------------------------------------------------------------- /16. Joins in Action/Joins on Cricket Players/01. data exploration.sql: -------------------------------------------------------------------------------- 1 | -- select * from cricket.teams; 2 | -- select * from cricket.player_basic; 3 | -- select * from cricket.players_and_team; 4 | -- select * from cricket.players_full; 5 | -- select * from cricket.final; 6 | 7 | 8 | # Cross Join 9 | select count(*) from cricket.players_and_team; 10 | select count(*) from cricket.teams; 11 | select count(*) from cricket.players_and_team cross join cricket.teams; 12 | select * from cricket.players_and_team cross join cricket.teams; 13 | 14 | # Left Join 15 | select * from cricket.players_and_team; 16 | select * from cricket.teams; 17 | select * from cricket.players_and_team t1 left join cricket.teams t2 on t1.team = t2.name; 18 | 19 | # Right Join 20 | select * from cricket.players_and_team; 21 | select * from cricket.teams; 22 | select * from cricket.players_and_team t1 right join cricket.teams t2 on t1.team = t2.name; -------------------------------------------------------------------------------- /16. Joins in Action/Joins on Cricket Players/players_and_team.csv: -------------------------------------------------------------------------------- 1 | player,team,link 2 | Virat Kohli,India,https://www.cricbuzz.com/profiles/1413/virat-kohli 3 | Rohit Sharma,India,https://www.cricbuzz.com/profiles/576/rohit-sharma 4 | Shikhar Dhawan,India,https://www.cricbuzz.com/profiles/1446/shikhar-dhawan 5 | Shubman Gill,India,https://www.cricbuzz.com/profiles/11808/shubman-gill 6 | Shreyas Iyer,India,https://www.cricbuzz.com/profiles/9428/shreyas-iyer 7 | Manish Pandey,India,https://www.cricbuzz.com/profiles/1836/manish-pandey 8 | Mayank Agarwal,India,https://www.cricbuzz.com/profiles/2195/mayank-agarwal 9 | Prithvi Shaw,India,https://www.cricbuzz.com/profiles/12094/prithvi-shaw 10 | Cheteshwar Pujara,India,https://www.cricbuzz.com/profiles/1448/cheteshwar-pujara 11 | Ajinkya Rahane,India,https://www.cricbuzz.com/profiles/1447/ajinkya-rahane 12 | Hardik Pandya,India,https://www.cricbuzz.com/profiles/9647/hardik-pandya 13 | Hanuma Vihari,India,https://www.cricbuzz.com/profiles/8424/hanuma-vihari 14 | Ravindra Jadeja,India,https://www.cricbuzz.com/profiles/587/ravindra-jadeja 15 | Ravichandran Ashwin,India,https://www.cricbuzz.com/profiles/1593/ravichandran-ashwin 16 | KL Rahul,India,https://www.cricbuzz.com/profiles/8733/kl-rahul 17 | Sanju Samson,India,https://www.cricbuzz.com/profiles/8271/sanju-samson 18 | Wriddhiman Saha,India,https://www.cricbuzz.com/profiles/1465/wriddhiman-saha 19 | Rishabh Pant,India,https://www.cricbuzz.com/profiles/10744/rishabh-pant 20 | Mohammed Shami,India,https://www.cricbuzz.com/profiles/7909/mohammed-shami 21 | Jasprit Bumrah,India,https://www.cricbuzz.com/profiles/9311/jasprit-bumrah 22 | Kuldeep Yadav,India,https://www.cricbuzz.com/profiles/8292/kuldeep-yadav 23 | Yuzvendra Chahal,India,https://www.cricbuzz.com/profiles/7910/yuzvendra-chahal 24 | Navdeep Saini,India,https://www.cricbuzz.com/profiles/9715/navdeep-saini 25 | Shardul Thakur,India,https://www.cricbuzz.com/profiles/8683/shardul-thakur 26 | Umesh Yadav,India,https://www.cricbuzz.com/profiles/1858/umesh-yadav 27 | Mohammed Siraj,India,https://www.cricbuzz.com/profiles/10808/mohammed-siraj 28 | William Porterfield,Ireland,https://www.cricbuzz.com/profiles/625/william-porterfield 29 | James McCollum,Ireland,https://www.cricbuzz.com/profiles/13942/james-mccollum 30 | Andrew Balbirnie,Ireland,https://www.cricbuzz.com/profiles/6710/andrew-balbirnie 31 | Tyrone Kane,Ireland,https://www.cricbuzz.com/profiles/10415/tyrone-kane 32 | Kevin O Brien,Ireland,https://www.cricbuzz.com/profiles/690/kevin-o-brien 33 | Paul Stirling,Ireland,https://www.cricbuzz.com/profiles/1114/paul-stirling 34 | Stuart Thompson,Ireland,https://www.cricbuzz.com/profiles/7985/stuart-thompson 35 | Gary Wilson,Ireland,https://www.cricbuzz.com/profiles/739/gary-wilson 36 | Lorcan Tucker,Ireland,https://www.cricbuzz.com/profiles/11131/lorcan-tucker 37 | Peter Chase,Ireland,https://www.cricbuzz.com/profiles/10099/peter-chase 38 | George Dockrell,Ireland,https://www.cricbuzz.com/profiles/6329/george-dockrell 39 | Tim Murtagh,Ireland,https://www.cricbuzz.com/profiles/6661/tim-murtagh 40 | Mark Adair,Ireland,https://www.cricbuzz.com/profiles/10449/mark-adair 41 | Andy McBrine,Ireland,https://www.cricbuzz.com/profiles/9667/andy-mcbrine 42 | Barry McCarthy,Ireland,https://www.cricbuzz.com/profiles/10451/barry-mccarthy 43 | Boyd Rankin,Ireland,https://www.cricbuzz.com/profiles/626/boyd-rankin 44 | Aaron Finch,Australia,https://www.cricbuzz.com/profiles/1643/aaron-finch 45 | Steven Smith,Australia,https://www.cricbuzz.com/profiles/2250/steven-smith 46 | David Warner,Australia,https://www.cricbuzz.com/profiles/1739/david-warner 47 | Joe Burns,Australia,https://www.cricbuzz.com/profiles/8121/joe-burns 48 | Will Pucovski,Australia,https://www.cricbuzz.com/profiles/12228/will-pucovski 49 | Glenn Maxwell,Australia,https://www.cricbuzz.com/profiles/7662/glenn-maxwell 50 | Marcus Stoinis,Australia,https://www.cricbuzz.com/profiles/8989/marcus-stoinis 51 | Moises Henriques,Australia,https://www.cricbuzz.com/profiles/1437/moises-henriques 52 | Marnus Labuschagne,Australia,https://www.cricbuzz.com/profiles/10182/marnus-labuschagne 53 | Travis Head,Australia,https://www.cricbuzz.com/profiles/8497/travis-head 54 | Michael Neser,Australia,https://www.cricbuzz.com/profiles/8130/michael-neser 55 | Ashton Agar,Australia,https://www.cricbuzz.com/profiles/8508/ashton-agar 56 | Cameron Green,Australia,https://www.cricbuzz.com/profiles/12225/cameron-green 57 | Daniel Sams,Australia,https://www.cricbuzz.com/profiles/13162/daniel-sams 58 | Sean Abbott,Australia,https://www.cricbuzz.com/profiles/8136/sean-abbott 59 | Alex Carey,Australia,https://www.cricbuzz.com/profiles/9200/alex-carey 60 | Matthew Wade,Australia,https://www.cricbuzz.com/profiles/1649/matthew-wade 61 | Tim Paine,Australia,https://www.cricbuzz.com/profiles/1994/tim-paine 62 | Pat Cummins,Australia,https://www.cricbuzz.com/profiles/8095/pat-cummins 63 | Nathan Lyon,Australia,https://www.cricbuzz.com/profiles/7850/nathan-lyon 64 | Mitchell Starc,Australia,https://www.cricbuzz.com/profiles/7710/mitchell-starc 65 | Adam Zampa,Australia,https://www.cricbuzz.com/profiles/8642/adam-zampa 66 | Josh Hazlewood,Australia,https://www.cricbuzz.com/profiles/6258/josh-hazlewood 67 | Andrew Tye,Australia,https://www.cricbuzz.com/profiles/9617/andrew-tye 68 | James Pattinson,Australia,https://www.cricbuzz.com/profiles/2283/james-pattinson 69 | Mitchell Swepson,Australia,https://www.cricbuzz.com/profiles/10626/mitchell-swepson 70 | Tamim Iqbal,Bangladesh,https://www.cricbuzz.com/profiles/612/tamim-iqbal 71 | Soumya Sarkar,Bangladesh,https://www.cricbuzz.com/profiles/8233/soumya-sarkar 72 | Sabbir Rahman,Bangladesh,https://www.cricbuzz.com/profiles/8231/sabbir-rahman 73 | Mahmudullah,Bangladesh,https://www.cricbuzz.com/profiles/1121/mahmudullah 74 | Shakib Al Hasan,Bangladesh,https://www.cricbuzz.com/profiles/544/shakib-al-hasan 75 | Mosaddek Hossain,Bangladesh,https://www.cricbuzz.com/profiles/8541/mosaddek-hossain 76 | Mohammad Saifuddin,Bangladesh,https://www.cricbuzz.com/profiles/11084/mohammad-saifuddin 77 | Mehidy Hasan Miraz,Bangladesh,https://www.cricbuzz.com/profiles/11081/mehidy-hasan-miraz 78 | Mushfiqur Rahim,Bangladesh,https://www.cricbuzz.com/profiles/394/mushfiqur-rahim 79 | Litton Das,Bangladesh,https://www.cricbuzz.com/profiles/8540/litton-das 80 | Mohammad Mithun,Bangladesh,https://www.cricbuzz.com/profiles/8221/mohammad-mithun 81 | Mashrafe Mortaza,Bangladesh,https://www.cricbuzz.com/profiles/323/mashrafe-mortaza 82 | Rubel Hossain,Bangladesh,https://www.cricbuzz.com/profiles/1736/rubel-hossain 83 | Mustafizur Rahman,Bangladesh,https://www.cricbuzz.com/profiles/9863/mustafizur-rahman 84 | Abu Jayed,Bangladesh,https://www.cricbuzz.com/profiles/8536/abu-jayed 85 | Darren Bravo,West Indies,https://www.cricbuzz.com/profiles/1610/darren-bravo 86 | Chris Gayle,West Indies,https://www.cricbuzz.com/profiles/247/chris-gayle 87 | Evin Lewis,West Indies,https://www.cricbuzz.com/profiles/8579/evin-lewis 88 | Shimron Hetmyer,West Indies,https://www.cricbuzz.com/profiles/9789/shimron-hetmyer 89 | Fabian Allen,West Indies,https://www.cricbuzz.com/profiles/9785/fabian-allen 90 | Jason Holder,West Indies,https://www.cricbuzz.com/profiles/8313/jason-holder 91 | Andre Russell,West Indies,https://www.cricbuzz.com/profiles/7736/andre-russell 92 | Carlos Brathwaite,West Indies,https://www.cricbuzz.com/profiles/8110/carlos-brathwaite 93 | Shai Hope,West Indies,https://www.cricbuzz.com/profiles/10384/shai-hope 94 | Nicholas Pooran,West Indies,https://www.cricbuzz.com/profiles/9406/nicholas-pooran 95 | Sheldon Cottrell,West Indies,https://www.cricbuzz.com/profiles/9395/sheldon-cottrell 96 | Shannon Gabriel,West Indies,https://www.cricbuzz.com/profiles/8092/shannon-gabriel 97 | Kemar Roach,West Indies,https://www.cricbuzz.com/profiles/1359/kemar-roach 98 | Ashley Nurse,West Indies,https://www.cricbuzz.com/profiles/7922/ashley-nurse 99 | Oshane Thomas,West Indies,https://www.cricbuzz.com/profiles/11442/oshane-thomas 100 | Craig Ervine,Zimbabwe,https://www.cricbuzz.com/profiles/6334/craig-ervine 101 | Hamilton Masakadza,Zimbabwe,https://www.cricbuzz.com/profiles/425/hamilton-masakadza 102 | Sikandar Raza,Zimbabwe,https://www.cricbuzz.com/profiles/9354/sikandar-raza 103 | Sean Williams,Zimbabwe,https://www.cricbuzz.com/profiles/441/sean-williams 104 | Solomon Mire,Zimbabwe,https://www.cricbuzz.com/profiles/9737/solomon-mire 105 | Brian Chari,Zimbabwe,https://www.cricbuzz.com/profiles/10168/brian-chari 106 | Timycen Maruma,Zimbabwe,https://www.cricbuzz.com/profiles/782/timycen-maruma 107 | Elton Chigumbura,Zimbabwe,https://www.cricbuzz.com/profiles/170/elton-chigumbura 108 | Regis Chakabva,Zimbabwe,https://www.cricbuzz.com/profiles/781/regis-chakabva 109 | Brendan Taylor,Zimbabwe,https://www.cricbuzz.com/profiles/180/brendan-taylor 110 | Peter Moor,Zimbabwe,https://www.cricbuzz.com/profiles/10195/peter-moor 111 | Tendai Chatara,Zimbabwe,https://www.cricbuzz.com/profiles/6483/tendai-chatara 112 | Donald Tiripano,Zimbabwe,https://www.cricbuzz.com/profiles/10066/donald-tiripano 113 | Chris Mpofu,Zimbabwe,https://www.cricbuzz.com/profiles/428/chris-mpofu 114 | Kyle Jarvis,Zimbabwe,https://www.cricbuzz.com/profiles/2289/kyle-jarvis 115 | Brandon Mavuta,Zimbabwe,https://www.cricbuzz.com/profiles/11224/brandon-mavuta 116 | Kila Pala,Papua New Guinea,https://www.cricbuzz.com/profiles/10113/kila-pala 117 | Charles Amini,Papua New Guinea,https://www.cricbuzz.com/profiles/10428/charles-amini 118 | Mahuru Dai,Papua New Guinea,https://www.cricbuzz.com/profiles/10191/mahuru-dai 119 | Vani Morea,Papua New Guinea,https://www.cricbuzz.com/profiles/10118/vani-morea 120 | Lega Siaka,Papua New Guinea,https://www.cricbuzz.com/profiles/8467/lega-siaka 121 | Assad Vala,Papua New Guinea,https://www.cricbuzz.com/profiles/10120/assad-vala 122 | Assad Vala,Papua New Guinea,https://www.cricbuzz.com/profiles/10120/assad-vala 123 | Chris Amini,Papua New Guinea,https://www.cricbuzz.com/profiles/10116/chris-amini 124 | Sese Bau,Papua New Guinea,https://www.cricbuzz.com/profiles/8470/sese-bau 125 | Willie Gavera,Papua New Guinea,https://www.cricbuzz.com/profiles/10111/willie-gavera 126 | Pipi Raho,Papua New Guinea,https://www.cricbuzz.com/profiles/10119/pipi-raho 127 | John Reva,Papua New Guinea,https://www.cricbuzz.com/profiles/10114/john-reva 128 | Norman Vanua,Papua New Guinea,https://www.cricbuzz.com/profiles/8480/norman-vanua 129 | Dogodo Bau,Papua New Guinea,https://www.cricbuzz.com/profiles/8469/dogodo-bau 130 | Geraint Jones,Papua New Guinea,https://www.cricbuzz.com/profiles/165/geraint-jones 131 | Tony Ura,Papua New Guinea,https://www.cricbuzz.com/profiles/10115/tony-ura 132 | Jack Vare,Papua New Guinea,https://www.cricbuzz.com/profiles/10121/jack-vare 133 | Hiri Hiri,Papua New Guinea,https://www.cricbuzz.com/profiles/9906/hiri-hiri 134 | Loa Nou,Papua New Guinea,https://www.cricbuzz.com/profiles/10425/loa-nou 135 | Nosaina Pokana,Papua New Guinea,https://www.cricbuzz.com/profiles/9910/nosaina-pokana 136 | Chad Soper,Papua New Guinea,https://www.cricbuzz.com/profiles/8477/chad-soper 137 | Jatinder Singh,Oman,https://www.cricbuzz.com/profiles/10453/jatinder-singh 138 | Arun Poulose,Oman,https://www.cricbuzz.com/profiles/10454/arun-poulose 139 | Vaibhav Wategaonkar,Oman,https://www.cricbuzz.com/profiles/10455/vaibhav-wategaonkar 140 | Zeeshan Siddiqui,Oman,https://www.cricbuzz.com/profiles/10456/zeeshan-siddiqui 141 | Zeeshan Maqsood,Oman,https://www.cricbuzz.com/profiles/10466/zeeshan-maqsood 142 | Amir Ali,Oman,https://www.cricbuzz.com/profiles/10452/amir-ali 143 | Khawar Ali,Oman,https://www.cricbuzz.com/profiles/10467/khawar-ali 144 | Mehran Khan,Oman,https://www.cricbuzz.com/profiles/10464/mehran-khan 145 | Twinkal Bhandari,Oman,https://www.cricbuzz.com/profiles/11833/twinkal-bhandari 146 | Ajay Lalcheta,Oman,https://www.cricbuzz.com/profiles/10458/ajay-lalcheta 147 | Sufyan Mehmood,Oman,https://www.cricbuzz.com/profiles/10461/sufyan-mehmood 148 | Rajeshkumar Ranpura,Oman,https://www.cricbuzz.com/profiles/10460/rajeshkumar-ranpura 149 | Munis Ansari,Oman,https://www.cricbuzz.com/profiles/10457/munis-ansari 150 | Mohammad Nadeem,Oman,https://www.cricbuzz.com/profiles/10459/mohammad-nadeem 151 | Swapnil Khadye,Oman,https://www.cricbuzz.com/profiles/10821/swapnil-khadye 152 | Trevin Bastiampillai,Canada,https://www.cricbuzz.com/profiles/815/trevin-bastiampillai 153 | Tyson Gordon,Canada,https://www.cricbuzz.com/profiles/7852/tyson-gordon 154 | Ruvindu Gunasekera,Canada,https://www.cricbuzz.com/profiles/1422/ruvindu-gunasekera 155 | Jimmy Hansra,Canada,https://www.cricbuzz.com/profiles/7676/jimmy-hansra 156 | Junaid Siddique,Canada,https://www.cricbuzz.com/profiles/784/junaid-siddique 157 | Nitish Kumar,Canada,https://www.cricbuzz.com/profiles/6474/nitish-kumar 158 | Hiral Patel,Canada,https://www.cricbuzz.com/profiles/3289/hiral-patel 159 | Rizwan Cheema,Canada,https://www.cricbuzz.com/profiles/1426/rizwan-cheema 160 | Durand Soraine,Canada,https://www.cricbuzz.com/profiles/823/durand-soraine 161 | Zubin Surkari,Canada,https://www.cricbuzz.com/profiles/1432/zubin-surkari 162 | Usman Limbada,Canada,https://www.cricbuzz.com/profiles/6475/usman-limbada 163 | Harvir Baidwan,Canada,https://www.cricbuzz.com/profiles/1419/harvir-baidwan 164 | Damodar Daesrat,Canada,https://www.cricbuzz.com/profiles/8377/damodar-daesrat 165 | Raza-ur-Rehman,Canada,https://www.cricbuzz.com/profiles/8256/raza-ur-rehman 166 | Ashish Bagai,Canada,https://www.cricbuzz.com/profiles/676/ashish-bagai 167 | R A Bhatti,Canada,https://www.cricbuzz.com/profiles/8033/r-a-bhatti 168 | Hamza Tariq,Canada,https://www.cricbuzz.com/profiles/7675/hamza-tariq 169 | Darren Ramsammy,Canada,https://www.cricbuzz.com/profiles/8379/darren-ramsammy 170 | Zeeshan Siddiqi,Canada,https://www.cricbuzz.com/profiles/8027/zeeshan-siddiqi 171 | Manny Aulakh,Canada,https://www.cricbuzz.com/profiles/8100/manny-aulakh 172 | Abzal Dean,Canada,https://www.cricbuzz.com/profiles/1478/abzal-dean 173 | Parth Desai,Canada,https://www.cricbuzz.com/profiles/7674/parth-desai 174 | Nikhil Dutta,Canada,https://www.cricbuzz.com/profiles/9230/nikhil-dutta 175 | Jeremy Gordon,Canada,https://www.cricbuzz.com/profiles/8378/jeremy-gordon 176 | Kenneth Kamyuka,Canada,https://www.cricbuzz.com/profiles/9549/kenneth-kamyuka 177 | Khurram Chohan,Canada,https://www.cricbuzz.com/profiles/3285/khurram-chohan 178 | Henry Osinde,Canada,https://www.cricbuzz.com/profiles/686/henry-osinde 179 | Rayyan Pathan,Canada,https://www.cricbuzz.com/profiles/9229/rayyan-pathan 180 | John Davison,Canada,https://www.cricbuzz.com/profiles/675/john-davison 181 | Zahid Hussain,Canada,https://www.cricbuzz.com/profiles/8026/zahid-hussain 182 | Babar Hayat,Hong Kong,https://www.cricbuzz.com/profiles/9979/babar-hayat 183 | Waqas Khan,Hong Kong,https://www.cricbuzz.com/profiles/10208/waqas-khan 184 | Kinchit Shah,Hong Kong,https://www.cricbuzz.com/profiles/9981/kinchit-shah 185 | Aizaz Khan,Hong Kong,https://www.cricbuzz.com/profiles/9975/aizaz-khan 186 | Nizakat Khan,Hong Kong,https://www.cricbuzz.com/profiles/9980/nizakat-khan 187 | Tanwir Afzal,Hong Kong,https://www.cricbuzz.com/profiles/9982/tanwir-afzal 188 | Anshuman Rath,Hong Kong,https://www.cricbuzz.com/profiles/10035/anshuman-rath 189 | Jamie Atkinson,Hong Kong,https://www.cricbuzz.com/profiles/1378/jamie-atkinson 190 | Christopher Carter,Hong Kong,https://www.cricbuzz.com/profiles/10793/christopher-carter 191 | Shahid Wasif,Hong Kong,https://www.cricbuzz.com/profiles/11479/shahid-wasif 192 | Ehsan Khan,Hong Kong,https://www.cricbuzz.com/profiles/11485/ehsan-khan 193 | Nadeem Ahmed,Hong Kong,https://www.cricbuzz.com/profiles/1385/nadeem-ahmed 194 | Tanveer Ahmed,Hong Kong,https://www.cricbuzz.com/profiles/11342/tanveer-ahmed 195 | Aijaz Ali,United States,https://www.cricbuzz.com/profiles/3374/aijaz-ali 196 | Rohan Alexander,United States,https://www.cricbuzz.com/profiles/3375/rohan-alexander 197 | Donovan Blake,United States,https://www.cricbuzz.com/profiles/3385/donovan-blake 198 | Jignesh Desai,United States,https://www.cricbuzz.com/profiles/3376/jignesh-desai 199 | Howard Johnson,United States,https://www.cricbuzz.com/profiles/3377/howard-johnson 200 | Mark Johnson,United States,https://www.cricbuzz.com/profiles/3378/mark-johnson 201 | Clayton Lambert,United States,https://www.cricbuzz.com/profiles/3379/clayton-lambert 202 | Steve Massiah,United States,https://www.cricbuzz.com/profiles/3380/steve-massiah 203 | Nasir Javed,United States,https://www.cricbuzz.com/profiles/3386/nasir-javed 204 | Rashid,United States,https://www.cricbuzz.com/profiles/3381/rashid 205 | Tony Reid,United States,https://www.cricbuzz.com/profiles/3382/tony-reid 206 | Leon Romero,United States,https://www.cricbuzz.com/profiles/3383/leon-romero 207 | Richard Staple,United States,https://www.cricbuzz.com/profiles/3384/richard-staple 208 | Tom Cooper,Netherlands,https://www.cricbuzz.com/profiles/6708/tom-cooper 209 | Tom de Grooth,Netherlands,https://www.cricbuzz.com/profiles/1409/tom-de-grooth 210 | Wilfred Diepeveen,Netherlands,https://www.cricbuzz.com/profiles/7609/wilfred-diepeveen 211 | Tim Gruijters,Netherlands,https://www.cricbuzz.com/profiles/6477/tim-gruijters 212 | Alexei Kervezee,Netherlands,https://www.cricbuzz.com/profiles/634/alexei-kervezee 213 | Neil Kruger,Netherlands,https://www.cricbuzz.com/profiles/7960/neil-kruger 214 | Stephan Myburgh,Netherlands,https://www.cricbuzz.com/profiles/8073/stephan-myburgh 215 | Sikander Zulfiqar,Netherlands,https://www.cricbuzz.com/profiles/11330/sikander-zulfiqar 216 | Michael Swart,Netherlands,https://www.cricbuzz.com/profiles/7962/michael-swart 217 | Peter Borren,Netherlands,https://www.cricbuzz.com/profiles/631/peter-borren 218 | Max ODowd,Netherlands,https://www.cricbuzz.com/profiles/10424/max-odowd 219 | Vinoo Tewarie,Netherlands,https://www.cricbuzz.com/profiles/7969/vinoo-tewarie 220 | Daan van Bunge,Netherlands,https://www.cricbuzz.com/profiles/641/daan-van-bunge 221 | Ben Cooper,Netherlands,https://www.cricbuzz.com/profiles/9532/ben-cooper 222 | Tom Heggelman,Netherlands,https://www.cricbuzz.com/profiles/7610/tom-heggelman 223 | Michael Rippon,Netherlands,https://www.cricbuzz.com/profiles/8098/michael-rippon 224 | Pieter Seelaar,Netherlands,https://www.cricbuzz.com/profiles/1412/pieter-seelaar 225 | Timm van der Gugten,Netherlands,https://www.cricbuzz.com/profiles/7801/timm-van-der-gugten 226 | Roelof van der Merwe,Netherlands,https://www.cricbuzz.com/profiles/1669/roelof-van-der-merwe 227 | Rahil Ahmed,Netherlands,https://www.cricbuzz.com/profiles/10423/rahil-ahmed 228 | Wesley Barresi,Netherlands,https://www.cricbuzz.com/profiles/6707/wesley-barresi 229 | Tobias Visee,Netherlands,https://www.cricbuzz.com/profiles/10429/tobias-visee 230 | Ahsan Malik,Netherlands,https://www.cricbuzz.com/profiles/7964/ahsan-malik 231 | Mudassar Bukhari,Netherlands,https://www.cricbuzz.com/profiles/1408/mudassar-bukhari 232 | Vivian Kingma,Netherlands,https://www.cricbuzz.com/profiles/9391/vivian-kingma 233 | Logan van Beek,Netherlands,https://www.cricbuzz.com/profiles/9983/logan-van-beek 234 | Paul van Meekeren,Netherlands,https://www.cricbuzz.com/profiles/9357/paul-van-meekeren 235 | Thijs van Schelven,Netherlands,https://www.cricbuzz.com/profiles/10125/thijs-van-schelven 236 | Edgar Schiferli,Netherlands,https://www.cricbuzz.com/profiles/692/edgar-schiferli 237 | Eric Gouka,Netherlands,https://www.cricbuzz.com/profiles/3298/eric-gouka 238 | Asghar Afghan,Afghanistan,https://www.cricbuzz.com/profiles/3004/asghar-afghan 239 | Hashmatullah Shahidi,Afghanistan,https://www.cricbuzz.com/profiles/8277/hashmatullah-shahidi 240 | Hazratullah Zazai,Afghanistan,https://www.cricbuzz.com/profiles/12068/hazratullah-zazai 241 | Najibullah Zadran,Afghanistan,https://www.cricbuzz.com/profiles/8375/najibullah-zadran 242 | Noor Ali Zadran,Afghanistan,https://www.cricbuzz.com/profiles/3010/noor-ali-zadran 243 | Rahmat Shah,Afghanistan,https://www.cricbuzz.com/profiles/8571/rahmat-shah 244 | Samiullah Shinwari,Afghanistan,https://www.cricbuzz.com/profiles/3011/samiullah-shinwari 245 | Gulbadin Naib,Afghanistan,https://www.cricbuzz.com/profiles/8030/gulbadin-naib 246 | Mohammad Nabi,Afghanistan,https://www.cricbuzz.com/profiles/3007/mohammad-nabi 247 | Rashid Khan,Afghanistan,https://www.cricbuzz.com/profiles/10738/rashid-khan 248 | Mohammad Shahzad,Afghanistan,https://www.cricbuzz.com/profiles/3014/mohammad-shahzad 249 | Aftab Alam,Afghanistan,https://www.cricbuzz.com/profiles/6476/aftab-alam 250 | Dawlat Zadran,Afghanistan,https://www.cricbuzz.com/profiles/8029/dawlat-zadran 251 | Hamid Hassan,Afghanistan,https://www.cricbuzz.com/profiles/3001/hamid-hassan 252 | Mujeeb Ur Rahman,Afghanistan,https://www.cricbuzz.com/profiles/12071/mujeeb-ur-rahman 253 | Fakhar Zaman,Pakistan,https://www.cricbuzz.com/profiles/10863/fakhar-zaman 254 | Imam-ul-Haq,Pakistan,https://www.cricbuzz.com/profiles/8364/imam-ul-haq 255 | Babar Azam,Pakistan,https://www.cricbuzz.com/profiles/8359/babar-azam 256 | Asif Ali,Pakistan,https://www.cricbuzz.com/profiles/9565/asif-ali 257 | Haris Sohail,Pakistan,https://www.cricbuzz.com/profiles/8299/haris-sohail 258 | Mohammad Hafeez,Pakistan,https://www.cricbuzz.com/profiles/360/mohammad-hafeez 259 | Shoaib Malik,Pakistan,https://www.cricbuzz.com/profiles/33/shoaib-malik 260 | Shadab Khan,Pakistan,https://www.cricbuzz.com/profiles/11186/shadab-khan 261 | Imad Wasim,Pakistan,https://www.cricbuzz.com/profiles/10408/imad-wasim 262 | Sarfaraz Ahmed,Pakistan,https://www.cricbuzz.com/profiles/881/sarfaraz-ahmed 263 | Wahab Riaz,Pakistan,https://www.cricbuzz.com/profiles/1051/wahab-riaz 264 | Shaheen Afridi,Pakistan,https://www.cricbuzz.com/profiles/12160/shaheen-afridi 265 | Hasan Ali,Pakistan,https://www.cricbuzz.com/profiles/11320/hasan-ali 266 | Mohammad Hasnain,Pakistan,https://www.cricbuzz.com/profiles/14248/mohammad-hasnain 267 | Mohammad Amir,Pakistan,https://www.cricbuzz.com/profiles/1917/mohammad-amir 268 | Lahiru Thirimanne,Sri Lanka,https://www.cricbuzz.com/profiles/6243/lahiru-thirimanne 269 | Dimuth Karunaratne,Sri Lanka,https://www.cricbuzz.com/profiles/7952/dimuth-karunaratne 270 | Avishka Fernando,Sri Lanka,https://www.cricbuzz.com/profiles/10939/avishka-fernando 271 | Angelo Mathews,Sri Lanka,https://www.cricbuzz.com/profiles/1629/angelo-mathews 272 | Dhananjaya de Silva,Sri Lanka,https://www.cricbuzz.com/profiles/7872/dhananjaya-de-silva 273 | Milinda Siriwardana,Sri Lanka,https://www.cricbuzz.com/profiles/7891/milinda-siriwardana 274 | Thisara Perera,Sri Lanka,https://www.cricbuzz.com/profiles/2281/thisara-perera 275 | Isuru Udana,Sri Lanka,https://www.cricbuzz.com/profiles/1920/isuru-udana 276 | Jeevan Mendis,Sri Lanka,https://www.cricbuzz.com/profiles/6470/jeevan-mendis 277 | Kusal Perera,Sri Lanka,https://www.cricbuzz.com/profiles/8116/kusal-perera 278 | Kusal Mendis,Sri Lanka,https://www.cricbuzz.com/profiles/9494/kusal-mendis 279 | Suranga Lakmal,Sri Lanka,https://www.cricbuzz.com/profiles/1785/suranga-lakmal 280 | Lasith Malinga,Sri Lanka,https://www.cricbuzz.com/profiles/111/lasith-malinga 281 | Jeffrey Vandersay,Sri Lanka,https://www.cricbuzz.com/profiles/10469/jeffrey-vandersay 282 | Nuwan Pradeep,Sri Lanka,https://www.cricbuzz.com/profiles/6730/nuwan-pradeep 283 | Eoin Morgan,England,https://www.cricbuzz.com/profiles/624/eoin-morgan 284 | Joe Root,England,https://www.cricbuzz.com/profiles/8019/joe-root 285 | Jason Roy,England,https://www.cricbuzz.com/profiles/6534/jason-roy 286 | Liam Livingstone,England,https://www.cricbuzz.com/profiles/10045/liam-livingstone 287 | Dawid Malan,England,https://www.cricbuzz.com/profiles/6660/dawid-malan 288 | Moeen Ali,England,https://www.cricbuzz.com/profiles/6692/moeen-ali 289 | Ben Stokes,England,https://www.cricbuzz.com/profiles/6557/ben-stokes 290 | Chris Woakes,England,https://www.cricbuzz.com/profiles/6670/chris-woakes 291 | Tom Curran,England,https://www.cricbuzz.com/profiles/9522/tom-curran 292 | Lewis Gregory,England,https://www.cricbuzz.com/profiles/7854/lewis-gregory 293 | Sam Curran,England,https://www.cricbuzz.com/profiles/10420/sam-curran 294 | Jonny Bairstow,England,https://www.cricbuzz.com/profiles/6507/jonny-bairstow 295 | Jos Buttler,England,https://www.cricbuzz.com/profiles/2258/jos-buttler 296 | Sam Billings,England,https://www.cricbuzz.com/profiles/7990/sam-billings 297 | Adil Rashid,England,https://www.cricbuzz.com/profiles/1742/adil-rashid 298 | Mark Wood,England,https://www.cricbuzz.com/profiles/8383/mark-wood 299 | Jofra Archer,England,https://www.cricbuzz.com/profiles/11540/jofra-archer 300 | Olly Stone,England,https://www.cricbuzz.com/profiles/8022/olly-stone 301 | Reece Topley,England,https://www.cricbuzz.com/profiles/8349/reece-topley 302 | Chris Jordan,England,https://www.cricbuzz.com/profiles/6532/chris-jordan 303 | Faf du Plessis,South Africa,https://www.cricbuzz.com/profiles/7825/faf-du-plessis 304 | David Miller,South Africa,https://www.cricbuzz.com/profiles/6349/david-miller 305 | Aiden Markram,South Africa,https://www.cricbuzz.com/profiles/9582/aiden-markram 306 | Rassie van der Dussen,South Africa,https://www.cricbuzz.com/profiles/9554/rassie-van-der-dussen 307 | Jean-Paul Duminy,South Africa,https://www.cricbuzz.com/profiles/211/jean-paul-duminy 308 | Andile Phehlukwayo,South Africa,https://www.cricbuzz.com/profiles/9601/andile-phehlukwayo 309 | Dwaine Pretorius,South Africa,https://www.cricbuzz.com/profiles/8651/dwaine-pretorius 310 | Chris Morris,South Africa,https://www.cricbuzz.com/profiles/8333/chris-morris 311 | Quinton de Kock,South Africa,https://www.cricbuzz.com/profiles/8520/quinton-de-kock 312 | Imran Tahir,South Africa,https://www.cricbuzz.com/profiles/6667/imran-tahir 313 | Kagiso Rabada,South Africa,https://www.cricbuzz.com/profiles/9585/kagiso-rabada 314 | Lungi Ngidi,South Africa,https://www.cricbuzz.com/profiles/9603/lungi-ngidi 315 | Tabraiz Shamsi,South Africa,https://www.cricbuzz.com/profiles/10173/tabraiz-shamsi 316 | Beuran Hendricks,South Africa,https://www.cricbuzz.com/profiles/9490/beuran-hendricks 317 | Martin Guptill,New Zealand,https://www.cricbuzz.com/profiles/1457/martin-guptill 318 | Ross Taylor,New Zealand,https://www.cricbuzz.com/profiles/521/ross-taylor 319 | Kane Williamson,New Zealand,https://www.cricbuzz.com/profiles/6326/kane-williamson 320 | Henry Nicholls,New Zealand,https://www.cricbuzz.com/profiles/10694/henry-nicholls 321 | James Neesham,New Zealand,https://www.cricbuzz.com/profiles/8983/james-neesham 322 | Colin de Grandhomme,New Zealand,https://www.cricbuzz.com/profiles/8081/colin-de-grandhomme 323 | Colin Munro,New Zealand,https://www.cricbuzz.com/profiles/8085/colin-munro 324 | Mitchell Santner,New Zealand,https://www.cricbuzz.com/profiles/10100/mitchell-santner 325 | Tom Latham,New Zealand,https://www.cricbuzz.com/profiles/8216/tom-latham 326 | Tom Blundell,New Zealand,https://www.cricbuzz.com/profiles/10717/tom-blundell 327 | Trent Boult,New Zealand,https://www.cricbuzz.com/profiles/8117/trent-boult 328 | Matt Henry,New Zealand,https://www.cricbuzz.com/profiles/9067/matt-henry 329 | Tim Southee,New Zealand,https://www.cricbuzz.com/profiles/1057/tim-southee 330 | Ish Sodhi,New Zealand,https://www.cricbuzz.com/profiles/8561/ish-sodhi 331 | Lockie Ferguson,New Zealand,https://www.cricbuzz.com/profiles/10692/lockie-ferguson 332 | Naresh Bahadur Budayair,Nepal,https://www.cricbuzz.com/profiles/9962/naresh-bahadur-budayair 333 | Rajesh Pulami,Nepal,https://www.cricbuzz.com/profiles/10204/rajesh-pulami 334 | Gyanendra Malla,Nepal,https://www.cricbuzz.com/profiles/9966/gyanendra-malla 335 | Anil Mandal,Nepal,https://www.cricbuzz.com/profiles/10205/anil-mandal 336 | Sharad Vesawkar,Nepal,https://www.cricbuzz.com/profiles/9971/sharad-vesawkar 337 | Paras Khadka,Nepal,https://www.cricbuzz.com/profiles/9958/paras-khadka 338 | Amrit Bhattarai,Nepal,https://www.cricbuzz.com/profiles/9985/amrit-bhattarai 339 | Shakti Gauchan,Nepal,https://www.cricbuzz.com/profiles/9963/shakti-gauchan 340 | Avinash Karn,Nepal,https://www.cricbuzz.com/profiles/9964/avinash-karn 341 | Basant Regmi,Nepal,https://www.cricbuzz.com/profiles/9968/basant-regmi 342 | Sagar Pun,Nepal,https://www.cricbuzz.com/profiles/9969/sagar-pun 343 | Pradeep Airee,Nepal,https://www.cricbuzz.com/profiles/9959/pradeep-airee 344 | Binod Bhandari,Nepal,https://www.cricbuzz.com/profiles/9961/binod-bhandari 345 | Subash Khakurel,Nepal,https://www.cricbuzz.com/profiles/9965/subash-khakurel 346 | Karan KC,Nepal,https://www.cricbuzz.com/profiles/10203/karan-kc 347 | Jitendra Mukhiya,Nepal,https://www.cricbuzz.com/profiles/9967/jitendra-mukhiya 348 | Sompal Kami,Nepal,https://www.cricbuzz.com/profiles/9970/sompal-kami 349 | Riaan Walters,Namibia,https://www.cricbuzz.com/profiles/7693/riaan-walters 350 | Sarel Burger,Namibia,https://www.cricbuzz.com/profiles/7706/sarel-burger 351 | Gerrie Snyman,Namibia,https://www.cricbuzz.com/profiles/7701/gerrie-snyman 352 | Stefan Swanepoel,Namibia,https://www.cricbuzz.com/profiles/7694/stefan-swanepoel 353 | Jan-Berrie Burger,Namibia,https://www.cricbuzz.com/profiles/7695/jan-berrie-burger 354 | Burton van Rooi,Namibia,https://www.cricbuzz.com/profiles/7707/burton-van-rooi 355 | Louis Burger,Namibia,https://www.cricbuzz.com/profiles/7698/louis-burger 356 | Morne Karg,Namibia,https://www.cricbuzz.com/profiles/7700/morne-karg 357 | Danie Keulder,Namibia,https://www.cricbuzz.com/profiles/7696/danie-keulder 358 | Bjorn Kotze,Namibia,https://www.cricbuzz.com/profiles/7703/bjorn-kotze 359 | Deon Kotze,Namibia,https://www.cricbuzz.com/profiles/7699/deon-kotze 360 | Lennie Louw,Namibia,https://www.cricbuzz.com/profiles/7702/lennie-louw 361 | Melt van Schoor,Namibia,https://www.cricbuzz.com/profiles/7704/melt-van-schoor 362 | Rudi van Vuuren,Namibia,https://www.cricbuzz.com/profiles/7705/rudi-van-vuuren 363 | Ali Bushahri,Kuwait,https://www.cricbuzz.com/profiles/10138/ali-bushahri 364 | Alkandari Mohammad,Kuwait,https://www.cricbuzz.com/profiles/10141/alkandari-mohammad 365 | Alnadi Faleh,Kuwait,https://www.cricbuzz.com/profiles/10142/alnadi-faleh 366 | Bastaki Abdullah,Kuwait,https://www.cricbuzz.com/profiles/10145/bastaki-abdullah 367 | Eissa Jassim,Kuwait,https://www.cricbuzz.com/profiles/10149/eissa-jassim 368 | Salem Mishary,Kuwait,https://www.cricbuzz.com/profiles/10150/salem-mishary 369 | Bastaki Mahmoud,Kuwait,https://www.cricbuzz.com/profiles/10147/bastaki-mahmoud 370 | Abdulrahman Dashti,Kuwait,https://www.cricbuzz.com/profiles/10136/abdulrahman-dashti 371 | Alkandari Abdulrahman,Kuwait,https://www.cricbuzz.com/profiles/10140/alkandari-abdulrahman 372 | Alotaibi Mohammed,Kuwait,https://www.cricbuzz.com/profiles/10143/alotaibi-mohammed 373 | Bastaki Fahad,Kuwait,https://www.cricbuzz.com/profiles/10146/bastaki-fahad 374 | Ebrahim Aldhabyan,Kuwait,https://www.cricbuzz.com/profiles/10137/ebrahim-aldhabyan 375 | Ali Zainal,Kuwait,https://www.cricbuzz.com/profiles/10139/ali-zainal 376 | Beidas Tareq,Kuwait,https://www.cricbuzz.com/profiles/10148/beidas-tareq 377 | Yousif Alzaid,Kuwait,https://www.cricbuzz.com/profiles/10151/yousif-alzaid 378 | Mohammad Usman,United Arab Emirates,https://www.cricbuzz.com/profiles/10869/mohammad-usman 379 | Shaiman Anwar,United Arab Emirates,https://www.cricbuzz.com/profiles/9837/shaiman-anwar 380 | Chundangapoyil Rizwan,United Arab Emirates,https://www.cricbuzz.com/profiles/14554/chundangapoyil-rizwan 381 | Chirag Suri,United Arab Emirates,https://www.cricbuzz.com/profiles/9829/chirag-suri 382 | Ashfaq Ahmed,United Arab Emirates,https://www.cricbuzz.com/profiles/13278/ashfaq-ahmed 383 | Mohammad Boota,United Arab Emirates,https://www.cricbuzz.com/profiles/13279/mohammad-boota 384 | Fayyaz Ahmed,United Arab Emirates,https://www.cricbuzz.com/profiles/10038/fayyaz-ahmed 385 | Rohan Mustafa,United Arab Emirates,https://www.cricbuzz.com/profiles/9835/rohan-mustafa 386 | Ahmed Raza,United Arab Emirates,https://www.cricbuzz.com/profiles/9828/ahmed-raza 387 | Mohammad Naveed,United Arab Emirates,https://www.cricbuzz.com/profiles/9832/mohammad-naveed 388 | Ghulam Shabber,United Arab Emirates,https://www.cricbuzz.com/profiles/11817/ghulam-shabber 389 | Imran Haider,United Arab Emirates,https://www.cricbuzz.com/profiles/10874/imran-haider 390 | Qadeer Ahmed,United Arab Emirates,https://www.cricbuzz.com/profiles/10814/qadeer-ahmed 391 | Sultan Ahmed,United Arab Emirates,https://www.cricbuzz.com/profiles/12746/sultan-ahmed 392 | Amir Hayat,United Arab Emirates,https://www.cricbuzz.com/profiles/13269/amir-hayat 393 | Zahoor Khan,United Arab Emirates,https://www.cricbuzz.com/profiles/12247/zahoor-khan 394 | Alex Obanda,Kenya,https://www.cricbuzz.com/profiles/704/alex-obanda 395 | Duncan Allan,Kenya,https://www.cricbuzz.com/profiles/8075/duncan-allan 396 | Collins Obuya,Kenya,https://www.cricbuzz.com/profiles/550/collins-obuya 397 | Seren Waters,Kenya,https://www.cricbuzz.com/profiles/1592/seren-waters 398 | Ragheb Aga,Kenya,https://www.cricbuzz.com/profiles/188/ragheb-aga 399 | Nelson Odhiambo,Kenya,https://www.cricbuzz.com/profiles/6478/nelson-odhiambo 400 | Nehemiah Odhiambo,Kenya,https://www.cricbuzz.com/profiles/552/nehemiah-odhiambo 401 | Emmanuel Bundi,Kenya,https://www.cricbuzz.com/profiles/9356/emmanuel-bundi 402 | Irfan Karim,Kenya,https://www.cricbuzz.com/profiles/8077/irfan-karim 403 | Morris Ouma,Kenya,https://www.cricbuzz.com/profiles/194/morris-ouma 404 | Rakep Patel,Kenya,https://www.cricbuzz.com/profiles/1434/rakep-patel 405 | Ibrahim Akello,Kenya,https://www.cricbuzz.com/profiles/8074/ibrahim-akello 406 | Alfred Luseno,Kenya,https://www.cricbuzz.com/profiles/824/alfred-luseno 407 | James Ngoche,Kenya,https://www.cricbuzz.com/profiles/7727/james-ngoche 408 | Shem Ngoche,Kenya,https://www.cricbuzz.com/profiles/7708/shem-ngoche 409 | Lucas Oluoch,Kenya,https://www.cricbuzz.com/profiles/8078/lucas-oluoch 410 | Elijah Otieno,Kenya,https://www.cricbuzz.com/profiles/791/elijah-otieno 411 | Hiren Varaiya,Kenya,https://www.cricbuzz.com/profiles/553/hiren-varaiya 412 | Dominic Wesonga,Kenya,https://www.cricbuzz.com/profiles/7888/dominic-wesonga 413 | Kyle Coetzer,Scotland,https://www.cricbuzz.com/profiles/1415/kyle-coetzer 414 | Dylan Budge,Scotland,https://www.cricbuzz.com/profiles/13738/dylan-budge 415 | Michael Jones,Scotland,https://www.cricbuzz.com/profiles/13527/michael-jones 416 | George Munsey,Scotland,https://www.cricbuzz.com/profiles/10103/george-munsey 417 | Richie Berrington,Scotland,https://www.cricbuzz.com/profiles/1398/richie-berrington 418 | Calum MacLeod,Scotland,https://www.cricbuzz.com/profiles/1416/calum-macleod 419 | Michael Leask,Scotland,https://www.cricbuzz.com/profiles/9383/michael-leask 420 | Alasdair Evans,Scotland,https://www.cricbuzz.com/profiles/3053/alasdair-evans 421 | Tom Sole,Scotland,https://www.cricbuzz.com/profiles/12771/tom-sole 422 | Matthew Cross,Scotland,https://www.cricbuzz.com/profiles/9365/matthew-cross 423 | Craig Wallace,Scotland,https://www.cricbuzz.com/profiles/8255/craig-wallace 424 | Mark Watt,Scotland,https://www.cricbuzz.com/profiles/9928/mark-watt 425 | Safyaan Sharif,Scotland,https://www.cricbuzz.com/profiles/7966/safyaan-sharif 426 | Chris Sole,Scotland,https://www.cricbuzz.com/profiles/9925/chris-sole 427 | Gavin Main,Scotland,https://www.cricbuzz.com/profiles/9922/gavin-main 428 | Brad Wheal,Scotland,https://www.cricbuzz.com/profiles/10389/brad-wheal 429 | Delyone Borden,Bermuda,https://www.cricbuzz.com/profiles/662/delyone-borden 430 | James Celestine,Bermuda,https://www.cricbuzz.com/profiles/861/james-celestine 431 | Fiqre Crockwell,Bermuda,https://www.cricbuzz.com/profiles/3373/fiqre-crockwell 432 | Chris Foggo,Bermuda,https://www.cricbuzz.com/profiles/1755/chris-foggo 433 | David Hemp,Bermuda,https://www.cricbuzz.com/profiles/664/david-hemp 434 | Kyle Hodsoll,Bermuda,https://www.cricbuzz.com/profiles/863/kyle-hodsoll 435 | Steven Outerbridge,Bermuda,https://www.cricbuzz.com/profiles/670/steven-outerbridge 436 | Azeem Pitcher,Bermuda,https://www.cricbuzz.com/profiles/864/azeem-pitcher 437 | Tamauri Tucker,Bermuda,https://www.cricbuzz.com/profiles/866/tamauri-tucker 438 | Malachi Jones,Bermuda,https://www.cricbuzz.com/profiles/666/malachi-jones 439 | Oliver Pitcher,Bermuda,https://www.cricbuzz.com/profiles/671/oliver-pitcher 440 | Rodney Trott,Bermuda,https://www.cricbuzz.com/profiles/865/rodney-trott 441 | Janeiro Tucker,Bermuda,https://www.cricbuzz.com/profiles/673/janeiro-tucker 442 | Jekon Edness,Bermuda,https://www.cricbuzz.com/profiles/862/jekon-edness 443 | Oronde Bascome,Bermuda,https://www.cricbuzz.com/profiles/1427/oronde-bascome 444 | Chris Douglas,Bermuda,https://www.cricbuzz.com/profiles/1428/chris-douglas 445 | Dennico Hollis,Bermuda,https://www.cricbuzz.com/profiles/1431/dennico-hollis 446 | Kevin Hurdle,Bermuda,https://www.cricbuzz.com/profiles/665/kevin-hurdle 447 | Stefan Kelly,Bermuda,https://www.cricbuzz.com/profiles/667/stefan-kelly 448 | Chris Lonsdale,Bermuda,https://www.cricbuzz.com/profiles/1762/chris-lonsdale 449 | George O Brien,Bermuda,https://www.cricbuzz.com/profiles/3343/george-o-brien 450 | Jacobi Robinson,Bermuda,https://www.cricbuzz.com/profiles/1775/jacobi-robinson 451 | McLaren Smith,Bermuda,https://www.cricbuzz.com/profiles/1779/mclaren-smith 452 | Ryan Steede,Bermuda,https://www.cricbuzz.com/profiles/1430/ryan-steede 453 | Glenn Blakeney,Bermuda,https://www.cricbuzz.com/profiles/3372/glenn-blakeney 454 | Irving Romaine,Bermuda,https://www.cricbuzz.com/profiles/660/irving-romaine 455 | -------------------------------------------------------------------------------- /16. Joins in Action/Joins on Cricket Players/teams.csv: -------------------------------------------------------------------------------- 1 | name,link 2 | India,https://www.cricbuzz.com/cricket-team/india/2/players 3 | Ireland,https://www.cricbuzz.com/cricket-team/ireland/27/players 4 | Australia,https://www.cricbuzz.com/cricket-team/australia/4/players 5 | Bangladesh,https://www.cricbuzz.com/cricket-team/bangladesh/6/players 6 | West Indies,https://www.cricbuzz.com/cricket-team/west-indies/10/players 7 | Zimbabwe,https://www.cricbuzz.com/cricket-team/zimbabwe/12/players 8 | Malaysia,https://www.cricbuzz.com/cricket-team/malaysia/71/players 9 | Germany,https://www.cricbuzz.com/cricket-team/germany/77/players 10 | Denmark,https://www.cricbuzz.com/cricket-team/denmark/185/players 11 | Papua New Guinea,https://www.cricbuzz.com/cricket-team/papua-new-guinea/287/players 12 | Vanuatu,https://www.cricbuzz.com/cricket-team/vanuatu/300/players 13 | Oman,https://www.cricbuzz.com/cricket-team/oman/304/players 14 | Italy,https://www.cricbuzz.com/cricket-team/italy/527/players 15 | Belgium,https://www.cricbuzz.com/cricket-team/belgium/541/players 16 | Canada,https://www.cricbuzz.com/cricket-team/canada/26/players 17 | Hong Kong,https://www.cricbuzz.com/cricket-team/hong-kong/8/players 18 | United States,https://www.cricbuzz.com/cricket-team/united-states/15/players 19 | Netherlands,https://www.cricbuzz.com/cricket-team/netherlands/24/players 20 | Iran,https://www.cricbuzz.com/cricket-team/iran/675/players 21 | Afghanistan,https://www.cricbuzz.com/cricket-team/afghanistan/96/players 22 | Pakistan,https://www.cricbuzz.com/cricket-team/pakistan/3/players 23 | Sri Lanka,https://www.cricbuzz.com/cricket-team/sri-lanka/5/players 24 | England,https://www.cricbuzz.com/cricket-team/england/9/players 25 | South Africa,https://www.cricbuzz.com/cricket-team/south-africa/11/players 26 | New Zealand,https://www.cricbuzz.com/cricket-team/new-zealand/13/players 27 | Nepal,https://www.cricbuzz.com/cricket-team/nepal/72/players 28 | Namibia,https://www.cricbuzz.com/cricket-team/namibia/161/players 29 | Singapore,https://www.cricbuzz.com/cricket-team/singapore/190/players 30 | Kuwait,https://www.cricbuzz.com/cricket-team/kuwait/298/players 31 | Jersey,https://www.cricbuzz.com/cricket-team/jersey/303/players 32 | Fiji,https://www.cricbuzz.com/cricket-team/fiji/343/players 33 | Botswana,https://www.cricbuzz.com/cricket-team/botswana/529/players 34 | Uganda,https://www.cricbuzz.com/cricket-team/uganda/44/players 35 | United Arab Emirates,https://www.cricbuzz.com/cricket-team/united-arab-emirates/7/players 36 | Kenya,https://www.cricbuzz.com/cricket-team/kenya/14/players 37 | Scotland,https://www.cricbuzz.com/cricket-team/scotland/23/players 38 | Bermuda,https://www.cricbuzz.com/cricket-team/bermuda/25/players 39 | -------------------------------------------------------------------------------- /17. Set Operators in Action/01. creating database.sql: -------------------------------------------------------------------------------- 1 | DROP DATABASE IF EXISTS users; 2 | CREATE DATABASE users; 3 | 4 | CREATE TABLE IF NOT EXISTS users.users_2021 (UserID INT PRIMARY KEY, Name VARCHAR(50)); 5 | CREATE TABLE IF NOT EXISTS users.users_2022 (UserID INT PRIMARY KEY, Name VARCHAR(50)); 6 | CREATE TABLE IF NOT EXISTS users.users_2023 (UserID INT PRIMARY KEY, Name VARCHAR(50)); 7 | 8 | INSERT INTO users.users_2021 (UserID, Name) VALUES (1, 'Ashish'), (2, 'Laura'), (7, 'Prakash'); 9 | INSERT INTO users.users_2022 (UserID, Name) VALUES (1, 'Ashish'), (2, 'Laura'), (3, 'Charlie'), (4, 'Grace'); 10 | INSERT INTO users.users_2023 (UserID, Name) VALUES (1, 'Ashish'), (2, 'Laura'), (3, 'Charlie'), (4, 'Grace'), (5, 'Henry'); 11 | 12 | select * from users.users_2021; 13 | select * from users.users_2022; 14 | select * from users.users_2023; -------------------------------------------------------------------------------- /17. Set Operators in Action/02. operators.sql: -------------------------------------------------------------------------------- 1 | select * from users.users_2021; 2 | select * from users.users_2022; 3 | select * from users.users_2023; 4 | 5 | -- UNION | remove duplicates 6 | select * from users.users_2021 union select * from users.users_2022; 7 | select * from users.users_2022 union select * from users.users_2021; 8 | select * from users.users_2021 union select * from users.users_2023; 9 | select * from users.users_2022 union select * from users.users_2023; 10 | select * from users.users_2021 union select * from users.users_2022 union select * from users.users_2023; 11 | 12 | -- UNION ALL | Dont't remove duplicates 13 | select * from users.users_2021 union all select * from users.users_2022; 14 | select * from users.users_2022 union all select * from users.users_2021; 15 | select * from users.users_2021 union all select * from users.users_2023; 16 | select * from users.users_2022 union all select * from users.users_2023; 17 | select * from users.users_2021 union all select * from users.users_2022 union all select * from users.users_2023; 18 | select * from users.users_2021 union all select * from users.users_2022 union select * from users.users_2023; 19 | (select * from users.users_2021 union all select * from users.users_2022) union select * from users.users_2023; 20 | 21 | -- EXCEPT | Show all the data of dominating table that is not there is other table 22 | select * from users.users_2021 except select * from users.users_2022; 23 | select * from users.users_2022 except select * from users.users_2021; 24 | select * from users.users_2021 except select * from users.users_2023; 25 | select * from users.users_2023 except select * from users.users_2021; 26 | select * from users.users_2021 except select * from users.users_2022 except select * from users.users_2023; 27 | select * from users.users_2023 except select * from users.users_2022 except select * from users.users_2021; 28 | 29 | -- INTERSECT | Showing the common data 30 | select * from users.users_2021 intersect select * from users.users_2022; 31 | select * from users.users_2022 intersect select * from users.users_2023; 32 | select * from users.users_2021 intersect select * from users.users_2023; 33 | 34 | select * from users.users_2021 intersect select * from users.users_2022 intersect select * from users.users_2023; -------------------------------------------------------------------------------- /17. Set Operators in Action/03. operators assignment.sql: -------------------------------------------------------------------------------- 1 | select * from users.users_2021; 2 | select * from users.users_2022; 3 | select * from users.users_2023; 4 | 5 | -- 1. List the new users added in 2022 6 | select * from users.users_2022 except select * from users.users_2021; 7 | 8 | -- 2. List the new users added in 2023 9 | select * from users.users_2023 except select * from users.users_2022; 10 | 11 | -- 3. List the users who left us 12 | select * from users.users_2021 except select * from users.users_2022 except select * from users.users_2023; 13 | 14 | -- 4. List all the users that are there throughout the database for year 2021 & 2022 15 | select * from users.users_2022 union select * from users.users_2021; 16 | 17 | -- 5. List all the users that are there throughout the database 18 | select * from users.users_2021 union select * from users.users_2022 union select * from users.users_2023; 19 | 20 | -- 6. List the users that are there with us from last 3 years 21 | select * from users.users_2021 intersect select * from users.users_2022 intersect select * from users.users_2023; 22 | 23 | -- 7. List the all the users except that users who are there with us from 3 years 24 | select * from users.users_2021 union select * from users.users_2022 union select * from users.users_2023 25 | except 26 | select * from users.users_2021 intersect select * from users.users_2022 intersect select * from users.users_2023; 27 | 28 | -- 8. Operators with Join 29 | select * from users.users_2021 as t_2021 left join users.users_2022 as t_2022 on t_2021.UserID = t_2022.UserID; 30 | select * from users.users_2021 as t_2021 right join users.users_2022 as t_2022 on t_2021.UserID = t_2022.UserID; 31 | 32 | select * from users.users_2021 as t_2021 left join users.users_2022 as t_2022 on t_2021.UserID = t_2022.UserID 33 | union 34 | select * from users.users_2021 as t_2021 right join users.users_2022 as t_2022 on t_2021.UserID = t_2022.UserID; 35 | -------------------------------------------------------------------------------- /18. Restaurant Data Analysis with SQL/01. assignment.sql: -------------------------------------------------------------------------------- 1 | use swiggy; 2 | select * from swiggy.restaurants; 3 | 4 | -- 1. Which restaurant of abohar is visied by least number of people? 5 | 6 | -- 2. Which restaurant has generated maximum revenue all over india? 7 | 8 | -- 3. How many restaurants are having rating more than the average rating? 9 | 10 | -- 4. Which restaurant of Delhi has generated most revenue? 11 | 12 | -- 5. Which restaurant chain has maximum number of restaurants? 13 | 14 | -- 6. Which restaurant chain has generated maximum revenue? 15 | 16 | -- 7. Which city has maximum number of restaurants? 17 | 18 | -- 8. Which city has generated maximum revenue all over india? 19 | 20 | -- 9. List 10 least expensive cuisines? 21 | 22 | -- 10. List 10 most expensive cuisines? 23 | 24 | -- 11. What is the city is having Biryani as most popular cuisine 25 | 26 | -- 12. List top 10 unique restaurants with unique name only thorughout the dataset as per generate maximum revenue (Single restaurant with that name) -------------------------------------------------------------------------------- /18. Restaurant Data Analysis with SQL/02. solution.sql: -------------------------------------------------------------------------------- 1 | select * from swiggy.restaurants; 2 | 3 | -- 1. Which restaurant of abohar is visied by least number of people? 4 | select * from restaurants where city = 'Abohar' and rating_count = (select min(rating_count) from restaurants where city = 'Abohar'); 5 | 6 | -- 2. Which restaurant has generated maximum revenue all over india? 7 | select * from restaurants where cost*rating_count = (select max(cost*rating_count) from restaurants); 8 | 9 | -- 3. How many restaurants are having rating more than the average rating? 10 | select * from restaurants where rating > (select avg(rating) from restaurants); 11 | 12 | -- 4. Which restaurant of Delhi has generated most revenue? 13 | select * from restaurants where city = 'Delhi' and 14 | cost*rating_count = (select max(cost*rating_count) from restaurants where city = 'Delhi'); 15 | 16 | -- 5. Which restaurant chain has maximum number of restaurants? 17 | select name , count(name) as 'no_of_chains' from restaurants 18 | group by name order by count(name) desc limit 10; 19 | 20 | -- 6. Which restaurant chain has generated maximum revenue? 21 | select name , sum(rating_count * cost) as 'revenue' from restaurants 22 | group by name order by sum(rating_count*cost) desc limit 10; 23 | 24 | -- 7. Which city has maximum number of restaurants? 25 | select city , count(*) as 'no_of_restaurants' from restaurants 26 | group by city order by count(*) desc limit 10; 27 | 28 | -- 8. Which city has generated maximum revenue all over india? 29 | select city , sum(rating_count * cost) as 'revenue' from restaurants 30 | group by city order by sum(rating_count*cost) desc limit 10; 31 | 32 | -- 9. List 10 least expensive cuisines? 33 | select cuisine , avg(cost) as 'avg_cost' from restaurants 34 | group by cuisine 35 | order by avg_cost asc limit 10; 36 | 37 | -- 10. List 10 most expensive cuisines? 38 | select cuisine , avg(cost) as 'avg_cost' from restaurants 39 | group by cuisine 40 | order by avg_cost desc limit 10; 41 | 42 | -- 11. What is the city is having Biryani as most popular cuisine 43 | select city, avg(cost), count(*) as 'restaurants' from restaurants 44 | where cuisine = 'Biryani' 45 | group by city 46 | order by restaurants desc; 47 | 48 | -- 12. List top 10 unique restaurants with unique name only thorughout the dataset as per generate maximum revenue (Single restaurant with that name) 49 | select name, sum(cost * rating_count) as 'revenue' from restaurants 50 | group by name having count(name) = 1 51 | order by revenue desc limit 10; -------------------------------------------------------------------------------- /19. Window Functions/01. window.sql: -------------------------------------------------------------------------------- 1 | use swiggy; 2 | select * from restaurants; 3 | 4 | -- 1. Create new column containing average rating of restaurants throught the dataset 5 | select *, round(avg(rating) over(),2) as 'avg_rating' from restaurants; 6 | 7 | -- 2. Create new column containing average rating_count of restaurants throught the dataset 8 | select *, round(avg(rating_count) over(),0) as 'avg_rating_count' from restaurants; 9 | 10 | -- 3. Create new column containing average cost of restaurants throught the dataset 11 | select *, round(avg(cost) over(),0) as 'avg_cost' from restaurants; 12 | 13 | -- 4. Create column containing average, min, max of cost,rating,rating_count of restaurants throught the dataset 14 | select id, name, city, cuisine, rating, 15 | round(max(rating) over(), 2) as 'max_rating', 16 | round(avg(rating) over(), 2) as 'avg_rating', 17 | round(min(rating) over(), 2) as 'min_rating', 18 | 19 | round(max(cost) over(), 2) as 'max_cost', 20 | round(avg(cost) over(), 2) as 'avg_cost', 21 | round(min(cost) over(), 2) as 'min_cost' 22 | 23 | from restaurants; 24 | 25 | -- 5. Create column containing average cost of the city where that specific restaurant is from 26 | select *, round(avg(cost) over( partition by city) ) as 'avg_cost' from restaurants; 27 | 28 | -- 6. Create column containing average cost of the cuisine which that specific restaurant is serving 29 | select *, round(avg(cost) over( partition by cuisine) ) as 'avg_cost' from restaurants; 30 | 31 | -- 7. Create both column together 32 | select *, 33 | round(avg(cost) over( partition by city) ) as 'avg_cost_city', 34 | round(avg(cost) over( partition by cuisine) ) as 'avg_cost_cuisine' 35 | from restaurants; 36 | 37 | -- 8. List the restaurants whose cost is more than the average cost of the restaurants? 38 | select * from restaurants where cost > (select avg(cost) from restaurants); 39 | select * from (select *, avg(cost) over() as 'avg_cost' from restaurants) t where t.cost > t.avg_cost; 40 | 41 | 42 | -- 10. List the restaurants whose cuisine cost is more than the average cost? 43 | select * from (select *, avg(cost) over(partition by cuisine) as 'avg_cost' from restaurants) t where t.cost > t.avg_cost; 44 | -------------------------------------------------------------------------------- /20. Ranks in SQL/01. rank.sql: -------------------------------------------------------------------------------- 1 | use swiggy; 2 | select * from restaurants; 3 | 4 | -- 1. Rank every restaurant from most expensive to least expensive 5 | select * ,rank() over(order by cost desc) as 'rank' from restaurants; 6 | 7 | -- 2. Rank every restaurant from most visited to least visited 8 | select * ,rank() over(order by rating_count desc) as 'rank' from restaurants; 9 | 10 | -- 3. Rank every restaurant from most expensive to least expensive as per their city 11 | select * ,rank() over(partition by city order by cost desc) as 'rank' from restaurants; 12 | 13 | -- 4. Dense-rank every restaurant from most expensive to least expensive as per their city 14 | select * , 15 | rank() over(order by cost desc) as 'rank' , 16 | dense_rank() over(order by cost desc) as 'dense_rank' 17 | from restaurants; 18 | 19 | -- 5. Row-number every restaurant from most expensive to least expensive as per their city 20 | select * , 21 | rank() over(order by cost desc) as 'rank' , 22 | dense_rank() over(order by cost desc) as 'dense_rank', 23 | row_number() over(order by cost desc) as 'row_number' 24 | from restaurants; 25 | 26 | -- 6. Rank every restaurant from most expensive to least expensive as per their city along with its city [Adilabad - 1, Adilabad - 2] 27 | select *, concat(city,' - ' ,row_number() over(partition by city order by cost desc)) as 'rank' from restaurants; 28 | 29 | -- 7. Find top 5 restaurants of every city as per their revenue 30 | select * from (select *, 31 | cost*rating_count as 'revenue', 32 | row_number() over(partition by city order by rating_count*cost desc) as 'rank' from restaurants) t 33 | where t.rank < 6; 34 | 35 | -- 8. Find top 5 restaurants of every cuisine as per their revenue 36 | select * from (select *, 37 | cost*rating_count as 'revenue', 38 | row_number() over(partition by cuisine order by rating_count*cost desc) as 'rank' from restaurants) t 39 | where t.rank < 6; 40 | 41 | -------------------------------------------------------------------------------- /21. Advance Window Functions/01. practice.sql: -------------------------------------------------------------------------------- 1 | use swiggy; 2 | select * from restaurants; 3 | 4 | -- 1. List the top 5 cuisines as per the revenue generated by top 5 restaurants of every cuisine 5 | select cuisine, sum(rating_count*cost) as 'revenue'from 6 | ( select *, cost*rating_count, 7 | row_number() over(partition by cuisine order by cost*rating_count desc) as 'rank' 8 | from restaurants 9 | ) t 10 | where t.rank < 6 11 | group by cuisine 12 | order by revenue desc; 13 | 14 | -- 2. What is the of the total revenue is generated by top 1% restaurants 15 | select sum(cost*rating_count) as 'revenue' from 16 | (select *, cost*rating_count, row_number() over(order by cost*rating_count desc) as 'rank' 17 | from restaurants) t 18 | where t.rank <= 614; 19 | 20 | -- 3. Check the same for top 20% restaurants 21 | select sum(cost*rating_count) as 'revenue' from 22 | (select *, cost*rating_count, row_number() over(order by cost*rating_count desc) as 'rank' 23 | from restaurants) t 24 | where t.rank <= 12280; 25 | 26 | 27 | -- 4. What % of revenue is generated by top 20% of restaurants with respect to total revenue? 28 | with 29 | q1 as (select sum(cost*rating_count) as 'top_revenue' from 30 | (select *, cost*rating_count, row_number() over(order by cost*rating_count desc) as 'rank' 31 | from restaurants) t 32 | where t.rank <= 12280), 33 | q2 as (select sum(cost*rating_count) as 'total_revenue' from restaurants) 34 | 35 | select (top_revenue/total_revenue)*100 as 'revenue %' from q1,q2; 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /22. Views in SQL/01. views.sql: -------------------------------------------------------------------------------- 1 | use swiggy; 2 | select * from restaurants; 3 | 4 | -- 1. Create the view 5 | 6 | drop view if exists rest; 7 | create view rest as ( 8 | select name, city, rating, rating_count as 'orders', 9 | cuisine, cost, cost*rating_count as 'revenue' from restaurants); 10 | select * from rest; 11 | 12 | -- 2. Create a view for end_user 13 | drop view if exists user_view; 14 | create view user_view as ( 15 | select name, city, rating, rating_count as 'orders', 16 | cuisine, cost from restaurants); 17 | select * from user_view; 18 | 19 | -- 3. Create a view of sweet dishes 20 | drop view if exists rest_of_sweet_dishes; 21 | create view rest_of_sweet_dishes as ( 22 | select * from restaurants where cuisine in ('Sweets', 'Desserts','Bakery','Ice Cream')); 23 | select * from rest_of_sweet_dishes; 24 | 25 | -- 4. Create a view of top 100 restaurants 26 | drop view if exists top_100; 27 | create view top_100 as ( 28 | select * from restaurants order by rating_count desc limit 100); 29 | select * from top_100; 30 | 31 | -- 5. Create a view of restaurant atleast 100 people visited 32 | drop view if exists least_100; 33 | create view least_100 as ( 34 | select * from restaurants order by rating_count asc limit 100); 35 | select * from least_100; 36 | 37 | -- 6. Create a view of top 1000 most expensive restaurants 38 | drop view if exists top_1000_exp; 39 | create view top_1000_exp as ( 40 | select * from restaurants order by cost desc limit 1000); 41 | select * from top_1000_exp; 42 | 43 | -- 7. Create a view for top-rated restaurants in each city 44 | drop view if exists top_rated_rest_per_city; 45 | create view top_rated_rest_per_city as ( 46 | select * from ( 47 | select *, row_number() over(partition by city order by rating*rating_count desc) as 'rank' 48 | from restaurants) 49 | as ranked_table 50 | where ranked_table.rank = 1); 51 | select * from top_rated_rest_per_city; -------------------------------------------------------------------------------- /23. Table Aliases/1. Exporting Tables.sql: -------------------------------------------------------------------------------- 1 | -- Select all columns from the 'restaurants' table with alias 'r' 2 | SELECT * FROM restaurants; 3 | 4 | drop table if exists sirsa_restaurants; 5 | drop table if exists city_statistics; 6 | drop table if exists expensive_restaurants; 7 | 8 | -- Create a new table names 'sirsa_restaurants' containing restaurants of sirsa only 9 | create table if not exists sirsa_restaurants as SELECT * FROM restaurants where city = 'sirsa'; 10 | select * from sirsa_restaurants; 11 | 12 | -- Create a new table named 'city_statistics' containing aggregated statistics for each city 13 | create table if not exists city_statistics as 14 | SELECT city , avg(rating) as avg_rating, count(*) as num_of_restaurants FROM restaurants group by city; 15 | select * from city_statistics; 16 | 17 | -- Create a new table named 'expensive_restaurants' containing restaurants with a cost greater than 500 18 | create table if not exists expensive_restaurants as 19 | SELECT * from restaurants where cost > 500; 20 | select * from expensive_restaurants; 21 | -------------------------------------------------------------------------------- /23. Table Aliases/3. Temporary Tables.sql: -------------------------------------------------------------------------------- 1 | drop table if exists temp_restaurants; 2 | drop table if exists temp_restaurants_sirsa; 3 | drop table if exists temp_city_statistics; 4 | drop table if exists temp_expensive_restaurants; 5 | 6 | -- Creatae your first temporary table 7 | create temporary table temp_restaurants as SELECT * FROM restaurants; 8 | select * from temp_restaurants; 9 | 10 | -- Drop temporary tables if they exist 11 | drop table if exists temp_restaurants; 12 | 13 | -- Create a new temporary table named 'sirsa_restaurants' containing restaurants of Sirsa only 14 | create temporary table temp_restaurants_sirsa as SELECT * FROM restaurants where city = 'sirsa'; 15 | select * from temp_restaurants_sirsa; 16 | 17 | -- Create a new temporary table named 'city_statistics' containing aggregated statistics for each city 18 | create temporary table if not exists temp_city_statistics as 19 | SELECT city , avg(rating) as avg_rating, count(*) as num_of_restaurants FROM restaurants group by city; 20 | select * from city_statistics; 21 | 22 | -- Create a new temporary table named 'expensive_restaurants' containing restaurants with a cost greater than 500 23 | create temporary table if not exists temp_expensive_restaurants as 24 | SELECT * from restaurants where cost > 500; 25 | select * from temp_expensive_restaurants; -------------------------------------------------------------------------------- /23. Table Aliases/3. alias.sql: -------------------------------------------------------------------------------- 1 | -- Select all columns from the 'restaurants' table with alias 'r' 2 | select * from restaurants as rest; 3 | 4 | -- Select the city and the average cost of restaurants in each city using table aliases and aggregate functions 5 | select rest.city, avg(rest.cost) as 'avg_cost' 6 | from restaurants as rest 7 | group by rest.city; 8 | 9 | -- Select restaurants that have a higher rating than the average rating of all restaurants in the same city using self-referencing query with table aliases 10 | select rest.* 11 | from restaurants as rest 12 | where rest.rating > (select avg(rating) from restaurants where city = rest.city); 13 | -------------------------------------------------------------------------------- /24. Functions & Procedures/01. dataset creation.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS messy_indian_dataset; 2 | CREATE TABLE IF NOT EXISTS messy_indian_dataset ( 3 | id INT, 4 | name VARCHAR(50), 5 | age INT, 6 | gender VARCHAR(10), 7 | email VARCHAR(100), 8 | phone_number VARCHAR(20), 9 | city VARCHAR(50), 10 | state VARCHAR(50), 11 | purchase_amount DECIMAL(10, 2), 12 | purchase_date DATE 13 | ); 14 | 15 | -- Insert messy data into the table for Indian users 16 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) VALUES 17 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 18 | (2, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', NULL, '2023-02-15'), 19 | (3, 'Amit Kumar', 35, 'Male', 'amit@example.com', '9876543212', 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 20 | (4, 'Ritu Singh', 28, 'Female', NULL, '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 21 | (5, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 22 | (6, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', 800.00, '2023-02-15'), 23 | (7, 'Amit Kumar', NULL, 'Male', 'amit@example.com', NULL, 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 24 | (8, 'Ritu Singh', 28, 'Female', 'ritu@example.com', '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 25 | (9, 'Ankit Tiwari', 32, 'Male', 'ankit@example.com', '9876543214', 'Lucknow', 'Uttar Pradesh', 900.00, '2023-05-20'), 26 | (10, 'Swati Gupta', 27, 'Female', 'swati@example.com', '9876543215', 'Jaipur', 'Rajasthan', 1500.00, NULL); 27 | select * from messy_indian_dataset; 28 | -------------------------------------------------------------------------------- /24. Functions & Procedures/02. functions.sql: -------------------------------------------------------------------------------- 1 | USE swiggy; 2 | SELECT * FROM messy_indian_dataset; 3 | 4 | -- Function to Calculate Age 5 | drop function if exists CalculateAge; 6 | delimiter // 7 | create function CalculateAge(birthdate DATE) RETURNS INT 8 | deterministic 9 | begin 10 | declare age int; 11 | set age = YEAR(CURDATE()) - YEAR(birthdate); 12 | return age; 13 | end // 14 | delimiter ; 15 | 16 | select CalculateAge('1990-05-15') as age; 17 | 18 | -- Function to Calculate Tax 19 | 20 | drop function if exists CalculateTax; 21 | delimiter // 22 | create function CalculateTax(amount DECIMAL(10,2), tax_rate DECIMAL(5,2)) RETURNS DECIMAL(10,2) 23 | deterministic 24 | begin 25 | declare tax decimal(10,2); 26 | set tax = amount * (tax_rate / 100); 27 | return tax; 28 | end // 29 | delimiter ; 30 | 31 | select CalculateTax(1000.15,18) as tax_amount; 32 | 33 | 34 | -- Function to Categorize Age 35 | 36 | drop function if exists GetAgeCategory; 37 | delimiter // 38 | create function GetAgeCategory(age INT) RETURNS VARCHAR(20) 39 | deterministic 40 | begin 41 | declare category varchar(20); 42 | if age < 18 then 43 | set category = 'child'; 44 | elseif age between 18 and 65 then 45 | set category = 'adult'; 46 | else 47 | set category = 'senior'; 48 | end if; 49 | 50 | return category; 51 | end // 52 | delimiter ; 53 | 54 | select GetAgeCategory(19) as category; 55 | 56 | -------------------------------------------------------------------------------- /24. Functions & Procedures/03. procedures.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM messy_indian_dataset; 2 | 3 | -- 1. Creating your first procedure 4 | drop procedure if exists GetUserByID; 5 | delimiter // 6 | create procedure GetUserByID(IN userID INT) 7 | begin 8 | select * from messy_indian_dataset where id = userID; 9 | end // 10 | delimiter ; 11 | call GetUserByID(1); 12 | 13 | -- 2. Procedure to Update Purchase Amount by Percentage 14 | 15 | drop procedure if exists UpdatePurchaseAmount; 16 | delimiter // 17 | create procedure UpdatePurchaseAmount(IN percentage decimal(5,2)) 18 | begin 19 | update messy_indian_dataset 20 | set purchase_amount = purchase_amount * (1+percentage/100); 21 | select * from messy_indian_dataset; 22 | end // 23 | delimiter ; 24 | 25 | call UpdatePurchaseAmount(10); 26 | 27 | -- 3. Deleting Low-Rated Restaurants and Logging 28 | 29 | create table if not exists deleted_rest_log( 30 | id int, 31 | name varchar(255), 32 | rating decimal(3,2), 33 | deletion_time timestamp); 34 | 35 | drop procedure if exists delete_low_rate_rest; 36 | delimiter // 37 | create procedure delete_low_rate_rest(min_rating decimal(3,2)) 38 | begin 39 | 40 | insert into deleted_rest_log (id, name, rating, deletion_time) 41 | select id, name, rating, now() from swiggy.restaurants 42 | where rating < min_rating; 43 | 44 | delete from swiggy.restaurants 45 | where rating < min_rating; 46 | end // 47 | delimiter ; 48 | 49 | call delete_low_rate_rest(3.0); 50 | 51 | select * from swiggy.restaurants; -------------------------------------------------------------------------------- /25. Transections/01. dataset creation.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS messy_indian_dataset; 2 | CREATE TABLE IF NOT EXISTS messy_indian_dataset ( 3 | id INT, 4 | name VARCHAR(50), 5 | age INT, 6 | gender VARCHAR(10), 7 | email VARCHAR(100), 8 | phone_number VARCHAR(15), 9 | city VARCHAR(50), 10 | state VARCHAR(50), 11 | purchase_amount DECIMAL(10, 2), 12 | purchase_date DATE 13 | ); 14 | 15 | -- Insert messy data into the table for Indian users 16 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) VALUES 17 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 18 | (2, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', NULL, '2023-02-15'), 19 | (3, 'Amit Kumar', 35, 'Male', 'amit@example.com', '9876543212', 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 20 | (4, 'Ritu Singh', 28, 'Female', NULL, '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 21 | (5, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 22 | (6, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', 800.00, '2023-02-15'), 23 | (7, 'Amit Kumar', NULL, 'Male', 'amit@example.com', NULL, 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 24 | (8, 'Ritu Singh', 28, 'Female', 'ritu@example.com', '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 25 | (9, 'Ankit Tiwari', 32, 'Male', 'ankit@example.com', '9876543214', 'Lucknow', 'Uttar Pradesh', 900.00, '2023-05-20'), 26 | (10, 'Swati Gupta', 27, 'Female', 'swati@example.com', '9876543215', 'Jaipur', 'Rajasthan', 1500.00, NULL); 27 | select * from messy_indian_dataset; 28 | -------------------------------------------------------------------------------- /25. Transections/02. transections.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM messy_indian_dataset; 2 | 3 | -- Start a transaction 4 | START TRANSACTION; 5 | UPDATE messy_indian_dataset SET purchase_amount = 1500 where id = 1; 6 | SELECT * FROM messy_indian_dataset; 7 | 8 | -- Transection with Rollback 9 | START TRANSACTION; 10 | UPDATE messy_indian_dataset SET purchase_amount = 1400 where id = 1; 11 | SELECT * FROM messy_indian_dataset; 12 | ROLLBACK; 13 | SELECT * FROM messy_indian_dataset; 14 | 15 | 16 | -- Transections with Commit 17 | START TRANSACTION; 18 | UPDATE messy_indian_dataset SET purchase_amount = 1300 where id = 1; 19 | SELECT * FROM messy_indian_dataset; 20 | COMMIT; 21 | SELECT * FROM messy_indian_dataset; 22 | 23 | -- Commit & Rollback 24 | START TRANSACTION; 25 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) 26 | VALUES (11, 'Mahesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'); 27 | SELECT * FROM messy_indian_dataset where id = 11; 28 | SELECT * FROM messy_indian_dataset; 29 | COMMIT; 30 | ROLLBACK; 31 | SELECT * FROM messy_indian_dataset; 32 | 33 | -- Commit & Rollback with multiple commands 34 | 35 | START TRANSACTION; 36 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) 37 | VALUES (12, 'Prakash', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'); 38 | 39 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) 40 | VALUES (13, 'Aarti', 30, 'Female', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'); 41 | 42 | SELECT * FROM messy_indian_dataset; 43 | COMMIT; 44 | ROLLBACK; 45 | SELECT * FROM messy_indian_dataset; 46 | 47 | -- Rollback with multiple commands 48 | 49 | START TRANSACTION; 50 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) 51 | VALUES (14, 'Prakash', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'); 52 | 53 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) 54 | VALUES (15, 'Aarti', 30, 'Female', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'); 55 | 56 | SELECT * FROM messy_indian_dataset; 57 | ROLLBACK; 58 | SELECT * FROM messy_indian_dataset; 59 | 60 | -------------------------------------------------------------------------------- /26. Errors & Exceptions/Errors & Exceptions.sql: -------------------------------------------------------------------------------- 1 | use swiggy; 2 | 3 | -- 1. Syntax Errors 4 | select * form restaurants; 5 | 6 | -- 2. Logical Exception 7 | select * from restaurants where rating > 4.0 and rating < 2.0; 8 | 9 | -- 3.Data Type Exception 10 | select * from restaurants where rating = 'high'; 11 | 12 | -- 4. Performance Exception 13 | select * from restaurants where rating = 4.5; 14 | 15 | -- 5. Aggregate Function Errors 16 | select city, count(*) from restaurants; 17 | 18 | -- 6. Subquery Exception 19 | select * from restaurants where id = (select id from restaurants where city = 'NonExistedCity'); 20 | 21 | -- 7. Constraint Errors 22 | INSERT into restaurants (id, name, city, rating) 23 | values (301,'Invalid','Mumbai',6) 24 | 25 | -- Function Errors 26 | -- Indexing Errors 27 | -- Transaction Errors 28 | -- Permission and Access Errors 29 | -- Temporary Table Errors 30 | -- Data Import/Export Errors -------------------------------------------------------------------------------- /26. Errors & Exceptions/Exception Handling.sql: -------------------------------------------------------------------------------- 1 | use swiggy; 2 | 3 | -- 1. If table doesn't exists exception handling 4 | delimiter // 5 | drop procedure if exists handle_non_existant_table; 6 | create procedure handle_non_existant_table() 7 | begin 8 | declare continue handler for SQLEXCEPTION 9 | begin 10 | select 'The table "non_existant_table" does not exists' as message; 11 | end; 12 | select * from non_existant_table; 13 | end // 14 | delimiter ; 15 | 16 | call handle_non_existant_table(); 17 | 18 | -- 2. 19 | 20 | delimiter // 21 | drop procedure if exists handle_unique_violation; 22 | create procedure handle_unique_violation() 23 | begin 24 | declare continue handler for SQLEXCEPTION 25 | begin 26 | select 'Duplicate restaurant ID or name Found' as message; 27 | end; 28 | insert into restaurants(ids, name, city, rating, rating_count, cuisine, cost, link) 29 | values (211, 'Tandoori Hut','Banglore', 4.3, 100, 'Biryani', 300, 'https://www.swiggy.com/rest/tandoor_hut'); 30 | end // 31 | delimiter ; 32 | 33 | call handle_unique_violation(); -------------------------------------------------------------------------------- /27. Introduction to Data Cleaning/01. dataset creation.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS messy_indian_dataset; 2 | CREATE TABLE IF NOT EXISTS messy_indian_dataset ( 3 | id INT PRIMARY KEY, 4 | name VARCHAR(50), 5 | age INT, 6 | gender VARCHAR(10), 7 | email VARCHAR(100), 8 | phone_number VARCHAR(15), 9 | city VARCHAR(50), 10 | state VARCHAR(50), 11 | purchase_amount DECIMAL(10, 2), 12 | purchase_date DATE 13 | ); 14 | 15 | -- Insert messy data into the table for Indian users 16 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) VALUES 17 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 18 | (2, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', NULL, '2023-02-15'), 19 | (3, 'Amit Kumar', 35, 'Male', 'amit@example.com', '9876543212', 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 20 | (4, 'Ritu Singh', 28, 'Female', NULL, '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 21 | (5, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 22 | (6, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', 800.00, '2023-02-15'), 23 | (7, 'Amit Kumar', NULL, 'Male', 'amit@example.com', NULL, 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 24 | (8, 'Ritu Singh', 28, 'Female', 'ritu@example.com', '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 25 | (9, 'Ankit Tiwari', 32, 'Male', 'ankit@example.com', '9876543214', 'Lucknow', 'Uttar Pradesh', 900.00, '2023-05-20'), 26 | (10, 'Swati Gupta', 27, 'Female', 'swati@example.com', '9876543215', 'Jaipur', 'Rajasthan', 1500.00, NULL), 27 | (11, 'Deepak Sharma', 45, 'Male', 'deepak@example.com', '9876543216', 'Chennai', 'Tamil Nadu', 1100.50, '2023-06-15'), 28 | (12, 'Anjali Gupta', 29, 'Female', 'anjali@example.com', '9876543217', 'Hyderabad', 'Telangana', 850.25, '2023-07-25'), 29 | (13, 'Rohit Singh', 33, 'Male', 'rohit@example.com', '9876543218', 'Pune', 'Maharashtra', 950.75, '2023-08-10'), 30 | (14, 'Pooja Patel', 27, 'Female', 'pooja@example.com', '9876543219', 'Ahmedabad', 'Gujarat', 1300.00, '2023-09-20'), 31 | (15, 'Sandeep Verma', 31, 'Male', 'sandeep@example.com', '9876543220', 'Surat', 'Gujarat', 1150.50, '2023-10-05'), 32 | (16, 'Aarti Sharma', 26, 'Female', 'aarti@example.org', '9876543221', 'Varanasi', 'Uttar Pradesh', 850.00, '2023-11-15'), 33 | (17, 'Vikram Singh', 37, 'Male', 'vikram@example.co.in', '9876543222', 'Indore', 'Madhya Pradesh', 1250.25, '2023-12-25'), 34 | (18, 'Ananya Sen', 28, 'Female', 'ananya@example.net', '9876543223', 'Kochi', 'Kerala', 900.75, '2024-01-10'), 35 | (19, 'Nikhil Sharma', 34, 'Male', 'nikhil@example.co.uk', '9876543224', 'Jaipur', 'Rajasthan', 1400.00, '2024-02-20'), 36 | (20, 'Isha Singh', 30, 'Female', 'isha@example.edu.in', '9876543225', 'Lucknow', 'Uttar Pradesh', 1050.50, '2024-03-05'), 37 | (21, 'Alok Kumar', NULL, 'Male', 'alok@example.com', '9876543226', 'Bhopal', 'Madhya Pradesh', 950.25, '2024-04-15'), 38 | (22, 'Shreya Patel', 25, NULL, 'shreya@example.com', '9876543227', 'Vadodara', 'Gujarat', 1350.00, '2024-05-25'), 39 | (23, 'Rahul Verma', 33, 'Male', NULL, '9876543228', 'Nagpur', 'Maharashtra', 1000.75, '2024-06-10'), 40 | (24, 'Anushka Singh', 28, 'Female', 'anushka@example.com', NULL, 'Gurgaon', 'Haryana', NULL, '2024-07-20'), 41 | (25, 'Ravi Kumar', 32, 'Male', 'ravi@example.com', '9876543229', NULL, 'Uttar Pradesh', 1100.50, '2024-08-05'), 42 | (26, 'Aarav Gupta', 27, 'Male', 'aarav@example.com', '9876543230', 'Patna', NULL, 1250.25, '2024-09-15'), 43 | (27, 'Kritika Sharma', 29, 'Female', 'kritika@example.com', '9876543231', 'Chandigarh', 'Punjab', 1200.75, NULL), 44 | (28, NULL, 35, 'Male', 'test@example.com', '9876543232', 'Jaipur', 'Rajasthan', 1300.00, '2024-11-20'), 45 | (29, 'Surbhi Gupta', 26, NULL, 'surbhi@example.com', '9876543233', 'Nashik', 'Maharashtra', 1150.50, '2024-12-05'), 46 | (30, 'Ajay Sharma', NULL, NULL, 'ajay@example.com', '9876543234', 'Jodhpur', 'Rajasthan', 1400.25, '2025-01-15'); 47 | select * from messy_indian_dataset; 48 | -------------------------------------------------------------------------------- /27. Introduction to Data Cleaning/02. Introduction to Data Cleaning.sql: -------------------------------------------------------------------------------- 1 | select * from messy_indian_dataset; 2 | 3 | -- Handling Missing Values 4 | 5 | -- Removing Duplicates 6 | 7 | -- Converting Data Types 8 | 9 | -- Correcting Inconsistent Data | misspelled , inconsistent capitalization, or inconsistent date formats. 10 | 11 | -- Validating Data Integrity |Validate the integrity of data 12 | 13 | -- Handling Outliers 14 | 15 | -- Parsing and Extracting Information | Email extraction 16 | 17 | -- Data Transformation | Scaling, Normalization, or Encoding Categorical Variables -------------------------------------------------------------------------------- /28. Handling Missing Values/01. dataset creation.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS messy_indian_dataset; 2 | CREATE TABLE IF NOT EXISTS messy_indian_dataset ( 3 | id INT PRIMARY KEY, 4 | name VARCHAR(50), 5 | age INT, 6 | gender VARCHAR(10), 7 | email VARCHAR(100), 8 | phone_number VARCHAR(15), 9 | city VARCHAR(50), 10 | state VARCHAR(50), 11 | purchase_amount DECIMAL(10, 2), 12 | purchase_date DATE 13 | ); 14 | 15 | -- Insert messy data into the table for Indian users 16 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) VALUES 17 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 18 | (2, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', NULL, '2023-02-15'), 19 | (3, 'Amit Kumar', 35, 'Male', 'amit@example.com', '9876543212', 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 20 | (4, 'Ritu Singh', 28, 'Female', NULL, '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 21 | (5, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 22 | (6, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', 800.00, '2023-02-15'), 23 | (7, 'Amit Kumar', NULL, 'Male', 'amit@example.com', NULL, 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 24 | (8, 'Ritu Singh', 28, 'Female', 'ritu@example.com', '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 25 | (9, 'Ankit Tiwari', 32, 'Male', 'ankit@example.com', '9876543214', 'Lucknow', 'Uttar Pradesh', 900.00, '2023-05-20'), 26 | (10, 'Swati Gupta', 27, 'Female', 'swati@example.com', '9876543215', 'Jaipur', 'Rajasthan', 1500.00, NULL), 27 | (11, 'Deepak Sharma', 45, 'Male', 'deepak@example.com', '9876543216', 'Chennai', 'Tamil Nadu', 1100.50, '2023-06-15'), 28 | (12, 'Anjali Gupta', 29, 'Female', 'anjali@example.com', '9876543217', 'Hyderabad', 'Telangana', 850.25, '2023-07-25'), 29 | (13, 'Rohit Singh', 33, 'Male', 'rohit@example.com', '9876543218', 'Pune', 'Maharashtra', 950.75, '2023-08-10'), 30 | (14, 'Pooja Patel', 27, 'Female', 'pooja@example.com', '9876543219', 'Ahmedabad', 'Gujarat', 1300.00, '2023-09-20'), 31 | (15, 'Sandeep Verma', 31, 'Male', 'sandeep@example.com', '9876543220', 'Surat', 'Gujarat', 1150.50, '2023-10-05'), 32 | (16, 'Aarti Sharma', 26, 'Female', 'aarti@example.org', '9876543221', 'Varanasi', 'Uttar Pradesh', 850.00, '2023-11-15'), 33 | (17, 'Vikram Singh', 37, 'Male', 'vikram@example.co.in', '9876543222', 'Indore', 'Madhya Pradesh', 1250.25, '2023-12-25'), 34 | (18, 'Ananya Sen', 28, 'Female', 'ananya@example.net', '9876543223', 'Kochi', 'Kerala', 900.75, '2024-01-10'), 35 | (19, 'Nikhil Sharma', 34, 'Male', 'nikhil@example.co.uk', '9876543224', 'Jaipur', 'Rajasthan', 1400.00, '2024-02-20'), 36 | (20, 'Isha Singh', 30, 'Female', 'isha@example.edu.in', '9876543225', 'Lucknow', 'Uttar Pradesh', 1050.50, '2024-03-05'), 37 | (21, 'Alok Kumar', NULL, 'Male', 'alok@example.com', '9876543226', 'Bhopal', 'Madhya Pradesh', 950.25, '2024-04-15'), 38 | (22, 'Shreya Patel', 25, NULL, 'shreya@example.com', '9876543227', 'Vadodara', 'Gujarat', 1350.00, '2024-05-25'), 39 | (23, 'Rahul Verma', 33, 'Male', NULL, '9876543228', 'Nagpur', 'Maharashtra', 1000.75, '2024-06-10'), 40 | (24, 'Anushka Singh', 28, 'Female', 'anushka@example.com', NULL, 'Gurgaon', 'Haryana', NULL, '2024-07-20'), 41 | (25, 'Ravi Kumar', 32, 'Male', 'ravi@example.com', '9876543229', NULL, 'Uttar Pradesh', 1100.50, '2024-08-05'), 42 | (26, 'Aarav Gupta', 27, 'Male', 'aarav@example.com', '9876543230', 'Patna', NULL, 1250.25, '2024-09-15'), 43 | (27, 'Kritika Sharma', 29, 'Female', 'kritika@example.com', '9876543231', 'Chandigarh', 'Punjab', 1200.75, NULL), 44 | (28, NULL, 35, 'Male', 'test@example.com', '9876543232', 'Jaipur', 'Rajasthan', 1300.00, '2024-11-20'), 45 | (29, 'Surbhi Gupta', 26, NULL, 'surbhi@example.com', '9876543233', 'Nashik', 'Maharashtra', 1150.50, '2024-12-05'), 46 | (30, 'Ajay Sharma', NULL, NULL, 'ajay@example.com', '9876543234', 'Jodhpur', 'Rajasthan', 1400.25, '2025-01-15'); 47 | select * from messy_indian_dataset; 48 | -------------------------------------------------------------------------------- /28. Handling Missing Values/02. Handling Missing Values.sql: -------------------------------------------------------------------------------- 1 | select * from messy_indian_dataset; 2 | 3 | -- Find rows with missing values in any column 4 | select * from messy_indian_dataset 5 | where name is null or age is null or gender is null or email is null or city is null or 6 | phone_number is null or state is null or purchase_amount is null or purchase_date is null; 7 | 8 | -- Find rows without missing values 9 | select * from messy_indian_dataset 10 | where name is not null and age is not null and gender is not null and email is not null and city is not null and 11 | phone_number is not null and state is not null and purchase_amount is not null and purchase_date is not null; 12 | 13 | -- Saving table without null values 14 | create table if not exists clean_table as 15 | select * from messy_indian_dataset 16 | where name is not null and age is not null and gender is not null and email is not null and city is not null and 17 | phone_number is not null and state is not null and purchase_amount is not null and purchase_date is not null; 18 | select * from clean_table; 19 | 20 | -- Filling missing age all with specific values 21 | update messy_indian_dataset set age = coalesce(age,0); 22 | select * from messy_indian_dataset; 23 | 24 | -- Filling nulls with specific values 25 | update messy_indian_dataset 26 | set 27 | name = coalesce(name, 'Unknown'), 28 | gender = coalesce(gender, 'Unknown'), 29 | email = coalesce(email, 'Unknown'), 30 | phone_number = coalesce(phone_number, 'Unknown'), 31 | state = coalesce(state, 'Unknown'), 32 | purchase_date = coalesce(purchase_date, '2023-01-01'); 33 | select * from messy_indian_dataset; 34 | 35 | -- Filling null amount with average amount values 36 | 37 | update messy_indian_dataset 38 | set purchase_amount = ( 39 | select avg_purchase_amount from 40 | ( 41 | select avg(purchase_amount) as avg_purchase_amount from messy_indian_dataset 42 | ) 43 | as subquery 44 | ) where purchase_amount is null; 45 | 46 | select * from messy_indian_dataset; 47 | 48 | -- Filling null city with most frequent city 49 | 50 | update messy_indian_dataset 51 | set city = ( 52 | select most_frequent_city from 53 | ( 54 | select city as most_frequent_city , count(*) as 'frequecy' 55 | from messy_indian_dataset 56 | where city is not null 57 | group by city 58 | order by count(*) desc 59 | limit 1 60 | ) as subquery 61 | ) where city is null; 62 | 63 | select * from messy_indian_dataset; -------------------------------------------------------------------------------- /29. Handling Duplicate Values/01. dataset creation.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS messy_indian_dataset; 2 | CREATE TABLE IF NOT EXISTS messy_indian_dataset ( 3 | id INT, 4 | name VARCHAR(50), 5 | age INT, 6 | gender VARCHAR(10), 7 | email VARCHAR(100), 8 | phone_number VARCHAR(15), 9 | city VARCHAR(50), 10 | state VARCHAR(50), 11 | purchase_amount DECIMAL(10, 2), 12 | purchase_date DATE 13 | ); 14 | 15 | -- Insert messy data into the table for Indian users 16 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) VALUES 17 | (1, 'Rajesh Patil', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 18 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 19 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 20 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 21 | (2, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', NULL, '2023-02-15'), 22 | (3, 'Amit Kumar', 35, 'Male', 'amit@example.com', '9876543212', 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 23 | (4, 'Ritu Singh', 28, 'Female', NULL, '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 24 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 25 | (6, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', 800.00, '2023-02-15'), 26 | (7, 'Amit Kumar', NULL, 'Male', 'amit@example.com', NULL, 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 27 | (8, 'Ritu Singh', 28, 'Female', 'ritu@example.com', '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 28 | (1, 'Ankit Tiwari', 32, 'Male', 'ankit@example.com', '9876543214', 'Lucknow', 'Uttar Pradesh', 900.00, '2023-05-20'), 29 | (11, 'Swati Gupta', 27, 'Female', 'swati@example.com', '9876543215', 'Jaipur', 'Rajasthan', 1500.00, NULL), 30 | (11, 'Deepak Sharma', 45, 'Male', 'deepak@example.com', '9876543216', 'Chennai', 'Tamil Nadu', 1100.50, '2023-06-15'), 31 | (12, 'Anjali Gupta', 29, 'Female', 'anjali@example.com', '9876543217', 'Hyderabad', 'Telangana', 850.25, '2023-07-25'), 32 | (13, 'Rohit Singh', 33, 'Male', 'rohit@example.com', '9876543218', 'Pune', 'Maharashtra', 950.75, '2023-08-10'), 33 | (1, 'Pooja Patel', 27, 'Female', 'pooja@example.com', '9876543219', 'Ahmedabad', 'Gujarat', 1300.00, '2023-09-20'), 34 | (15, 'Sandeep Verma', 31, 'Male', 'sandeep@example.com', '9876543220', 'Surat', 'Gujarat', 1150.50, '2023-10-05'), 35 | (16, 'Aarti Sharma', 26, 'Female', 'aarti@example.org', '9876543221', 'Varanasi', 'Uttar Pradesh', 850.00, '2023-11-15'), 36 | (1, 'Vikram Singh', 37, 'Male', 'vikram@example.co.in', '9876543222', 'Indore', 'Madhya Pradesh', 1250.25, '2023-12-25'), 37 | (18, 'Ananya Sen', 28, 'Female', 'ananya@example.net', '9876543223', 'Kochi', 'Kerala', 900.75, '2024-01-10'), 38 | (19, 'Nikhil Sharma', 34, 'Male', 'nikhil@example.co.uk', '9876543224', 'Jaipur', 'Rajasthan', 1400.00, '2024-02-20'), 39 | (20, 'Isha Singh', 30, 'Female', 'isha@example.edu.in', '9876543225', 'Lucknow', 'Uttar Pradesh', 1050.50, '2024-03-05'), 40 | (2, 'Alok Kumar', NULL, 'Male', 'alok@example.com', '9876543226', 'Bhopal', 'Madhya Pradesh', 950.25, '2024-04-15'), 41 | (22, 'Shreya Patel', 25, NULL, 'shreya@example.com', '9876543227', 'Vadodara', 'Gujarat', 1350.00, '2024-05-25'), 42 | (23, 'Rahul Verma', 33, 'Male', NULL, '9876543228', 'Nagpur', 'Maharashtra', 1000.75, '2024-06-10'), 43 | (2, 'Anushka Singh', 28, 'Female', 'anushka@example.com', NULL, 'Gurgaon', 'Haryana', NULL, '2024-07-20'), 44 | (25, 'Ravi Kumar', 32, 'Male', 'ravi@example.com', '9876543229', NULL, 'Uttar Pradesh', 1100.50, '2024-08-05'), 45 | (26, 'Aarav Gupta', 27, 'Male', 'aarav@example.com', '9876543230', 'Patna', NULL, 1250.25, '2024-09-15'), 46 | (2, 'Kritika Sharma', 29, 'Female', 'kritika@example.com', '9876543231', 'Chandigarh', 'Punjab', 1200.75, NULL), 47 | (2, NULL, 35, 'Male', 'test@example.com', '9876543232', 'Jaipur', 'Rajasthan', 1300.00, '2024-11-20'), 48 | (2, 'Surbhi Gupta', 26, NULL, 'surbhi@example.com', '9876543233', 'Nashik', 'Maharashtra', 1150.50, '2024-12-05'), 49 | (30, 'Ajay Sharma', NULL, NULL, 'ajay@example.com', '9876543234', 'Jodhpur', 'Rajasthan', 1400.25, '2025-01-15'); 50 | select * from messy_indian_dataset; 51 | -------------------------------------------------------------------------------- /29. Handling Duplicate Values/02. handling duplicate values.sql: -------------------------------------------------------------------------------- 1 | select * from messy_indian_dataset; 2 | 3 | -- Finding only unique rows 4 | select distinct * from messy_indian_dataset; 5 | 6 | -- Finding unique values based on ID 7 | select id 8 | from messy_indian_dataset 9 | group by id 10 | order by id; 11 | 12 | -- Finding unique values based on name 13 | select name 14 | from messy_indian_dataset 15 | group by name 16 | order by name; 17 | 18 | -- Finding unique values based on id using rank 19 | select id, name, age, gender,email, phone_number , city, state, purchase_amount, purchase_date from ( 20 | select *, row_number() over(partition by id order by id) as 'rank' from messy_indian_dataset 21 | ) as subtable 22 | where subtable.rank = 1; 23 | 24 | -- Finding unique values based on multiple columns | Rajesh Patel & Patil 25 | select id, name, age, gender,email, phone_number , city, state, purchase_amount, purchase_date from ( 26 | select *, row_number() over(partition by id,name order by id) as 'rank' from messy_indian_dataset 27 | ) as subtable 28 | where subtable.rank = 1; 29 | 30 | -------------------------------------------------------------------------------- /30. Handling Outliers/01. dataset creation.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS messy_indian_dataset; 2 | CREATE TABLE IF NOT EXISTS messy_indian_dataset ( 3 | id INT, 4 | name VARCHAR(50), 5 | age INT, 6 | gender VARCHAR(10), 7 | email VARCHAR(100), 8 | phone_number VARCHAR(15), 9 | city VARCHAR(50), 10 | state VARCHAR(50), 11 | purchase_amount DECIMAL(10, 2), 12 | purchase_date DATE 13 | ); 14 | 15 | -- Insert messy data into the table for Indian users 16 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) VALUES 17 | (1, 'Rajesh Patil', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 2500.50, '2023-01-05'), 18 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 100.50, '2023-01-05'), 19 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 20 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 21 | (2, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', NULL, '2023-02-15'), 22 | (3, 'Amit Kumar', 35, 'Male', 'amit@example.com', '9876543212', 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 23 | (4, 'Ritu Singh', 28, 'Female', NULL, '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 24 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 25 | (6, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', 800.00, '2023-02-15'), 26 | (7, 'Amit Kumar', NULL, 'Male', 'amit@example.com', NULL, 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 27 | (8, 'Ritu Singh', 28, 'Female', 'ritu@example.com', '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 28 | (1, 'Ankit Tiwari', 32, 'Male', 'ankit@example.com', '9876543214', 'Lucknow', 'Uttar Pradesh', 900.00, '2023-05-20'), 29 | (11, 'Swati Gupta', 27, 'Female', 'swati@example.com', '9876543215', 'Jaipur', 'Rajasthan', 1500.00, NULL), 30 | (11, 'Deepak Sharma', 45, 'Male', 'deepak@example.com', '9876543216', 'Chennai', 'Tamil Nadu', 1100.50, '2023-06-15'), 31 | (12, 'Anjali Gupta', 29, 'Female', 'anjali@example.com', '9876543217', 'Hyderabad', 'Telangana', 850.25, '2023-07-25'), 32 | (13, 'Rohit Singh', 33, 'Male', 'rohit@example.com', '9876543218', 'Pune', 'Maharashtra', 950.75, '2023-08-10'), 33 | (1, 'Pooja Patel', 27, 'Female', 'pooja@example.com', '9876543219', 'Ahmedabad', 'Gujarat', 1300.00, '2023-09-20'), 34 | (15, 'Sandeep Verma', 31, 'Male', 'sandeep@example.com', '9876543220', 'Surat', 'Gujarat', 1150.50, '2023-10-05'), 35 | (16, 'Aarti Sharma', 26, 'Female', 'aarti@example.org', '9876543221', 'Varanasi', 'Uttar Pradesh', 850.00, '2023-11-15'), 36 | (1, 'Vikram Singh', 37, 'Male', 'vikram@example.co.in', '9876543222', 'Indore', 'Madhya Pradesh', 1250.25, '2023-12-25'), 37 | (18, 'Ananya Sen', 28, 'Female', 'ananya@example.net', '9876543223', 'Kochi', 'Kerala', 900.75, '2024-01-10'), 38 | (19, 'Nikhil Sharma', 34, 'Male', 'nikhil@example.co.uk', '9876543224', 'Jaipur', 'Rajasthan', 1400.00, '2024-02-20'), 39 | (20, 'Isha Singh', 30, 'Female', 'isha@example.edu.in', '9876543225', 'Lucknow', 'Uttar Pradesh', 1050.50, '2024-03-05'), 40 | (2, 'Alok Kumar', NULL, 'Male', 'alok@example.com', '9876543226', 'Bhopal', 'Madhya Pradesh', 950.25, '2024-04-15'), 41 | (22, 'Shreya Patel', 25, NULL, 'shreya@example.com', '9876543227', 'Vadodara', 'Gujarat', 1350.00, '2024-05-25'), 42 | (23, 'Rahul Verma', 33, 'Male', NULL, '9876543228', 'Nagpur', 'Maharashtra', 1000.75, '2024-06-10'), 43 | (2, 'Anushka Singh', 28, 'Female', 'anushka@example.com', NULL, 'Gurgaon', 'Haryana', NULL, '2024-07-20'), 44 | (25, 'Ravi Kumar', 32, 'Male', 'ravi@example.com', '9876543229', NULL, 'Uttar Pradesh', 1100.50, '2024-08-05'), 45 | (26, 'Aarav Gupta', 27, 'Male', 'aarav@example.com', '9876543230', 'Patna', NULL, 1250.25, '2024-09-15'), 46 | (2, 'Kritika Sharma', 29, 'Female', 'kritika@example.com', '9876543231', 'Chandigarh', 'Punjab', 1200.75, NULL), 47 | (2, NULL, 35, 'Male', 'test@example.com', '9876543232', 'Jaipur', 'Rajasthan', 1300.00, '2024-11-20'), 48 | (2, 'Surbhi Gupta', 26, NULL, 'surbhi@example.com', '9876543233', 'Nashik', 'Maharashtra', 1150.50, '2024-12-05'), 49 | (30, 'Ajay Sharma', NULL, NULL, 'ajay@example.com', '9876543234', 'Jodhpur', 'Rajasthan', 1400.25, '2025-01-15'); 50 | select * from messy_indian_dataset; 51 | -------------------------------------------------------------------------------- /30. Handling Outliers/02. handling outliers.sql: -------------------------------------------------------------------------------- 1 | select * from messy_indian_dataset; 2 | 3 | -- Finding outliers based on Z-Score 4 | select *, 5 | ABS(purchase_amount - avg(purchase_amount) over()) / STDDEV(purchase_amount) OVER() as 'z_score' 6 | from messy_indian_dataset; 7 | 8 | -- Remove outliers based on specific Z-Score 9 | select * from 10 | ( 11 | select *, 12 | ABS(purchase_amount - avg(purchase_amount) over()) / STDDEV(purchase_amount) OVER() as 'z_score' 13 | from messy_indian_dataset 14 | ) as sub_table where sub_table.z_score < 3; 15 | -------------------------------------------------------------------------------- /31. Working with Dates/01. dataset creation.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS messy_indian_dataset; 2 | CREATE TABLE IF NOT EXISTS messy_indian_dataset ( 3 | id INT, 4 | name VARCHAR(50), 5 | age INT, 6 | gender VARCHAR(10), 7 | email VARCHAR(100), 8 | phone_number VARCHAR(15), 9 | city VARCHAR(50), 10 | state VARCHAR(50), 11 | purchase_amount DECIMAL(10, 2), 12 | purchase_date DATE 13 | ); 14 | 15 | -- Insert messy data into the table for Indian users 16 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) VALUES 17 | (1, 'Rajesh Patil', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 2500.50, '2023-01-05'), 18 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 100.50, '2023-01-05'), 19 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 20 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 21 | (2, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', NULL, '2023-02-15'), 22 | (3, 'Amit Kumar', 35, 'Male', 'amit@example.com', '9876543212', 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 23 | (4, 'Ritu Singh', 28, 'Female', NULL, '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 24 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 25 | (6, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', 800.00, '2023-02-15'), 26 | (7, 'Amit Kumar', NULL, 'Male', 'amit@example.com', NULL, 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 27 | (8, 'Ritu Singh', 28, 'Female', 'ritu@example.com', '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 28 | (1, 'Ankit Tiwari', 32, 'Male', 'ankit@example.com', '9876543214', 'Lucknow', 'Uttar Pradesh', 900.00, '2023-05-20'), 29 | (11, 'Swati Gupta', 27, 'Female', 'swati@example.com', '9876543215', 'Jaipur', 'Rajasthan', 1500.00, NULL), 30 | (11, 'Deepak Sharma', 45, 'Male', 'deepak@example.com', '9876543216', 'Chennai', 'Tamil Nadu', 1100.50, '2023-06-15'), 31 | (12, 'Anjali Gupta', 29, 'Female', 'anjali@example.com', '9876543217', 'Hyderabad', 'Telangana', 850.25, '2023-07-25'), 32 | (13, 'Rohit Singh', 33, 'Male', 'rohit@example.com', '9876543218', 'Pune', 'Maharashtra', 950.75, '2023-08-10'), 33 | (1, 'Pooja Patel', 27, 'Female', 'pooja@example.com', '9876543219', 'Ahmedabad', 'Gujarat', 1300.00, '2023-09-20'), 34 | (15, 'Sandeep Verma', 31, 'Male', 'sandeep@example.com', '9876543220', 'Surat', 'Gujarat', 1150.50, '2023-10-05'), 35 | (16, 'Aarti Sharma', 26, 'Female', 'aarti@example.org', '9876543221', 'Varanasi', 'Uttar Pradesh', 850.00, '2023-11-15'), 36 | (1, 'Vikram Singh', 37, 'Male', 'vikram@example.co.in', '9876543222', 'Indore', 'Madhya Pradesh', 1250.25, '2023-12-25'), 37 | (18, 'Ananya Sen', 28, 'Female', 'ananya@example.net', '9876543223', 'Kochi', 'Kerala', 900.75, '2024-01-10'), 38 | (19, 'Nikhil Sharma', 34, 'Male', 'nikhil@example.co.uk', '9876543224', 'Jaipur', 'Rajasthan', 1400.00, '2024-02-20'), 39 | (20, 'Isha Singh', 30, 'Female', 'isha@example.edu.in', '9876543225', 'Lucknow', 'Uttar Pradesh', 1050.50, '2024-03-05'), 40 | (2, 'Alok Kumar', NULL, 'Male', 'alok@example.com', '9876543226', 'Bhopal', 'Madhya Pradesh', 950.25, '2024-04-15'), 41 | (22, 'Shreya Patel', 25, NULL, 'shreya@example.com', '9876543227', 'Vadodara', 'Gujarat', 1350.00, '2024-05-25'), 42 | (23, 'Rahul Verma', 33, 'Male', NULL, '9876543228', 'Nagpur', 'Maharashtra', 1000.75, '2024-06-10'), 43 | (2, 'Anushka Singh', 28, 'Female', 'anushka@example.com', NULL, 'Gurgaon', 'Haryana', NULL, '2024-07-20'), 44 | (25, 'Ravi Kumar', 32, 'Male', 'ravi@example.com', '9876543229', NULL, 'Uttar Pradesh', 1100.50, '2024-08-05'), 45 | (26, 'Aarav Gupta', 27, 'Male', 'aarav@example.com', '9876543230', 'Patna', NULL, 1250.25, '2024-09-15'), 46 | (2, 'Kritika Sharma', 29, 'Female', 'kritika@example.com', '9876543231', 'Chandigarh', 'Punjab', 1200.75, NULL), 47 | (2, NULL, 35, 'Male', 'test@example.com', '9876543232', 'Jaipur', 'Rajasthan', 1300.00, '2024-11-20'), 48 | (2, 'Surbhi Gupta', 26, NULL, 'surbhi@example.com', '9876543233', 'Nashik', 'Maharashtra', 1150.50, '2024-12-05'), 49 | (30, 'Ajay Sharma', NULL, NULL, 'ajay@example.com', '9876543234', 'Jodhpur', 'Rajasthan', 1400.25, '2025-01-15'); 50 | select * from messy_indian_dataset; 51 | -------------------------------------------------------------------------------- /31. Working with Dates/02. working with dates.sql: -------------------------------------------------------------------------------- 1 | select * from messy_indian_dataset; 2 | 3 | -- Add new columns for day, month, and year 4 | alter table messy_indian_dataset 5 | add column day int, 6 | add column month int, 7 | add column year int; 8 | select * from messy_indian_dataset; 9 | 10 | -- Update the new columns with the extracted day, month, and year values 11 | update messy_indian_dataset 12 | set day = DAY(purchase_date), 13 | month = MONTH(purchase_date), 14 | year = YEAR(purchase_date); 15 | select * from messy_indian_dataset; 16 | 17 | -- Add a new column for the day of the week & save day name there 18 | alter table messy_indian_dataset 19 | add column day_of_week varchar(10); 20 | 21 | update messy_indian_dataset 22 | set day_of_week = DAYNAME(purchase_date); 23 | select * from messy_indian_dataset; 24 | 25 | 26 | -- Add a new column for the name of the month & save day name there 27 | alter table messy_indian_dataset 28 | add column month_name varchar(10); 29 | 30 | update messy_indian_dataset 31 | set month_name = MONTHNAME(purchase_date); 32 | select * from messy_indian_dataset; 33 | -------------------------------------------------------------------------------- /32. Data Cleaning/01. dataset creation.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS messy_indian_dataset; 2 | CREATE TABLE IF NOT EXISTS messy_indian_dataset ( 3 | id INT, 4 | name VARCHAR(50), 5 | age INT, 6 | gender VARCHAR(10), 7 | email VARCHAR(100), 8 | phone_number VARCHAR(15), 9 | city VARCHAR(50), 10 | state VARCHAR(50), 11 | purchase_amount DECIMAL(10, 2), 12 | purchase_date DATE 13 | ); 14 | 15 | -- Insert messy data into the table for Indian users 16 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) VALUES 17 | (1, 'Rajesh patil', 30, 'M', 'raje#$#sh@example.com', '9876a43210', ' Mumbai ', ' Maharashtra ', 2500.50, '2023-01-05'), 18 | (1, 'rajesh patel', 30, ' m ', 'raje$#@sh@example.com', '987a543210', ' Mumbai', ' Maharashtra ', 100.50, '2023-01-05'), 19 | (1, ' Rajesh Patel ', 30, 'Male', 'raje(*&(sh@example.com', '98a6543210', 'Mumbai', ' Maharashtra ', 1000.50, '2023-01-05'), 20 | (1, ' Rajesh Patel ', 30, 'Male', 'rajesh@example.com', '98x6543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 21 | (2, 'Priya Sharma', 25, 'F', 'priya@example.com', '98#6543211', ' Delhi', 'Delhi', NULL, '2023-02-15'), 22 | (3, 'Amit Kumar', 110, ' M ', 'amit@example.com', '9876543212', 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 23 | (4, 'Ritu Singh', -5, 'f', NULL, '9876543213', ' Kolkata ', 'West Bengal', 1200.75, '2023-04-10'), 24 | (1, ' Rajesh Patel ', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 25 | (6, 'Priya Sharma', 25, ' female', 'priya@example.com', '9876543211', 'Delhi', ' Delhi ', 800.00, '2023-02-15'), 26 | (7, ' Amit Kumar ', NULL, 'Male', 'amit@example.com', NULL, ' Bangalore ', 'Karnataka', 750.25, '2023-03-25'), 27 | (8, ' Ritu Singh ', 28, 'Female', 'ritu@example.com', '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 28 | (1, 'Ankit Tiwari', 32, ' Male ', 'ankit@example.com', '9876543214', ' Lucknow', 'Uttar Pradesh', 900.00, '2023-05-20'), 29 | (11, 'Swati Gupta', 27, ' Female', 'swati@example.com', '9876543215', ' Jaipur', 'Rajasthan', 1500.00, NULL), 30 | (11, ' Deepak Sharma ', 45, ' Male', 'deepak@example.com', '9876543216', 'Chennai', ' Tamil Nadu ', 1100.50, '2023-06-15'), 31 | (12, 'Anjali Gupta', 29, ' Female ', 'anjali@example.com', '9876543217', ' Hyderabad', 'Telangana', 850.25, '2023-07-25'), 32 | (13, 'Rohit Singh', 33, 'Male', 'rohit@example.com', '9876543218', ' Pune ', ' Maharashtra ', 950.75, '2023-08-10'), 33 | (1, ' Pooja Patel ', 27, 'Female', 'pooja@example.com', '9876543219', ' Ahmedabad', 'Gujarat', 1300.00, '2023-09-20'), 34 | (15, 'Sandeep Verma', 31, 'Male', 'sandeep@example.com', '9876543220', ' Surat ', ' Gujarat', 1150.50, '2023-10-05'), 35 | (16, 'Aarti Sharma', 26, 'Female', 'aarti@example.org', '9876543221', ' Varanasi ', ' Uttar Pradesh', 850.00, '2023-11-15'), 36 | (1, 'Vikram Singh', 37, ' Male', 'vikram@example.co.in', '9876543222', ' Indore', 'Madhya Pradesh', 1250.25, '2023-12-25'), 37 | (18, 'Ananya Sen', 28, 'Female', 'ananya@example.net', '9876543223', ' Kochi ', ' Kerala', 900.75, '2024-01-10'), 38 | (19, 'Nikhil Sharma', 34, 'Male', 'nikhil@example.co.uk', '9876543224', ' Jaipur ', ' Rajasthan', 1400.00, '2024-02-20'), 39 | (20, 'Isha Singh', 30, ' Female', 'isha@example.edu.in', '9876543225', 'Lucknow', 'Uttar Pradesh', 1050.50, '2024-03-05'), 40 | (2, 'Alok Kumar', NULL, ' Male', 'alok@example.com', '9876543226', ' Bhopal', 'Madhya Pradesh', 950.25, '2024-04-15'), 41 | (22, 'Shreya Patel', 25, NULL, 'shreya@example.com', '9876543227', ' Vadodara ', 'Gujarat', 1350.00, '2024-05-25'), 42 | (23, 'Rahul Verma', 33, ' Male ', NULL, '9876543228', ' Nagpur ', ' Maharashtra', 1000.75, '2024-06-10'), 43 | (2, 'Anushka Singh', 28, ' Female', 'anushka@example.com', NULL, ' Gurgaon', 'Haryana', NULL, '2024-07-20'), 44 | (25, 'Ravi Kumar', 32, 'Male', 'ravi@example.com', '9876543229', NULL, 'Uttar Pradesh', 1100.50, '2024-08-05'), 45 | (26, 'Aarav Gupta', 27, ' Male', 'aarav@example.com', '9876543230', 'Patna', NULL, 1250.25, '2024-09-15'), 46 | (2, 'Kritika Sharma', 29, 'Female', 'kritika@example.com', '9876543231', 'Chandigarh', 'Punjab', 1200.75, NULL), 47 | (2, NULL, 35, ' Male', 'test@example.com', '9876543232', 'Jaipur', 'Rajasthan', 1300.00, '2024-11-20'), 48 | (2, 'Surbhi Gupta', 26, NULL, 'surbhi@example.com', '9876543233', 'Nashik', ' Maharashtra ', 1150.50, '2024-12-05'), 49 | (30, 'Ajay Sharma', NULL, NULL, 'ajay@example.com', '9876543234', 'Jodhpur', 'Rajasthan', 1400.25, '2025-01-15'); 50 | 51 | SELECT * FROM messy_indian_dataset; -------------------------------------------------------------------------------- /32. Data Cleaning/02. data cleaning.sql: -------------------------------------------------------------------------------- 1 | select * from messy_indian_dataset; 2 | 3 | -- Update Name 4 | update messy_indian_dataset 5 | set name = trim(name), 6 | name = LOWER(name); 7 | 8 | select * from messy_indian_dataset; 9 | 10 | -- Update Gender 11 | update messy_indian_dataset 12 | set gender = lower(trim(gender)); 13 | 14 | select * from messy_indian_dataset; 15 | 16 | -- Update City 17 | update messy_indian_dataset 18 | set city = lower(trim(city)); 19 | 20 | select * from messy_indian_dataset; 21 | 22 | -- Update State 23 | update messy_indian_dataset 24 | set state = lower(trim(state)); 25 | 26 | select * from messy_indian_dataset; 27 | 28 | -- Clean & Update email 29 | update messy_indian_dataset 30 | set email = trim(REGEXP_REPLACE(lower(email), '[^a-z0-9@.]+','')); 31 | 32 | select * from messy_indian_dataset; 33 | 34 | -- Clean Phone Number 35 | update messy_indian_dataset 36 | set phone_number = REGEXP_REPLACE(phone_number, '[^0-9]+',''); 37 | 38 | select * from messy_indian_dataset; 39 | 40 | -- Extracting username 41 | alter table messy_indian_dataset 42 | add column username varchar(30); 43 | 44 | update messy_indian_dataset 45 | set username = SUBSTRING_INDEX(email, '@',1); 46 | 47 | select * from messy_indian_dataset; 48 | 49 | -- Create location 50 | alter table messy_indian_dataset 51 | add column location varchar(50); 52 | 53 | update messy_indian_dataset 54 | set location = CONCAT(city ,', ', state); 55 | 56 | select * from messy_indian_dataset; 57 | 58 | -- amount_without_gst 59 | alter table messy_indian_dataset 60 | add column without_gst decimal(10,2); 61 | 62 | update messy_indian_dataset 63 | set without_gst = purchase_amount * .82; 64 | 65 | select * from messy_indian_dataset; 66 | 67 | -- Adding Expiry Date 68 | alter table messy_indian_dataset 69 | add column exp_date DATE; 70 | 71 | update messy_indian_dataset 72 | set exp_date = DATE_ADD(purchase_date, INTERVAL 3 Year); 73 | 74 | select * from messy_indian_dataset; 75 | 76 | -- Removing rows with Invalid Phone Number 77 | delete from messy_indian_dataset 78 | where length(phone_number) != 10 OR phone_number REGEXP '[^0-9]'; 79 | 80 | select * from messy_indian_dataset; 81 | 82 | -- Cleaning Gender even further 83 | update messy_indian_dataset 84 | set gender = case 85 | when gender in ('M' ,'m', 'Male', 'MALE','male') then 'M' 86 | when gender in ('F' ,'f', 'Female', 'FEMALE','female') then 'F' 87 | else 'other' 88 | end; 89 | select * from messy_indian_dataset; 90 | 91 | -- Cleaning/Validating Age 92 | delete from messy_indian_dataset 93 | where age <= 0 or age >= 100; 94 | 95 | select * from messy_indian_dataset; 96 | -------------------------------------------------------------------------------- /33. Restaurant Data Cleaning/01. data cleaning.sql: -------------------------------------------------------------------------------- 1 | use swiggy; 2 | select * from restaurants; 3 | 4 | drop table if exists rest_1, rest_2, rest_3, rest_4; 5 | -- 1. Finding the restaurant_id from the link 6 | select *, substring_index(link,'-',-1) as id from restaurants; 7 | 8 | -- 2. Updating the restaurant_id in the original table 9 | create table if not exists rest_1 as 10 | (select *, substring_index(link,'-',-1) as new_id from restaurants); 11 | select * from rest_1; 12 | 13 | -- 3. Clean the name column and update it on the table 14 | create table if not exists rest_2 as 15 | (select *, lower(trim(name)) as new_name from rest_1); 16 | select * from rest_2; 17 | 18 | -- 4. Clean the city & cuisine column and update it on the table 19 | create table if not exists rest_3 as 20 | (select new_id , new_name, lower(trim(city)) as clean_city, 21 | rating, rating_count, lower(trim(cuisine)) as clean_cuisine ,cost from rest_2); 22 | select * from rest_3; 23 | 24 | -- 5. Remove the odd cuisines from the table 25 | create table if not exists rest_4 as 26 | ( 27 | select new_id , new_name, clean_city, rating, rating_count, 28 | clean_cuisine ,cost 29 | from rest_3 30 | where clean_cuisine not in ('combo','na','discount offer from garden cafe express kankurgachi', 31 | 'svanidhi street food vendor','tex-mex', 32 | 'special discount from (hotel swagath)', 33 | 'free delivery ! limited stocks!')); 34 | select * from rest_4; 35 | 36 | drop table rest_1, rest_2, rest_3; 37 | -------------------------------------------------------------------------------- /34. Analysing Query Execution/01. dataset creation.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS messy_indian_dataset; 2 | CREATE TABLE IF NOT EXISTS messy_indian_dataset ( 3 | id INT PRIMARY KEY, 4 | name VARCHAR(50), 5 | age INT, 6 | gender VARCHAR(10), 7 | email VARCHAR(100), 8 | phone_number VARCHAR(15), 9 | city VARCHAR(50), 10 | state VARCHAR(50), 11 | purchase_amount DECIMAL(10, 2), 12 | purchase_date DATE 13 | ); 14 | 15 | -- Insert messy data into the table for Indian users 16 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) VALUES 17 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 18 | (2, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', NULL, '2023-02-15'), 19 | (3, 'Amit Kumar', 35, 'Male', 'amit@example.com', '9876543212', 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 20 | (4, 'Ritu Singh', 28, 'Female', NULL, '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 21 | (5, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 22 | (6, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', 800.00, '2023-02-15'), 23 | (7, 'Amit Kumar', NULL, 'Male', 'amit@example.com', NULL, 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 24 | (8, 'Ritu Singh', 28, 'Female', 'ritu@example.com', '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 25 | (9, 'Ankit Tiwari', 32, 'Male', 'ankit@example.com', '9876543214', 'Lucknow', 'Uttar Pradesh', 900.00, '2023-05-20'), 26 | (10, 'Swati Gupta', 27, 'Female', 'swati@example.com', '9876543215', 'Jaipur', 'Rajasthan', 1500.00, NULL), 27 | (11, 'Deepak Sharma', 45, 'Male', 'deepak@example.com', '9876543216', 'Chennai', 'Tamil Nadu', 1100.50, '2023-06-15'), 28 | (12, 'Anjali Gupta', 29, 'Female', 'anjali@example.com', '9876543217', 'Hyderabad', 'Telangana', 850.25, '2023-07-25'), 29 | (13, 'Rohit Singh', 33, 'Male', 'rohit@example.com', '9876543218', 'Pune', 'Maharashtra', 950.75, '2023-08-10'), 30 | (14, 'Pooja Patel', 27, 'Female', 'pooja@example.com', '9876543219', 'Ahmedabad', 'Gujarat', 1300.00, '2023-09-20'), 31 | (15, 'Sandeep Verma', 31, 'Male', 'sandeep@example.com', '9876543220', 'Surat', 'Gujarat', 1150.50, '2023-10-05'), 32 | (16, 'Aarti Sharma', 26, 'Female', 'aarti@example.org', '9876543221', 'Varanasi', 'Uttar Pradesh', 850.00, '2023-11-15'), 33 | (17, 'Vikram Singh', 37, 'Male', 'vikram@example.co.in', '9876543222', 'Indore', 'Madhya Pradesh', 1250.25, '2023-12-25'), 34 | (18, 'Ananya Sen', 28, 'Female', 'ananya@example.net', '9876543223', 'Kochi', 'Kerala', 900.75, '2024-01-10'), 35 | (19, 'Nikhil Sharma', 34, 'Male', 'nikhil@example.co.uk', '9876543224', 'Jaipur', 'Rajasthan', 1400.00, '2024-02-20'), 36 | (20, 'Isha Singh', 30, 'Female', 'isha@example.edu.in', '9876543225', 'Lucknow', 'Uttar Pradesh', 1050.50, '2024-03-05'), 37 | (21, 'Alok Kumar', NULL, 'Male', 'alok@example.com', '9876543226', 'Bhopal', 'Madhya Pradesh', 950.25, '2024-04-15'), 38 | (22, 'Shreya Patel', 25, NULL, 'shreya@example.com', '9876543227', 'Vadodara', 'Gujarat', 1350.00, '2024-05-25'), 39 | (23, 'Rahul Verma', 33, 'Male', NULL, '9876543228', 'Nagpur', 'Maharashtra', 1000.75, '2024-06-10'), 40 | (24, 'Anushka Singh', 28, 'Female', 'anushka@example.com', NULL, 'Gurgaon', 'Haryana', NULL, '2024-07-20'), 41 | (25, 'Ravi Kumar', 32, 'Male', 'ravi@example.com', '9876543229', NULL, 'Uttar Pradesh', 1100.50, '2024-08-05'), 42 | (26, 'Aarav Gupta', 27, 'Male', 'aarav@example.com', '9876543230', 'Patna', NULL, 1250.25, '2024-09-15'), 43 | (27, 'Kritika Sharma', 29, 'Female', 'kritika@example.com', '9876543231', 'Chandigarh', 'Punjab', 1200.75, NULL), 44 | (28, NULL, 35, 'Male', 'test@example.com', '9876543232', 'Jaipur', 'Rajasthan', 1300.00, '2024-11-20'), 45 | (29, 'Surbhi Gupta', 26, NULL, 'surbhi@example.com', '9876543233', 'Nashik', 'Maharashtra', 1150.50, '2024-12-05'), 46 | (30, 'Ajay Sharma', NULL, NULL, 'ajay@example.com', '9876543234', 'Jodhpur', 'Rajasthan', 1400.25, '2025-01-15'); 47 | select * from messy_indian_dataset; 48 | -------------------------------------------------------------------------------- /34. Analysing Query Execution/02. query execution plan.sql: -------------------------------------------------------------------------------- 1 | 2 | SELECT name, city 3 | FROM messy_indian_dataset; 4 | -- 1. FROM 5 | -- 2. SELECT 6 | 7 | 8 | SELECT name, city, purchase_amount 9 | FROM messy_indian_dataset 10 | WHERE purchase_amount > 1000; 11 | -- 1. FROM 12 | -- 2. WHERE 13 | -- 3. SELECT 14 | 15 | 16 | 17 | SELECT name, city, purchase_amount 18 | FROM messy_indian_dataset 19 | WHERE purchase_amount > 1000 20 | ORDER BY purchase_amount DESC; 21 | -- 1. FROM 22 | -- 2. WHERE 23 | -- 3. SELECT 24 | -- 4. ORDER BY 25 | 26 | 27 | 28 | SELECT city, AVG(purchase_amount) AS avg_purchase 29 | FROM messy_indian_dataset 30 | GROUP BY city; 31 | -- 1. FROM 32 | -- 2. GROUP BY 33 | -- 3. SELECT 34 | 35 | 36 | 37 | SELECT city, AVG(purchase_amount) AS avg_purchase 38 | FROM messy_indian_dataset 39 | GROUP BY city 40 | HAVING AVG(purchase_amount) > 1000; 41 | -- 1. FROM 42 | -- 2. GROUP BY 43 | -- 3. HAVING 44 | -- 4. SELECT 45 | 46 | 47 | DROP TABLE states; 48 | CREATE TABLE states ( state VARCHAR(50), region VARCHAR(50) ); 49 | INSERT INTO states (state, region) VALUES ('Maharashtra', 'West'), ('Delhi', 'North'), ('Karnataka', 'South'), 50 | ('West Bengal', 'East'), ('Rajasthan', 'West'); 51 | SELECT m.name, m.city, s.region 52 | FROM messy_indian_dataset m 53 | JOIN states s ON m.state = s.state; 54 | -- 1. FROM 55 | -- 2. JOIN 56 | -- 3. SELECT 57 | 58 | 59 | 60 | SELECT name, city, purchase_amount 61 | FROM messy_indian_dataset 62 | WHERE purchase_amount > (SELECT AVG(purchase_amount) FROM messy_indian_dataset); 63 | -- 1. Subquery(FROM) 64 | -- 2. Subquery(SELECT) 65 | -- 3. FROM 66 | -- 4. WHERE 67 | -- 5. SELECT 68 | 69 | 70 | 71 | -- Final 72 | 73 | -- Order of Execution 74 | -- 1. FROM: Determines the source tables and joins if any. 75 | -- 2. WHERE: Applies row filtering. 76 | -- 3. GROUP BY: Groups rows by specified columns. 77 | -- 4. HAVING: Applies group filtering. 78 | -- 5. SELECT: Determines which columns to include in the final result set. 79 | -- 6. ORDER BY: Sorts the result set. 80 | -- 7. LIMIT: Restricts the number of rows in the result set. -------------------------------------------------------------------------------- /35. Improving Query Performance/01. dataset creation.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS messy_indian_dataset; 2 | CREATE TABLE IF NOT EXISTS messy_indian_dataset ( 3 | id INT PRIMARY KEY, 4 | name VARCHAR(50), 5 | age INT, 6 | gender VARCHAR(10), 7 | email VARCHAR(100), 8 | phone_number VARCHAR(15), 9 | city VARCHAR(50), 10 | state VARCHAR(50), 11 | purchase_amount DECIMAL(10, 2), 12 | purchase_date DATE 13 | ); 14 | 15 | -- Insert messy data into the table for Indian users 16 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) VALUES 17 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 18 | (2, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', NULL, '2023-02-15'), 19 | (3, 'Amit Kumar', 35, 'Male', 'amit@example.com', '9876543212', 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 20 | (4, 'Ritu Singh', 28, 'Female', NULL, '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 21 | (5, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 22 | (6, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', 800.00, '2023-02-15'), 23 | (7, 'Amit Kumar', NULL, 'Male', 'amit@example.com', NULL, 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 24 | (8, 'Ritu Singh', 28, 'Female', 'ritu@example.com', '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 25 | (9, 'Ankit Tiwari', 32, 'Male', 'ankit@example.com', '9876543214', 'Lucknow', 'Uttar Pradesh', 900.00, '2023-05-20'), 26 | (10, 'Swati Gupta', 27, 'Female', 'swati@example.com', '9876543215', 'Jaipur', 'Rajasthan', 1500.00, NULL), 27 | (11, 'Deepak Sharma', 45, 'Male', 'deepak@example.com', '9876543216', 'Chennai', 'Tamil Nadu', 1100.50, '2023-06-15'), 28 | (12, 'Anjali Gupta', 29, 'Female', 'anjali@example.com', '9876543217', 'Hyderabad', 'Telangana', 850.25, '2023-07-25'), 29 | (13, 'Rohit Singh', 33, 'Male', 'rohit@example.com', '9876543218', 'Pune', 'Maharashtra', 950.75, '2023-08-10'), 30 | (14, 'Pooja Patel', 27, 'Female', 'pooja@example.com', '9876543219', 'Ahmedabad', 'Gujarat', 1300.00, '2023-09-20'), 31 | (15, 'Sandeep Verma', 31, 'Male', 'sandeep@example.com', '9876543220', 'Surat', 'Gujarat', 1150.50, '2023-10-05'), 32 | (16, 'Aarti Sharma', 26, 'Female', 'aarti@example.org', '9876543221', 'Varanasi', 'Uttar Pradesh', 850.00, '2023-11-15'), 33 | (17, 'Vikram Singh', 37, 'Male', 'vikram@example.co.in', '9876543222', 'Indore', 'Madhya Pradesh', 1250.25, '2023-12-25'), 34 | (18, 'Ananya Sen', 28, 'Female', 'ananya@example.net', '9876543223', 'Kochi', 'Kerala', 900.75, '2024-01-10'), 35 | (19, 'Nikhil Sharma', 34, 'Male', 'nikhil@example.co.uk', '9876543224', 'Jaipur', 'Rajasthan', 1400.00, '2024-02-20'), 36 | (20, 'Isha Singh', 30, 'Female', 'isha@example.edu.in', '9876543225', 'Lucknow', 'Uttar Pradesh', 1050.50, '2024-03-05'), 37 | (21, 'Alok Kumar', NULL, 'Male', 'alok@example.com', '9876543226', 'Bhopal', 'Madhya Pradesh', 950.25, '2024-04-15'), 38 | (22, 'Shreya Patel', 25, NULL, 'shreya@example.com', '9876543227', 'Vadodara', 'Gujarat', 1350.00, '2024-05-25'), 39 | (23, 'Rahul Verma', 33, 'Male', NULL, '9876543228', 'Nagpur', 'Maharashtra', 1000.75, '2024-06-10'), 40 | (24, 'Anushka Singh', 28, 'Female', 'anushka@example.com', NULL, 'Gurgaon', 'Haryana', NULL, '2024-07-20'), 41 | (25, 'Ravi Kumar', 32, 'Male', 'ravi@example.com', '9876543229', NULL, 'Uttar Pradesh', 1100.50, '2024-08-05'), 42 | (26, 'Aarav Gupta', 27, 'Male', 'aarav@example.com', '9876543230', 'Patna', NULL, 1250.25, '2024-09-15'), 43 | (27, 'Kritika Sharma', 29, 'Female', 'kritika@example.com', '9876543231', 'Chandigarh', 'Punjab', 1200.75, NULL), 44 | (28, NULL, 35, 'Male', 'test@example.com', '9876543232', 'Jaipur', 'Rajasthan', 1300.00, '2024-11-20'), 45 | (29, 'Surbhi Gupta', 26, NULL, 'surbhi@example.com', '9876543233', 'Nashik', 'Maharashtra', 1150.50, '2024-12-05'), 46 | (30, 'Ajay Sharma', NULL, NULL, 'ajay@example.com', '9876543234', 'Jodhpur', 'Rajasthan', 1400.25, '2025-01-15'); 47 | select * from messy_indian_dataset; 48 | -------------------------------------------------------------------------------- /35. Improving Query Performance/overall.sql: -------------------------------------------------------------------------------- 1 | 2 | -- 1. Avoid accessing unnecessary result 3 | select * from messy_indian_dataset; 4 | select id,name, city, purchase_amount from messy_indian_dataset; 5 | select id,name, city, purchase_amount from messy_indian_dataset where purchase_amount > 1000 limit 5; 6 | select id,name, city, purchase_amount, purchase_amount * 1.18 as 'price with gst' from messy_indian_dataset where purchase_amount > 1000 limit 5; 7 | 8 | 9 | -- 2. Use right kind of JOIN 10 | 11 | 12 | -- 3. Use of Appropriate Data Types 13 | create table customer_correct(id INT, name varchar(50), description varchar(255)); 14 | create table customer_correct(id INT, name varchar(50), description text); 15 | 16 | 17 | -- 4. Query Execution Plans 18 | explain select id, name, city , purchase_amount from messy_indian_dataset 19 | where purchase_amount > 1000; 20 | 21 | -- 5. Caching 22 | select SQL_CATCH id, name, city , purchase_amount from messy_indian_dataset 23 | where purchase_amount > 1000; 24 | 25 | 26 | -- 6. Use of Temporary Tables 27 | create temporary table high_purchase as 28 | ( 29 | select id, name, city , purchase_amount from messy_indian_dataset 30 | where purchase_amount > 1000 31 | ); 32 | 33 | select * from high_purchase where city = 'Mumbai' ; 34 | select id, name, city , purchase_amount from high_purchase where city = 'Mumbai' ; 35 | 36 | -- Partition Table, CTE & Indexing -------------------------------------------------------------------------------- /36. Table Partitioning/01. dataset creation.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS messy_indian_dataset; 2 | CREATE TABLE IF NOT EXISTS messy_indian_dataset ( 3 | id INT PRIMARY KEY, 4 | name VARCHAR(50), 5 | age INT, 6 | gender VARCHAR(10), 7 | email VARCHAR(100), 8 | phone_number VARCHAR(15), 9 | city VARCHAR(50), 10 | state VARCHAR(50), 11 | purchase_amount DECIMAL(10, 2), 12 | purchase_date DATE 13 | ); 14 | 15 | -- Insert messy data into the table for Indian users 16 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) VALUES 17 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 18 | (2, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', NULL, '2023-02-15'), 19 | (3, 'Amit Kumar', 35, 'Male', 'amit@example.com', '9876543212', 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 20 | (4, 'Ritu Singh', 28, 'Female', NULL, '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 21 | (5, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 22 | (6, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', 800.00, '2023-02-15'), 23 | (7, 'Amit Kumar', NULL, 'Male', 'amit@example.com', NULL, 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 24 | (8, 'Ritu Singh', 28, 'Female', 'ritu@example.com', '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 25 | (9, 'Ankit Tiwari', 32, 'Male', 'ankit@example.com', '9876543214', 'Lucknow', 'Uttar Pradesh', 900.00, '2023-05-20'), 26 | (10, 'Swati Gupta', 27, 'Female', 'swati@example.com', '9876543215', 'Jaipur', 'Rajasthan', 1500.00, NULL), 27 | (11, 'Deepak Sharma', 45, 'Male', 'deepak@example.com', '9876543216', 'Chennai', 'Tamil Nadu', 1100.50, '2023-06-15'), 28 | (12, 'Anjali Gupta', 29, 'Female', 'anjali@example.com', '9876543217', 'Hyderabad', 'Telangana', 850.25, '2023-07-25'), 29 | (13, 'Rohit Singh', 33, 'Male', 'rohit@example.com', '9876543218', 'Pune', 'Maharashtra', 950.75, '2023-08-10'), 30 | (14, 'Pooja Patel', 27, 'Female', 'pooja@example.com', '9876543219', 'Ahmedabad', 'Gujarat', 1300.00, '2023-09-20'), 31 | (15, 'Sandeep Verma', 31, 'Male', 'sandeep@example.com', '9876543220', 'Surat', 'Gujarat', 1150.50, '2023-10-05'), 32 | (16, 'Aarti Sharma', 26, 'Female', 'aarti@example.org', '9876543221', 'Varanasi', 'Uttar Pradesh', 850.00, '2023-11-15'), 33 | (17, 'Vikram Singh', 37, 'Male', 'vikram@example.co.in', '9876543222', 'Indore', 'Madhya Pradesh', 1250.25, '2023-12-25'), 34 | (18, 'Ananya Sen', 28, 'Female', 'ananya@example.net', '9876543223', 'Kochi', 'Kerala', 900.75, '2024-01-10'), 35 | (19, 'Nikhil Sharma', 34, 'Male', 'nikhil@example.co.uk', '9876543224', 'Jaipur', 'Rajasthan', 1400.00, '2024-02-20'), 36 | (20, 'Isha Singh', 30, 'Female', 'isha@example.edu.in', '9876543225', 'Lucknow', 'Uttar Pradesh', 1050.50, '2024-03-05'), 37 | (21, 'Alok Kumar', NULL, 'Male', 'alok@example.com', '9876543226', 'Bhopal', 'Madhya Pradesh', 950.25, '2024-04-15'), 38 | (22, 'Shreya Patel', 25, NULL, 'shreya@example.com', '9876543227', 'Vadodara', 'Gujarat', 1350.00, '2024-05-25'), 39 | (23, 'Rahul Verma', 33, 'Male', NULL, '9876543228', 'Nagpur', 'Maharashtra', 1000.75, '2024-06-10'), 40 | (24, 'Anushka Singh', 28, 'Female', 'anushka@example.com', NULL, 'Gurgaon', 'Haryana', NULL, '2024-07-20'), 41 | (25, 'Ravi Kumar', 32, 'Male', 'ravi@example.com', '9876543229', NULL, 'Uttar Pradesh', 1100.50, '2024-08-05'), 42 | (26, 'Aarav Gupta', 27, 'Male', 'aarav@example.com', '9876543230', 'Patna', NULL, 1250.25, '2024-09-15'), 43 | (27, 'Kritika Sharma', 29, 'Female', 'kritika@example.com', '9876543231', 'Chandigarh', 'Punjab', 1200.75, NULL), 44 | (28, NULL, 35, 'Male', 'test@example.com', '9876543232', 'Jaipur', 'Rajasthan', 1300.00, '2024-11-20'), 45 | (29, 'Surbhi Gupta', 26, NULL, 'surbhi@example.com', '9876543233', 'Nashik', 'Maharashtra', 1150.50, '2024-12-05'), 46 | (30, 'Ajay Sharma', NULL, NULL, 'ajay@example.com', '9876543234', 'Jodhpur', 'Rajasthan', 1400.25, '2025-01-15'); 47 | select * from messy_indian_dataset; 48 | -------------------------------------------------------------------------------- /36. Table Partitioning/partition tables.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS dataset; 2 | CREATE TABLE dataset ( 3 | id INT, 4 | name VARCHAR(50), 5 | city VARCHAR(50), 6 | purchase_amount DECIMAL(10, 2), 7 | purchase_date DATE); 8 | 9 | -- 1. Range Partitioning 10 | 11 | DROP TABLE IF EXISTS partitioned_dataset; 12 | CREATE TABLE partitioned_dataset ( id INT, name VARCHAR(50), city VARCHAR(50), purchase_amount DECIMAL(10, 2), purchase_date DATE ) 13 | PARTITION BY RANGE (YEAR(purchase_date)) 14 | ( 15 | PARTITION p0 VALUES LESS THAN (2020), 16 | PARTITION p1 VALUES LESS THAN (2021), 17 | PARTITION p2 VALUES LESS THAN (2022), 18 | PARTITION p3 VALUES LESS THAN (2023), 19 | PARTITION p4 VALUES LESS THAN (2024) 20 | ); 21 | SELECT name, city, purchase_amount FROM partitioned_dataset WHERE purchase_date BETWEEN '2022-01-01' AND '2022-12-31'; 22 | 23 | -- 2. List Partitioning 24 | 25 | DROP TABLE IF EXISTS partitioned_dataset; 26 | CREATE TABLE partitioned_dataset ( id INT, name VARCHAR(50), city VARCHAR(50), purchase_amount DECIMAL(10, 2), purchase_date DATE, 27 | city_code INT GENERATED ALWAYS AS ( 28 | CASE 29 | WHEN city = 'Mumbai' THEN 1 30 | WHEN city = 'Delhi' THEN 2 31 | WHEN city = 'Bangalore' THEN 3 32 | WHEN city IN ('Kolkata', 'Chennai', 'Hyderabad', 'Pune') THEN 4 33 | ELSE NULL 34 | END 35 | ) STORED 36 | ) 37 | PARTITION BY LIST (city_code) ( 38 | PARTITION p_mumbai VALUES IN (1), 39 | PARTITION p_delhi VALUES IN (2), 40 | PARTITION p_bangalore VALUES IN (3), 41 | PARTITION p_other VALUES IN (4)); 42 | 43 | INSERT INTO partitioned_dataset (id, name, city, purchase_amount, purchase_date) 44 | VALUES 45 | (1, 'Rajesh Patel', 'Mumbai', 1000.50, '2023-01-05'), 46 | (2, 'Priya Sharma', 'Delhi', 850.75, '2023-02-10'), 47 | (3, 'Amit Kumar', 'Bangalore', 1200.00, '2023-03-15'), 48 | (4, 'Ritu Singh', 'Kolkata', 950.00, '2023-04-20'), 49 | (5, 'Anjali Gupta', 'Chennai', 1100.25, '2023-05-25'); 50 | 51 | 52 | SELECT name, purchase_amount 53 | FROM partitioned_dataset 54 | WHERE city = 'Mumbai'; 55 | 56 | 57 | -- 3. Hash Partitioning 58 | 59 | DROP TABLE IF EXISTS partitioned_dataset; 60 | CREATE TABLE partitioned_dataset ( id INT, name VARCHAR(50), city VARCHAR(50), purchase_amount DECIMAL(10, 2), purchase_date DATE ) 61 | PARTITION BY HASH(id) PARTITIONS 4; 62 | 63 | SELECT name, city, purchase_amount 64 | FROM partitioned_dataset 65 | WHERE id = 12345; 66 | 67 | -- 4. Subpartitioning 68 | 69 | DROP TABLE IF EXISTS partitioned_dataset; 70 | CREATE TABLE partitioned_dataset ( 71 | id INT, 72 | name VARCHAR(50), 73 | city VARCHAR(50), 74 | purchase_amount DECIMAL(10, 2), 75 | purchase_date DATE 76 | ) 77 | PARTITION BY RANGE (YEAR(purchase_date)) 78 | SUBPARTITION BY HASH(id) SUBPARTITIONS 4 ( 79 | PARTITION p0 VALUES LESS THAN (2020), 80 | PARTITION p1 VALUES LESS THAN (2021), 81 | PARTITION p2 VALUES LESS THAN (2022), 82 | PARTITION p3 VALUES LESS THAN (2023), 83 | PARTITION p4 VALUES LESS THAN (2024) 84 | ); 85 | 86 | SELECT name, city, purchase_amount 87 | FROM partitioned_dataset 88 | WHERE purchase_date BETWEEN '2022-01-01' AND '2022-12-31'; 89 | -------------------------------------------------------------------------------- /37. Indexing in SQL/01. dataset creation.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS messy_indian_dataset; 2 | CREATE TABLE IF NOT EXISTS messy_indian_dataset ( 3 | id INT PRIMARY KEY, 4 | name VARCHAR(50), 5 | age INT, 6 | gender VARCHAR(10), 7 | email VARCHAR(100), 8 | phone_number VARCHAR(15), 9 | city VARCHAR(50), 10 | state VARCHAR(50), 11 | purchase_amount DECIMAL(10, 2), 12 | purchase_date DATE 13 | ); 14 | 15 | -- Insert messy data into the table for Indian users 16 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) VALUES 17 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 18 | (2, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', NULL, '2023-02-15'), 19 | (3, 'Amit Kumar', 35, 'Male', 'amit@example.com', '9876543212', 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 20 | (4, 'Ritu Singh', 28, 'Female', NULL, '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 21 | (5, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 22 | (6, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', 800.00, '2023-02-15'), 23 | (7, 'Amit Kumar', NULL, 'Male', 'amit@example.com', NULL, 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 24 | (8, 'Ritu Singh', 28, 'Female', 'ritu@example.com', '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 25 | (9, 'Ankit Tiwari', 32, 'Male', 'ankit@example.com', '9876543214', 'Lucknow', 'Uttar Pradesh', 900.00, '2023-05-20'), 26 | (10, 'Swati Gupta', 27, 'Female', 'swati@example.com', '9876543215', 'Jaipur', 'Rajasthan', 1500.00, NULL), 27 | (11, 'Deepak Sharma', 45, 'Male', 'deepak@example.com', '9876543216', 'Chennai', 'Tamil Nadu', 1100.50, '2023-06-15'), 28 | (12, 'Anjali Gupta', 29, 'Female', 'anjali@example.com', '9876543217', 'Hyderabad', 'Telangana', 850.25, '2023-07-25'), 29 | (13, 'Rohit Singh', 33, 'Male', 'rohit@example.com', '9876543218', 'Pune', 'Maharashtra', 950.75, '2023-08-10'), 30 | (14, 'Pooja Patel', 27, 'Female', 'pooja@example.com', '9876543219', 'Ahmedabad', 'Gujarat', 1300.00, '2023-09-20'), 31 | (15, 'Sandeep Verma', 31, 'Male', 'sandeep@example.com', '9876543220', 'Surat', 'Gujarat', 1150.50, '2023-10-05'), 32 | (16, 'Aarti Sharma', 26, 'Female', 'aarti@example.org', '9876543221', 'Varanasi', 'Uttar Pradesh', 850.00, '2023-11-15'), 33 | (17, 'Vikram Singh', 37, 'Male', 'vikram@example.co.in', '9876543222', 'Indore', 'Madhya Pradesh', 1250.25, '2023-12-25'), 34 | (18, 'Ananya Sen', 28, 'Female', 'ananya@example.net', '9876543223', 'Kochi', 'Kerala', 900.75, '2024-01-10'), 35 | (19, 'Nikhil Sharma', 34, 'Male', 'nikhil@example.co.uk', '9876543224', 'Jaipur', 'Rajasthan', 1400.00, '2024-02-20'), 36 | (20, 'Isha Singh', 30, 'Female', 'isha@example.edu.in', '9876543225', 'Lucknow', 'Uttar Pradesh', 1050.50, '2024-03-05'), 37 | (21, 'Alok Kumar', NULL, 'Male', 'alok@example.com', '9876543226', 'Bhopal', 'Madhya Pradesh', 950.25, '2024-04-15'), 38 | (22, 'Shreya Patel', 25, NULL, 'shreya@example.com', '9876543227', 'Vadodara', 'Gujarat', 1350.00, '2024-05-25'), 39 | (23, 'Rahul Verma', 33, 'Male', NULL, '9876543228', 'Nagpur', 'Maharashtra', 1000.75, '2024-06-10'), 40 | (24, 'Anushka Singh', 28, 'Female', 'anushka@example.com', NULL, 'Gurgaon', 'Haryana', NULL, '2024-07-20'), 41 | (25, 'Ravi Kumar', 32, 'Male', 'ravi@example.com', '9876543229', NULL, 'Uttar Pradesh', 1100.50, '2024-08-05'), 42 | (26, 'Aarav Gupta', 27, 'Male', 'aarav@example.com', '9876543230', 'Patna', NULL, 1250.25, '2024-09-15'), 43 | (27, 'Kritika Sharma', 29, 'Female', 'kritika@example.com', '9876543231', 'Chandigarh', 'Punjab', 1200.75, NULL), 44 | (28, NULL, 35, 'Male', 'test@example.com', '9876543232', 'Jaipur', 'Rajasthan', 1300.00, '2024-11-20'), 45 | (29, 'Surbhi Gupta', 26, NULL, 'surbhi@example.com', '9876543233', 'Nashik', 'Maharashtra', 1150.50, '2024-12-05'), 46 | (30, 'Ajay Sharma', NULL, NULL, 'ajay@example.com', '9876543234', 'Jodhpur', 'Rajasthan', 1400.25, '2025-01-15'); 47 | select * from messy_indian_dataset; 48 | -------------------------------------------------------------------------------- /37. Indexing in SQL/Indexing.sql: -------------------------------------------------------------------------------- 1 | 2 | -- 1. Simple Index 3 | drop index idx_city on messy_indian_dataset; 4 | create index idx_city on messy_indian_dataset(city); 5 | select name,city,purchase_amount from messy_indian_dataset where city = "Mumbai"; 6 | 7 | 8 | -- 2. Composite Index 9 | drop index idx_city on messy_indian_dataset; 10 | create index idx_city on messy_indian_dataset(city, gender); 11 | select name,city,purchase_amount from messy_indian_dataset where city = "Mumbai"; 12 | 13 | -- 3. Unique Index 14 | drop index idx_city on messy_indian_dataset; 15 | create unique index idx_city on messy_indian_dataset(id); 16 | select name,city,purchase_amount from messy_indian_dataset where city = "Mumbai"; 17 | 18 | -- 4. Full-Text Index 19 | drop index idx_city on messy_indian_dataset; 20 | create fulltext index idx_city on messy_indian_dataset(name, email); 21 | select name,city,purchase_amount from messy_indian_dataset where match(name,email) against('Rajesh'); 22 | 23 | 24 | -- This query searches for the exact phrase "Rajesh Patel" in the name and email columns. 25 | select name,city,purchase_amount from messy_indian_dataset where match(name,email) against('Patel'); 26 | 27 | -- + operator ensures 'Rajesh' must be present, and the - operator ensures 'example' must be absent. 28 | select name,city,purchase_amount from messy_indian_dataset where match(name,email) 29 | against('+Patel -Pooja' in boolean mode); -------------------------------------------------------------------------------- /38. Common Table Expressions/01. dataset creation.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS messy_indian_dataset; 2 | CREATE TABLE IF NOT EXISTS messy_indian_dataset ( 3 | id INT PRIMARY KEY, 4 | name VARCHAR(50), 5 | age INT, 6 | gender VARCHAR(10), 7 | email VARCHAR(100), 8 | phone_number VARCHAR(15), 9 | city VARCHAR(50), 10 | state VARCHAR(50), 11 | purchase_amount DECIMAL(10, 2), 12 | purchase_date DATE 13 | ); 14 | 15 | -- Insert messy data into the table for Indian users 16 | INSERT INTO messy_indian_dataset (id, name, age, gender, email, phone_number, city, state, purchase_amount, purchase_date) VALUES 17 | (1, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 18 | (2, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', NULL, '2023-02-15'), 19 | (3, 'Amit Kumar', 35, 'Male', 'amit@example.com', '9876543212', 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 20 | (4, 'Ritu Singh', 28, 'Female', NULL, '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 21 | (5, 'Rajesh Patel', 30, 'Male', 'rajesh@example.com', '9876543210', 'Mumbai', 'Maharashtra', 1000.50, '2023-01-05'), 22 | (6, 'Priya Sharma', 25, 'Female', 'priya@example.com', '9876543211', 'Delhi', 'Delhi', 800.00, '2023-02-15'), 23 | (7, 'Amit Kumar', NULL, 'Male', 'amit@example.com', NULL, 'Bangalore', 'Karnataka', 750.25, '2023-03-25'), 24 | (8, 'Ritu Singh', 28, 'Female', 'ritu@example.com', '9876543213', 'Kolkata', 'West Bengal', 1200.75, '2023-04-10'), 25 | (9, 'Ankit Tiwari', 32, 'Male', 'ankit@example.com', '9876543214', 'Lucknow', 'Uttar Pradesh', 900.00, '2023-05-20'), 26 | (10, 'Swati Gupta', 27, 'Female', 'swati@example.com', '9876543215', 'Jaipur', 'Rajasthan', 1500.00, NULL), 27 | (11, 'Deepak Sharma', 45, 'Male', 'deepak@example.com', '9876543216', 'Chennai', 'Tamil Nadu', 1100.50, '2023-06-15'), 28 | (12, 'Anjali Gupta', 29, 'Female', 'anjali@example.com', '9876543217', 'Hyderabad', 'Telangana', 850.25, '2023-07-25'), 29 | (13, 'Rohit Singh', 33, 'Male', 'rohit@example.com', '9876543218', 'Pune', 'Maharashtra', 950.75, '2023-08-10'), 30 | (14, 'Pooja Patel', 27, 'Female', 'pooja@example.com', '9876543219', 'Ahmedabad', 'Gujarat', 1300.00, '2023-09-20'), 31 | (15, 'Sandeep Verma', 31, 'Male', 'sandeep@example.com', '9876543220', 'Surat', 'Gujarat', 1150.50, '2023-10-05'), 32 | (16, 'Aarti Sharma', 26, 'Female', 'aarti@example.org', '9876543221', 'Varanasi', 'Uttar Pradesh', 850.00, '2023-11-15'), 33 | (17, 'Vikram Singh', 37, 'Male', 'vikram@example.co.in', '9876543222', 'Indore', 'Madhya Pradesh', 1250.25, '2023-12-25'), 34 | (18, 'Ananya Sen', 28, 'Female', 'ananya@example.net', '9876543223', 'Kochi', 'Kerala', 900.75, '2024-01-10'), 35 | (19, 'Nikhil Sharma', 34, 'Male', 'nikhil@example.co.uk', '9876543224', 'Jaipur', 'Rajasthan', 1400.00, '2024-02-20'), 36 | (20, 'Isha Singh', 30, 'Female', 'isha@example.edu.in', '9876543225', 'Lucknow', 'Uttar Pradesh', 1050.50, '2024-03-05'), 37 | (21, 'Alok Kumar', NULL, 'Male', 'alok@example.com', '9876543226', 'Bhopal', 'Madhya Pradesh', 950.25, '2024-04-15'), 38 | (22, 'Shreya Patel', 25, NULL, 'shreya@example.com', '9876543227', 'Vadodara', 'Gujarat', 1350.00, '2024-05-25'), 39 | (23, 'Rahul Verma', 33, 'Male', NULL, '9876543228', 'Nagpur', 'Maharashtra', 1000.75, '2024-06-10'), 40 | (24, 'Anushka Singh', 28, 'Female', 'anushka@example.com', NULL, 'Gurgaon', 'Haryana', NULL, '2024-07-20'), 41 | (25, 'Ravi Kumar', 32, 'Male', 'ravi@example.com', '9876543229', NULL, 'Uttar Pradesh', 1100.50, '2024-08-05'), 42 | (26, 'Aarav Gupta', 27, 'Male', 'aarav@example.com', '9876543230', 'Patna', NULL, 1250.25, '2024-09-15'), 43 | (27, 'Kritika Sharma', 29, 'Female', 'kritika@example.com', '9876543231', 'Chandigarh', 'Punjab', 1200.75, NULL), 44 | (28, NULL, 35, 'Male', 'test@example.com', '9876543232', 'Jaipur', 'Rajasthan', 1300.00, '2024-11-20'), 45 | (29, 'Surbhi Gupta', 26, NULL, 'surbhi@example.com', '9876543233', 'Nashik', 'Maharashtra', 1150.50, '2024-12-05'), 46 | (30, 'Ajay Sharma', NULL, NULL, 'ajay@example.com', '9876543234', 'Jodhpur', 'Rajasthan', 1400.25, '2025-01-15'); 47 | select * from messy_indian_dataset; 48 | -------------------------------------------------------------------------------- /38. Common Table Expressions/CTE.sql: -------------------------------------------------------------------------------- 1 | -- Example - 1 2 | -- Original Query 3 | SELECT id, name, city, purchase_amount 4 | FROM messy_indian_dataset 5 | WHERE city = 'Mumbai' AND purchase_amount > 1000 AND gender = 'Male'; 6 | 7 | 8 | 9 | -- Simplified Query Using CTE 10 | 11 | 12 | 13 | 14 | -- Example - 2 15 | -- Simplifying Complex Queries 16 | 17 | 18 | 19 | -- Example - 3 20 | -- Reusing the Same Subquery 21 | 22 | 23 | 24 | 25 | -- Example - 4 26 | -- Improving Aggregation Performance 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | SELECT 35 | city, 36 | COUNT(*) AS num_purchases, 37 | SUM(purchase_amount) AS total_purchase, 38 | AVG(purchase_amount) AS avg_purchase, 39 | MAX(purchase_amount) AS max_purchase 40 | FROM messy_indian_dataset 41 | GROUP BY city 42 | HAVING SUM(purchase_amount) > 100; 43 | 44 | 45 | 46 | WITH PreAggregated AS ( 47 | SELECT 48 | city, 49 | COUNT(*) AS num_purchases, 50 | SUM(purchase_amount) AS total_purchase, 51 | AVG(purchase_amount) AS avg_purchase, 52 | MAX(purchase_amount) AS max_purchase 53 | FROM messy_indian_dataset 54 | GROUP BY city) 55 | SELECT 56 | p.city, 57 | p.num_purchases, 58 | p.total_purchase, 59 | p.avg_purchase, 60 | p.max_purchase 61 | FROM PreAggregated p 62 | WHERE p.total_purchase > 100; 63 | 64 | 65 | -------------------------------------------------------------------------------- /39. SQL with ChatGPT/SQLite.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AshishJangra27/SQL-for-Data-Analysis/a40aa1b7015cead9cae2fbc5bd1874dcca68a2d3/39. SQL with ChatGPT/SQLite.csv -------------------------------------------------------------------------------- /39. SQL with ChatGPT/SQLite.sql: -------------------------------------------------------------------------------- 1 | -- Create the sale table 2 | CREATE TABLE sale ( 3 | sale_id INT PRIMARY KEY, 4 | sale_date DATE, 5 | sale_time TIME, 6 | product_id INT, 7 | product_name VARCHAR(50), 8 | customer_id INT, 9 | customer_name VARCHAR(50), 10 | quantity INT, 11 | price DECIMAL(10, 2), 12 | total_amount DECIMAL(10, 2) 13 | ); 14 | 15 | -- Insert data into the sale table 16 | INSERT INTO sale (sale_id, sale_date, sale_time, product_id, product_name, customer_id, customer_name, quantity, price, total_amount) 17 | VALUES 18 | (1, '2023-01-01', '10:30:00', 101, 'Laptop', 201, 'Arjun', 2, 50000, 95000), 19 | (2, '2023-01-02', '11:15:00', 102, 'Mobile', 202, 'Bhavya', 1, 20000, 18000), 20 | (3, '2023-01-03', '12:00:00', 103, 'Tablet', 203, 'Chaitanya', 3, 15000, 40500), 21 | (4, '2023-01-04', '13:45:00', 104, 'TV', 204, 'Deepak', 1, 30000, 27000), 22 | (5, '2023-01-05', '14:30:00', 105, 'Headphones', 205, 'Esha', 4, 2000, 7200), 23 | (6, '2023-01-06', '15:15:00', 101, 'Laptop', 206, 'Farhan', 2, 50000, 95000), 24 | (7, '2023-01-07', '16:00:00', 102, 'Mobile', 207, 'Gauri', 1, 20000, 18000), 25 | (8, '2023-01-08', '17:30:00', 103, 'Tablet', 208, 'Harsh', 3, 15000, 40500), 26 | (9, '2023-01-09', '18:45:00', 104, 'TV', 209, 'Ishita', 1, 30000, 27000), 27 | (10, '2023-01-10', '19:15:00', 105, 'Headphones', 210, 'Jai', 4, 2000, 7200); 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQL-for-Data-Analysis 2 | This repository contains a collection of SQL scripts and resources designed for the in SQL course I've made with GFG. 3 | 4 | ## Introduction 5 | 6 | SQL (Structured Query Language) is essential for managing and analyzing data in relational databases. This repository provides practical examples and best practices for using SQL effectively in data analysis. 7 | 8 | ## Getting Started 9 | 10 | ### Prerequisites 11 | 12 | - SQL database management system (MySQL) 13 | - Basic knowledge of SQL 14 | 15 | ### Cloning the Repository 16 | 17 | ```bash 18 | git clone https://github.com/AshishJangra27/SQL-for-Data-Analysis.git 19 | cd SQL-for-Data-Analysis 20 | ``` 21 | 22 | 23 | ## Learning Path 24 | 25 | ### Week 1 26 | 1. **Introduction to Databases** 27 | - Basic concepts and architecture of databases. 28 | 29 | 2. **Introduction to SQL & Setup** 30 | - Setting up your SQL environment and learning basic SQL syntax. 31 | 32 | 3. **Data Retrieval with SQL** 33 | - Retrieving data using `SELECT` statements. 34 | 35 | ### Week 2 36 | 4. **Cases in SQL** 37 | - Handling different scenarios and conditions using `CASE` statements. 38 | 39 | 5. **Introduction to DDL Commands** 40 | - Creating and modifying database objects using DDL commands. 41 | 42 | 6. **Keys & Constraints** 43 | - Defining primary keys, foreign keys, and constraints to maintain data integrity. 44 | 45 | ### Week 3 46 | 7. **Insert Data in a Table** 47 | - Techniques for inserting new data into tables. 48 | 49 | 8. **Update Data from a Table** 50 | - Methods for updating existing data in tables. 51 | 52 | 9. **Delete Data from a Table** 53 | - Deleting data from tables. 54 | 55 | ### Week 4 56 | 10. **SQL in Action** 57 | - Practical examples of SQL queries in real-world scenarios. 58 | 59 | 11. **Pattern Matching** 60 | - Using `LIKE` and regular expressions for pattern matching. 61 | 62 | 12. **Aggregate Functions** 63 | - Using functions like `SUM`, `AVG`, `COUNT` for data aggregation. 64 | 65 | ### Week 5 66 | 13. **Grouping and Filtering Data** 67 | - Grouping data with `GROUP BY` and filtering groups with `HAVING`. 68 | 69 | 14. **Working with Subqueries** 70 | - Using subqueries for complex data retrieval. 71 | 72 | 15. **Joins in Action** 73 | - Combining data from multiple tables using different types of joins. 74 | 75 | ### Week 6 76 | 16. **Set Operators in Action** 77 | - Using `UNION`, `INTERSECT`, and `EXCEPT` operators to combine query results. 78 | 79 | 17. **Restaurant Data Analysis with SQL** 80 | - Analyzing restaurant data with SQL queries. 81 | 82 | 18. **Window Functions** 83 | - Using window functions like `ROW_NUMBER`, `RANK`, and `DENSE_RANK`. 84 | 85 | ### Week 7 86 | 19. **Ranks in SQL** 87 | - Ranking data using SQL. 88 | 89 | 20. **Advanced Window Functions** 90 | - Advanced uses of window functions for data analysis. 91 | 92 | 21. **Views in SQL** 93 | - Creating and using views to simplify data access. 94 | 95 | ### Week 8 96 | 22. **Table Aliases** 97 | - Using aliases to make queries more readable. 98 | 99 | 23. **Functions & Procedures** 100 | - Creating and using functions and stored procedures in SQL. 101 | 102 | 24. **Transactions** 103 | - Managing transactions to ensure data integrity. 104 | 105 | ### Week 9 106 | 25. **Errors & Exceptions** 107 | - Handling errors and exceptions in SQL. 108 | 109 | 26. **Introduction to Data Cleaning** 110 | - Overview of data cleaning techniques. 111 | 112 | 27. **Handling Missing Values** 113 | - Techniques for handling missing data. 114 | 115 | ### Week 10 116 | 28. **Handling Duplicate Values** 117 | - Identifying and removing duplicate data. 118 | 119 | 29. **Handling Outliers** 120 | - Methods for detecting and handling outliers. 121 | 122 | 30. **Working with Dates** 123 | - Manipulating date and time data in SQL. 124 | 125 | ### Week 11 126 | 31. **Data Cleaning** 127 | - Comprehensive techniques for cleaning data. 128 | 129 | 32. **Restaurant Data Cleaning** 130 | - Cleaning and preparing restaurant data for analysis. 131 | 132 | 33. **Analyzing Query Execution** 133 | - Using `EXPLAIN` to analyze query execution plans. 134 | 135 | ### Week 12 136 | 34. **Improving Query Performance** 137 | - Techniques for optimizing SQL queries. 138 | 139 | 35. **Table Partitioning** 140 | - Partitioning tables to improve query performance. 141 | 142 | 36. **Indexing in SQL** 143 | - Creating and using indexes to speed up data retrieval. 144 | 145 | ### Week 13 146 | 37. **Common Table Expressions** 147 | - Using CTEs to simplify complex queries. 148 | 149 | 38. **SQL with ChatGPT** 150 | - Using ChatGPT for SQL query generation and assistance. 151 | 152 | 153 | ----- 154 | 155 | 156 | ### Who am I? 157 | 158 | My name is Ashish and I'm an AI Trainer. I've trained more than 20000 students on different technologies like AI, Data Science, Computer Vision, and the Internet of Things. I'm passionate about teaching and giving students the skillset to learn cutting-edge skills. 159 | 160 | ----- 161 | 162 | ### Other Useful Links: 163 | 164 | LinkedIn - https://linkedin.com/in/ashish-jangra 165 | 166 | Instagram - https://instagram.com/ashish_zangra 167 | 168 | Facebook - https://facebook.com/ashishzangra 169 | 170 | --------------------------------------------------------------------------------