SQL Assignment Aggregation Solution
SQL Assignment Aggregation Solution
AGGREGATION
SOLUTION: AGGREGATION QUESTIONS
Below is the solution for the SQL Aggregation Assignment. For visualization questions, you can
use anything you want, the preferred way is to use Mode’s Dashboard. After finishing the
assignment, you can create a complete Exploratory Analysis Dashboard (pdf attachment) to put
in your portfolio or share with your employers or your friends.
1. CRUNCHBASE_COMPANIES
Answer the following questions using tutorial.crunchbase_companies table
2. Write a query that determines counts of every single column. Which column has the most
null values? Can you check the number of NULL records in that column?
• Column has the most NULL values: last_milestone_at
SELECT
COUNT(permalink) as permalink,
COUNT(name) AS name,
COUNT(homepage_url) AS homepage_url,
COUNT(category_code) AS category_code,
COUNT(funding_total_usd) AS funding_total_usd,
COUNT(status) AS status,
COUNT(country_code) AS country_code,
COUNT(state_code) AS state_code,
COUNT(region) AS region,
COUNT(city) AS city,
COUNT(funding_rounds) AS funding_rounds,
COUNT(founded_at) AS founded_at,
COUNT(founded_month) AS founded_month,
COUNT(founded_quarter) AS founded_quarter,
COUNT(founded_year) AS founded_year,
COUNT(first_funding_at) AS first_funding_at,
COUNT(last_funding_at) AS last_funding_at,
3. What is the minimum, maximum, total, and average funding total amount in USD? Find
out which company has the highest funding amount.
SELECT
MIN(funding_total_usd) AS min_funding,
MAX(funding_total_usd) AS max_funding,
SUM(funding_total_usd) AS total_funding,
AVG(funding_total_usd) AS avg_funding
FROM tutorial.crunchbase_companies
4. Each company is categorized with a category code. Can you find the number of
companies, total funding per category code? Which category has the highest total funding
amount?
• Query to find the number of companies, total funding per category code
SELECT
category_code,
COUNT(*) AS num_companies,
SUM(funding_total_usd) AS total_funding
FROM tutorial.crunchbase_companies
GROUP BY 1
ORDER BY 3 DESC
5. What is the average funding amount for each category code? Is the top 10 highest
average funding category code the same as the top 10 category code in the previous
questions? Why do you think that is?
• This is a tricky question, we cannot use the AVG function to get the average of
the funding amount since this aggregate function ignores the NULL values. If you
want to treat the NULLs value as zero, you will need to use both SUM and
• Visualization: Plot a chart to visualize the average funding amount from different
category codes.
7. Write a query that determines counts of every single column. Which column has the most
null values? Can you check the number of NULL records in that column?
• Column has the most NULL values: price_amount
SELECT
COUNT(company_permalink) as company_permalink,
COUNT(company_name) AS company_name,
COUNT(company_category_code) AS company_category_code,
COUNT(company_country_code) AS company_country_code,
COUNT(company_state_code) AS company_state_code,
COUNT(company_region) AS company_region,
COUNT(company_city) AS company_city,
COUNT(acquirer_permalink) AS acquirer_permalink,
COUNT(acquirer_category_code) AS acquirer_category_code,
COUNT(acquirer_country_code) AS acquirer_country_code,
COUNT(acquirer_state_code) AS acquirer_state_code,
COUNT(acquirer_region) AS acquirer_region,
COUNT(acquirer_city) AS acquirer_city,
COUNT(acquired_month) AS acquired_month,
COUNT(acquired_quarter) AS acquired_quarter,
COUNT(acquired_year) AS acquired_year,
COUNT(price_amount) AS price_amount,
COUNT(price_currency_code) AS price_currency_code
FROM tutorial.crunchbase_acquisitions
10. For each acquirer, find the number of companies they acquire and the total amount they
spend.
SELECT
acquirer_name,
COUNT(DISTINCT company_permalink) AS num_companies,
11. What is the number of acquired companies and total acquiring amount each year?
SELECT
acquired_year,
COUNT(DISTINCT company_permalink) AS num_companies,
SUM(price_amount) AS price_amount
FROM tutorial.crunchbase_acquisitions