#rs/assignment #rs/class/ad450 - - - ## Task 1 ```SQL SELECT country, "1990" + "1991" + "1992" + "1993" + "1994" AS total_volume FROM coffee_export ORDER BY total_volume DESC ``` ![[Week4Task1.csv]] Looking at the data we can see that countries like Brazil and Columbia are some of the largest exporters of coffee between 1990 and 1994. This likely means that they have large industries and are great countries to look to for production since coffee production is more common in them. ## Task 2 ```SQL SELECT RANK() OVER (ORDER BY b.total_volume DESC) AS rank, b.country, b.total_volume FROM ( SELECT country, "1999_2000" + "2000_2001" + "2001_2002" + "2002_2003" + "2003_2004" AS total_volume FROM coffee_production ) b LIMIT 10 ``` ![[Week4Task2.csv]] Alternate for task 2 ```SQL WITH summed_years as ( SELECT country, "1999_2000" + "2000_2001" + "2001_2002" + "2002_2003" + "2003_2004" AS total_volume FROM coffee_production ) SELECT RANK() OVER (ORDER BY total_volume DESC) AS rank, country, total_volume FROM summed_years LIMIT 10 ``` There are a lot of the same countries on the top exporters list on the top producers list meaning that their coffee industries are likely largely focused on international markers rather than domestic ones. Sourcing all product from one country could be a risk since if something happened to their coffee industry or economy it would be challenging to pivot somewhere else for production. ## Task 3 ```SQL WITH summed_years as ( SELECT country, coffee_type, "1990_1991" + "1991_1992" + "1992_1993" + "1993_1994" AS total_volume FROM coffee_production ), ranked as ( SELECT DENSE_RANK() OVER (PARTITION BY coffee_type ORDER BY total_volume DESC) AS rank, country, coffee_type, total_volume FROM summed_years ) SELECT coffee_type, country, total_volume from ranked WHERE rank = 2 ``` "Runner-up" countries such as these could present an opportunity since they likely still have established coffee industries but there might not be as much competition from other large companies. It could be easier to expand and grow without as much competition from other large brands. ## Task 4 ```SQL WITH summed_exports as ( SELECT country, "1995" + "1996" + "1997" + "1998" + "1999" + "2000" AS total_export FROM coffee_re_export --looking at countries that are re-exporting coffee rather than exporting for the first time ), summed_imports AS ( SELECT country, "1995" + "1996" + "1997" + "1998" + "1999" + "2000" AS total_import FROM coffee_import ) SELECT COALESCE(e.country, i.country) AS country, COALESCE(e.total_export, 0) AS export_vol, COALESCE(i.total_import, 0) AS import_vol, COALESCE(e.total_export, 0) + COALESCE(i.total_import, 0) as total_vol FROM summed_exports e FULL JOIN summed_imports i ON e.country = i.country ORDER BY total_vol DESC LIMIT 5 ``` Countries such as the United States, Germany and France act as some of the world's primary "coffee clearinghouses" since they import and then re-export the largest quantities of coffee. This means that they are focusing more on processing more than production. ## Task 5 ```SQL SELECT i.country AS importing_country, i.total_import AS importing_amount, e.country AS exporting_country, e.total_export AS exporting_amount FROM coffee_import i CROSS JOIN LATERAL ( SELECT e.country, e.total_export FROM coffee_export e ORDER BY ABS(i.total_import - e.total_export) ASC LIMIT 1 ) e ``` With the data we have it is not possible to show the country of origin for the coffee. The closest I got was guessing based on matching similar values of imports and exports but in most cases countries will be importing from or exporting to multiple sources. Additional data in the form of percentage import or export from each country or origin of export and import would be needed to complete this request.