SELECT company.name, tag_type.tag, tag_type.type
FROM company
-- Join to the tag_company table
INNER JOIN tag_company
ON company.id = tag_company.company_id
-- Join to the tag_type table
INNER JOIN tag_type
ON tag_company.tag = tag_type.tag
-- Filter to most common type
WHERE type='cloud';
coalesce()fuction = specifying a defauly or backup value when a column contains NULL values
-- Use coalesce
SELECT coalesce(industry, sector, 'Unknown') AS industry2,
-- Don't forget to count!
count(*)
FROM fortune500
-- Group by what? (What are you counting by?)
GROUP BY industry2
-- Order results to see most common first
ORDER BY count DESC
-- Limit results to get just the one value you want
LIMIT 1;
SELECT company_original.name, title, rank
-- Start with original company information
FROM company AS company_original
-- Join to another copy of company with parent
-- company information
LEFT JOIN company AS company_parent
ON company_original.parent_id = company_parent.id
-- Join to fortune500, only keep rows that match
INNER JOIN fortune500
-- Use parent ticker if there is one,
-- otherwise original ticker
ON coalesce(company_parent.ticker,
company_original.ticker) =
fortune500.ticker
-- For clarity, order by rank
ORDER BY rank;
Column types and constraints
common