{"id":20021768,"url":"https://github.com/ndleah/northwind","last_synced_at":"2026-03-06T10:31:06.341Z","repository":{"id":105089173,"uuid":"405143514","full_name":"ndleah/northwind","owner":"ndleah","description":"💼 SQL, company data analysis","archived":false,"fork":false,"pushed_at":"2021-11-12T09:52:49.000Z","size":3792,"stargazers_count":12,"open_issues_count":0,"forks_count":6,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-02T03:43:08.423Z","etag":null,"topics":["sql"],"latest_commit_sha":null,"homepage":"","language":"SQL","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/ndleah.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":"2021-09-10T16:23:29.000Z","updated_at":"2024-12-06T03:36:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"5934ad97-6bac-4131-acd7-caf78c8a8f88","html_url":"https://github.com/ndleah/northwind","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ndleah/northwind","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndleah%2Fnorthwind","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndleah%2Fnorthwind/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndleah%2Fnorthwind/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndleah%2Fnorthwind/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ndleah","download_url":"https://codeload.github.com/ndleah/northwind/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndleah%2Fnorthwind/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30171869,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-06T07:56:45.623Z","status":"ssl_error","status_checked_at":"2026-03-06T07:55:55.621Z","response_time":250,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: 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":["sql"],"created_at":"2024-11-13T08:38:08.586Z","updated_at":"2026-03-06T10:31:06.276Z","avatar_url":"https://github.com/ndleah.png","language":"SQL","readme":" ![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F\u0026message=If%20Useful\u0026style=style=flat\u0026color=BC4E99)\n![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)\n[![View My Profile](https://img.shields.io/badge/View-My_Profile-green?logo=GitHub)](https://github.com/ndleah)\n[![View Repositories](https://img.shields.io/badge/View-My_Repositories-blue?logo=GitHub)](https://github.com/ndleah?tab=repositories)\n\n# Northwind Company Analysis \u003cimg src=\"https://cdnb.artstation.com/p/assets/images/images/007/413/479/medium/althea-krebelj-pixel-computer.jpg?1505969564\" align=\"right\" width=\"120\" /\u003e\n\n\u003e The Northwind database contains the sales data for a fictitious company called **Northwind Traders**, which imports and exports specialty foods from around the world.\n\u003e \n\u003e 👉 **My task in this case study is to perform an analysis of the performance of this company using SQL.**\n\n\u003c!-- omit in toc --\u003e\n## 📕  Table Of Contents\n* [📂 Dataset](#-dataset)\n* [🔎 Data Dictionary](#-data-dictionary)\n* [🧙‍♂️ Business Question](#️-business-question)\n\n\n\n## 📂 Dataset\n\n\u003e 📍 SQL script to generate the dataset is [here](/northwind.postgre_renamed.sql).\n\nThe dataset is composed of 13 tables:\n\n\u003cdetails\u003e\n\u003csummary\u003e\nShow summary\n\u003c/summary\u003e\n\n| Table               | Rows |\n|---------------------|------|\n| Categories          | 8    |\n| Customers           | 91   |\n| Employees           | 9    |\n| EmployeeTerritories | 49   |\n| Order Details       | 2155 |\n| Orders              | 830  |\n| Region              | 4    |\n| Products            | 77   |\n| Shippers            | 6    |\n| Suppliers           | 29   |\n| Territories         | 53   |\n| Usstates            | 51   |\n\n\u003c/details\u003e\n\nHere is the entity relationship diagram (ERD) for this dataset\n\u003cp align=\"center\"\u003e\n  \u003cimg width=60% height=60%\" src=\"/img/ERD.png\"\u003e\n\n## 🔎 Data Dictionary\n\n\u003cdetails\u003e\n\u003csummary\u003e\nCategories Table\n\u003c/summary\u003e\n\n| Primary Key | Foreign Key | Index | Name         | Data type | Length | Nullable | Default | Description                                        |\n|-------------|-------------|-------|--------------|-----------|--------|----------|---------|----------------------------------------------------|\n| PK          |             | IX    | CategoryID   | int       |        |          |         | Categories of Northwind products.                  |\n|             |             | IX    | CategoryName | varchar   | -15    |          |         | Name of food category.                             |\n|             |             |       | Description  | text      |        | Yes      |         | Full description of the category. Provide samples. |\n|             |             |       | Picture      | image     |        | Yes      |         | A picture representing the food category.          |\n\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n\u003csummary\u003e\nCustomerCustomerDemo Table\n\u003c/summary\u003e\n\n| Primary Key | Foreign Key | Index | Name           | Data type | Length | Nullable | Default | Description |\n|-------------|-------------|-------|----------------|-----------|--------|----------|---------|-------------|\n| PK          | FK          | IX    | CustomerID     | varchar   | -5     |          |         |             |\n| PK          | FK          | IX    | CustomerTypeID | varchar   | -10    |          |         |             |\n\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\nCustomerDemographics Table\n\u003c/summary\u003e\n\n\n| Primary Key | Foreign Key | Index | Name           | Data type | Length | Nullable | Default | Description |\n|-------------|-------------|-------|----------------|-----------|--------|----------|---------|-------------|\n| PK          |             | IX    | CustomerTypeID | varchar   | -10    |          |         |             |\n|             |             |       | CustomerDesc   | ntext     |        | Yes      |         |             |\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\nCustomers Table     \n\u003c/summary\u003e\n\n| Primary Key | Foreign Key | Index | Name         | Data type | Length | Nullable | Default | Description                                        |\n|-------------|-------------|-------|--------------|-----------|--------|----------|---------|----------------------------------------------------|\n| PK          |             | IX    | CustomerID   | varchar   | -5     |          |         | Unique five-character code based on customer name. |\n|             |             | IX    | CompanyName  | varchar   | -40    |          |         |                                                    |\n|             |             |       | ContactName  | varchar   | -30    | Yes      |         |                                                    |\n|             |             |       | ContactTitle | varchar   | -30    | Yes      |         |                                                    |\n|             |             |       | Address      | varchar   | -60    | Yes      |         | Street or post-office box.                         |\n|             |             | IX    | City         | varchar   | -15    | Yes      |         |                                                    |\n|             |             | IX    | Region       | varchar   | -15    | Yes      |         | State or province.                                 |\n|             |             | IX    | PostalCode   | varchar   | -10    | Yes      |         |                                                    |\n|             |             |       | Country      | varchar   | -15    | Yes      |         |                                                    |\n|             |             |       | Phone        | varchar   | -24    | Yes      |         | Phone number includes country code or area code.   |\n|             |             |       | Fax          | varchar   | -24    | Yes      |         | Phone number includes country code or area code.   |\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\nEmployees Table\n\u003c/summary\u003e\n\n| Primary Key | Foreign Key | Index | Name            | Data type | Length | Nullable | Default | Description                                      |\n|-------------|-------------|-------|-----------------|-----------|--------|----------|---------|--------------------------------------------------|\n| PK          |             | IX    | EmployeeID      | int       |        |          |         | Number automatically assigned to new employee.   |\n|             |             | IX    | LastName        | varchar   | -20    |          |         |                                                  |\n|             |             |       | FirstName       | varchar   | -10    |          |         |                                                  |\n|             |             |       | Title           | varchar   | -30    | Yes      |         | Employee's title.                                |\n|             |             |       | TitleOfCourtesy | varchar   | -25    | Yes      |         |                                                  |\n|             |             |       | BirthDate       | datetime  |        | Yes      |         |                                                  |\n|             |             |       | HireDate        | datetime  |        | Yes      |         |                                                  |\n|             |             |       | Address         | varchar   | -60    | Yes      |         | Street or post-office box.                       |\n|             |             |       | City            | varchar   | -15    | Yes      |         |                                                  |\n|             |             |       | Region          | varchar   | -15    | Yes      |         | Street or post-office box.                       |\n|             |             | IX    | PostalCode      | varchar   | -10    | Yes      |         |                                                  |\n|             |             |       | Country         | varchar   | -15    | Yes      |         |                                                  |\n|             |             |       | HomePhone       | varchar   | -24    | Yes      |         | Phone number includes country code or area code. |\n|             |             |       | Extension       | varchar   | -4     | Yes      |         | Internal telephone extension number.             |\n|             |             |       | Photo           | image     |        | Yes      |         | Picture of employee.                             |\n|             |             |       | Notes           | text      |        | Yes      |         | General information about employee's background. |\n|             | FK          |       | ReportsTo       | int       |        | Yes      |         | Employee's supervisor.                           |\n|             |             |       | PhotoPath       | varchar   | -255   | Yes      |         |                                                  |\n\n\n\u003c/details\u003e\n\n\n\n\u003cdetails\u003e\n\u003csummary\u003e\nEmployeeTerritories Table\n\u003c/summary\u003e\n\n| Primary Key | Foreign Key | Index | Name        | Data type | Length | Nullable | Default | Description |\n|-------------|-------------|-------|-------------|-----------|--------|----------|---------|-------------|\n| PK          | FK          | IX    | EmployeeID  | int       |        |          |         |             |\n| PK          | FK          | IX    | TerritoryID | varchar   | -20    |          |         |             |\n\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\nOrderDetails Table\n\u003c/summary\u003e\n\n| Primary Key | Foreign Key | Index | Name      | Data type | Length | Nullable | Default | Description                           |\n|-------------|-------------|-------|-----------|-----------|--------|----------|---------|---------------------------------------|\n| PK          | FK          | IX    | OrderID   | int       |        |          |         | Same as Order ID in Orders table.     |\n| PK          | FK          | IX    | ProductID | int       |        |          |         | Same as Product ID in Products table. |\n|             |             |       | UnitPrice | decimal   |        |          | 0       |                                       |\n|             |             |       | Quantity  | smallint  |        |          | -1      |                                       |\n|             |             |       | Discount  | decimal   |        |          | 0       |                                       |\n\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\nOrders Table\n\u003c/summary\u003e\n\n| Primary Key | Foreign Key | Index | Name           | Data type | Length | Nullable | Default | Description                                        |\n|-------------|-------------|-------|----------------|-----------|--------|----------|---------|----------------------------------------------------|\n| PK          |             | IX    | OrderID        | int       |        |          |         |                                                    |\n|             | FK          | IX    | CustomerID     | varchar   | -5     | Yes      |         |                                                    |\n|             | FK          | IX    | EmployeeID     | int       |        | Yes      |         | Same entry as in Employees table.                  |\n|             |             | IX    | OrderDate      | datetime  |        | Yes      |         |                                                    |\n|             |             |       | RequiredDate   | datetime  |        | Yes      |         |                                                    |\n|             |             | IX    | ShippedDate    | datetime  |        | Yes      |         |                                                    |\n|             | FK          | IX    | ShipVia        | int       |        | Yes      |         | Same as Shipper ID in Shippers table.              |\n|             |             |       | Freight        | decimal   |        | Yes      | 0       |                                                    |\n|             |             |       | ShipName       | varchar   | -40    | Yes      |         | Name of person or company to receive the shipment. |\n|             |             |       | ShipAddress    | varchar   | -60    | Yes      |         | Street address only -- no post-office box allowed. |\n|             |             |       | ShipCity       | varchar   | -15    | Yes      |         |                                                    |\n|             |             |       | ShipRegion     | varchar   | -15    | Yes      |         | State or province.                                 |\n|             |             | IX    | ShipPostalCode | varchar   | -10    | Yes      |         |                                                    |\n|             |             |       | ShipCountry    | varchar   | -15    | Yes      |         |                                                    |\n\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\nProducts Table\n\u003c/summary\u003e\n\n| Primary Key | Foreign Key | Index | Name            | Data type | Length | Nullable | Default | Description                                   |\n|-------------|-------------|-------|-----------------|-----------|--------|----------|---------|-----------------------------------------------|\n| PK          |             | IX    | ProductID       | int       |        |          |         | Number automatically assigned to new product. |\n|             |             | IX    | ProductName     | nvarchar  | -40    |          |         |                                               |\n|             | FK          | IX    | SupplierID      | int       |        | Yes      |         | Same entry as in Suppliers table.             |\n|             | FK          | IX    | CategoryID      | int       |        | Yes      |         | Same entry as in Categories table.            |\n|             |             |       | QuantityPerUnit | nvarchar  | -20    | Yes      |         | \"(e.g., 24-count case, 1-liter bottle).\"      |\n|             |             |       | UnitPrice       | money     |        | Yes      | 0       |                                               |\n|             |             |       | UnitsInStock    | smallint  |        | Yes      | 0       |                                               |\n|             |             |       | UnitsOnOrder    | smallint  |        | Yes      | 0       |                                               |\n|             |             |       | ReorderLevel    | smallint  |        | Yes      | 0       | Minimum units to maintain in stock.           |\n|             |             |       | Discontinued    | bit       |        |          | 0       | Yes means item is no longer available.        |\n\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\nRegion Table\n\u003c/summary\u003e\n\n| Primary Key | Foreign Key | Index | Name              | Data type | Length | Nullable | Default | Description |\n|-------------|-------------|-------|-------------------|-----------|--------|----------|---------|-------------|\n| PK          |             | IX    | RegionID          | int       |        |          |         |             |\n|             |             |       | RegionDescription | varchar   | -50    |          |         |             |\n\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\nShippers Table\n\u003c/summary\u003e\n\n| Primary Key | Foreign Key | Index | Name        | Data type | Length | Nullable | Default | Description                                      |\n|-------------|-------------|-------|-------------|-----------|--------|----------|---------|--------------------------------------------------|\n| PK          |             | IX    | ShipperID   | int       |        |          |         | Number automatically assigned to new shipper.    |\n|             |             |       | CompanyName | varchar   | -40    |          |         | Name of shipping company.                        |\n|             |             |       | Phone       | varchar   | -24    | Yes      |         | Phone number includes country code or area code. |\n\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\nSuppliers Table\n\u003c/summary\u003e\n\n| Primary Key | Foreign Key | Index | Name         | Data type | Length | Nullable | Default | Description                                      |\n|-------------|-------------|-------|--------------|-----------|--------|----------|---------|--------------------------------------------------|\n| PK          |             | IX    | SupplierID   | int       |        |          |         | Number automatically assigned to new supplier.   |\n|             |             | IX    | CompanyName  | varchar   | -40    |          |         |                                                  |\n|             |             |       | ContactName  | varchar   | -30    | Yes      |         |                                                  |\n|             |             |       | ContactTitle | varchar   | -30    | Yes      |         |                                                  |\n|             |             |       | Address      | varchar   | -60    | Yes      |         | Street or post-office box.                       |\n|             |             |       | City         | varchar   | -15    | Yes      |         |                                                  |\n|             |             |       | Region       | varchar   | -15    | Yes      |         | State or province.                               |\n|             |             | IX    | PostalCode   | varchar   | -10    | Yes      |         |                                                  |\n|             |             |       | Country      | varchar   | -15    | Yes      |         |                                                  |\n|             |             |       | Phone        | varchar   | -24    | Yes      |         | Phone number includes country code or area code. |\n|             |             |       | Fax          | varchar   | -24    | Yes      |         | Phone number includes country code or area code. |\n|             |             |       | HomePage     | ntext     |        | Yes      |         | Supplier's home page on World Wide Web.          |\n\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\nTerritories Table\n\u003c/summary\u003e\n\n| Primary Key | Foreign Key | Index | Name                 | Data type | Length | Nullable | Default | Description |\n|-------------|-------------|-------|----------------------|-----------|--------|----------|---------|-------------|\n| PK          |             | IX    | TerritoryID          | varchar   | -20    |          |         |             |\n|             |             |       | TerritoryDescription | varchar   | -50    |          |         |             |\n|             | FK          |       | RegionID             | int       |        |          |         |             |\n\n\n\u003c/details\u003e\n\n## 🧙‍♂️ Business Question\n### ✅ **Question 1**\n\nFor their annual review of the company pricing strategy, the Product Team wants to look at the products that are currently being offered for a specific price range ($20 to $50). In order to help them they asked you to provide them with a list of products with the following information:\n\n1. their name\n2. their unit price\n\nFiltered on the following conditions:\n\n1. their unit price is between 20 and 50\n2. they are not discontinued\n\nFinally order the results by unit price in a descending order (highest first).\n\n\u003cdetails\u003e\n\u003csummary\u003e\nOutput example\n\u003c/summary\u003e\n\n![Q1_output](/img/Q1.png)\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\n💡 Solution\n\u003c/summary\u003e\n\n```sql\n-- Solution Query\nSELECT\n\tproduct_name,\n\tunit_price\nFROM products\nWHERE unit_price BETWEEN 20 AND 50\nAND discontinued = 0\nORDER BY unit_price DESC;\n```\n\n| \"product_name\"                     | \"unit_price\" |\n|------------------------------------|--------------|\n| \"Tarte au sucre\"                   | 49.3         |\n| \"Ipoh Coffee\"                      | 46           |\n| \"Vegie-spread\"                     | 43.9         |\n| \"Schoggi Schokolade\"               | 43.9         |\n| \"Northwoods Cranberry Sauce\"       | 40           |\n| \"Gnocchi di nonna Alice\"           | 38           |\n| \"Queso Manchego La Pastora\"        | 38           |\n| \"Gudbrandsdalsost\"                 | 36           |\n| \"Mozzarella di Giovanni\"           | 34.8         |\n| \"Camembert Pierrot\"                | 34           |\n| \"Wimmers gute Semmelknödel\"        | 33.25        |\n| \"Mascarpone Fabioli\"               | 32           |\n| \"Gumbär Gummibärchen\"              | 31.23        |\n| \"Ikura\"                            | 31           |\n| \"Uncle Bob's Organic Dried Pears\"  | 30           |\n| \"Sirop d'érable\"                   | 28.5         |\n| \"Gravad lax\"                       | 26           |\n| \"Nord-Ost Matjeshering\"            | 25.89        |\n| \"Grandma's Boysenberry Spread\"     | 25           |\n| \"Pâté chinois\"                     | 24           |\n| \"Tofu\"                             | 23.25        |\n| \"Chef Anton's Cajun Seasoning\"     | 22           |\n| \"Flotemysost\"                      | 21.5         |\n| \"Louisiana Fiery Hot Pepper Sauce\" | 21.05        |\n| \"Queso Cabrales\"                   | 21           |\n| \"Gustaf's Knäckebröd\"              | 21           |\n| \"Maxilaku\"                         | 20           |\n\n\n\u003c/details\u003e\n\n### ✅ **Question 2**\n\nThe Logistics Team wants to do a retrospection of their performances for the year 1998, in order to identify for which countries they didn’t perform well. They asked you to provide them a list of countries with the following information:\n\n1. their average days between the order date and the shipping date (formatted to have only 2 decimals)\n2. their total number of orders (based on the order date) \nFiltered on the following conditions:\n\n1. the year of order date is 1998\n2. their average days between the order date and the shipping date is greater or equal 5 days\n3. their total number of orders is greater than 10 orders\n\nFinally order the results by country name in an ascending order (lowest first).\n\n\u003cdetails\u003e\n\u003csummary\u003e\nOutput example\n\u003c/summary\u003e\n\n![Q2_output](/img/Q2.png)\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\n💡 Solution\n\u003c/summary\u003e\n\n```sql\n-- Solution Query\nWITH cte_avg_days AS (\n\tSELECT\n\t\tship_country,\n\t\tROUND(AVG(\n\t\t\tEXTRACT(DAY FROM (shipped_date - order_date) * INTERVAL '1 DAY')\n\t\t\t)::NUMERIC,\n\t\t2) AS average_days_between_order_shipping,\n\t\tCOUNT(*) AS total_number_orders\n\tFROM orders\n\tWHERE EXTRACT(YEAR FROM order_date) = 1998\n\tGROUP BY \n\t\tship_country\n\tORDER BY ship_country\n\t)\nSELECT * FROM cte_avg_days\nWHERE average_days_between_order_shipping \u003e= 5\nAND total_number_orders \u003e 10;\n```\n\n| \"ship_country\" | \"average_days_between_order_shipping\" | \"total_number_orders\" |\n|----------------|---------------------------------------|-----------------------|\n| \"Austria\"      | \"5.89\"                                | \"11\"                  |\n| \"Brazil\"       | \"8.12\"                                | \"28\"                  |\n| \"France\"       | \"9.43\"                                | \"23\"                  |\n| \"Germany\"      | \"5.38\"                                | \"34\"                  |\n| \"Spain\"        | \"7.83\"                                | \"12\"                  |\n| \"Sweden\"       | \"13.29\"                               | \"14\"                  |\n| \"UK\"           | \"6.25\"                                | \"16\"                  |\n| \"USA\"          | \"7.89\"                                | \"39\"                  |\n| \"Venezuela\"    | \"8.73\"                                | \"18\"                  |\n\n\n\u003c/details\u003e\n\n### ✅ **Question 3**\nThe HR Team wants to know for each employee what was their age on the date they joined the company and who they currently report to. Provide them with a list of every employees with the following information:\n\n1. their full name (first name and last name combined in a single field)\n2. their job title\n3. their age at the time they were hired\n4. their manager full name (first name and last name combined in a single field)\n5. their manager job title\n\nFinally order the results by employee age and employee full name in an ascending order (lowest first).\n\n\u003cdetails\u003e\n\u003csummary\u003e\nOutput example\n\u003c/summary\u003e\n\n![Q3_output](/img/Q3.png)\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\n💡 Solution\n\u003c/summary\u003e\n\n```sql\n-- Solution Query\nSELECT\n    CONCAT(e.first_name, ' ', e.last_name) AS employee_full_name,\n\te.title AS employee_title,\n\tEXTRACT(YEAR FROM AGE(e.hire_date, e.birth_date))::INT AS employee_age,\n\tCONCAT(m.first_name, ' ', m.last_name) AS manager_full_name,\n\tm.title AS manager_title\nFROM\n    employees AS e\nINNER JOIN employees AS m \nON m.employee_id = e.reports_to\nORDER BY\n    employee_age,\n\temployee_full_name;\n```\n\n\n| \"employee_full_name\" | \"employee_title\"           | \"employee_age\" | \"manager_full_name\" | \"manager_title\"         |\n|----------------------|----------------------------|----------------|---------------------|-------------------------|\n| \"Anne Dodsworth\"     | \"Sales Representative\"     | 28             | \"Steven Buchanan\"   | \"Sales Manager\"         |\n| \"Janet Leverling\"    | \"Sales Representative\"     | 28             | \"Andrew Fuller\"     | \"Vice President, Sales\" |\n| \"Michael Suyama\"     | \"Sales Representative\"     | 30             | \"Steven Buchanan\"   | \"Sales Manager\"         |\n| \"Robert King\"        | \"Sales Representative\"     | 33             | \"Steven Buchanan\"   | \"Sales Manager\"         |\n| \"Laura Callahan\"     | \"Inside Sales Coordinator\" | 36             | \"Andrew Fuller\"     | \"Vice President, Sales\" |\n| \"Steven Buchanan\"    | \"Sales Manager\"            | 38             | \"Andrew Fuller\"     | \"Vice President, Sales\" |\n| \"Nancy Davolio\"      | \"Sales Representative\"     | 43             | \"Andrew Fuller\"     | \"Vice President, Sales\" |\n| \"Margaret Peacock\"   | \"Sales Representative\"     | 55             | \"Andrew Fuller\"     | \"Vice President, Sales\" |\n\n\n\n\u003c/details\u003e\n\n### ✅ **Question 4**\nThe Logistics Team wants to do a retrospection of their global performances over 1997-1998, in order to identify for which month they perform well. They asked you to provide them a list with:\n\n1. their year/month as single field in a date format (e.g. “1996-01-01” January 1996)\n2. their total number of orders\n3. their total freight (formatted to have no decimals)\n\nFiltered on the following conditions:\n\n1. the order date is between 1997 and 1998\n2. their total number of orders is greater than 35 orders\n\nFinally order the results by total freight (descending order).\n\n\u003cdetails\u003e\n\u003csummary\u003e\nOutput example\n\u003c/summary\u003e\n\n![Q4_output](/img/Q4.png)\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\n💡 Solution\n\u003c/summary\u003e\n\n```sql\n-- Solution Query\nWITH cte_freight AS (\n\tSELECT\n\t\tCONCAT(EXTRACT(YEAR FROM order_date), \n\t\t\t   '-', \n\t\t\t   EXTRACT(MONTH FROM order_date), \n\t\t\t   '-01'\n\t\t\t  ) AS year_month,\n\t\tCOUNT(*) AS total_number_orders,\n\t\tROUND(\n\t\t\tSUM(freight)\n\t\t\t)::INT AS total_freight\n\tFROM orders\n\tWHERE order_date \u003e= '1997-01-01' AND order_date \u003c '1998-01-31'\n\tGROUP BY \n\t\tCONCAT(EXTRACT(YEAR FROM order_date), \n\t\t\t   '-', \n\t\t\t   EXTRACT(MONTH FROM order_date), \n\t\t\t   '-01'\n\t\t\t  )\n)\nSELECT * FROM cte_freight\nWHERE total_number_orders \u003e 35\nORDER BY total_freight DESC;\n```\n\n\n| \"year_month\" | \"total_number_orders\" | \"total_freight\" |\n|--------------|-----------------------|-----------------|\n| \"1998-4-01\"  | 74                    | 6394            |\n| \"1998-1-01\"  | 55                    | 5463            |\n| \"1998-3-01\"  | 73                    | 5379            |\n| \"1998-2-01\"  | 54                    | 4273            |\n| \"1997-10-01\" | 38                    | 3946            |\n| \"1997-12-01\" | 48                    | 3758            |\n| \"1997-9-01\"  | 37                    | 3237            |\n\n\n\n\u003c/details\u003e\n\n### ✅ **Question 5**\nThe Pricing Team wants to know which products had an unit price increase and the percentage increase was not between 20% and 30%. In order to help them they asked you to provide them a list of products with:\n\n1. their product name\n2. their current unit price (formatted to have only 2 decimals)\n3. their previous unit price (formatted to have only 2 decimals)\n4. their percentage increase as: \n\n*(New Number -  Original Number) ÷ Original Number × 100* (with the result formatted to an integer, e.g 50 for 50%)\n\nFiltered on the following conditions:\n\n1. their percentage increase is not between 20% and 30%\n2. their total number of orders is greater than 10 orders\n\nFinally order the results by percentage increase (ascending order).\n\n\u003cdetails\u003e\n\u003csummary\u003e\nOutput example\n\u003c/summary\u003e\n\n![Q5_output](/img/Q5.png)\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\n💡 Solution\n\u003c/summary\u003e\n\n```sql\n-- Solution Query\nWITH cte_price AS (\nSELECT\n\td.product_id,\n\tp.product_name,\n\tROUND(LEAD(d.unit_price) OVER (PARTITION BY p.product_name ORDER BY o.order_date)::NUMERIC,2) AS current_price,\n\tROUND(LAG(d.unit_price) OVER (PARTITION BY p.product_name ORDER BY o.order_date)::NUMERIC,2) AS previous_unit_price\nFROM products AS p\nINNER JOIN order_details AS d\nON p.product_id = d.product_id\nINNER JOIN orders AS o\nON d.order_id = o.order_id\n)\nSELECT\n\tc.product_name,\n\tc.current_price,\n\tc.previous_unit_price,\n\tROUND(100*(c.current_price - c.previous_unit_price)/c.previous_unit_price) AS percentage_increase\nFROM cte_price AS c\nINNER JOIN order_details AS d\nON c.product_id = d.product_id\nWHERE c.current_price != c.previous_unit_price\nGROUP BY \n\tc.product_name,\n\tc.current_price,\n\tc.previous_unit_price\nHAVING COUNT(DISTINCT d.order_id) \u003e 10\nAND ROUND(100*(c.current_price - c.previous_unit_price)/c.previous_unit_price) NOT BETWEEN 20 AND 30;\n```\n\n\n| \"product_name\"                  | \"current_price\" | \"previous_unit_price\" | \"percentage_increase\" |\n|---------------------------------|-----------------|-----------------------|-----------------------|\n| \"Mozzarella di Giovanni\"        | 27.8            | 34.8                  | -20                   |\n| \"Singaporean Hokkien Fried Mee\" | 11.2            | 9.8                   | 14                    |\n\n\n\u003c/details\u003e\n\n### ✅ **Question 6**\nThe Pricing Team wants to know how each category performs according to their price range. In order to help them they asked you to provide them a list of categories with:\n\n1. their category name\n2. their price range as: \n\n    * “1.Below $20”\n\n    *  “2. $20 - $50”\n\n    * “3. Over $50”\n\n3. their total amount (formatted to be integer)\n4. their number of orders\n\nFinally order the results by category name then price range (both ascending order).\n\n\u003cdetails\u003e\n\u003csummary\u003e\nOutput example\n\u003c/summary\u003e\n\n![Q6_output](/img/Q6.png)\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\n💡 Solution\n\u003c/summary\u003e\n\n```sql\n--Solution query\nSELECT\n\tc.category_name,\n\tCASE \n\t\tWHEN p.unit_price \u003c 20 THEN '1. Below $20'\n\t\tWHEN p.unit_price \u003e= 20 AND p.unit_price \u003c= 50 THEN '2. $20 - $50'\n\t\tWHEN p.unit_price \u003e 50 THEN '3. Over $50'\n\t\tEND AS price_range,\n\tROUND(SUM(d.unit_price * d.quantity)) AS total_amount,\n\tCOUNT(DISTINCT d.order_id) AS total_number_orders\nFROM categories AS c\nINNER JOIN products AS p\nON c.category_id =  p.category_id\nINNER JOIN order_details AS d\nON d.product_id =  p.product_id\nGROUP BY \n\tc.category_name,\n\tprice_range\nORDER BY \n\tc.category_name,\n\tprice_range;\n```\n\n\n| \"category_name\"  | \"price_range\"  | \"total_amount\" | \"total_number_orders\" |\n|------------------|----------------|----------------|-----------------------|\n| \"Beverages\"      | \"1. Below $20\" | 111464         | 317                   |\n| \"Beverages\"      | \"2. $20 - $50\" | 25079          | 28                    |\n| \"Beverages\"      | \"3. Over $50\"  | 149984         | 24                    |\n| \"Condiments\"     | \"1. Below $20\" | 28622          | 85                    |\n| \"Condiments\"     | \"2. $20 - $50\" | 85073          | 121                   |\n| \"Confections\"    | \"1. Below $20\" | 57369          | 197                   |\n| \"Confections\"    | \"2. $20 - $50\" | 96094          | 106                   |\n| \"Confections\"    | \"3. Over $50\"  | 23636          | 16                    |\n| \"Dairy Products\" | \"1. Below $20\" | 17886          | 81                    |\n| \"Dairy Products\" | \"2. $20 - $50\" | 157148         | 204                   |\n| \"Dairy Products\" | \"3. Over $50\"  | 76296          | 54                    |\n| \"Grains/Cereals\" | \"1. Below $20\" | 25364          | 99                    |\n| \"Grains/Cereals\" | \"2. $20 - $50\" | 75363          | 91                    |\n| \"Meat/Poultry\"   | \"1. Below $20\" | 5121           | 36                    |\n| \"Meat/Poultry\"   | \"2. $20 - $50\" | 76504          | 96                    |\n| \"Meat/Poultry\"   | \"3. Over $50\"  | 96563          | 36                    |\n| \"Produce\"        | \"1. Below $20\" | 2566           | 13                    |\n| \"Produce\"        | \"2. $20 - $50\" | 57960          | 81                    |\n| \"Produce\"        | \"3. Over $50\"  | 44743          | 39                    |\n| \"Seafood\"        | \"1. Below $20\" | 69673          | 217                   |\n| \"Seafood\"        | \"2. $20 - $50\" | 39963          | 70                    |\n| \"Seafood\"        | \"3. Over $50\"  | 31988          | 27                    |\n\n\u003c/details\u003e\n\n### ✅ **Question 7**\nThe Logistics Team wants to know what is the current state of our regional suppliers' stocks for each category of product. In order to help them they asked you to provide them a list of categories with:\n\n1. their supplier region” as: \n   * “America”\n   * “Europe”\n   * “Asia-Pacific”\n2. their category name\n3. their total units in stock\n4. their total units on order\n5. heir total reorder level\n\nFinally order the results by supplier region then category name then price range (each in ascending order).\n\n\u003cdetails\u003e\n\u003csummary\u003e\nOutput example\n\u003c/summary\u003e\n\n![Q7_output](/img/Q7.png)\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\n💡 Solution\n\u003c/summary\u003e\n\n```sql\n--Solution query\nSELECT\n\tc.category_name,\n\tCASE\n\t\tWHEN s.country IN ('Australia', 'Singapore', 'Japan' ) THEN 'Asia-Pacific'\n\t\tWHEN s.country IN ('US', 'Brazil', 'Canada') THEN 'America'\n\t\tELSE 'Europe'\n\tEND AS supplier_region,\n\tp.unit_in_stock AS units_in_stock,\n\tp.unit_on_order AS units_on_order,\n\tp.reorder_level \nFROM suppliers AS s\nINNER JOIN products AS p\nON s.supplier_id = p.supplier_id\nINNER JOIN categories AS c\nON p.category_id = c.category_id\nWHERE s.region IS NOT NULL\nORDER BY \n\tsupplier_region,\n\tc.category_name,\n\tp.unit_price;\n```\n\n\n| \"category_name\"  | \"supplier_region\" | \"units_in_stock\" | \"units_on_order\" | \"reorder_level\" |\n|------------------|-------------------|------------------|------------------|-----------------|\n| \"Condiments\"     | \"America\"         | 113              | 0                | 25              |\n| \"Confections\"    | \"America\"         | 17               | 0                | 0               |\n| \"Meat/Poultry\"   | \"America\"         | 21               | 0                | 10              |\n| \"Meat/Poultry\"   | \"America\"         | 115              | 0                | 20              |\n| \"Beverages\"      | \"Asia-Pacific\"    | 15               | 10               | 30              |\n| \"Condiments\"     | \"Asia-Pacific\"    | 24               | 0                | 5               |\n| \"Confections\"    | \"Asia-Pacific\"    | 29               | 0                | 10              |\n| \"Grains/Cereals\" | \"Asia-Pacific\"    | 38               | 0                | 25              |\n| \"Meat/Poultry\"   | \"Asia-Pacific\"    | 0                | 0                | 0               |\n| \"Meat/Poultry\"   | \"Asia-Pacific\"    | 0                | 0                | 0               |\n| \"Produce\"        | \"Asia-Pacific\"    | 20               | 0                | 10              |\n| \"Seafood\"        | \"Asia-Pacific\"    | 42               | 0                | 0               |\n| \"Beverages\"      | \"Europe\"          | 52               | 0                | 10              |\n| \"Beverages\"      | \"Europe\"          | 111              | 0                | 15              |\n| \"Beverages\"      | \"Europe\"          | 20               | 0                | 15              |\n| \"Condiments\"     | \"Europe\"          | 4                | 100              | 20              |\n| \"Condiments\"     | \"Europe\"          | 76               | 0                | 0               |\n| \"Condiments\"     | \"Europe\"          | 0                | 0                | 0               |\n| \"Condiments\"     | \"Europe\"          | 53               | 0                | 0               |\n| \"Condiments\"     | \"Europe\"          | 120              | 0                | 25              |\n| \"Condiments\"     | \"Europe\"          | 6                | 0                | 0               |\n| \"Dairy Products\" | \"Europe\"          | 22               | 30               | 30              |\n| \"Dairy Products\" | \"Europe\"          | 86               | 0                | 0               |\n| \"Produce\"        | \"Europe\"          | 15               | 0                | 10              |\n| \"Seafood\"        | \"Europe\"          | 85               | 0                | 10              |\n| \"Seafood\"        | \"Europe\"          | 123              | 0                | 30              |\n\n\n\u003c/details\u003e\n\n### ✅ **Question 8**\nThe Pricing Team wants to know for each currently offered product how their unit price compares against their categories average and median unit price. In order to help them they asked you to provide them a list of products with:\n\n1. their category name\n2. their product name\n3. their unit price\n4. their category average unit price (formatted to have only 2 decimals)\n5. their category median unit price (formatted to have only 2 decimals)\n6. their position against the category average unit price as:\n   * “Below Average”\n   * “Equal Average”\n   * “Over Average”\n7. their position against the category median unit price as:\n   * “Below Median”\n   * “Equal Median”\n   * “Over Median”\n\nFiltered on the following conditions:\n   * They are not discontinued \n\nFinally order the results by category name then product name (both ascending).\n\n\u003cdetails\u003e\n\u003csummary\u003e\nOutput example\n\u003c/summary\u003e\n\n![Q8_output](/img/Q8.png)\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\n💡 Solution\n\u003c/summary\u003e\n\n```sql\n--Solution query\nWITH cte_price AS (\n\tSELECT \n\t\tc.category_name,\n\t\tp.product_name,\n\t\tp.unit_price,\n\t\tROUND(AVG(d.unit_price)::NUMERIC,2) AS average_unit_price,\n\t\tROUND((PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY d.unit_price))::NUMERIC,\n\t\t2) AS median_unit_price\n\tFROM categories AS c\n\tINNER JOIN products AS p\n\tON c.category_id = p.category_id\n\tINNER JOIN order_details AS d\n\tON p.product_id = d.product_id\n\tWHERE p.discontinued = 0\n\tGROUP BY \n\t\tc.category_name,\n\t\tp.product_name,\n\t\tp.unit_price\n)\nSELECT\n\tcategory_name,\n\tproduct_name,\n\tunit_price,\n\taverage_unit_price,\n\tmedian_unit_price,\n\tCASE\n\t\tWHEN unit_price \u003e average_unit_price THEN 'Over Average'\n\t\tWHEN unit_price = average_unit_price THEN 'Equal Average'\n\t\tWHEN unit_price \u003c average_unit_price THEN 'Below Average'\n\tEND AS average_unit_price_position,\n\tCASE\n\t\tWHEN unit_price \u003e median_unit_price THEN 'Over Average'\n\t\tWHEN unit_price = median_unit_price THEN 'Equal Average'\n\t\tWHEN unit_price \u003c median_unit_price THEN 'Below Average'\n\tEND AS median_unit_price_position\nFROM cte_price\nORDER BY \n\tcategory_name,\n\tproduct_name;\n```\n\n| \"category_name\"  | \"product_name\"                     | \"unit_price\" | \"average_unit_price\" | \"median_unit_price\" | \"average_unit_price_position\" | \"median_unit_price_position\" |\n|------------------|------------------------------------|--------------|----------------------|---------------------|-------------------------------|------------------------------|\n| \"Beverages\"      | \"Côte de Blaye\"                    | 263.5        | 245.93               | 263.50              | \"Over Average\"                | \"Equal Average\"              |\n| \"Beverages\"      | \"Chartreuse verte\"                 | 18           | 16.68                | 18.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Beverages\"      | \"Ipoh Coffee\"                      | 46           | 43.04                | 46.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Beverages\"      | \"Lakkalikööri\"                     | 18           | 16.98                | 18.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Beverages\"      | \"Laughing Lumberjack Lager\"        | 14           | 13.72                | 14.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Beverages\"      | \"Outback Lager\"                    | 15           | 14.15                | 15.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Beverages\"      | \"Rhönbräu Klosterbier\"             | 7.75         | 7.38                 | 7.75                | \"Over Average\"                | \"Equal Average\"              |\n| \"Beverages\"      | \"Sasquatch Ale\"                    | 14           | 12.97                | 14.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Beverages\"      | \"Steeleye Stout\"                   | 18           | 17.00                | 18.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Condiments\"     | \"Aniseed Syrup\"                    | 10           | 9.50                 | 10.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Condiments\"     | \"Chef Anton's Cajun Seasoning\"     | 22           | 20.68                | 22.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Condiments\"     | \"Genen Shouyu\"                     | 13           | 14.47                | 15.50               | \"Below Average\"               | \"Below Average\"              |\n| \"Condiments\"     | \"Grandma's Boysenberry Spread\"     | 25           | 24.17                | 25.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Condiments\"     | \"Gula Malacca\"                     | 19.45        | 18.13                | 19.45               | \"Over Average\"                | \"Over Average\"               |\n| \"Condiments\"     | \"Louisiana Fiery Hot Pepper Sauce\" | 21.05        | 19.46                | 21.05               | \"Over Average\"                | \"Below Average\"              |\n| \"Condiments\"     | \"Louisiana Hot Spiced Okra\"        | 17           | 15.30                | 15.30               | \"Over Average\"                | \"Over Average\"               |\n| \"Condiments\"     | \"Northwoods Cranberry Sauce\"       | 40           | 38.77                | 40.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Condiments\"     | \"Original Frankfurter grüne Soße\"  | 13           | 12.11                | 13.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Condiments\"     | \"Sirop d'érable\"                   | 28.5         | 27.79                | 28.50               | \"Over Average\"                | \"Equal Average\"              |\n| \"Condiments\"     | \"Vegie-spread\"                     | 43.9         | 40.79                | 43.90               | \"Over Average\"                | \"Over Average\"               |\n| \"Confections\"    | \"Chocolade\"                        | 12.75        | 11.90                | 12.75               | \"Over Average\"                | \"Equal Average\"              |\n| \"Confections\"    | \"Gumbär Gummibärchen\"              | 31.23        | 28.86                | 31.23               | \"Over Average\"                | \"Below Average\"              |\n| \"Confections\"    | \"Maxilaku\"                         | 20           | 18.48                | 20.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Confections\"    | \"NuNuCa Nuß-Nougat-Creme\"          | 14           | 13.07                | 14.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Confections\"    | \"Pavlova\"                          | 17.45        | 16.38                | 17.45               | \"Over Average\"                | \"Over Average\"               |\n| \"Confections\"    | \"Scottish Longbreads\"              | 12.5         | 11.54                | 12.50               | \"Over Average\"                | \"Equal Average\"              |\n| \"Confections\"    | \"Schoggi Schokolade\"               | 43.9         | 40.97                | 43.90               | \"Over Average\"                | \"Over Average\"               |\n| \"Confections\"    | \"Sir Rodney's Marmalade\"           | 81           | 75.94                | 81.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Confections\"    | \"Sir Rodney's Scones\"              | 10           | 9.38                 | 10.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Confections\"    | \"Tarte au sucre\"                   | 49.3         | 46.41                | 49.30               | \"Over Average\"                | \"Below Average\"              |\n| \"Confections\"    | \"Teatime Chocolate Biscuits\"       | 9.2          | 8.53                 | 9.20                | \"Over Average\"                | \"Below Average\"              |\n| \"Confections\"    | \"Valkoinen suklaa\"                 | 16.25        | 14.95                | 16.25               | \"Over Average\"                | \"Equal Average\"              |\n| \"Confections\"    | \"Zaanse koeken\"                    | 9.5          | 9.14                 | 9.50                | \"Over Average\"                | \"Equal Average\"              |\n| \"Dairy Products\" | \"Camembert Pierrot\"                | 34           | 32.13                | 34.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Dairy Products\" | \"Flotemysost\"                      | 21.5         | 19.76                | 21.50               | \"Over Average\"                | \"Equal Average\"              |\n| \"Dairy Products\" | \"Geitost\"                          | 2.5          | 2.33                 | 2.50                | \"Over Average\"                | \"Equal Average\"              |\n| \"Dairy Products\" | \"Gorgonzola Telino\"                | 12.5         | 11.67                | 12.50               | \"Over Average\"                | \"Equal Average\"              |\n| \"Dairy Products\" | \"Gudbrandsdalsost\"                 | 36           | 33.45                | 36.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Dairy Products\" | \"Mascarpone Fabioli\"               | 32           | 30.72                | 32.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Dairy Products\" | \"Mozzarella di Giovanni\"           | 34.8         | 32.04                | 34.80               | \"Over Average\"                | \"Below Average\"              |\n| \"Dairy Products\" | \"Queso Cabrales\"                   | 21           | 19.60                | 21.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Dairy Products\" | \"Queso Manchego La Pastora\"        | 38           | 36.91                | 38.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Dairy Products\" | \"Raclette Courdavault\"             | 55           | 51.13                | 55.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Grains/Cereals\" | \"Filo Mix\"                         | 7            | 6.76                 | 7.00                | \"Over Average\"                | \"Equal Average\"              |\n| \"Grains/Cereals\" | \"Gnocchi di nonna Alice\"           | 38           | 35.42                | 38.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Grains/Cereals\" | \"Gustaf's Knäckebröd\"              | 21           | 20.40                | 21.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Grains/Cereals\" | \"Ravioli Angelo\"                   | 19.5         | 18.14                | 19.50               | \"Over Average\"                | \"Equal Average\"              |\n| \"Grains/Cereals\" | \"Tunnbröd\"                         | 9            | 8.37                 | 9.00                | \"Over Average\"                | \"Equal Average\"              |\n| \"Grains/Cereals\" | \"Wimmers gute Semmelknödel\"        | 33.25        | 31.03                | 33.25               | \"Over Average\"                | \"Equal Average\"              |\n| \"Meat/Poultry\"   | \"Pâté chinois\"                     | 24           | 22.40                | 24.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Meat/Poultry\"   | \"Tourtière\"                        | 7.45         | 6.80                 | 7.45                | \"Over Average\"                | \"Below Average\"              |\n| \"Produce\"        | \"Longlife Tofu\"                    | 10           | 8.77                 | 8.00                | \"Over Average\"                | \"Over Average\"               |\n| \"Produce\"        | \"Manjimup Dried Apples\"            | 53           | 50.55                | 53.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Produce\"        | \"Tofu\"                             | 23.25        | 21.35                | 23.25               | \"Over Average\"                | \"Equal Average\"              |\n| \"Produce\"        | \"Uncle Bob's Organic Dried Pears\"  | 30           | 29.17                | 30.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Seafood\"        | \"Boston Crab Meat\"                 | 18.4         | 17.23                | 18.40               | \"Over Average\"                | \"Below Average\"              |\n| \"Seafood\"        | \"Carnarvon Tigers\"                 | 62.5         | 59.72                | 62.50               | \"Over Average\"                | \"Equal Average\"              |\n| \"Seafood\"        | \"Escargots de Bourgogne\"           | 13.25        | 12.66                | 13.25               | \"Over Average\"                | \"Equal Average\"              |\n| \"Seafood\"        | \"Gravad lax\"                       | 26           | 23.40                | 23.40               | \"Over Average\"                | \"Over Average\"               |\n| \"Seafood\"        | \"Ikura\"                            | 31           | 29.68                | 31.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Seafood\"        | \"Inlagd Sill\"                      | 19           | 17.90                | 19.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Seafood\"        | \"Jack's New England Clam Chowder\"  | 9.65         | 9.19                 | 9.65                | \"Over Average\"                | \"Below Average\"              |\n| \"Seafood\"        | \"Konbu\"                            | 6            | 5.76                 | 6.00                | \"Over Average\"                | \"Equal Average\"              |\n| \"Seafood\"        | \"Nord-Ost Matjeshering\"            | 25.89        | 24.27                | 25.89               | \"Over Average\"                | \"Below Average\"              |\n| \"Seafood\"        | \"Röd Kaviar\"                       | 15           | 14.36                | 15.00               | \"Over Average\"                | \"Equal Average\"              |\n| \"Seafood\"        | \"Rogede sild\"                      | 9.5          | 9.23                 | 9.50                | \"Over Average\"                | \"Equal Average\"              |\n| \"Seafood\"        | \"Spegesild\"                        | 12           | 11.11                | 12.00               | \"Over Average\"                | \"Equal Average\"              |\n\n\n\u003c/details\u003e\n\n### ✅ **Question 9**\n\nThe Sales Team wants to build a list of KPIs to measure employees' performances. In order to help them they asked you to provide them a list of employees with:\n\n1. their full name (first name and last name combined in a single field)\n2. their job title\n3. their total sales amount excluding discount (formatted to have only 2 decimals)\n4. their total number of orders\n5. their total number of entries (each row of an order)\n6. their average amount per entry (formatted to have only 2 decimals)\n7. their average amount per order (formatted to have only 2 decimals)\n8. their total discount amount (formatted to have only 2 decimals)\n9. their total sales amount including discount (formatted to have only 2 decimals)\n10. Their total discount percentage (formatted to have only 2 decimals)\n\nFinally order the results by total sales amount including discount (descending).\n\n\u003cdetails\u003e\n\u003csummary\u003e\nOutput example\n\u003c/summary\u003e\n\n![Q9_output](/img/Q9.png)\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\n💡 Solution\n\u003c/summary\u003e\n\n```sql\n--Solution query\nWITH cte_kpi AS (\nSELECT\n    CONCAT(e.first_name, ' ', e.last_name) AS employee_full_name,\n\te.title AS employee_title,\n\tROUND(\n\t\tSUM(d.quantity * d.unit_price)::NUMERIC,\n\t\t2) AS total_sale_amount_excluding_discount,\n    COUNT(DISTINCT d.order_id) AS total_number_orders,\n    COUNT(d.*) AS total_number_entries,\n\t\tROUND(\n\t\tSUM(d.discount*(d.quantity * d.unit_price))::NUMERIC,\n\t\t2) AS total_discount_amount,\n\tROUND(\n\t\tSUM((1 - d.discount)*(d.quantity * d.unit_price))::NUMERIC,\n\t\t2) AS total_sale_amount_including_discount\nFROM orders AS o\nINNER JOIN employees AS e\nON o.employee_id = e.employee_id\nINNER JOIN order_details AS d\nON d.order_id = o.order_id\nINNER JOIN products AS p\nON d.product_id = p.product_id\nGROUP BY \n\temployee_full_name,\n\temployee_title\n)\nSELECT \n\temployee_full_name,\n\temployee_title,\n\ttotal_sale_amount_excluding_discount,\n\ttotal_number_orders,\n\ttotal_number_entries,\n\tROUND(\n\t\tSUM(total_sale_amount_excluding_discount/total_number_entries),\n\t\t   2) AS average_amount_per_entry,\n\tROUND(\n\t\tSUM(total_sale_amount_excluding_discount/total_number_orders),\n\t\t   2) AS average_amount_per_order,\n\ttotal_discount_amount,\n\ttotal_sale_amount_including_discount,\n\tSUM(ROUND(\n\t\t(total_sale_amount_excluding_discount-total_sale_amount_including_discount)/\n\t\ttotal_sale_amount_excluding_discount*100,\n\t\t2)) AS total_discount_percentage\nFROM cte_kpi\nGROUP BY\n\temployee_full_name,\n\temployee_title,\n\ttotal_sale_amount_excluding_discount,\n\ttotal_number_orders,\n\ttotal_number_entries,\n\ttotal_sale_amount_including_discount,\n\ttotal_discount_amount\nORDER BY total_sale_amount_including_discount DESC;\n```\n\n\n| \"employee_full_name\" | \"employee_title\"           | \"total_sale_amount_excluding_discount\" | \"total_number_orders\" | \"total_number_entries\" | \"average_amount_per_entry\" | \"average_amount_per_order\" | \"total_discount_amount\" | \"total_sale_amount_including_discount\" | \"total_discount_percentage\" |\n|----------------------|----------------------------|----------------------------------------|-----------------------|------------------------|----------------------------|----------------------------|-------------------------|----------------------------------------|-----------------------------|\n| \"Margaret Peacock\"   | \"Sales Representative\"     | 250187.45                              | 156                   | 420                    | 595.68                     | 1603.77                    | 17296.60                | 232890.85                              | 6.91                        |\n| \"Janet Leverling\"    | \"Sales Representative\"     | 213051.30                              | 127                   | 321                    | 663.71                     | 1677.57                    | 10238.46                | 202812.84                              | 4.81                        |\n| \"Nancy Davolio\"      | \"Sales Representative\"     | 202143.71                              | 123                   | 345                    | 585.92                     | 1643.44                    | 10036.11                | 192107.60                              | 4.96                        |\n| \"Andrew Fuller\"      | \"Vice President, Sales\"    | 177749.26                              | 96                    | 241                    | 737.55                     | 1851.55                    | 11211.51                | 166537.76                              | 6.31                        |\n| \"Laura Callahan\"     | \"Inside Sales Coordinator\" | 133301.03                              | 104                   | 260                    | 512.70                     | 1281.74                    | 6438.75                 | 126862.28                              | 4.83                        |\n| \"Robert King\"        | \"Sales Representative\"     | 141295.99                              | 72                    | 176                    | 802.82                     | 1962.44                    | 16727.76                | 124568.23                              | 11.84                       |\n| \"Anne Dodsworth\"     | \"Sales Representative\"     | 82964.00                               | 43                    | 107                    | 775.36                     | 1929.40                    | 5655.93                 | 77308.07                               | 6.82                        |\n| \"Michael Suyama\"     | \"Sales Representative\"     | 78198.10                               | 67                    | 168                    | 465.46                     | 1167.14                    | 4284.97                 | 73913.13                               | 5.48                        |\n| \"Steven Buchanan\"    | \"Sales Manager\"            | 75567.75                               | 42                    | 117                    | 645.88                     | 1799.23                    | 6775.47                 | 68792.28                               | 8.97                        |\n\n\n\n\u003c/details\u003e\n\n### ✅ **Question 10**\nThe Sales Team wants to build another list of KPIs to measure employees' performances across each category. In order to help them they asked you to provide them a list of categories and employees with:\n\n1. their categories name\n2. their full name (first name and last name combined in a 3. single field)\n3. their total sales amount including discount (formatted to have only 2 decimals)\n4. their percentage of total sales amount against the total sales amount across all employees (formatted to have only 2 decimals)\n5. their percentage of total sales amount against the total sales amount for each employees (formatted to have only 2 decimals)\n\nFinally order the results by category name (ascending) then total sales amount (descending).\n\n\u003cdetails\u003e\n\u003csummary\u003e\nOutput example\n\u003c/summary\u003e\n\n![Q10_output](/img/Q10.png)\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\n💡 Solution\n\u003c/summary\u003e\n\t\n```sql\n--Solution query\nWITH cte_kpi AS (\nSELECT\n    c.category_name,\n    CONCAT(e.first_name, ' ', e.last_name) AS employee_full_name,\n    ROUND(\n\t\t  SUM(d.quantity * d.unit_price)::NUMERIC,\n\t\t  2) AS total_sale_amount_including_discount\nFROM employees AS e\nINNER JOIN orders AS o\n\tON e.employee_id = o.employee_id \nINNER JOIN order_details AS d\n\tON o.order_id = d.order_id\nINNER JOIN products AS p\n\tON d.product_id = p.product_id\nINNER JOIN categories AS c\n\tON c.category_id = p.category_id\nGROUP BY \n\tcategory_name,\n\temployee_full_name\n)\nSELECT \n\t*,\n\tROUND(\n\t\tSUM(total_sale_amount_including_discount) / \n\t\tSUM(SUM(total_sale_amount_including_discount))\n\t\t\tOVER (PARTITION BY employee_full_name) * 100,\n\t\t2) AS percentage_of_employee_sales,\n\tROUND(\t\n\t\tSUM(total_sale_amount_including_discount) / \n\t\tSUM(SUM(total_sale_amount_including_discount))\n\t\t\tOVER (PARTITION BY category_name) * 100,\n\t\t2) AS percentage_of_category_sales\nFROM cte_kpi\nGROUP BY\n\tcategory_name,\n\temployee_full_name,\n\ttotal_sale_amount_including_discount\nORDER BY\n\tcategory_name,\n\ttotal_sale_amount_including_discount DESC;\n```\n\u003c/details\u003e\n\n## ✨ Contribution\n\nContributions, issues, and feature requests are welcome!\n\nTo contribute to this project, see the GitHub documentation on **[creating a pull request](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request)**.\n\n\n## 👏 Support\n\nGive a ⭐️ if you like this project!\n___________________________________\n\n\u003cp\u003e\u0026copy; 2021 Leah Nguyen\u003c/p\u003e\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fndleah%2Fnorthwind","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fndleah%2Fnorthwind","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fndleah%2Fnorthwind/lists"}