├── Easy-Problem ├── tesla_unfinished_parts.sql ├── facebook_pages_with_no_likes.sql ├── JPMorgan_Chase_Card_Iussed_Difference ├── paypal_unique_money_transfer.sql ├── amazon_purchasing_activity_by_product_type.sql ├── ebay_highest_numbers_of_products.sql ├── robinhood_cities_with_completed_trades.sql ├── linkedin_power_creator.sql ├── Microsoft_teams_power_users.sql ├── tiktok_second_day_confirmation.sql ├── linkedin_duplicate_job_listings.sql ├── facebook_average_post_hiatus.sql ├── facebook_click_through_rate(CTR).sql ├── microsoft_spare_server_capacity.sql ├── twitter_histograms_of_tweets.sql ├── photoshop_revenue_analysis.sql ├── newyorktimes_laptop_Vs_mobile_viewership.sql ├── amazon_average_review_ratings.sql ├── amazon_highest_grossing_items.sql ├── linkedin_data_science_skill.sql ├── histograms_of_users_and_purchases.sql ├── paypal_final_account_balance.sql └── google_ad_campaigns.sql └── README.md /Easy-Problem/tesla_unfinished_parts.sql: -------------------------------------------------------------------------------- 1 | -- To find the parts which are not finished. 2 | 3 | SELECT part FROM parts_assembly where finish_date is NULL 4 | GROUP BY part; 5 | -------------------------------------------------------------------------------- /Easy-Problem/facebook_pages_with_no_likes.sql: -------------------------------------------------------------------------------- 1 | -- To find FB pages with zero likes 2 | 3 | SELECT page_id from pages 4 | WHERE page_id NOT IN (SELECT page_id FROM page_likes); 5 | -------------------------------------------------------------------------------- /Easy-Problem/JPMorgan_Chase_Card_Iussed_Difference: -------------------------------------------------------------------------------- 1 | SELECT card_name, 2 | (MAX(issued_amount)-min(issued_amount)) difference 3 | from monthly_cards_issued 4 | GROUP BY card_name 5 | ORDER BY difference DESC; 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DataLemur 2 | 3 | Has all the solutions of DataLemur sql problems. 4 | I have started to solve the problems and simultaneously adding my solutions too. 5 | 6 | Data Lemur Site: [Data Lemur](https://www.datalemur.com) 7 | -------------------------------------------------------------------------------- /Easy-Problem/paypal_unique_money_transfer.sql: -------------------------------------------------------------------------------- 1 | -- To find the two way payment done by user 2 | 3 | WITH two_way AS 4 | (SELECT payer_id, recipient_id FROM payments 5 | INTERSECT 6 | SELECT recipient_id, payer_id FROM payments) 7 | SELECT COUNT(*)/2 8 | from two_way; 9 | -------------------------------------------------------------------------------- /Easy-Problem/amazon_purchasing_activity_by_product_type.sql: -------------------------------------------------------------------------------- 1 | -- To get the cumulative sum of all products. 2 | 3 | SELECT order_date, product_type, 4 | SUM(quantity) OVER (PARTITION BY product_type 5 | ORDER BY order_date) AS cum_purchased 6 | FROM total_trans 7 | ORDER BY order_date; 8 | -------------------------------------------------------------------------------- /Easy-Problem/ebay_highest_numbers_of_products.sql: -------------------------------------------------------------------------------- 1 | -- To sort and find top 3 products with spend > 1000 2 | 3 | SELECT user_id, COUNT(*) product_id 4 | FROM user_transactions 5 | GROUP BY user_id 6 | HAVING SUM(spend) >= '1000' 7 | ORDER BY product_id DESC, SUM(spend) DESC 8 | LIMIT 3; 9 | -------------------------------------------------------------------------------- /Easy-Problem/robinhood_cities_with_completed_trades.sql: -------------------------------------------------------------------------------- 1 | -- To get cities with completed trades 2 | 3 | SELECT city, count(*) total_orders 4 | from trades join users on trades.user_id = users.user_id 5 | where status = 'Completed' 6 | group by city 7 | order by count(*) desc 8 | limit 3 9 | -------------------------------------------------------------------------------- /Easy-Problem/linkedin_power_creator.sql: -------------------------------------------------------------------------------- 1 | -- To find the max followers of employees compared to their employer 2 | 3 | SELECT pp.profile_id 4 | FROM personal_profiles pp 5 | JOIN company_pages cp ON pp.employer_id = cp.company_id 6 | WHERE pp.followers > cp.followers 7 | ORDER BY pp.profile_id; 8 | -------------------------------------------------------------------------------- /Easy-Problem/Microsoft_teams_power_users.sql: -------------------------------------------------------------------------------- 1 | -- To find top 2 message count in August 2022 2 | 3 | SELECT sender_id, COUNT(sender_id) AS message_count 4 | FROM messages 5 | WHERE sent_date BETWEEN '2022-08-01' AND '2022-08-31' 6 | GROUP BY sender_id 7 | ORDER BY message_count DESC LIMIT 2; 8 | 9 | -------------------------------------------------------------------------------- /Easy-Problem/tiktok_second_day_confirmation.sql: -------------------------------------------------------------------------------- 1 | -- To find the confirmation date next of signup date 2 | 3 | SELECT user_id 4 | FROM emails e 5 | LEFT JOIN texts t ON e.email_id = t.email_id 6 | WHERE e.signup_date = t.action_date - INTERVAL '1day' 7 | AND signup_action = 'Confirmed' 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Easy-Problem/linkedin_duplicate_job_listings.sql: -------------------------------------------------------------------------------- 1 | -- To find the duplicate jobs posted by same company 2 | 3 | SELECT COUNT(*) FROM ( 4 | SELECT company_id, COUNT(company_id), title, description 5 | FROM job_listings 6 | GROUP BY company_id, title, description 7 | HAVING COUNT(company_id) > 1) AS duplicate_companies; 8 | -------------------------------------------------------------------------------- /Easy-Problem/facebook_average_post_hiatus.sql: -------------------------------------------------------------------------------- 1 | To find the difference between the first and last post 2 | 3 | SELECT user_id, EXTRACT(DAY FROM MAX(post_date) - MIN(post_date)) AS days_between 4 | FROM posts WHERE post_date BETWEEN '2021-01-01' AND '2021-12-31' 5 | GROUP BY user_id 6 | HAVING EXTRACT(DAY FROM MAX(post_date) - MIN(post_date)) NOT IN ('0'); 7 | -------------------------------------------------------------------------------- /Easy-Problem/facebook_click_through_rate(CTR).sql: -------------------------------------------------------------------------------- 1 | -- To find CTR percentage 2 | 3 | SELECT 4 | app_id, 5 | ROUND(100.0* 6 | SUM(CASE WHEN event_type = 'click' THEN 1 ELSE 0 END)/ 7 | SUM(CASE WHEN event_type = 'impression' THEN 1 ELSE 0 END), 2) AS ctr 8 | FROM events 9 | WHERE timestamp >= '2022-01-01' AND timestamp < '2023-01-01' 10 | GROUP BY app_id; 11 | -------------------------------------------------------------------------------- /Easy-Problem/microsoft_spare_server_capacity.sql: -------------------------------------------------------------------------------- 1 | -- To find the spare capacity of datacenter 2 | 3 | SELECT d.datacenter_id, 4 | d.monthly_capacity - SUM(monthly_demand) AS spare_capacity 5 | FROM datacenters d 6 | JOIN forecasted_demand fr 7 | ON d.datacenter_id = fr.datacenter_id 8 | GROUP BY d.datacenter_id, d.monthly_capacity 9 | ORDER BY d.datacenter_id; 10 | -------------------------------------------------------------------------------- /Easy-Problem/twitter_histograms_of_tweets.sql: -------------------------------------------------------------------------------- 1 | -- To count users number of tweets 2 | with tweet_count 3 | AS ( 4 | SELECT COUNT(tweet_id) AS tweet_count, user_id FROM tweets 5 | WHERE tweet_date BETWEEN '2022-01-01' 6 | AND '2022-12-31' 7 | GROUP BY user_id) 8 | 9 | SELECT tweet_count AS tweet_bucket, COUNT(user_id) AS user_num 10 | FROM tweet_count 11 | GROUP BY tweet_count; 12 | -------------------------------------------------------------------------------- /Easy-Problem/photoshop_revenue_analysis.sql: -------------------------------------------------------------------------------- 1 | -- To find the sum of product cost from users where product is not Photoshop 2 | 3 | SELECT customer_id, SUM(revenue) AS revenue 4 | FROM adobe_transactions 5 | WHERE customer_id IN (SELECT customer_id 6 | FROM adobe_transactions WHERE product = 'Photoshop') 7 | AND product != 'Photoshop' 8 | GROUP BY customer_id 9 | ORDER BY customer_id; 10 | -------------------------------------------------------------------------------- /Easy-Problem/newyorktimes_laptop_Vs_mobile_viewership.sql: -------------------------------------------------------------------------------- 1 | -- To find the count of laptop and mobile viewership 2 | 3 | WITH cte_count AS 4 | (SELECT 5 | (SELECT COUNT(device_type) 6 | FROM viewership WHERE device_type = 'laptop') AS laptop, 7 | (SELECT COUNT(*) device_type 8 | FROM viewership WHERE device_type IN ('tablet', 'phone')) AS mobile 9 | ) 10 | SELECT laptop, mobile FROM cte_count; 11 | 12 | 13 | -------------------------------------------------------------------------------- /Easy-Problem/amazon_average_review_ratings.sql: -------------------------------------------------------------------------------- 1 | -- To find the average rating of the product order by month and group by porduct 2 | 3 | SELECT mth AS month, product, 4 | ROUND(AVG(stars), 2) AS avg_stars FROM 5 | (SELECT EXTRACT(MONTH FROM submit_date) AS mth, 6 | product_id AS product, stars FROM reviews 7 | GROUP BY submit_date, product_id, stars) AS review 8 | GROUP BY month, product 9 | ORDER BY month, product; 10 | -------------------------------------------------------------------------------- /Easy-Problem/amazon_highest_grossing_items.sql: -------------------------------------------------------------------------------- 1 | -- To find out sum of top 2 highest grossing product according to their category 2 | 3 | SELECT category, product, total_spend 4 | FROM 5 | (SELECT category, product, SUM(spend) total_spend, 6 | RANK() OVER(PARTITION BY category ORDER BY SUM(spend) DESC) 7 | total FROM product_spend 8 | WHERE transaction_date BETWEEN '2022-01-01' AND '2022-12-31' 9 | GROUP BY category, product) AS total_ 10 | WHERE total_.total <=2; 11 | 12 | -------------------------------------------------------------------------------- /Easy-Problem/linkedin_data_science_skill.sql: -------------------------------------------------------------------------------- 1 | -- To find the candidates who have all Python, Tableau, and Postgresql skills 2 | 3 | SELECT candidate_id FROM candidates 4 | WHERE skill IN ('Python','Tableau', 'Postgresql') 5 | GROUP BY candidate_id 6 | ORDER BY candidate_id 7 | LIMIT 2; 8 | 9 | -- More generic solution 10 | SELECT candidate_id FROM candidates 11 | WHERE skill IN ('Python','Tableau', 'PostgreSQL') 12 | GROUP BY candidate_id 13 | HAVING COUNT(skill) = 3 14 | ORDER BY candidate_id; 15 | -------------------------------------------------------------------------------- /Easy-Problem/histograms_of_users_and_purchases.sql: -------------------------------------------------------------------------------- 1 | -- To find out number of users and product having latest transaction date 2 | 3 | with count_user AS( 4 | SELECT 5 | user_id, 6 | product_id, 7 | transaction_date, 8 | RANK() OVER(PARTITION BY user_id 9 | ORDER BY transaction_date DESC) rk 10 | FROM user_transactions) 11 | SELECT 12 | transaction_date, 13 | COUNT(DISTINCT user_id) number_of_users, 14 | COUNT(product_id) number_of_products 15 | FROM count_user 16 | WHERE rk = 1 17 | GROUP BY transaction_date; 18 | -------------------------------------------------------------------------------- /Easy-Problem/paypal_final_account_balance.sql: -------------------------------------------------------------------------------- 1 | -- To find amount remaining in the account after withdrawal 2 | 3 | WITH final_amt 4 | AS 5 | (SELECT account_id, transaction_type, sum(amount) AS amt 6 | FROM transactions 7 | WHERE transaction_type = 'Deposit' 8 | GROUP BY account_id, transaction_type), 9 | with_amt AS (SELECT account_id, transaction_type, sum(amount) as with_am 10 | FROM transactions 11 | WHERE transaction_type = 'Withdrawal' 12 | GROUP BY account_id, transaction_type) 13 | SELECT final_amt.account_id, (final_amt.amt - with_amt.with_am) final_balance 14 | FROM final_amt 15 | INNER JOIN with_amt ON final_amt.account_id = with_amt.account_id; 16 | -------------------------------------------------------------------------------- /Easy-Problem/google_ad_campaigns.sql: -------------------------------------------------------------------------------- 1 | -- To find the return on ad spend for each advertiser 2 | 3 | -- Normal way 4 | SELECT advertiser_id, ROUND(CAST(SUM(revenue)/SUM(spend) as numeric), 2) AS ROAS 5 | FROM ad_campaigns 6 | GROUP BY advertiser_id 7 | ORDER BY advertiser_id; 8 | 9 | -- Using subquery 10 | SELECT advertiser_id, ROAS 11 | FROM (SELECT advertiser_id, ROUND(CAST(SUM(revenue)/SUM(spend) as numeric), 2) ROAS 12 | FROM ad_campaigns 13 | GROUP BY advertiser_id) roun 14 | GROUP BY advertiser_id, ROAS 15 | ORDER BY advertiser_id; 16 | 17 | -- Usig CTE 18 | WITH ROAS_ AS 19 | (SELECT advertiser_id, SUM(revenue)/SUM(spend) ROAS 20 | FROM ad_campaigns 21 | GROUP BY advertiser_id) 22 | SELECT advertiser_id, ROUND(CAST(ROAS as numeric), 2) 23 | FROM ROAS_ 24 | GROUP BY advertiser_id, roas 25 | ORDER BY advertiser_id; 26 | --------------------------------------------------------------------------------