Your task is to first identify:
- the three best-performing industries based on the number of new unicorns created in 2019, 2020, and 2021 combined.
From those industries (1), you will need to find the number of unicorns within these industries (2), the year that they became a unicorn (3), and their average valuation, converted to billions of dollars and rounded to two decimal places (4).
With the above information you can then finish your query to return a table containing:
industry
year
,num_unicorns
, andaverage_valuation_billions
.
For readability, the firm have asked you to sort your results by year and number of unicorns, both in descending order.
Note: If you use multiple SQL cells then please ensure your final query containing the desired output is stored as a pandas DataFrame called df at the top of the cell so that your answer can be validated:
Did you know that the average return from investing in stocks is 10% per year (not accounting for inflation)? But who wants to be average?!
You have been asked to support an investment firm by analyzing trends in high-growth companies. They are interested in understanding which industries are producing the highest valuations and the rate at which new high-value companies are emerging. Providing them with this information gives them a competitive insight as to industry trends and how they should structure their portfolio looking forward.
You have been given access to their unicorns
database, which contains the following tables:
dates
Column | Description |
---|---|
company_id | A unique ID for the company. |
date_joined | The date that the company became a unicorn. |
year_founded | The year that the company was founded. |
funding
Column | Description |
---|---|
company_id | A unique ID for the company. |
valuation | Company value in US dollars. |
funding | The amount of funding raised in US dollars. |
select_investors | A list of key investors in the company. |
industries
Column | Description |
---|---|
company_id | A unique ID for the company. |
industry | The industry that the company operates in. |
companies
Column | Description |
---|---|
company_id | A unique ID for the company. |
company | The name of the company. |
city | The city where the company is headquartered. |
country | The country where the company is headquartered. |
continent | The continent where the company is headquartered. |
The output
Your query should return a table in the following format:
industry | year | num_unicorns | average_valuation_billions |
---|---|---|---|
industry1 | 2021 | --- | --- |
industry2 | 2020 | --- | --- |
industry3 | 2019 | --- | --- |
industry1 | 2021 | --- | --- |
industry2 | 2020 | --- | --- |
industry3 | 2019 | --- | --- |
industry1 | 2021 | --- | --- |
industry2 | 2020 | --- | --- |
industry3 | 2019 | --- | --- |
Where industry1
, industry2
, and industry3
are the three top-performing industries.
--cte: get top 3 industries based on number of unicorns in 2019-2021
WITH top3 AS (
SELECT
i.industry,
COUNT(DISTINCT i.company_id) AS num_unicorns
FROM
industries AS i
INNER JOIN
dates AS d
ON i.company_id = d.company_id
INNER JOIN
funding AS f
ON d.company_id = f.company_id
WHERE
d.year_founded IN (2019, 2020, 2021)
GROUP BY
i.industry
ORDER BY
num_unicorns DESC
LIMIT 3)
SELECT
i.industry,
d.year_founded AS year,
COUNT(DISTINCT i.company_id) AS num_unicorns,
ROUND(AVG(f.valuation / 1000000000), 2) AS average_valuation_billions
FROM
industries AS i
JOIN
dates AS d
ON i.company_id = d.company_id
JOIN
funding AS f
ON d.company_id = f.company_id
WHERE
i.industry IN (SELECT industry FROM top3) AND
d.year_founded IN (2019,2020,2021)
GROUP BY
i.industry,
d.year_founded
ORDER BY
i.industry,
num_unicorns DESC,
d.year_founded DESC;
SELECT
c.company,
c.city,
c.country,
c.continent,
i.industry,
round(f.valuation / 1000000000.0, 2) AS valuation_billions,
round(f.funding / 1000000000.0, 2) AS funding_billions,
f.select_investors,
d.date_joined,
d.year_founded
FROM
companies c
JOIN
industries i ON c.company_id = i.company_id
JOIN
funding f ON c.company_id = f.company_id
JOIN
dates d ON c.company_id = d.company_id
ORDER BY
f.valuation DESC
LIMIT 1000;
SELECT
c.company,
c.city,
c.country,
c.continent,
i.industry,
ROUND(f.valuation / 1000000000.0, 2) AS valuation_in_billions,
ROUND(f.funding / 1000000000.0, 2) AS funding_in_billions,
f.select_investors,
d.date_joined,
d.year_founded
FROM
companies c
JOIN
industries i ON c.company_id = i.company_id
JOIN
funding f ON c.company_id = f.company_id
JOIN
dates d ON c.company_id = d.company_id
ORDER BY
f.valuation DESC
--LIMIT 10;