├── DataLemur-SQL-Challenges ├── Easy │ ├── Ad Campaign ROAS.sql │ ├── App Click-through Rate (CTR).sql │ ├── Apple Pay Volume.sql │ ├── Average Deal Size (Part 1).sql │ ├── Average Post Hiatus (Part 1).sql │ ├── Average Review Ratings.sql │ ├── Cards Issued Difference.sql │ ├── Cities With Completed Trades.sql │ ├── Compressed Mean.sql │ ├── Data Science Skills.sql │ ├── Duplicate Job Listings.sql │ ├── Final Account Balance.sql │ ├── Highest Number of Products.sql │ ├── Histogram of Tweets.sql │ ├── Laptop vs. Mobile Viewership.sql │ ├── LinkedIn Power Creators (Part 1).sql │ ├── Page With No Likes.sql │ ├── Patient Support Analysis (Part 1).sql │ ├── Pharmacy Analytics (Part 1).sql │ ├── Pharmacy Analytics (Part 2).sql │ ├── Pharmacy Analytics (Part 3).sql │ ├── Second Day Confirmation.sql │ ├── Spare Server Capacity.sql │ ├── Teams Power Users.sql │ ├── Top Rated Businesses.sql │ ├── Unfinished Parts.sql │ └── Well Paid Employees.sql ├── Hard │ ├── Active User Retention.sql │ ├── Advertiser Status.sql │ ├── Median Google Search Frequency.sql │ └── Y-on-Y Growth Rate.sql └── Medium │ ├── Best-Selling Product.sql │ ├── Card Launch Success.sql │ ├── Compensation Outliers.sql │ ├── Compressed Mode.sql │ ├── First Transaction.sql │ ├── Highest-Grossing Items.sql │ ├── Histogram of Users and Purchases.sql │ ├── International Call Percentage.sql │ ├── NYC Area Code.sql │ ├── Odd and Even Measurements.sql │ ├── Second Highest Salary.sql │ ├── Sending vs. Opening Snaps.sql │ ├── Signup Activation Rate.sql │ ├── Supercloud Customer.sql │ ├── Top 5 Artists.sql │ ├── Top Three Salaries.sql │ ├── Tweets' Rolling Averages.sql │ └── User's Third Transaction.sql ├── README.md └── Resources └── datalemurcover.jpg /DataLemur-SQL-Challenges/Easy/Ad Campaign ROAS.sql: -------------------------------------------------------------------------------- 1 | SELECT advertiser_id, ROUND((SUM(revenue) / SUM(spend))::decimal, 2) AS ROAS 2 | FROM ad_campaigns 3 | GROUP BY advertiser_id 4 | ORDER BY advertiser_id; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/App Click-through Rate (CTR).sql: -------------------------------------------------------------------------------- 1 | SELECT app_id, 2 | ROUND(100.0 * SUM(CASE WHEN event_type = 'click' THEN 1 ELSE 0 END) / SUM(CASE WHEN event_type = 'impression' THEN 1 ELSE 0 END), 2) AS ctr 3 | FROM events 4 | WHERE DATE_PART('YEAR', timestamp) = 2022 5 | GROUP BY app_id; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Apple Pay Volume.sql: -------------------------------------------------------------------------------- 1 | SELECT merchant_id, 2 | SUM(CASE WHEN LOWER(payment_method) = 'apple pay' THEN transaction_amount ELSE 0 END) total_transaction 3 | FROM transactions 4 | GROUP BY merchant_id 5 | ORDER BY total_transaction DESC; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Average Deal Size (Part 1).sql: -------------------------------------------------------------------------------- 1 | SELECT ROUND(SUM(yearly_seat_cost * num_seats) / COUNT(*)::DECIMAL, 2) 2 | FROM contracts; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Average Post Hiatus (Part 1).sql: -------------------------------------------------------------------------------- 1 | SELECT user_id, DATE_PART('DAY', MAX(post_date) - MIN(post_date)) AS days_between 2 | FROM posts 3 | WHERE DATE_PART('YEAR', post_date) = 2021 4 | GROUP BY user_id 5 | HAVING COUNT(user_id) > 1; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Average Review Ratings.sql: -------------------------------------------------------------------------------- 1 | SELECT DATE_PART('MONTH', submit_date) mth, product_id, ROUND(AVG(stars), 2) 2 | FROM reviews 3 | GROUP BY DATE_PART('MONTH', submit_date), product_id 4 | ORDER BY DATE_PART('MONTH', submit_date), product_id; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Cards Issued Difference.sql: -------------------------------------------------------------------------------- 1 | SELECT card_name, MAX(issued_amount) - MIN(issued_amount) AS difference 2 | FROM monthly_cards_issued 3 | GROUP BY card_name 4 | ORDER BY difference DESC; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Cities With Completed Trades.sql: -------------------------------------------------------------------------------- 1 | SELECT u.city, COUNT(t.user_id) AS total_orders 2 | FROM trades t 3 | INNER JOIN users u ON t.user_id = u.user_id 4 | WHERE t.status = 'Completed' 5 | GROUP BY u.city 6 | ORDER BY total_orders DESC 7 | LIMIT 3; 8 | 9 | -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Compressed Mean.sql: -------------------------------------------------------------------------------- 1 | SELECT ROUND(CAST(SUM(item_count * order_occurrences) / SUM(order_occurrences) AS NUMERIC), 1) AS mean 2 | FROM items_per_order; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Data Science Skills.sql: -------------------------------------------------------------------------------- 1 | SELECT candidate_id 2 | FROM candidates 3 | WHERE skill IN('Python', 'Tableau', 'PostgreSQL') 4 | GROUP BY candidate_id 5 | HAVING COUNT(candidate_id) = 3 6 | ORDER BY candidate_id; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Duplicate Job Listings.sql: -------------------------------------------------------------------------------- 1 | SELECT COUNT(DISTINCT t1.company_id) AS co_w_duplicate_jobs 2 | FROM job_listings t1 3 | JOIN job_listings t2 on t1.company_id = t2.company_id 4 | WHERE t1.title = t2.title AND t1.description = t2.description AND t1.job_id !=t2.job_id -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Final Account Balance.sql: -------------------------------------------------------------------------------- 1 | SELECT account_id, 2 | SUM(CASE WHEN transaction_type = 'Deposit' THEN amount ELSE -amount END) AS final_balance 3 | FROM transactions 4 | GROUP BY account_id; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Highest Number of Products.sql: -------------------------------------------------------------------------------- 1 | SELECT user_id, COUNT(product_id) AS product_num 2 | FROM user_transactions 3 | GROUP BY user_id 4 | HAVING SUM(spend) >= 1000 5 | ORDER BY COUNT(product_id) DESC, SUM(spend) DESC 6 | LIMIT 3; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Histogram of Tweets.sql: -------------------------------------------------------------------------------- 1 | WITH CT AS 2 | ( 3 | SELECT user_id, COUNT(tweet_id) tweet_bucket 4 | FROM tweets 5 | WHERE DATE_PART('YEAR', tweet_date) = 2022 6 | GROUP BY user_id 7 | ) 8 | 9 | SELECT tweet_bucket, COUNT(user_id) users_num 10 | FROM CT 11 | GROUP BY tweet_bucket; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Laptop vs. Mobile Viewership.sql: -------------------------------------------------------------------------------- 1 | SELECT COUNT(CASE WHEN device_type = 'laptop' THEN 1 END) laptop_views, 2 | COUNT(CASE WHEN device_type IN ('tablet', 'phone') THEN 1 END) mobile_views 3 | FROM viewership; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/LinkedIn Power Creators (Part 1).sql: -------------------------------------------------------------------------------- 1 | SELECT profile_id 2 | FROM personal_profiles pf 3 | INNER JOIN company_pages cp ON pf.employer_id = cp.company_id 4 | WHERE pf.followers > cp.followers 5 | ORDER BY profile_id; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Page With No Likes.sql: -------------------------------------------------------------------------------- 1 | SELECT P.page_id 2 | FROM pages P 3 | WHERE P.page_id NOT IN 4 | ( 5 | SELECT PL.page_id FROM page_likes PL 6 | ); 7 | 8 | -- Another Solution 9 | SELECT page_id 10 | FROM pages 11 | EXCEPT 12 | SELECT page_id 13 | FROM page_likes 14 | ORDER BY page_id 15 | -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Patient Support Analysis (Part 1).sql: -------------------------------------------------------------------------------- 1 | SELECT COUNT(cnt) AS member_count 2 | FROM 3 | ( 4 | SELECT COUNT(policy_holder_id) AS cnt 5 | FROM callers 6 | GROUP BY policy_holder_id 7 | HAVING COUNT(policy_holder_id) >= 3 8 | ) AS TEMP -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Pharmacy Analytics (Part 1).sql: -------------------------------------------------------------------------------- 1 | SELECT drug, 2 | (total_sales - cogs) AS total_profit 3 | FROM pharmacy_sales 4 | ORDER BY total_profit DESC 5 | LIMIT 3; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Pharmacy Analytics (Part 2).sql: -------------------------------------------------------------------------------- 1 | SELECT manufacturer, 2 | COUNT(drug) AS drug_count, 3 | ABS(SUM(total_sales - cogs)) AS total_loss 4 | FROM pharmacy_sales 5 | WHERE total_sales <= cogs 6 | GROUP BY manufacturer 7 | ORDER BY total_loss DESC 8 | -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Pharmacy Analytics (Part 3).sql: -------------------------------------------------------------------------------- 1 | 2 | SELECT manufacturer, 3 | CONCAT('$', total_result, ' million') AS sale 4 | FROM 5 | ( 6 | SELECT manufacturer, 7 | ROUND( SUM(total_sales) / 1000000) AS total_result 8 | FROM pharmacy_sales 9 | GROUP BY manufacturer 10 | ORDER BY SUM(total_sales) DESC, manufacturer 11 | ) AS TP 12 | -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Second Day Confirmation.sql: -------------------------------------------------------------------------------- 1 | SELECT E.user_id 2 | FROM texts T INNER JOIN emails E ON E.email_id = T.email_id 3 | WHERE T.signup_action = 'Confirmed' AND DATE(T.action_date) - DATE(E.signup_date) = 1; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Spare Server Capacity.sql: -------------------------------------------------------------------------------- 1 | WITH CT AS 2 | ( 3 | SELECT datacenter_id, SUM(monthly_demand) AS sum_cap 4 | FROM forecasted_demand 5 | GROUP BY datacenter_id 6 | ) 7 | 8 | SELECT CT.datacenter_id, dc.monthly_capacity - CT.sum_cap 9 | FROM CT, datacenters dc 10 | WHERE CT.datacenter_id = dc.datacenter_id 11 | ORDER BY CT.datacenter_id -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Teams Power Users.sql: -------------------------------------------------------------------------------- 1 | SELECT sender_id, COUNT(DISTINCT receiver_id) AS message_count 2 | FROM messages m1 3 | WHERE DATE_PART('year', sent_date) = 2022 4 | GROUP BY sender_id 5 | ORDER BY message_count DESC 6 | LIMIT 2; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Top Rated Businesses.sql: -------------------------------------------------------------------------------- 1 | SELECT SUM(CASE WHEN review_stars >= 4 THEN 1 ELSE 0 END) AS business_count, 2 | SUM(CASE WHEN review_stars >= 4 THEN 1 ELSE 0 END) * 100 / COUNT(*) AS top_rated_pct 3 | FROM reviews 4 | -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Unfinished Parts.sql: -------------------------------------------------------------------------------- 1 | SELECT part, 2 | assembly_step 3 | FROM parts_assembly 4 | WHERE finish_date IS NULL; 5 | -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Easy/Well Paid Employees.sql: -------------------------------------------------------------------------------- 1 | SELECT emp.employee_id, 2 | emp.name 3 | FROM employee AS emp 4 | INNER JOIN employee AS mang 5 | ON emp.manager_id = mang.employee_id AND emp.salary > mang.salary; 6 | -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Hard/Active User Retention.sql: -------------------------------------------------------------------------------- 1 | WITH CTE AS 2 | ( 3 | SELECT user_id, 4 | SUM( 5 | CASE WHEN DATE_PART('MONTH', event_date) = 6 AND DATE_PART('YEAR', event_date) = 2022 THEN 1 6 | ELSE 0 END 7 | ) AS jun, 8 | SUM( 9 | CASE WHEN DATE_PART('MONTH', event_date) = 7 AND DATE_PART('YEAR', event_date) = 2022 THEN 1 10 | ELSE 0 END 11 | ) AS jul 12 | 13 | FROM user_actions 14 | GROUP BY user_id 15 | ) 16 | 17 | SELECT '7' AS month, 18 | COUNT(user_id) AS monthly_active_users 19 | FROM CTE 20 | WHERE jun > 0 AND jul > 0; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Hard/Advertiser Status.sql: -------------------------------------------------------------------------------- 1 | SELECT COALESCE(advertiser.user_id, daily_pay.user_id) AS user_id, 2 | CASE 3 | WHEN paid IS NULL THEN 'CHURN' 4 | WHEN (paid IS NOT NULL AND status = 'CHURN') THEN 'RESURRECT' 5 | WHEN (paid IS NOT NULL AND status IN ('EXISTING', 'NEW', 'RESURRECT')) THEN 'EXISTING' 6 | WHEN (paid IS NOT NULL AND status IS NULL) THEN 'NEW' 7 | END AS new_status 8 | FROM advertiser 9 | FULL OUTER JOIN daily_pay 10 | USING(user_id) 11 | ORDER BY user_id; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Hard/Median Google Search Frequency.sql: -------------------------------------------------------------------------------- 1 | WITH CTE AS 2 | ( 3 | SELECT searches 4 | FROM search_frequency 5 | GROUP BY searches, GENERATE_SERIES(1, num_users) 6 | ) 7 | 8 | SELECT ROUND(PERCENTILE_CONT(0.50) WITHIN GROUP(ORDER BY searches)::DECIMAL, 1) AS median 9 | FROM CTE; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Hard/Y-on-Y Growth Rate.sql: -------------------------------------------------------------------------------- 1 | WITH CTE AS 2 | ( 3 | SELECT DATE_PART('year', transaction_date) AS year, 4 | product_id, 5 | spend AS curr_year_spend, 6 | LAG(spend) OVER(PARTITION BY product_id ORDER BY transaction_date) AS prev_year_spend 7 | 8 | FROM user_transactions 9 | ) 10 | 11 | SELECT *, ROUND( (curr_year_spend - prev_year_spend) / prev_year_spend * 100, 2) AS yoy_rate 12 | FROM CTE -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Best-Selling Product.sql: -------------------------------------------------------------------------------- 1 | WITH Oredered_Product_Selling AS 2 | ( 3 | SELECT category_name, 4 | product_name, 5 | DENSE_RANK() 6 | OVER(PARTITION BY category_name ORDER BY sales_quantity DESC, rating DESC)AS Rank 7 | FROM products 8 | INNER JOIN product_sales 9 | USING(product_id) 10 | ) 11 | 12 | 13 | SELECT category_name, 14 | product_name 15 | FROM Oredered_Product_Selling 16 | WHERE Rank = 1 17 | ORDER BY category_name, product_name; 18 | 19 | -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Card Launch Success.sql: -------------------------------------------------------------------------------- 1 | SELECT card_name, 2 | issued_amount 3 | FROM 4 | ( 5 | SELECT card_name, 6 | issued_amount, 7 | DENSE_RANK() OVER(PARTITION BY card_name ORDER BY issue_year, issue_month) AS rank 8 | FROM monthly_cards_issued 9 | ) AS TP 10 | WHERE rank = 1 11 | ORDER BY issued_amount DESC; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Compensation Outliers.sql: -------------------------------------------------------------------------------- 1 | WITH CT AS 2 | ( 3 | SELECT title, AVG(salary) av 4 | FROM employee_pay 5 | GROUP BY title 6 | ) 7 | 8 | SELECT DISTINCT ep.employee_id, salary, 9 | CASE WHEN salary >= 2 * CT.av THEN 'Overpaid' ELSE'Underpaid' END AS status 10 | 11 | FROM employee_pay ep, CT 12 | WHERE salary >= (2 * CT.av) OR salary * 2 < CT.av AND ep.title = CT.title 13 | ORDER BY employee_id; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Compressed Mode.sql: -------------------------------------------------------------------------------- 1 | SELECT item_count AS mode 2 | FROM items_per_order 3 | WHERE order_occurrences = 4 | ( 5 | SELECT MODE() WITHIN GROUP(ORDER BY order_occurrences DESC) 6 | FROM items_per_order 7 | ) 8 | ORDER BY mode; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/First Transaction.sql: -------------------------------------------------------------------------------- 1 | WITH CT AS 2 | ( 3 | SELECT *, 4 | ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY transaction_date) AS RN 5 | FROM user_transactions 6 | ) 7 | 8 | SELECT COUNT(user_id) AS users 9 | FROM CT 10 | WHERE RN = 1 AND spend >= 50; 11 | -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Highest-Grossing Items.sql: -------------------------------------------------------------------------------- 1 | WITH CT AS 2 | ( 3 | SELECT category, product, SUM(spend) AS total_spend, 4 | ROW_NUMBER() OVER(PARTITION BY category ORDER BY SUM(spend) DESC) AS RN 5 | FROM product_spend 6 | WHERE DATE_PART('year', transaction_date) = 2022 7 | GROUP BY category, product 8 | ) 9 | 10 | SELECT category, product, total_spend 11 | FROM CT 12 | WHERE RN IN(1, 2) 13 | -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Histogram of Users and Purchases.sql: -------------------------------------------------------------------------------- 1 | WITH CTE AS 2 | ( 3 | SELECT user_id, 4 | transaction_date, 5 | product_id, 6 | DENSE_RANK() OVER(PARTITION BY user_id ORDER BY transaction_date DESC) AS rank 7 | 8 | FROM user_transactions 9 | ) 10 | 11 | SELECT transaction_date, 12 | user_id, 13 | COUNT(product_id) AS purchase_count 14 | FROM CTE 15 | WHERE rank = 1 16 | GROUP BY transaction_date, user_id 17 | ORDER BY transaction_date -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/International Call Percentage.sql: -------------------------------------------------------------------------------- 1 | WITH International_Call AS 2 | ( 3 | SELECT SUM( 4 | CASE WHEN caller.country_id != receiver.country_id THEN 1.0 5 | ELSE 0.0 END 6 | ) AS Number_internationals 7 | FROM phone_calls AS pc 8 | INNER JOIN phone_info AS caller 9 | ON pc.caller_id = caller.caller_id 10 | INNER JOIN phone_info AS receiver 11 | ON pc.receiver_id = receiver.caller_id 12 | ) 13 | 14 | 15 | SELECT ROUND( 16 | (Number_internationals * 100 / (SELECT COUNT(*) FROM phone_calls)) 17 | , 1 18 | ) 19 | FROM International_Call -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/NYC Area Code.sql: -------------------------------------------------------------------------------- 1 | SELECT COUNT(*) AS nyc_count 2 | FROM phone_info AS pf 3 | INNER JOIN phone_calls AS pc 4 | ON pf.caller_id = pc.caller_id OR pf.caller_id = pc.receiver_id 5 | WHERE LEFT(phone_number, 6) = '+1-212'; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Odd and Even Measurements.sql: -------------------------------------------------------------------------------- 1 | WITH CT AS 2 | ( 3 | 4 | SELECT measurement_id, CAST(measurement_time AS DATE) AS measurement_day, measurement_value, 5 | ROW_NUMBER() OVER(PARTITION BY(CAST(measurement_time AS DATE)) ORDER BY measurement_id) AS RN 6 | FROM measurements 7 | ) 8 | 9 | SELECT measurement_day, 10 | SUM(CASE WHEN RN % 2 != 0 THEN measurement_value END) AS odd_sum, 11 | SUM(CASE WHEN RN % 2 = 0 THEN measurement_value END) AS even_sum 12 | FROM CT 13 | GROUP BY measurement_day; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Second Highest Salary.sql: -------------------------------------------------------------------------------- 1 | -- POSTGRESQL solution 2 | SELECT salary AS second_highest_salary 3 | FROM employee 4 | ORDER BY second_highest_salary DESC 5 | LIMIT 1 OFFSET 1; 6 | -- T-SQL solution 7 | SELECT TOP 1 salary AS second_highest_salary 8 | FROM employee 9 | WHERE salary < ( 10 | SELECT MAX(salary) 11 | FROM employee 12 | ) 13 | ORDER BY salary DESC; 14 | -- Another T-SQL solution 15 | WITH CTE AS 16 | ( 17 | SELECT salary, 18 | DENSE_RANK() OVER(ORDER BY salary DESC) AS RN 19 | 20 | FROM employee 21 | ) 22 | 23 | SELECT salary AS second_highest_salary 24 | FROM CTE 25 | WHERE RN = 2; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Sending vs. Opening Snaps.sql: -------------------------------------------------------------------------------- 1 | WITH GP AS 2 | ( 3 | SELECT age_bucket, 4 | SUM(CASE WHEN activity_type = 'open' THEN time_spent END) open, 5 | SUM(CASE WHEN activity_type = 'send' THEN time_spent END) send 6 | FROM activities, age_breakdown 7 | WHERE age_breakdown.user_id = activities.user_id 8 | GROUP BY age_bucket 9 | ) 10 | 11 | 12 | SELECT GP.age_bucket, 13 | ROUND( send / (SUM(open) + SUM(send)) * 100.0, 2) AS send_perc, 14 | ROUND( open / (SUM(open) + SUM(send)) * 100.0, 2) AS open_perc 15 | FROM GP 16 | GROUP BY GP.age_bucket, open, send 17 | ORDER BY age_bucket; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Signup Activation Rate.sql: -------------------------------------------------------------------------------- 1 | SELECT ROUND 2 | ( 3 | SUM(CASE WHEN signup_action = 'Confirmed' THEN 1.0 ELSE 0.0 END) / 4 | COUNT(emails.email_id) 5 | , 2 6 | ) 7 | FROM texts 8 | LEFT JOIN emails 9 | USING(email_id) -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Supercloud Customer.sql: -------------------------------------------------------------------------------- 1 | WITH SuperCloud AS 2 | ( 3 | 4 | SELECT customer_id, 5 | COUNT(DISTINCT products.product_category) AS NumberOfProducts 6 | FROM customer_contracts 7 | RIGHT JOIN products 8 | ON customer_contracts.product_id = products.product_id 9 | GROUP BY customer_id 10 | ) 11 | 12 | SELECT customer_id 13 | FROM SuperCloud 14 | WHERE NumberOfProducts = 15 | ( 16 | SELECT COUNT(DISTINCT product_category) 17 | FROM products 18 | ) -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Top 5 Artists.sql: -------------------------------------------------------------------------------- 1 | WITH CTE AS 2 | ( 3 | 4 | SELECT artist_name, 5 | COUNT(GSR.song_id) AS count_songs 6 | 7 | FROM global_song_rank AS GSR 8 | INNER JOIN songs 9 | ON songs.song_id = GSR.song_id 10 | INNER JOIN artists 11 | ON songs.artist_id = artists.artist_id 12 | 13 | WHERE rank <= 10 14 | GROUP BY artist_name 15 | ) 16 | 17 | SELECT * 18 | FROM 19 | ( 20 | SELECT artist_name, 21 | DENSE_RANK() OVER(ORDER BY count_songs DESC) AS artist_rank 22 | FROM CTE 23 | ) AS TP 24 | 25 | WHERE artist_rank <= 5 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Top Three Salaries.sql: -------------------------------------------------------------------------------- 1 | -- T-SQL Solution 2 | WITH CTE AS 3 | ( 4 | SELECT department_name, 5 | name, 6 | salary, 7 | DENSE_RANK() OVER(PARTITION BY department_name ORDER BY salary DESC) AS RN 8 | FROM employee AS E 9 | JOIN department AS D 10 | ON E.department_id = D.department_id 11 | ) 12 | 13 | SELECT department_name, 14 | name, 15 | salary 16 | FROM CTE 17 | WHERE RN IN (1, 2, 3) 18 | ORDER BY department_name, salary DESC, name; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/Tweets' Rolling Averages.sql: -------------------------------------------------------------------------------- 1 | SELECT user_id, 2 | tweet_date, 3 | ROUND( 4 | AVG(tweet_count) 5 | OVER(PARTITION BY user_id ORDER BY tweet_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) 6 | , 2 7 | ) 8 | FROM tweets; -------------------------------------------------------------------------------- /DataLemur-SQL-Challenges/Medium/User's Third Transaction.sql: -------------------------------------------------------------------------------- 1 | SELECT user_id, spend, transaction_date 2 | FROM ( 3 | SELECT *,ROW_NUMBER () OVER (PARTITION BY user_id ORDER BY transaction_date) AS RD 4 | FROM transactions 5 | ) AS T 6 | 7 | WHERE RD = 3 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Project Logo](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/Resources/datalemurcover.jpg) 2 | 3 | --- 4 | 5 |

Solutions for SQL Challenges hosted on DataLemur

6 | 7 |

8 | Overview • 9 | Contents • 10 |

11 | 12 | ## 🔎Overview 13 | 14 | This repository contains my solutions for the SQL challenges posted on [DataLemur](https://datalemur.com/sql-interview-questions) website. The solution queries are executed on the integrated PostgreSQL environment on the same website. 15 | 16 | ## 🎯Content 17 | 18 | The repository directory structure is as follows: 19 | 20 | **DataLemur-SQL-Challenges:** 21 | 22 | This directory contains the solution PostgreSQL scripts for the challenges mentioned in the **Solution Scripts** table. 23 | 24 | In the below table, the columns indicate the following attributes related to the challenge: 25 | 26 | - **ID :** Identity column for individual challenge 27 | - **Challenge :** Link to the SQL challenge hosted on [DataLemur](https://datalemur.com/sql-interview-questions) 28 | - **Solution Script :** Link to the solution SQL script hosted in this repository 29 | 30 | #### Difficulty Level: Easy 31 | 32 | | ID | Challenge | Solution Script | 33 | | :-: | ------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------: | 34 | | 01 | [Cities With Completed Trades](https://datalemur.com/questions/completed-trades) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Cities%20With%20Completed%20Trades.sql) | 35 | | 02 | [Page With No Likes](https://datalemur.com/questions/sql-page-with-no-likes) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Page%20With%20No%20Likes.sql) | 36 | | 03 | [Laptop vs Mobile Viewership](https://datalemur.com/questions/laptop-mobile-viewership) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Laptop%20vs.%20Mobile%20Viewership.sql) | 37 | | 04 | [Teams Power Users](https://datalemur.com/questions/teams-power-users) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Teams%20Power%20Users.sql) | 38 | | 05 | [Highest Number of Products](https://datalemur.com/questions/sql-highest-products) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Highest%20Number%20of%20Products.sql) | 39 | | 06 | [Histogram of Tweets](https://datalemur.com/questions/sql-histogram-tweets) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Histogram%20of%20Tweets.sql) | 40 | | 07 | [Spare Server Capacity](https://datalemur.com/questions/sql-spare-server-capacity) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Spare%20Server%20Capacity.sql) | 41 | | 08 | [Duplicate Job Listings](https://datalemur.com/questions/duplicate-job-listings) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Duplicate%20Job%20Listings.sql) | 42 | | 09 | [Average Post Hiatus (Part 1)](https://datalemur.com/questions/sql-average-post-hiatus-1) | [Solution]() | 43 | | 10 | [App Clickthrough Rate (CTR)](https://datalemur.com/questions/sql-app-ctr) | [Solution]() | 44 | | 11 | [LinkedIn Power Creators (Part 1)](https://datalemur.com/questions/linkedin-power-creators) | [Solution]() | 45 | | 12 | [Data Science Skills](https://datalemur.com/questions/matching-skills) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Data%20Science%20Skills.sql) | 46 | | 13 | [Final Account Balance](https://datalemur.com/questions/final-account-balance) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Final%20Account%20Balance.sql) | 47 | | 14 | [Average Review Ratings](https://datalemur.com/questions/sql-avg-review-ratings) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Average%20Review%20Ratings.sql) | 48 | | 15 | [Unfinished Parts](https://datalemur.com/questions/tesla-unfinished-parts) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Unfinished%20Parts.sql) | 49 | | 16 | [Ad Campaign ROAS](https://datalemur.com/questions/ad-campaign-roas) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Ad%20Campaign%20ROAS.sql) | 50 | | 17 | [Second Day Confirmation](https://datalemur.com/questions/second-day-confirmation) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Second%20Day%20Confirmation.sql) | 51 | | 18 | [Apple Pay Volume](https://datalemur.com/questions/apple-pay-volume) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Apple%20Pay%20Volume.sql) | 52 | | 19 | [Average Deal Size (Part 1)](https://datalemur.com/questions/sql-average-deal-size) | [Solution]() | 53 | | 20 | [Top Rated Businesses](https://datalemur.com/questions/sql-top-businesses) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Top%20Rated%20Businesses.sql) | 54 | | 21 | [Patient Support Analysis (Part 1)](https://datalemur.com/questions/frequent-callers) | [Solution]() | 55 | | 22 | [Pharmacy Analytics (Part 2)](https://datalemur.com/questions/non-profitable-drugs) | [Solution]() | 56 | | 23 | [Pharmacy Analytics (Part 3)](https://datalemur.com/questions/total-drugs-sales) | [Solution]() | 57 | | 24 | [Cards Issued Difference](https://datalemur.com/questions/cards-issued-difference) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Cards%20Issued%20Difference.sql) | 58 | | 25 | [Compressed Mean](https://datalemur.com/questions/alibaba-compressed-mean) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Compressed%20Mean.sql) | 59 | | 26 | [Well Paid Employees](https://datalemur.com/questions/sql-well-paid-employees) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Easy/Well%20Paid%20Employees.sql) | 60 | 61 | #### Difficulty Level: Medium 62 | 63 | | ID | Challenge | Solution Script | 64 | | :-: | ---------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------: | 65 | | 01 | [User's Third Transaction](https://datalemur.com/questions/sql-third-transaction) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/User's%20Third%20Transaction.sql) | 66 | | 02 | [Compensation Outliers](https://datalemur.com/questions/compensation-outliers) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Compensation%20Outliers.sql) | 67 | | 03 | [Sending vs. Opening Snaps](https://datalemur.com/questions/time-spent-snaps) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Sending%20vs.%20Opening%20Snaps.sql) | 68 | | 04 | [Tweets' Rolling Averages](https://datalemur.com/questions/rolling-average-tweets) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Tweets'%20Rolling%20Averages.sql) | 69 | | 05 | [Odd and Even Measurements](https://datalemur.com/questions/odd-even-measurements) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Odd%20and%20Even%20Measurements.sql) | 70 | | 06 | [Highest-Grossing Items](https://datalemur.com/questions/sql-highest-grossing) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Highest-Grossing%20Items.sql) | 71 | | 07 | [First Transaction](https://datalemur.com/questions/sql-first-transaction) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/First%20Transaction.sql) | 72 | | 08 | [Top 5 Artists](https://datalemur.com/questions/top-fans-rank) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Top%205%20Artists.sql) | 73 | | 09 | [Signup Confirmation Rate](https://datalemur.com/questions/signup-confirmation-rate) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Signup%20Activation%20Rate.sql) | 74 | | 10 | [Supercloud Customer](https://datalemur.com/questions/supercloud-customer) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Supercloud%20Customer.sql) | 75 | | 11 | [Histogram of Users and Purchases](https://datalemur.com/questions/histogram-users-purchases) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Histogram%20of%20Users%20and%20Purchases.sql) | 76 | | 12 | [Card Launch Success](https://datalemur.com/questions/card-launch-success) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Card%20Launch%20Success.sql) | 77 | | 13 | [Compressed Mode](https://datalemur.com/questions/alibaba-compressed-mode) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Compressed%20Mode.sql) | 78 | | 14 | [International Call Percentage](https://datalemur.com/questions/international-call-percentage) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/International%20Call%20Percentage.sql) | 79 | | 15 | [Best-Selling Product](https://datalemur.com/questions/best-selling-products) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Best-Selling%20Product.sql) | 80 | | 16 | [NYC Area Code](https://datalemur.com/questions/nyc-area-code) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/NYC%20Area%20Code.sql) | 81 | | 17 | [Second Highest Salary](https://datalemur.com/questions/sql-second-highest-salary) |[Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Second%20Highest%20Salary.sql) | 82 | | 18 | [Second Highest Salary](https://datalemur.com/questions/sql-top-three-salaries) |[Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Medium/Top%20Three%20Salaries.sql) | 83 | 84 | #### Difficulty Level: Hard 85 | 86 | | ID | Challenge | Solution Script | 87 | | :-: | ------------------------------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------: | 88 | | 01 | [Active User Retention](https://datalemur.com/questions/user-retention) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Hard/Active%20User%20Retention.sql) | 89 | | 02 | [Y-on-Y Growth Rate](https://datalemur.com/questions/yoy-growth-rate) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Hard/Y-on-Y%20Growth%20Rate.sql) | 90 | | 03 | [Advertiser Status](https://datalemur.com/questions/updated-status) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Hard/Advertiser%20Status.sql) | 91 | | 04 | [Median Google Search Frequency](https://datalemur.com/questions/median-search-freq) | [Solution](https://github.com/Ereh11/DateLemur-SQL-Interview-Questions/blob/main/DataLemur-SQL-Challenges/Hard/Median%20Google%20Search%20Frequency.sql) | 92 | -------------------------------------------------------------------------------- /Resources/datalemurcover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ereh11/DateLemur-SQL-Interview-Questions/7e8b592405f9c63ce24c11a9443da27149a5e801/Resources/datalemurcover.jpg --------------------------------------------------------------------------------