I am a beginner in SQL and learning to design data base tables and querying the data for special cases.
I came up with below use case for learning one many to relationships for which I have designed three SQL tables shown below containing the information of the products manufactured by companies and it's sales info:
TABLE1: COMPANY TABLE - contains the name of the companies
ID NAME 1 MICROSOFT 2 APPLE 3 SAMSUNG
TABLE2: PRODUCTSERIES(Many to One relationship with COMPANY) - contains the series names of the products manufactured by companies
ID NAME COMPANY_FK 1 WINDOWS 1 2 OFFICE 1 3 IPHONE 2 4 MACBOOK 2 5 GALAXY 3 6 GALAXYNOTE 3
TABLE3: PRODUCTS(Many to One relationship with PRODUCTSERIES) - contains actual products manufactured and their sales info
ID Name total_manufactured sold PRODUCTSERIES_FK 1 GALAXY7 11 1 5 2 GALAXY8 11 1 5 3 GALAXYNOTE7 11 1 6 4 GALAXYNOTE8 11 1 6 5 OFFICE10 11 1 2 6 OFFICE13 11 1 2 7 IPHONE10 11 1 3 8 IPHONE12 11 1 3 9 MACBOOK MINI 11 1 4 10 MACBOOK PRO 11 1 4
Now I am trying to query these tables to obtain the sales percentage of the companies for each product series as shown in below example:
[{"company": MICROSOFT, "TOTAL_SALES%": 0.09, "SERIES": [{"WINDOWS_SALES": 0.09}, {"OFFICE_SALES": 0.09}]}, {"company": APPLE, "TOTAL_SALES%": 0.09, "SERIES": [{"IPHONE_SALES": 0.09}, {"MACBOOK_SALES": 0.09}]}, {"company": SAMSUNG, "TOTAL_SALES%": 0.09, "SERIES": [{"GALAXY_SALES": 0.09}, {"GALAXYNOTE_SALES": 0.09}]}]
NOTE: "TOTAL_SALES%" of a company is the average of the sales (sold/total_manufactured) of all the productseries owned by the company.
I am a beginner in SQL and have no clue how to get the output using SQL. Could you please share how can i frame my query or some relevant documentation to learn? Also can you please correct if my table design is not proper? I want to execute the query using PYTHON and I already know how to execute and retrieve the results using PYTHON if i know the query.
DB-FIDDLE URL: