Strata Stratch SQL Question - Hard
Strata Stratch SQL Question - Hard
Twitter
ID 9897
Highest Salary In Department
Find the employee with the highest salary per department. Output the department
name, employee's first name along with the corresponding salary.
Soln1:
SELECT
FROM employee
FROM employee
GROUP BY department);
Soln2:
WHERE rank = 1
Soln3:
from employee)
Hard
Amazon
ID 9915
Highest Cost Orders
Find the customer with the highest order cost from 2019-02-1 to 2019-05-1. Output the
first name along with the order cost and the date.
- Use the WHERE clause set conditions regarding the period and the highest order cost.
- Use an inner query under WHERE clause conditions to find the highest order cost within the given
range of dates.
- Use the MAX() function to find the highest order cost and BETWEEN to set the range of dates.
Soln1:
ORDER BY 2 desc
LIMIT 1
Soln2:
FROM orders
AND order_cost IN (SELECT MAX(order_cost) FROM orders WHERE order_date BETWEEN '2019-02-1'
AND '2019-05-1' )
Hard
Microsoft
ID 10300
Premium vs Freemium
Find the total number of downloads for paying and non-paying users by date. Include
only records where non-paying customers have more downloads than paying
customers. The output should be sorted by earliest date first and contain 3 columns
date, non-paying downloads, paying downloads.
- ms_user_dimension will help link the number of downloads with whether an account is paying or non-
paying
- Use a CASE WHEN to help sum the total number of downloads between paying and non-paying users
Soln1:
END) AS paying,
sum(CASE
END) AS non_paying
FROM ms_user_dimension a
GROUP BY date
ORDER BY date) t
Soln2:
FROM ms_user_dimension a
JOIN ms_acc_dimension b
ON a.acc_id = b.acc_id
),
tmp2 AS(
FROM ms_download_facts c
JOIN tmp d
ON c.user_id = d.user_id
SELECT date,
FROM tmp2
GROUP BY 1
HAVING SUM(CASE WHEN paying_customer = 'yes' THEN downloads ELSE 0 end) < SUM(CASE WHEN
paying_customer = 'no' THEN downloads ELSE 0 end)
ORDER BY 1
Hard
Amazon
ID 10142
No Order Customers
Find customers who didn't place orders from 2019-02-01 to 2019-03-01. Output
customer's first name.
Soln1:
WHERE id not in (
Hard
Yelp
ID 10060
Top Cool Votes
Find the business and the review_text that received the highest number of 'cool' votes.
Output the business name along with the review text.
Soln1:
order by 3 desc)
Soln2:
SELECT
business_name, review_text
FROM yelp_reviews reviews
INNER JOIN
) mc
ON reviews.cool = mc.max_cool
Hard
Airbnb
ID 9632
Host Popularity Rental Prices
--You’re given a table of rental property searches by users. Find the minimum, average,
maximum rental prices for each host’s popularity rating. The host’s popularity rating is
defined as below:
0 reviews: New
1 to 5 reviews: Rising
6 to 15 reviews: Trending Up
16 to 40 reviews: Popular
more than 40 reviews: Hot
Soln1:
WITH tem1 AS (
ELSE 'Hot'
END as popularity_rating
from airbnb_search_details
GROUP BY popularity_rating