Skip to content

Commit 1598de5

Browse files
committed
code added
1 parent 7a28e46 commit 1598de5

7 files changed

+234
-21
lines changed

array_json.sql

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ VALUES
2121
('Atlanta', '{TV,"Wireless Internet","Air conditioning","Smoke detector","Carbon monoxide detector",Essentials,"Lock on bedroom door",Hangers,Iron,"Fire extinguisher"}');
2222

2323

24-
-- Count array length of column values
25-
SELECT city, array_length(string_to_array(array_values, ','), 1) AS number_of_amenities
26-
FROM arrayTable;
27-
2824
-- Convert a comma-separated string into an array using string_to_array.
2925
SELECT string_to_array('TV,Wireless Internet,Air conditioning', ',') AS amenities_array;
3026

3127

28+
-- Count array length of column values
29+
SELECT city, array_length(string_to_array(array_values, ','), 1) AS number_of_amenities
30+
FROM arrayTable;
31+
3232
-- Count the number of unique values in array_values column across all entries.
3333
SELECT COUNT(DISTINCT unnest(string_to_array(array_values, ','))) AS unique_amenities_count
3434
FROM arrayTable;

consequitive.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-- Using a CTE to calculate the most and least expensive products in each category
2+
23
WITH cte AS (
34
SELECT
45
name,
@@ -11,7 +12,6 @@ WITH cte AS (
1112
LAST_VALUE(model) OVER (PARTITION BY category ORDER BY price DESC RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS least_expensive_product_name
1213
FROM products
1314
)
14-
-- Select the category, model, price, most expensive product, and least expensive product from the CTE
1515
SELECT
1616
category,
1717
model,
@@ -21,6 +21,7 @@ SELECT
2121
FROM cte;
2222

2323
-- Query to find users who have logged in consecutively 3 or more times
24+
2425
SELECT DISTINCT repeated_names
2526
FROM (
2627
SELECT *,

tricky_questions/Marketing_Campaign_Success.sql

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
-- https://platform.stratascratch.com/coding/514-marketing-campaign-success-advanced?code_type=1
2+
3+
/*
4+
5+
You have a table of in-app purchases by user. Users that make their first in-app purchase are placed in a marketing campaign where they see call-to-actions for more in-app purchases.
6+
Find the number of users that made additional in-app purchases due to the success of the marketing campaign.
7+
8+
9+
The marketing campaign doesn't start until one day after the initial in-app purchase so users that only made one or multiple purchases on the first day do not count,
10+
nor do we count users that over time purchase only the products they purchased on the first day.
11+
12+
*/
13+
114
with cte as (
215
select *,
316
rank() over(partition by user_id order by created_at) as rn
+56-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,57 @@
1-
with cte as (
2-
select *,
3-
row_number() over(partition by company order by salary desc) rw,
4-
count(1) over(partition by company order by salary desc RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as scn
5-
from salaries
6-
), even_odd as (
7-
SELECT *
8-
FROM cte
9-
WHERE scn % 2 = 0 AND rw IN (scn / 2, (scn / 2) + 1)
10-
union all
11-
SELECT *
12-
FROM cte
13-
WHERE scn % 2 <> 0 and rw = (scn + 1) / 2
1+
/*
2+
3+
The Employee table holds all employees. The employee table has three columns: Employee Id, Company Name, and Salary.
4+
5+
+-----+------------+--------+
6+
|Id | Company | Salary |
7+
+-----+------------+--------+
8+
|1 | A | 2341 |
9+
|2 | A | 341 |
10+
|3 | A | 15 |
11+
|4 | A | 15314 |
12+
|5 | A | 451 |
13+
|6 | A | 513 |
14+
|7 | B | 15 |
15+
|8 | B | 13 |
16+
|9 | B | 1154 |
17+
|10 | B | 1345 |
18+
|11 | B | 1221 |
19+
|12 | B | 234 |
20+
|13 | C | 2345 |
21+
|14 | C | 2645 |
22+
|15 | C | 2645 |
23+
|16 | C | 2652 |
24+
|17 | C | 65 |
25+
+-----+------------+--------+
26+
27+
Write a SQL query to find the median salary of each company. Bonus points if you can solve it without using any built-in SQL functions.
28+
29+
+-----+------------+--------+
30+
|Id | Company | Salary |
31+
+-----+------------+--------+
32+
|5 | A | 451 |
33+
|6 | A | 513 |
34+
|12 | B | 234 |
35+
|9 | B | 1154 |
36+
|14 | C | 2645 |
37+
+-----+------------+--------+
38+
39+
*/
40+
WITH cte AS (
41+
SELECT *,
42+
ROW_NUMBER() OVER (PARTITION BY company ORDER BY salary DESC) AS rw,
43+
COUNT(1) OVER (PARTITION BY company ORDER BY salary DESC RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS scn
44+
FROM salaries
45+
),
46+
even_odd AS (
47+
SELECT *
48+
FROM cte
49+
WHERE scn % 2 = 0 AND rw IN (scn / 2, (scn / 2) + 1)
50+
UNION ALL
51+
SELECT *
52+
FROM cte
53+
WHERE scn % 2 <> 0 AND rw = (scn + 1) / 2
1454
)
15-
SELECT id, company, salary FROM even_odd group by company, salary, id
55+
SELECT id, company, salary
56+
FROM even_odd
57+
GROUP BY company, salary, id;

tricky_questions/amazon_questions.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ create table events
7373

7474
with cte as (
7575
select
76-
INITCAP(e.status) status, -- to make it capital
76+
INITCAP(e.status) status, -- to make first char capital
7777
Concat(a.first_name, ' ', a.last_name) customer,
7878
string_agg(distinct b.name, ', ') campaign,
7979
count(1) total,

tricky_questions/meta.sql

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
3+
The following schema is a subset of a relational database of a grocery store
4+
chain. This chain sells many products of different product classes to its
5+
customers across its different stores. It also conducts many different
6+
promotion campaigns.
7+
8+
The relationship between the four tables we want to analyze is depicted below:
9+
10+
# sales # products
11+
+------------------+---------+ +---------------------+---------+
12+
| product_id | INTEGER |>--------| product_id | INTEGER |
13+
| store_id | INTEGER | +---<| product_class_id | INTEGER |
14+
| customer_id | INTEGER | | | brand_name | VARCHAR |
15+
+---<| promotion_id | INTEGER | | | product_name | VARCHAR |
16+
| | store_sales | DECIMAL | | | is_low_fat_flg | TINYINT |
17+
| | store_cost | DECIMAL | | | is_recyclable_flg | TINYINT |
18+
| | units_sold | DECIMAL | | | gross_weight | DECIMAL |
19+
| | transaction_date | DATE | | | net_weight | DECIMAL |
20+
| +------------------+---------+ | +---------------------+---------+
21+
| |
22+
| # promotions | # product_classes
23+
| +------------------+---------+ | +---------------------+---------+
24+
+----| promotion_id | INTEGER | +----| product_class_id | INTEGER |
25+
| promotion_name | VARCHAR | | product_subcategory | VARCHAR |
26+
| media_type | VARCHAR | | product_category | VARCHAR |
27+
| cost | DECIMAL | | product_department | VARCHAR |
28+
| start_date | DATE | | product_family | VARCHAR |
29+
| end_date | DATE | +---------------------+---------+
30+
+------------------+---------+
31+
32+
Question 1
33+
34+
-- What percent of all products in the grocery chain's catalog
35+
-- are both low fat and recyclable?
36+
37+
38+
EXPECTED OUTPUT:
39+
Note: Please use the column name(s) specified in the expected output in your solution.
40+
+----------------------------+
41+
| pct_low_fat_and_recyclable |
42+
+----------------------------+
43+
| 15.384615384615385 |
44+
+----------------------------+
45+
46+
Question 2
47+
48+
-- What are the top five (ranked in decreasing order)
49+
-- single-channel media types that correspond to the most money
50+
-- the grocery chain had spent on its promotional campaigns?
51+
52+
Single Media Channel Types are promotions that contain only one media type.
53+
54+
EXPECTED OUPTUT:
55+
Note: Please use the column name(s) specified in the expected output in your solution.
56+
+---------------------------+------------+
57+
| single_channel_media_type | total_cost |
58+
+---------------------------+------------+
59+
| In-Store Coupon | 70800.0000 |
60+
| Street Handout | 70627.0000 |
61+
| Radio | 60192.0000 |
62+
| Sunday Paper | 56994.0000 |
63+
| Product Attachment | 50815.0000 |
64+
+---------------------------+------------+
65+
66+
Question 3
67+
68+
-- Of sales that had a valid promotion, the VP of marketing
69+
-- wants to know what % of transactions occur on either
70+
-- the very first day or the very last day of a promotion campaign.
71+
72+
73+
EXPECTED OUTPUT:
74+
Note: Please use the column name(s) specified in the expected output in your solution.
75+
+-------------------------------------------------------------+
76+
| pct_of_transactions_on_first_or_last_day_of_valid_promotion |
77+
+-------------------------------------------------------------+
78+
| 41.9047619047619048 |
79+
+-------------------------------------------------------------+
80+
81+
82+
Question 4
83+
84+
-- The CMO is interested in understanding how the sales of different
85+
-- product families are affected by promotional campaigns.
86+
-- To do so, for each of the available product families,
87+
-- show the total number of units sold,
88+
-- as well as the ratio of units sold that had a valid promotion
89+
-- to units sold without a promotion,
90+
-- ordered by increasing order of total units sold.
91+
92+
93+
EXPECTED OUTPUT
94+
Note: Please use the column name(s) specified in the expected output in your solution.
95+
+----------------+------------------+--------------------------------------------------+
96+
| product_family | total_units_sold | ratio_units_sold_with_promo_to_sold_without_promo|
97+
+----------------+------------------+--------------------------------------------------+
98+
| Drink | 43.0000 | 0.79166666666666666667 |
99+
| Non-Consumable | 176.0000 | 0.76000000000000000000 |
100+
| Food | 564.0000 | 0.75155279503105590062 |
101+
+----------------+------------------+--------------------------------------------------+
102+
103+
104+
Question 5
105+
106+
-- The VP of Sales feels that some product categories don't sell
107+
-- and can be completely removed from the inventory.
108+
-- As a first pass analysis, they want you to find what percentage
109+
-- of product categories have never been sold.
110+
111+
EXPECTED OUTPUT:
112+
Note: Please use the column name(s) specified in the expected output in your solution.
113+
+-----------------------------------+
114+
| pct_product_categories_never_sold |
115+
+-----------------------------------+
116+
| 13.8888888888888889 |
117+
+-----------------------------------+
118+
119+
*/
120+
121+
122+
1.
123+
124+
select count(case when is_low_fat_flg = 1 and is_recyclable_flg = 1 then 1 end) * 100.0 / count(*) from products
125+
126+
2.
127+
128+
select media_type as single_channel_media_type, sum(cost) total_cost
129+
from promotions where media_type not like '%,%'
130+
group by media_type
131+
order by 2 desc
132+
limit 5
133+
134+
3.
135+
136+
select count(case when transaction_date=start_date or transaction_date=end_date then 1 end) * 100.0 / count(*) pct_of_transactions_on_first_or_last_day_of_valid_promotion
137+
from sales s join promotions p on s.promotion_id=p.promotion_id
138+
139+
140+
4.
141+
142+
select product_family, sum(units_sold) as total_units_sold,
143+
sum(case when s.promotion_id != 0 then units_sold end) * 1.0 / sum(case when s.promotion_id = 0 then units_sold end) * 1.0 ratio_units_sold_with_promo_to_sold_without_promo
144+
from sales s join products p on s.product_id=p.product_id
145+
join product_classes pc on pc.product_class_id=p.product_class_id
146+
group by product_family
147+
148+
149+
5.
150+
151+
select (tc - count(distinct pc.product_category)) * 100.0 / tc
152+
from (select count(distinct product_category) tc from product_classes),
153+
sales s
154+
join products p on s.product_id=p.product_id
155+
join product_classes pc on pc.product_class_id=p.product_class_id

tricky_questions/yt-assignment.sql

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ Note - Data is not provided for these tables, you can insert some dummy data if
3131
At a time, one student can be active in one batch only. One Student can not be transferred more than four times.
3232
Calculate each students attendance percentage for all the sessions.
3333
34-
*/
34+
*/
35+
36+
-- TODO

0 commit comments

Comments
 (0)