{"id":20733383,"url":"https://github.com/moindalvs/excelr_data_analyst_sql_assignment_part3","last_synced_at":"2025-07-11T16:35:36.038Z","repository":{"id":127428629,"uuid":"568666619","full_name":"MoinDalvs/EXCELR_Data_Analyst_SQL_Assignment_Part3","owner":"MoinDalvs","description":"1. Write a stored procedure that accepts the month and year as inputs and prints the ordernumber, orderdate and status of the orders placed in that month. ","archived":false,"fork":false,"pushed_at":"2023-01-18T07:38:21.000Z","size":9,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-11T10:47:11.419Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/MoinDalvs.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":"2022-11-21T06:32:40.000Z","updated_at":"2023-08-01T09:47:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"a0c4654c-16b8-41ec-9e82-645fbbe6f374","html_url":"https://github.com/MoinDalvs/EXCELR_Data_Analyst_SQL_Assignment_Part3","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/MoinDalvs/EXCELR_Data_Analyst_SQL_Assignment_Part3","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MoinDalvs%2FEXCELR_Data_Analyst_SQL_Assignment_Part3","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MoinDalvs%2FEXCELR_Data_Analyst_SQL_Assignment_Part3/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MoinDalvs%2FEXCELR_Data_Analyst_SQL_Assignment_Part3/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MoinDalvs%2FEXCELR_Data_Analyst_SQL_Assignment_Part3/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MoinDalvs","download_url":"https://codeload.github.com/MoinDalvs/EXCELR_Data_Analyst_SQL_Assignment_Part3/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MoinDalvs%2FEXCELR_Data_Analyst_SQL_Assignment_Part3/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264851740,"owners_count":23673293,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-11-17T05:25:07.700Z","updated_at":"2025-07-11T16:35:36.017Z","avatar_url":"https://github.com/MoinDalvs.png","language":null,"readme":"# SET 3\n\n## [Download Link for SQL Database](https://raw.githubusercontent.com/MoinDalvs/EXCELR_Data_Analyst_SQL_Assignment_Part1/main/ConsolidatedTables.sql)\n\n    use assignment;\n\n## 1. Write a stored procedure that accepts the month and year as inputs and prints the ordernumber, orderdate and status of the orders placed in that month. \n\n-- Example:  call order_status(2005, 11);\n             \n    DELIMITER //\n    Create procedure order_status( IN t_year INT,\n                                        IN t_month INT )\n      BEGIN \n        select orderNumber,\n                   orderdate,\n                   status\n          from orders\n            where year(orderDate) = t_year\n                AND\n                    month(orderDate) = t_month;\n      END //\n    DELIMITER ;\n\n    call order_status(2005, 4);\n\n## 2. Write a stored procedure to insert a record into the cancellations table for all cancelled orders.\n\n-- STEPS: \n\n-- a.\tCreate a table called cancellations with the following fields\n\n-- id (primary key), \n-- customernumber (foreign key - Table customers), \n-- ordernumber (foreign key - Table Orders), \n-- comments\n\n-- All values except id should be taken from the order table.\n\n-- b. Read through the orders table . If an order is cancelled, then put an entry in the cancellations table.\n\n    DELIMITER //\n    CREATE PROCEDURE cancelled_order( )\n      BEGIN\n        DROP TABLE IF EXISTS cancellation ;\n        CREATE TABLE cancellation\n          (\n                 id int primary key auto_increment,\n                 customerNumber int,\n                 orderNumber int,\n                 comments text,\n                 FOREIGN KEY (customerNumber)\n            REFERENCES customers(customerNumber)\n              ON DELETE CASCADE,\n                 FOREIGN KEY (orderNumber)\n            REFERENCES orders(orderNumber)\n              ON DELETE CASCADE\n           );\n           INSERT INTO cancellation ( customerNumber, orderNumber, comments)\n                             SELECT   customerNumber, orderNumber, comments\n                      FROM orders\n                        WHERE status = 'Cancelled';\n           SELECT *\n            FROM cancellation;\n        END //\n    DELIMITER ;\n\n    CALL cancelled_order();\n\n-- ANOTHER APPROACH\n\n    DROP PROCEDURE IF EXISTS cancelled_order;\n\n    DELIMITER //\n    CREATE DEFINER=`root`@`localhost` PROCEDURE `cancelled_order`( )\n    BEGIN\n      CREATE TABLE IF NOT EXISTS cancellation\n          (\n                 id int primary key auto_increment,\n                 customerNumber int,\n                 orderNumber int,\n                 comments text,\n                 FOREIGN KEY (customerNumber)\n            REFERENCES customers(customerNumber)\n              ON DELETE CASCADE,\n                 FOREIGN KEY (orderNumber)\n            REFERENCES orders(orderNumber)\n              ON DELETE CASCADE\n           );\n      INSERT INTO cancellation ( customerNumber, orderNumber, comments)\n                SELECT   customerNumber, orderNumber, comments\n                FROM orders\n                  WHERE status = 'Cancelled' AND\n                  NOT EXISTS ( select customerNumber, orderNumber, comments from cancellation );\n      SELECT *\n        FROM cancellation;\t\t\n        END //\n    DELIMITER ;\n\n\n    CALL cancelled_order();\n\n## 3. a. Write function that takes the customernumber as input and returns the purchase_status based on the following criteria . [table:Payments]\n\n-- if the total purchase amount for the customer is \u003c 25000 status = Silver, amount between 25000 and 50000, status = Gold\n-- if amount \u003e 50000 Platinum\n\n    select *,\n         CASE\n          WHEN amount \u003c 25000 THEN 'Silver'\n          WHEN amount BETWEEN 25000 AND 50000 THEN 'Gold'\n                ELSE 'Platinum'\n                END AS Status\n      from payments;\n\n    DELIMITER //\n\n    CREATE PROCEDURE customer_status( cust_No INT )    \n        BEGIN\n        SELECT CASE\n             WHEN amount \u003c 25000 THEN 'Silver'\n             WHEN amount BETWEEN 25000 AND 50000 THEN 'Gold'\n                   ELSE 'Platinum'\n                   END AS Status\n          from payments\n            where customerNumber = cust_No;\n\n      END //\n\n    DELIMITER ;\n\n    CALL customer_status( 103 );\n\n-- b. Write a query that displays customerNumber, customername and purchase_status from customers table.\n\n    select c.customerNumber,\n         c.customerName,\n           o.status\n      from customers c\n        LEFT JOIN orders o\n        USING (customerNumber);\n\n## 4. Replicate the functionality of 'on delete cascade' and 'on update cascade' using triggers on movies and rentals tables.\n-- Note: Both tables - movies and rentals - don't have primary or foreign keys. Use only triggers to implement the above.\n\n-- Q. For ON DELETE CASCADE, if a parent with an id is deleted, a record in child with parent_id = parent.id will be automatically deleted. This should be no problem.\n\n-- 1. This means that ON UPDATE CASCADE will do the same thing when id of the parent is updated?\n\n-- 2. If (1) is true, it means that there is no need to use ON UPDATE CASCADE if parent.id is not updatable (or will never be updated) like when it is AUTO_INCREMENT or always set to be TIMESTAMP. Is that right?\n\n-- 3. If (2) is not true, in what other kind of situation should we use ON UPDATE CASCADE?\n\n-- A. It's true that if your primary key is just an identity value auto incremented, you would have no real use for ON UPDATE CASCADE.\n\n-- However, let's say that your primary key is a 10 digit UPC bar code and because of expansion, you need to change it to a 13-digit UPC bar code. In that case, \n-- ON UPDATE CASCADE would allow you to change the primary key value and any tables that have foreign key references to the value will be changed accordingly.\n\n-- In reference to #4, if you change the child ID to something that doesn't exist in the parent table (and you have referential integrity), you should get a foreign key error.\n\nWhat if I (for some reason) update the child.parent_id to be something not existing, will it then be automatically deleted?\n\n    DELIMITER //\n    CREATE TRIGGER delete_cascade\n      AFTER DELETE on movies\n        FOR EACH ROW \n        BEGIN\n          UPDATE rentals\n            SET movieid = NULL\n              WHERE movieid\n                           NOT IN\n                ( SELECT distinct id\n                  from movies );\n        END //\n    DELIMITER ;\n\n    drop trigger if exists delete_cascade;\n\n    select *\n      from movies;\n\n    INSERT INTO movies ( id,             title,          category )\n          Values ( 11, 'The Dark Knight', 'Action/Adventure');\n\n    INSERT INTO rentals ( memid, first_name, last_name, movieid ) \n               Values (     9,     'Moin',   'Dalvi',      11 );\n\n    delete from movies\n      where id = 11;\n\n    SELECT id\n      from movies;\n\n    SELECT *\n      from rentals;\n\n    DELIMITER //\n    CREATE TRIGGER update_cascade\n      AFTER UPDATE on movies\n        FOR EACH ROW \n        BEGIN\n          UPDATE rentals\n            SET movieid = new.id\n              WHERE movieid = old.id;\n        END //\n    DELIMITER ;\n\n    DROP trigger if exists update_cascade;\n\n    INSERT INTO movies ( id,             title,          category )\n          Values ( 12, 'The Dark Knight', 'Action/Adventure'); \n\n    UPDATE rentals\n      SET movieid = 12\n        WHERE memid = 9;\n\n    UPDATE movies\n      SET id = 11\n        WHERE title regexp 'Dark Knight';\n\n    select *\n      from movies;\n\n    select *\n      from rentals;\n\n## 5. Select the first name of the employee who gets the third highest salary. [table: employee]\n\n    select *\n      from employee\n        order by salary desc\n          limit 2,1;\n\n## 6. Assign a rank to each employee  based on their salary. The person having the highest salary has rank 1. [table: employee]\n\n    select *,\n         dense_rank () OVER (order by salary desc) as Rank_salary\n      from employee;\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoindalvs%2Fexcelr_data_analyst_sql_assignment_part3","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoindalvs%2Fexcelr_data_analyst_sql_assignment_part3","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoindalvs%2Fexcelr_data_analyst_sql_assignment_part3/lists"}