Skip to content
New Workbook
Sign up
SQL Exercise: Analysing Unicorn Companies

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, and
  • average_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

ColumnDescription
company_idA unique ID for the company.
date_joinedThe date that the company became a unicorn.
year_foundedThe year that the company was founded.

funding

ColumnDescription
company_idA unique ID for the company.
valuationCompany value in US dollars.
fundingThe amount of funding raised in US dollars.
select_investorsA list of key investors in the company.

industries

ColumnDescription
company_idA unique ID for the company.
industryThe industry that the company operates in.

companies

ColumnDescription
company_idA unique ID for the company.
companyThe name of the company.
cityThe city where the company is headquartered.
countryThe country where the company is headquartered.
continentThe continent where the company is headquartered.

The output

Your query should return a table in the following format:

industryyearnum_unicornsaverage_valuation_billions
industry12021------
industry22020------
industry32019------
industry12021------
industry22020------
industry32019------
industry12021------
industry22020------
industry32019------

Where industry1, industry2, and industry3 are the three top-performing industries.

Spinner
DataFrameavailable as
df
variable
--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;
Spinner
DataFrameavailable as
df1
variable
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;
Spinner
DataFrameavailable as
df3
variable
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;