Posted by Surbhi, March 13, 2022, 4:10 p.m.
Answers
Thanks for the response Mike. Couple of follow up questions 1. Do you have recommendations on how to select between JOINS vs CTEs like in this case? 2. Could you provide an example of the join that would should incorrect results?
Formatting is a little weird here but: > Thanks for the response Mike. Couple of follow up questions > 1. Do you have recommendations on how to select between JOINS vs CTEs like in this case? Typically CTEs can be used to provide clarity as a shorthand for subqueries. It just makes things easier to read, and in some cases, the CTE can be cached / reused. It's also very similar to a temp table depending on the database server in question. Most interviewers are likely fine with either, as long as you can explain what you're doing. Typically I use CTEs unless it's a stupidly short query that doesn't really make sense to pull out of the main query. > 2. Could you provide an example of the join that would should incorrect results? It's a little tricky to show in here, but if you look at a simple: select * from ad_info left outer join advertiser on ad_info.ad_id = advertiser.ad_id This will give you several results where you can start to see the problem in the cost column. You'll notice that the ad_id and the cost are the same in multiple columns where the user_id is different. This fits that the total customer_spend for the ad is higher, but the costs are duplicated in the rows (which is shouldn't be.) The example solution just shows all costs for a given ad mapped to the advertiser id, and then all the user spend mapped to an ad_id without having to worry about the join duplication issue.
Hey Mike,
I felt like I have fallen for this trap too. MY code is something like this:
WITH advertiser_info AS
(SELECT advertiser_id,a.ad_id, SUM(spend) as spend, SUM(cost) as cost
FROM advertiser a
LEFT JOIN ad_info ai
ON a.ad_id = ai.ad_id
GROUP BY 1,2
)
SELECT advertiser_id, COALESCE(SUM(spend),0)*1.0/SUM(cost) as roi
FROM advertiser_info
GROUP BY 1
I think the CTE part is where things goes wrong but I am not so sure why