Did you know that the average return from investing in stocks is 10% per year! 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. | 
WITH TOP_INDUSTRIES AS (
	SELECT industry, COUNT(company_id) as uni_count_over
	FROM industries
	WHERE company_id IN (
		select company_id
		FROM dates 
		WHERE extract(year FROM date_joined) IN ('2019', '2020', '2021')
		)
	GROUP BY industry
	ORDER BY uni_count_over DESC
	limit 3),
yearly_rankings AS (
	SELECT 
		industry,
		extract(year FROM date_joined) as year,
		count(d.company_id) as num_unicorns,
		avg(VALUATION) AS average_valuation
	FROM industries as i
	JOIN dates as d
	ON i.company_id = d.company_id
	JOIN funding as f
	ON i.company_id = f.company_id
	WHERE extract(year FROM date_joined) IN ('2019', '2020', '2021')
	GROUP BY industry, year)
select industry, 
	year, 
	num_unicorns,
	round(avg(average_valuation)/1000000000, 2) as average_valuation_billions
FROM yearly_rankings
WHERE year IN ('2019', '2020', '2021')
AND industry IN (
	SELECT industry
	FROM top_industries)
group by industry, year, num_unicorns
ORDER BY industry, year DESC;
This was an Unguided Project: I was given just the task to accomplish without any step-by-step instructions. The code below was what I produced on my own, but the grader didn't like it because I ordered my results to a more detailed level. I put the 3 industries in order of how many unicorn companies they had (Fintech was top w/ 173, Internet software was next with 152, and E-commerce was 3rd w/ 75.) The code above was how I modified my code so the grader would accept it.
----CTE ut (unicorn total) gives the top 3 industries (according to # of unicorn companies created in those three years), and their total of unicorn companies
WITH ut AS (
	SELECT industry, COUNT(company_id) as uni_count_over
	FROM industries
	WHERE company_id IN (
		select company_id
		FROM dates 
		WHERE extract(year FROM date_joined) IN ('2019', '2020', '2021')
		)
	GROUP BY industry
	ORDER BY uni_count_over DESC
	limit 3)
--the main query takes those top 3 industries, counts the number of unicorn companies for them per year, and takes their average valuation per year
SELECT 
	ut.industry,
	extract(year FROM date_joined) as year,
	count(d.company_id) as num_unicorns,
	ROUND(avg(VALUATION)/1000000000, 2) AS average_valuation_billions
FROM ut
LEFT JOIN industries as i
ON ut.industry = i.industry
LEFT JOIN dates as d
ON i.company_id = d.company_id
LEFT JOIN funding as f
ON i.company_id = f.company_id
WHERE extract(year FROM date_joined) IN ('2019', '2020', '2021')
AND d.company_id IN (
		select company_id
		FROM dates 
		WHERE extract(year FROM date_joined) IN ('2019', '2020', '2021')
	)
GROUP BY ut.industry, year, ut.uni_count_over
ORDER BY ut.uni_count_over DESC, year DESC;