Medium
Amazon
ID 10309
Marketing Campaign Success
You have a table of in-app purchases by user. Users that make their first in-app
purchase are placed in a marketing campaign where they see call-to-actions for more
in-app purchases. Find the number of users that made additional in-app purchases due
to the success of the marketing campaign. The marketing campaign doesn't start until
one day after the initial in-app purchase so users that make multiple purchases on the
same day do not count, nor do we count users that make only the same purchases over
time.
SELECT count(DISTINCT user_id) AS user_count
FROM
(SELECT user_id,
count(DISTINCT created_at) AS purchased_dates,
count(DISTINCT product_id) AS products_purchased
FROM marketing_campaign
GROUP BY user_id) a
WHERE purchased_dates > 1
AND products_purchased > 1
Medium
Amazon
ID 10309
Marketing Campaign Success
You have a table of in-app purchases by user. Users that make their first in-app
purchase are placed in a marketing campaign where they see call-to-actions for more
in-app purchases. Find the number of users that made additional in-app purchases due
to the success of the marketing campaign. The marketing campaign doesn't start until
one day after the initial in-app purchase so users that make multiple purchases on the
same day do not count, nor do we count users that make only the same purchases over
time.
Soln1:
with tem as (
SELECT user_id,
count(DISTINCT created_at) AS purchased_dates,
count(DISTINCT product_id) AS products_purchased
FROM marketing_campaign
GROUP BY user_id
having count(DISTINCT created_at) > 1
AND count(DISTINCT product_id) > 1)
SELECT count(DISTINCT user_id) AS user_count
FROM tem
Soln2:
SELECT count(DISTINCT user_id) AS user_count
FROM
(SELECT user_id,
count(DISTINCT created_at) AS purchased_dates,
count(DISTINCT product_id) AS products_purchased
FROM marketing_campaign
GROUP BY user_id) a
WHERE purchased_dates > 1
AND products_purchased > 1
Medium
Facebook
ID 10064
Highest Energy Consumption DOY
Find the day of the year with the highest energy consumption from Facebook data
centers. Output the day of the year along with the total energy consumption across all
data centers. Round the energy consumption to the nearest whole number.
Soln1:
SELECT EXTRACT (DOY
FROM (e.date :: DATE)) AS dayofyear,
round(SUM(e.consumption + a.consumption + n.consumption)) AS energy_consumption
FROM facebook_europe_energy_consumption e
INNER JOIN facebook_asia_energy_consumption a ON e.date = a.date
INNER JOIN facebook_na_energy_consumption n ON a.date = n.date
GROUP BY dayofyear
ORDER BY energy_consumption DESC
LIMIT 1
Medium
City of San Francisco
ID 9728
Count the number of inspections with violations that happened to
'Roxanne Cafe' in each year
Count the number of inspections with violations that happened to 'Roxanne Cafe' for
each year. Output the result along with the year. Order the result based on the year in
ascending order.
Soln1:
select left(inspection_date, 4) AS year, count(inspection_id) as n_inspections from
sf_restaurant_health_violations
WHERE violation_id IS NOT NULL
AND business_name = 'Roxanne Cafe'
GROUP BY year
ORDER BY year
Soln2:
SELECT
EXTRACT (YEAR FROM inspection_date :: DATE) AS year,
count(*) AS n_inspections
FROM sf_restaurant_health_violations
WHERE
business_name = 'Roxanne Cafe' AND
risk_category IS NOT NULL
GROUP BY year
ORDER BY year