Grubhub And Doordash Membership SQL​

Goal

For this question, coderpad is not needed and no datasets are available – simply write out your solution. Imagine you have two datasets: grubfub_dataset and doorfash_dataset. Each dataset contains a column titled user_id and the values are a string of numbers to identify a user. If user_id 1234 is in the grubfhub dataset if means that this person has a grubfub membership. If the user is in both datasets, it means the user has both memberships. Write a query to return something like the output below – percent of users that have only a grubfub membership, percent that have only doorfash, and percent that have both. 

Output

Column NameValueDescription
grubhub7Percent of users that have a grubfub membership
doordash34Percent of users that have a doorfash membership
both59Percent of users that have a both memberships
* The numbers here are made up – do not expect in final solution final solution.

Solution

SELECT 100*SUM(CASE WHEN d.user_id IS null THEN 1 ELSE 0 END)/COUNT(*) as g_only,
       100*SUM(CASE WHEN g.user_id IS null THEN 1 ELSE 0 END)/COUNT(*) as d_only,
       100*SUM(CASE WHEN g.user_id IS NOT null AND d.user_id IS NOT null THEN 1 ELSE 0 END)/COUNT(*) as BOTH
FROM
(SELECT distinct user_id FROM grubfub_dataset) g
FULL OUTER JOIN
(SELECT distinct user_id FROM doorfash_dataset) d
ON m.user_id = w.user_id;