{"id":19834788,"url":"https://github.com/gino-freud-hobayan/instacart-analysis__sql-capstone-project","last_synced_at":"2026-03-19T11:58:28.467Z","repository":{"id":190838565,"uuid":"682409906","full_name":"Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project","owner":"Gino-Freud-Hobayan","description":"Capstone Project: Analyzing the Instacart dataset (Microsoft SQL) with a 10-minute live video presentation on YouTube","archived":false,"fork":false,"pushed_at":"2024-02-08T08:10:39.000Z","size":173,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-23T09:50:38.458Z","etag":null,"topics":["adhoc-analysis","aggregate-functions","datadefinitionlanguage","datamanipulationlanguage","dataquerylanguage","joins","sql"],"latest_commit_sha":null,"homepage":"https://www.youtube.com/@ginohobayan001","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Gino-Freud-Hobayan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-24T05:44:00.000Z","updated_at":"2023-10-01T10:16:32.000Z","dependencies_parsed_at":"2023-08-31T18:39:16.280Z","dependency_job_id":"a6881771-ccb3-45c0-b835-34c9d97272fc","html_url":"https://github.com/Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project","commit_stats":null,"previous_names":["gino-freud-hobayan/sql-projects-from-bootcamp","gino-freud-hobayan/instacart-analysis__sql-capstone-project"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gino-Freud-Hobayan%2FInstacart-Analysis__SQL-Capstone-Project","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gino-Freud-Hobayan%2FInstacart-Analysis__SQL-Capstone-Project/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gino-Freud-Hobayan%2FInstacart-Analysis__SQL-Capstone-Project/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gino-Freud-Hobayan%2FInstacart-Analysis__SQL-Capstone-Project/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Gino-Freud-Hobayan","download_url":"https://codeload.github.com/Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gino-Freud-Hobayan%2FInstacart-Analysis__SQL-Capstone-Project/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29178544,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-06T20:14:21.878Z","status":"ssl_error","status_checked_at":"2026-02-06T20:14:21.443Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["adhoc-analysis","aggregate-functions","datadefinitionlanguage","datamanipulationlanguage","dataquerylanguage","joins","sql"],"created_at":"2024-11-12T12:05:37.806Z","updated_at":"2026-02-06T22:01:25.179Z","avatar_url":"https://github.com/Gino-Freud-Hobayan.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Instacart Dataset Analysis\n\n\u003cimg width=\"749\" alt=\"Instacart banner\" src=\"https://github.com/Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project/assets/117270964/756041f9-b96a-43ce-bf4e-cdf87ac47cca\"\u003e\n\n\u003cbr\u003e\u003cbr\u003e\n\n\n### (Scroll down for the Instacart Capstone Project ⬇️⬇️)\n\n\u003cbr\u003e\n\nThis also serves as the Repository of all the major projects I made \u003cbr\u003e\nduring the 2 week SQL Bootcamp by [Data Vanguard](https://datavanguard.ph/)\n\n\u003cbr\u003e\n\n1.) Aggregate functions and Ad Hoc Analysis on covid19_italy dataset\n\n2.) Assignment # 2 - More Ad Hoc Analysis on covid19_italy dataset\n\n3.) Capstone Project: Analyzing the Instacart dataset using Microsoft SQL\n\n\n\u003cbr\u003e\u003cbr\u003e\u003cbr\u003e\n\n\n## 📊 SQL Capstone Project: [10-minute Presentation on YouTube](https://youtu.be/5TJ5p7ZGM2U)\n\n\n\u003cimg width=\"850\" alt=\"SQL Capstone Proj - Title card w my face\" src=\"https://github.com/Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project/assets/117270964/d3349432-89fe-4384-8e2a-9912310a7d98\"\u003e\n\n\u003cBR\u003e\u003cBR\u003e\u003cBR\u003e\n\n## 📊 Tableau link for the Dashboard: [Instacart dataset analysis Dashboard](https://public.tableau.com/app/profile/gino.freud.hobayan/viz/InstacartdatasetanalysisbyGINOFREUDD_HOBAYAN/Dashboard1)\n\n\n\u003cimg width=\"400\" alt=\"Instacart - Tableau Viz\" src=\"https://github.com/Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project/assets/117270964/15f017e5-873b-4585-be58-58050d3af045\"\u003e\n\u003cimg width=\"400\" alt=\"Instacart - Tableau Viz2\" src=\"https://github.com/Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project/assets/117270964/d02a6fb4-b5f4-45a8-8a07-67232cc7118d\"\u003e\n\n\n\u003cBR\u003e\u003cBR\u003e\u003cBR\u003e\n\n### 👉 [Click here for the Slideshow on Google Slides](https://docs.google.com/presentation/d/1noOMPc8odMdqlKVYHrEb0HAb7qxFdL1T1e0-G1OFej4/edit?usp=sharing)\n\n\u003cBR\u003e\n\n### Analyzing the Instacart Dataset using Microsoft SQL\n\n### Source : https://www.kaggle.com/datasets/yasserh/instacart-online-grocery-basket-analysis-dataset?select=order_products__train.csv\t\n\n\u003cbr\u003e \u003cbr\u003e\u003cbr\u003e \n\n## Awards and Certificate of Completion:\n\n\u003cimg width=\"600\" alt=\"image\" src=\"https://github.com/Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project/assets/117270964/dbd28661-d0c3-49f1-95bc-42dd8c0cc425\"\u003e\n\n\u003cbr\u003e\n\n\u003cimg width=\"600\" alt=\"image\" src=\"https://github.com/Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project/assets/117270964/1b36196a-9ea3-4319-926c-51b9c8d7ce78\"\u003e\n\n\u003cbr\u003e\n\n\u003cimg width=\"600\" alt=\"image\" src=\"https://github.com/Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project/assets/117270964/2f61a366-2923-4e04-9535-872d38b65eb5\"\u003e\n\n\n\n\n\u003cbr\u003e \u003cbr\u003e\u003cbr\u003e \n\n## (ERD) Entity Relationship Diagram \n\u003cimg width=\"810\" alt=\"ERD - Instacart DATABASE\" src=\"https://github.com/Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project/assets/117270964/47f9b05d-ed9c-4fd0-a39c-dac380dbaae8\"\u003e\n\n\u003cbr\u003e \u003cbr\u003e\n\n## Data Dictionary \n\u003cimg width=\"874\" alt=\"DATA DICTIONARY - Instacart DATABASE\" src=\"https://github.com/Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project/assets/117270964/f91d005e-7388-49e2-b28b-c80f58b4f8a3\"\u003e\n\n\u003cbr\u003e\u003cbr\u003e\n\n\n\u003cimg width=\"500\" alt=\"image\" src=\"https://github.com/Gino-Freud-Hobayan/Instacart-Analysis__SQL-Capstone-Project/assets/117270964/9642578d-c5fb-49a3-a9c0-b7d80f7f04f6\"\u003e\n\n\n\n\u003cBR\u003e\u003cBR\u003e\u003cBR\u003e\u003cBR\u003e\n\n\n# DATA DEFINITION LANGUAGE (DDL)\n\n```sql\n\n\n-----------------------------------------------------------\n-----------------------------------------------------------\n-- SQL BOOTCAMP CAPSTONE PROJECT (Instacart Dataset) using Microsoft SQL\n-- BY: GINO FREUD D. HOBAYAN\n-----------------------------------------------------------\n-----------------------------------------------------------\n\n\n-----------------------------------------------------------\n-----------------------------------------------------------\n-- Data Definition Language (DDL)\n-----------------------------------------------------------\n-----------------------------------------------------------\n\nCREATE DATABASE Instacart_DATABASE;\n\n\n\n\n\n-----------------------------------------------------------\n-- products TABLE  (shape: 49.688, 4)\n-----------------------------------------------------------\n\nCREATE TABLE products\n(\n    product_id VARCHAR(50) PRIMARY KEY NOT NULL,\n    product_name VARCHAR(250),\n    aisle_id INTEGER,\n    department_id INTEGER\n);\n\n\n--- Insert all the data from the CSV file into our table (COMMA DELIMITER)\nBULK INSERT products\nFROM \"C:\\Users\\GINO\\Desktop\\SQL Capstone Project\\InstaCart Online Grocery Basket Analysis Dataset\\products.csv\"\nWITH (\n    FIELDTERMINATOR = ',', -- Use COMMA as the delimiter\n    ROWTERMINATOR = '\\n',\n    FIRSTROW = 2 -- Skip the header row if present\n);\n\n\n\n-- Check the TABLE\nSELECT TOP 600\n\t*\nFROM\n\tproducts\nORDER BY \n\tCAST(product_id AS INT);\n\n\n\n\n\n\n\n-----------------------------------------------------------\n-- orders TABLE   (shape: 3.421.083, 7)\n-----------------------------------------------------------\n\nCREATE TABLE orders\n(\n    order_id INTEGER PRIMARY KEY NOT NULL,\n    user_id INTEGER,\n    eval_set VARCHAR(100),\n    order_number INTEGER,\n    order_dow INTEGER,\n    order_hour_of_day INTEGER,\n    days_since_prior_order FLOAT\n);\n\nBULK INSERT orders\nFROM \"C:\\Users\\GINO\\Desktop\\SQL Capstone Project\\InstaCart Online Grocery Basket Analysis Dataset\\orders.csv\"\nWITH (\n    FIELDTERMINATOR = ',',         -- Use COMMA as the delimiter\n    ROWTERMINATOR = '0x0a',        -- Hexadecimal representation of line feed\n    FIRSTROW = 2                   -- Skip the header row if present\n);\n\n\n\n-- Check the TABLE\nSELECT TOP 1000\n\t*\nFROM\n\torders\nORDER BY\n\tuser_id\n\n\n-- days_since_prior_order COLUMN HAS NULL VALUES.\n\n\n\n\n\n\n\n\n\n\n-----------------------------------------------------------\n-- order_product_prior TABLE  (shape: 32.434.489, 4)\n-----------------------------------------------------------\n\nCREATE TABLE order_products__prior\n(\n    order_id INT,\n    product_id VARCHAR(50),\n    add_to_cart_order INT,\n    reordered INT,\n);\n\nBULK INSERT order_products__prior\nFROM \"C:\\Users\\GINO\\Desktop\\SQL Capstone Project\\InstaCart Online Grocery Basket Analysis Dataset\\order_products__prior.csv\"\nWITH (\n    FIELDTERMINATOR = ',',\n    ROWTERMINATOR = '0x0a', -- Hexadecimal representation of line feed\n    FIRSTROW = 2\n);\n\n----------------------------------------------------------------------------------\n-- SUCCESSFULLY CREATED A TABLE WITH 32,400,000 ROWS\n-- QUERIES TAKE QUITE A WHILE\n\n\n-- Check the TABLE\nSELECT TOP 1000\n\t*\nFROM\n\torder_products__prior\nORDER BY\n\torder_id,\n\tadd_to_cart_order\n\n\n\n\n\n\n\n\n\n\n\n\n\n-----------------------------------------------------------\n-- order_product_train TABLE  (shape: 1.384.617, 4)\n-----------------------------------------------------------\n\nCREATE TABLE order_products__train\n(\n    order_id INT,\n    product_id VARCHAR(50),\n    add_to_cart_order INT,\n    reordered INT,\n);\n\nBULK INSERT order_products__train\nFROM \"C:\\Users\\GINO\\Desktop\\SQL Capstone Project\\InstaCart Online Grocery Basket Analysis Dataset\\order_products__train.csv\"\nWITH (\n    FIELDTERMINATOR = ',',\n    ROWTERMINATOR = '0x0a',\n    FIRSTROW = 2\n);\n\n\n-- Check the TABLE\nSELECT TOP 1000\n\t*\nFROM\n\torder_products__train\nORDER BY\n\torder_id,\n\tadd_to_cart_order\n\n\n\n\n\n\n\n\n\n-----------------------------------------------------------\n-- aisles TABLE (shape: 134,2)\n-----------------------------------------------------------\nCREATE TABLE aisles\n(\n    aisle_id INTEGER PRIMARY KEY,\n    aisle_name VARCHAR(255)\n);\n\n\nINSERT INTO aisles \n\t(aisle_id, aisle_name)\nVALUES\n(1, 'prepared soups salads'),\n(2, 'specialty cheeses'),\n(3, 'energy granola bars'),\n(4, 'instant foods'),\n(5, 'marinades meat preparation'),\n(6, 'other'),\n(7, 'packaged meat'),\n(8, 'bakery desserts'),\n(9, 'pasta sauce'),\n(10, 'kitchen supplies'),\n(11, 'cold flu allergy'),\n(12, 'fresh pasta'),\n(13, 'prepared meals'),\n(14, 'tofu meat alternatives'),\n(15, 'packaged seafood'),\n(16, 'fresh herbs'),\n(17, 'baking ingredients'),\n(18, 'bulk dried fruits vegetables'),\n(19, 'oils vinegars'),\n(20, 'oral hygiene'),\n(21, 'packaged cheese'),\n(22, 'hair care'),\n(23, 'popcorn jerky'),\n(24, 'fresh fruits'),\n(25, 'soap'),\n(26, 'coffee'),\n(27, 'beers coolers'),\n(28, 'red wines'),\n(29, 'honeys syrups nectars'),\n(30, 'latino foods'),\n(31, 'refrigerated'),\n(32, 'packaged produce'),\n(33, 'kosher foods'),\n(34, 'frozen meat seafood'),\n(35, 'poultry counter'),\n(36, 'butter'),\n(37, 'ice cream ice'),\n(38, 'frozen meals'),\n(39, 'seafood counter'),\n(40, 'dog food care'),\n(41, 'cat food care'),\n(42, 'frozen vegan vegetarian'),\n(43, 'buns rolls'),\n(44, 'eye ear care'),\n(45, 'candy chocolate'),\n(46, 'mint gum'),\n(47, 'vitamins supplements'),\n(48, 'breakfast bars pastries'),\n(49, 'packaged poultry'),\n(50, 'fruit vegetable snacks'),\n(51, 'preserved dips spreads'),\n(52, 'frozen breakfast'),\n(53, 'cream'),\n(54, 'paper goods'),\n(55, 'shave needs'),\n(56, 'diapers wipes'),\n(57, 'granola'),\n(58, 'frozen breads doughs'),\n(59, 'canned meals beans'),\n(60, 'trash bags liners'),\n(61, 'cookies cakes'),\n(62, 'white wines'),\n(63, 'grains rice dried goods'),\n(64, 'energy sports drinks'),\n(65, 'protein meal replacements'),\n(66, 'asian foods'),\n(67, 'fresh dips tapenades'),\n(68, 'bulk grains rice dried goods'),\n(69, 'soup broth bouillon'),\n(70, 'digestion'),\n(71, 'refrigerated pudding desserts'),\n(72, 'condiments'),\n(73, 'facial care'),\n(74, 'dish detergents'),\n(75, 'laundry'),\n(76, 'indian foods'),\n(77, 'soft drinks'),\n(78, 'crackers'),\n(79, 'frozen pizza'),\n(80, 'deodorants'),\n(81, 'canned jarred vegetables'),\n(82, 'baby accessories'),\n(83, 'fresh vegetables'),\n(84, 'milk'),\n(85, 'food storage'),\n(86, 'eggs'),\n(87, 'more household'),\n(88, 'spreads'),\n(89, 'salad dressing toppings'),\n(90, 'cocoa drink mixes'),\n(91, 'soy lactosefree'),\n(92, 'baby food formula'),\n(93, 'breakfast bakery'),\n(94, 'tea'),\n(95, 'canned meat seafood'),\n(96, 'lunch meat'),\n(97, 'baking supplies decor'),\n(98, 'juice nectars'),\n(99, 'canned fruit applesauce'),\n(100, 'missing'),\n(101, 'air fresheners candles'),\n(102, 'baby bath body care'),\n(103, 'ice cream toppings'),\n(104, 'spices seasonings'),\n(105, 'doughs gelatins bake mixes'),\n(106, 'hot dogs bacon sausage'),\n(107, 'chips pretzels'),\n(108, 'other creams cheeses'),\n(109, 'skin care'),\n(110, 'pickled goods olives'),\n(111, 'plates bowls cups flatware'),\n(112, 'bread'),\n(113, 'frozen juice'),\n(114, 'cleaning products'),\n(115, 'water seltzer sparkling water'),\n(116, 'frozen produce'),\n(117, 'nuts seeds dried fruit'),\n(118, 'first aid'),\n(119, 'frozen dessert'),\n(120, 'yogurt'),\n(121, 'cereal'),\n(122, 'meat counter'),\n(123, 'packaged vegetables fruits'),\n(124, 'spirits'),\n(125, 'trail mix snack mix'),\n(126, 'feminine care'),\n(127, 'body lotions soap'),\n(128, 'tortillas flat bread'),\n(129, 'frozen appetizers sides'),\n(130, 'hot cereal pancake mixes'),\n(131, 'dry pasta'),\n(132, 'beauty'),\n(133, 'muscles joints pain relief'),\n(134, 'specialty wines champagnes');\n\n\n\n-- Check the TABLE\nSELECT\n\t*\nFROM\n\taisles\n\n\n\n\n\n-----------------------------------------------------------\n-- departments TABLE (shape: 21,2)\n-----------------------------------------------------------\n\nCREATE TABLE departments \n(\n  department_id INT PRIMARY KEY,\n  department VARCHAR(255) NOT NULL\n);\n\nINSERT INTO departments\n  (department_id, department)\nVALUES\n  ('1', 'frozen'),\n  ('2', 'other'),\n  ('3', 'bakery'),\n  ('4', 'produce'),\n  ('5', 'alcohol'),\n  ('6', 'international'),\n  ('7', 'beverages'),\n  ('8', 'pets'),\n  ('9', 'dry goods pasta'),\n  ('10', 'bulk'),\n  ('11', 'personal care'),\n  ('12', 'meat seafood'),\n  ('13', 'pantry'),\n  ('14', 'breakfast'),\n  ('15', 'canned goods'),\n  ('16', 'dairy eggs'),\n  ('17', 'household'),\n  ('18', 'babies'),\n  ('19', 'snacks'),\n  ('20', 'deli'),\n  ('21', 'missing');\n\n\n-- Check the TABLE\nSELECT\n\t*\nFROM\n\tdepartments\n\n\n\n\n\n-- TRIAL ------------------------------------------------------------------\n\n\n-- MERGE THE IDENTICAL TABLES INTO ONE\nCREATE TABLE all_order_products_combined\n(\n    order_id INT,\n    product_id VARCHAR(50),\n    add_to_cart_order INT,\n    reordered INT\n);\n\n-- Insert data from order_products__prior\nINSERT INTO all_order_products_combined (order_id, product_id, add_to_cart_order, reordered)\nSELECT order_id, product_id, add_to_cart_order, reordered FROM order_products__prior;\n\n-- Insert data from order_products__train\nINSERT INTO all_order_products_combined (order_id, product_id, add_to_cart_order, reordered)\nSELECT order_id, product_id, add_to_cart_order, reordered FROM order_products__train;\n\n\n\n\n-----------------------------------------------\nSELECT TOP 1000\n\t*\nFROM\n\tall_order_products_combined\n\n\n------------------------------------------------\nSELECT \n\tCOUNT(*) AS total_num_of_rows\nFROM\n\tall_order_products_combined\n\n-- 33,819,106 rows\n\n\n\n-- TRIAL ------------------------------------------------------------------\n\n```\n\u003cbr\u003e\u003cbr\u003e\n\n\n\n\n\n# DATA MANIPULATION LANGUAGE (DML)\n\n```sql\n\n-----------------------------------------------------------\n-----------------------------------------------------------\n-- DATA CLEANING\n-- Data Manipulation Language (DML)\n-----------------------------------------------------------\n-----------------------------------------------------------\n\n\n---------------------------------------------------------\n-- CHECKING FOR DUPLICATE VALUES\n---------------------------------------------------------\n\n-- products TABLE\nSELECT\n\tproduct_name,\n\tCOUNT(*) AS total_num\n\nFROM \n\tproducts\n\nGROUP BY \n\tproduct_name\nHAVING COUNT(product_name) \u003e 1\n\n/*\nANSWER: 101 DUPLICATE VALUES FOUND\n\n\n\n\nBusiness Logic: Consider the business logic behind the data. \n\nThey look like duplicate values on paper,\nbut they have their own unique primary keys, they might be a different variation of the product\nand might have orders connected with them and their unique primary key and product_id\n\nI did not delete these, as it will affect the order counts, item popularity and other metrics\n\nDeleting these duplicates could potentially disrupt the integrity of the data and impact the analysis.\n\n*/\n\n\n\n/* \n\nExample: Bag of Oranges\n1.) product_id = 4377\n2.) product_id = 45231\n\n*/\n\nSELECT \n\t*\nFROM\n\tproducts AS p\n\nLEFT JOIN all_order_products_combined AS AOP\nON AOP.product_id = p.product_id\n\nLEFT JOIN orders AS o\nON o.order_id = AOP.order_id\n\nWHERE\n\tp.product_name = 'Bag of Oranges'\n\n\n\n\n\n\n\n---------------------------------------------------------\n-- CHECKING FOR NULL VALUES\n---------------------------------------------------------\n\n-- orders TABLE\nSELECT\n    COUNT(*) AS TOTAL_NUM_OF_RECORDS,\n    COUNT(order_id) AS non_null_order_id,\n    COUNT(user_id) AS non_null_user_id,\n    COUNT(eval_set) AS non_null_eval_set,\n    COUNT(order_number) AS non_null_order_number,\n    COUNT(order_dow) AS non_null_order_dow,\n    COUNT(order_hour_of_day) AS non_null_order_hour_of_day,\n    COUNT(days_since_prior_order) AS non_null_days_since_prior_order\nFROM \n    orders;\n\n-- 206,209 NULL VALUES FOUND on \"orders\" TABLE and \"days_since_prior_order\" COLUMN\n\n\nSELECT\n\t*\nFROM\n\torders\nWHERE\n\tdays_since_prior_order IS NULL\n\n-- 206,209 null values CONFIRMED\n\n\n\n\n\n-- orders TABLE\nSELECT\n    COUNT(*) AS TOTAL_NUM_OF_RECORDS\nFROM \n    orders\nWHERE\n\torder_number = 1\n\n/*\nBusiness Logic: Consider the business logic behind the data. \n\nThis is normal\nIt can be seen that for every users 1st order (order_number = 1) \nthe days_since_prior_order is NULL, which makes sense, since it's the very first order.\n\nTherefore the NULL VALUES are valid.\n\n\n\n\n\nBusiness Logic: Consider the business logic behind the data. \n\nThe NULL VALUES are valid. \nBecause they correspond to the first orders for each user, \nwhere there is no prior order to calculate the time since.\n\nIn this case, it's not necessary to replace these null values with \"NA\" or any other value, \nas they already hold a meaningful interpretation.\n\nKeeping them as null values preserves the distinction between the first orders and subsequent orders.\n\n\nWe can also infer that there are 206,209 users/customers.\n\n*/\n\n\n\n\n\n---------------------------------------------------------\n-- Leading and Trailing spaces using TRIM()?\n---------------------------------------------------------\n\n-------- aisles table - aisle_name\nUPDATE aisles\nSET aisle_name = TRIM(aisle_name);\n\n\n-------- departments table - department\nUPDATE departments\nSET department = TRIM(department);\n\n\n-------- products table - product_name\nUPDATE products\nSET product_name = TRIM(product_name);\n\n\n--------- orders table - eval_set\nUPDATE orders\nSET eval_set = TRIM(eval_set);\n\n\n/*\nFind all string columns for TRIM()\nI made sure that all string columns have no excessive spaces using TRIM() function.\n*/\n\n\n```\n\n\u003cbr\u003e\u003cbr\u003e\n\n\n\n# DATA QUERY LANGUAGE (DQL)\n\n```sql\n\n\n-- 1.) What are the most frequently ordered products? \n\nSELECT TOP 10\n\tp.product_name,\n\tCOUNT(o.order_number) AS ORDER_COUNT\n\nFROM\n\tproducts AS p\n\nJOIN all_order_products_combined AS AOP\nON AOP.product_id = p.product_id\n\nJOIN orders AS o\nON o.order_id = AOP.order_id\n\nGROUP BY\n\tp.product_name\nORDER BY\n\tORDER_COUNT DESC\n\n\n\n/*\n\nANSWER:\n\nproduct_name, ORDER_COUNT\n\nBanana,\t491291\nBag of Organic Bananas,\t394930\nOrganic Strawberries,\t275577\nOrganic Baby Spinach,\t251705\nOrganic Hass Avocado,\t220877\nOrganic Avocado,\t184224\nLarge Lemon,\t160792\nStrawberries,\t149445\nLimes,\t146660\nOrganic Whole Milk,\t142813\n\n*/\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n-- 2.) What are the least frequently ordered products?\nSELECT TOP 10\n\tp.product_name,\n\tCOUNT(o.order_number) AS ORDER_COUNT\n\nFROM\n\tproducts AS p\n\nJOIN all_order_products_combined AS AOP\nON AOP.product_id = p.product_id\n\nJOIN orders AS o\nON o.order_id = AOP.order_id\n\nGROUP BY\n\tproduct_name\nORDER BY\n\tORDER_COUNT ASC\n\n\n/*\n\nANSWER: \n\n(product_name, ORDER_COUNT)\n\n'Swingtop' Premium Lager,\t1\n1 000 Mg Vitamin C Tangerine Grapefruit Effervescent Powdered Drink Mix,\t1\n11.3 Oz. Oreo Fudge Creme Double Chocolate,\t1\n12 Inch Taper Candle White,\t1\n7.04 Oz. Grahamfuls Banana Vanilla 8ct,\t1\nAged Parmesan Cheese Sticks,\t1\nAll Natural Stevia Liquid Extract Sweetener,\t1\nAnarchy For Her Daily Fragrance,\t1\nAnjou Pear Hand Soap,\t1\nBerry Sprouted Blend Cereal,\t1\n\n*/\n\n\n\n\n\n\n\n\n\n\n\n\n\n-- 3.) Are there specific days of the week when orders are more frequent?\n\n-- The days are anonymized, Day 0 could mean Sunday (or Monday if we go by ISO 8601)\nSELECT\n\torder_dow,\n\tCOUNT(order_number) AS ORDER_COUNT\nFROM\n\torders\nGROUP BY\n\torder_dow\nORDER BY\n\torder_dow ASC\n\n/*\n\nANSWER: \n\n(order_dow, ORDER_COUNT)\n0\t600905\n1\t587478\n2\t467260\n3\t436972\n4\t426339\n5\t453368\n6\t448761\n\n*/\n\n\n\n\n\n\n\n\n\n\n\n\n-- 4.) Are there specific hours of the day when orders are more frequent?\n\nSELECT\n\torder_hour_of_day,\n\tCOUNT(order_number) AS ORDER_COUNT\nFROM\n\torders\nGROUP BY\n\torder_hour_of_day\nORDER BY\n\torder_hour_of_day ASC\n\n\n\n/*\n\nANSWER: \n\n(order_hour_of_day, ORDER_COUNT)\n\n0\t22758\n1\t12398\n2\t7539\n3\t5474\n4\t5527\n5\t9569\n6\t30529\n7\t91868\n8\t178201\n9\t257812\n10\t288418\n11\t284728\n12\t272841\n13\t277999\n14\t283042\n15\t283639\n16\t272553\n17\t228795\n18\t182912\n19\t140569\n20\t104292\n21\t78109\n22\t61468\n23\t40043\n\n\n*/\n\n\n\n\n\n\n\n\n\n\n\n\n\n-- 5.) What are the most frequently REORDERED products?\n\nSELECT TOP 10\n    p.product_name,\n    COUNT(AOP.product_id) AS REORDER_COUNT\n\nFROM\n    products AS p \n\nJOIN all_order_products_combined AS AOP\nON AOP.product_id = p.product_id\n\nJOIN orders AS o\nON o.order_id = AOP.order_id\n\nWHERE\n    AOP.reordered = 1\nGROUP BY\n    p.product_name\nORDER BY\n\tREORDER_COUNT DESC\n\n\n/*\n\nANSWER: \n\n(product_name, REORDER_COUNT)\n\nBanana,\t415166\nBag of Organic Bananas,\t329275\nOrganic Strawberries,\t214448\nOrganic Baby Spinach,\t194939\nOrganic Hass Avocado,\t176173\nOrganic Avocado,\t140270\nOrganic Whole Milk,\t118684\nLarge Lemon,\t112178\nOrganic Raspberries,\t109688\nStrawberries,\t104588\n\n\nOrganic fruits like Banana, Strawberries, and Avocados are frequently reordered\nMilk is also frequently reordered.\n\nThese are the products that customer tend to stick with over time.\n\n\n*/\n\n\n\n\n\n\n\n\n\n\n\n\n\n-- 6.) Which products were never ordered / reordered?\n\nSELECT \n\tp.product_name,\n\tCOUNT(o.order_id) AS ORDER_COUNT\nFROM\n\tproducts AS p\n\nLEFT JOIN all_order_products_combined AS AOP \nON AOP.product_id = p.product_id\n\nLEFT JOIN orders AS o \nON o.order_id = AOP.order_id\n\nGROUP BY\n\tp.product_name\nHAVING\n\tCOUNT(o.order_id) = 0\n\n\n/*\n\nANSWER: \n\n(product_name, ORDER_COUNT)\n\nProtein Granola Apple Crisp\t0\nSingle Barrel Kentucky Straight Bourbon Whiskey\t0\nUnpeeled Apricot Halves in Heavy Syrup\t0\n\n\n\n\nWe used LEFT JOIN to include all products even if there are no corresponding orders.\nWe used GROUP BY p.product_name to count the orders for each product.\nWe used HAVING clause to filter out products that have a count of orders equal to 0, meaning they were never ordered or reordered.\n\n\n*/\n\n\n\n\n\n\n\n\n\n-- TRIAL -------------------------------------------------------------------\n\nSELECT \n\t--*\n\tp.product_name,\n\tCOUNT(o.order_number) AS ORDER_COUNT\n\nFROM\n\tproducts AS p\n\nJOIN all_order_products_combined AS AOP\nON AOP.product_id = p.product_id\n\nJOIN orders AS o\nON o.order_id = AOP.order_id\n\nGROUP BY\n\tp.product_name\nHAVING\n      o.order_number = 0\n\n-- TRIAL -------------------------------------------------------------------\n\n\n\n\n\n\n\n-- 7.) What aisles are the most popular? (by order count)\n\nSELECT TOP 20\n\ta.aisle_name,\n\tCOUNT(o.order_number) AS ORDER_COUNT\n\nFROM \n\taisles AS a\n\nJOIN products AS p\nON a.aisle_id = p.aisle_id\n\nJOIN all_order_products_combined AS AOP\nON AOP.product_id = p.product_id\n\nJOIN orders as o\nON o.order_id = AOP.order_id\n\nGROUP BY\n\ta.aisle_name\nORDER BY\n\tORDER_COUNT DESC\n\n\n\n/*\n\nANSWER: \n\n(aisle_name, ORDER_COUNT)\n\nfresh fruits,\t3792661\nfresh vegetables,\t3568630\npackaged vegetables fruits,\t1843806\nyogurt,\t1507583\npackaged cheese,\t1021462\nmilk,\t923659\nwater seltzer sparkling water,\t878150\nchips pretzels,\t753739\nsoy lactosefree,\t664493\nbread,\t608469\nrefrigerated,\t599109\nfrozen produce,\t545107\nice cream ice,\t521101\ncrackers,\t478430\nenergy granola bars,\t473835\neggs,\t472009\nlunch meat,\t412087\nfrozen meals,\t408520\nbaby food formula,\t395654\nfresh herbs,\t393793\n\n*/\n\n\n\n-- Using the ERD makes it easier to visualize what tables we need and what PATH we need to take\n\n\n\n-- TRIAL AND ERROR -----------------------------------------------------\nSELECT TOP 20\n\t--*\n\t--DISTINCT a.aisle_name AS DISTINCT_aisle_name\n\ta.aisle_name,\n\t--o.order_number,\n\tCOUNT(o.order_number) AS COUNT_of_order_number\n\nFROM\n\taisles AS a\n\nJOIN products AS p\nON a.aisle_id = p.aisle_id\n\nJOIN order_products__prior as op_p\nON op_p.product_id = p.product_id\n\nJOIN orders as o\nON o.order_id = op_p.order_id\n\nGROUP BY\n\ta.aisle_name\n\t--o.order_number\nORDER BY\n\tcount_of_order_number DESC\n-- TRIAL AND ERROR -----------------------------------------------------\n\n\n\n\n\n\n-- 8.) What departments are the most popular? (by order count)\n\nSELECT\n\td.department,\n\tCOUNT(o.order_number) AS ORDER_COUNT\n\nFROM\n\tdepartments AS d\n\nJOIN products AS p\nON d.department_id = p.department_id\n\nJOIN all_order_products_combined AS AOP\nON AOP.product_id = p.product_id\n\nJOIN orders as o\nON o.order_id = AOP.order_id\n\nGROUP BY\n\td.department\nORDER BY\n\tORDER_COUNT DESC\n\n\n\n/*\n\nANSWER: \n\n(department, ORDER_COUNT)\n\nproduce,\t9888378\ndairy eggs,\t5631067\nsnacks,\t3006412\nbeverages,\t2804175\nfrozen,\t2336858\npantry,\t1956819\nbakery,\t1225181\ncanned goods,\t1114857\ndeli,\t1095540\ndry goods pasta,\t905340\nhousehold,\t774652\nmeat seafood,\t739238\nbreakfast,\t739069\npersonal care,\t468693\nbabies\t438743\ninternational\t281155\nalcohol\t159294\npets\t102221\nmissing\t77396\nother\t38086\nbulk\t35932\n\n\n*/\n\n\n\n\n\n\n\n\n\n\n-- 9.) When do customers usually reorder? \n\nSELECT\n\tdays_since_prior_order,\n\tCOUNT(order_number) AS ORDER_COUNT\n\nFROM\n\torders\nGROUP BY\n\tdays_since_prior_order\n\nORDER BY\n\tdays_since_prior_order ASC\n\n/*\n\nANSWERS:\n\n(days_since_prior_order, ORDER_COUNT)\n\nNULL\t206209\n0\t67755\n1\t145247\n2\t193206\n3\t217005\n4\t221696\n5\t214503\n6\t240013\n7\t320608\n8\t181717\n9\t118188\n10\t95186\n11\t80970\n12\t76146\n13\t83214\n14\t100230\n15\t66579\n16\t46941\n17\t39245\n18\t35881\n19\t34384\n20\t38527\n21\t45470\n22\t32012\n23\t23885\n24\t20712\n25\t19234\n26\t19016\n27\t22013\n28\t26777\n29\t19191\n30\t369323\n\n\n(LIMITATIONS: days_since_prior_order is capped at 30)\n\nUsers/customers tend to order the most after 7 days or 30 days.\nMost customers buy groceries either Weekly or Monthly.\n\n*/\n\n\n\n\n\n-- END\n\n\n```\n\n\n\u003cbr\u003e\u003cbr\u003e\n\n![Thank you wordcloud1](https://github.com/Gino-Freud-Hobayan/SQL-Projects-from-Bootcamp/assets/117270964/a4aef423-bd7f-423a-a657-b40b8b25f000)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgino-freud-hobayan%2Finstacart-analysis__sql-capstone-project","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgino-freud-hobayan%2Finstacart-analysis__sql-capstone-project","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgino-freud-hobayan%2Finstacart-analysis__sql-capstone-project/lists"}