Тёмный

DELOITTE SQL Interview Question | SQL Intermediate Question 19 

Code-Con
Подписаться 1,1 тыс.
Просмотров 2,1 тыс.
50% 1

Question - Find the products whose sales are increasing every year.
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(50),
category VARCHAR(50)
);
INSERT INTO products (product_id, product_name, category) VALUES
(1, 'Laptops', 'Electronics'),
(2, 'Jeans', 'Clothing'),
(3, 'Chairs', 'Home Appliances');
CREATE TABLE sales (
product_id INT,
year INT,
total_sales_revenue DECIMAL(10, 2),
PRIMARY KEY (product_id, year),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
INSERT INTO sales (product_id, year, total_sales_revenue) VALUES
(1, 2019, 1000.00),
(1, 2020, 1200.00),
(1, 2021, 1100.00),
(2, 2019, 500.00),
(2, 2020, 600.00),
(2, 2021, 900.00),
(3, 2019, 300.00),
(3, 2020, 450.00),
(3, 2021, 400.00);
#placement #college #ai #deloittejobs #amazon #meta #facebook #dataanalytics #sqldeveloper #sql

Опубликовано:

 

5 мар 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 12   
@ardsha
@ardsha 3 месяца назад
we can also achive this by using following : with cte as ( select *, LEAD(total_sales_revenue,1) over (partition by product_id order by year) as nxt_rev, LEAD(total_sales_revenue,2) over (partition by product_id order by year) as nxtnext_rev from sales) select p.product_id,p.product_name,p.category from cte c join products p on c.product_id=p.product_id where nxtnext_rev>nxt_rev and nxt_rev>total_sales_revenue
@vijaygupta7059
@vijaygupta7059 2 месяца назад
my solution on MSSQL DB: with cte as ( Select * ,case when total_sales_revenue< lead(total_sales_revenue,1, total_sales_revenue+1)over(partition by product_id order by year) then 1 else null end as new from sales ), sales_cte as( Select * from sales where product_id not in (select product_id from cte where new is null) ) select products.* from sales_cte inner join products on sales_cte.product_id = products.product_id group by products.product_id, products.product_name, products.category
@saicharan5846
@saicharan5846 3 месяца назад
First view
@shryk0s963
@shryk0s963 Месяц назад
with cte as (select *,(lead(total_sales_revenue) over (partition by product_id order by year)-total_sales_revenue)as x from sales) select distinct c.product_id,p.product_name from cte c inner join productss p on c.product_id=p.product_id where c.product_id not in (select product_id from cte where x
@Wardaddy3
@Wardaddy3 День назад
Thanks for the video. I used the lag function and used distinct instead of max in the output line. Is it valid? Please check. with cte as ( select p.product_id, p.product_name, s.year, total_sales_revenue, LAG(total_sales_revenue,1) over (partition by p.product_id order by year) as prev_year_revenue from products p join sales s on p.product_id = s.product_id --order by p.product_id, s.year ) select distinct product_id, product_name from cte where product_id not in (select product_id from cte where total_sales_revenue < prev_year_revenue)
@suvadipkundu152
@suvadipkundu152 Месяц назад
can we do it without using a CTE? i suspect we can , though unlikely to be optimal, thoughts??
@Code-Con
@Code-Con Месяц назад
Try it out
@chandanpatra1053
@chandanpatra1053 3 месяца назад
Hi bro, can you please send me the solution for this question create table brands ( Year int, Brand varchar(20), Amount int ) insert into brands values (2018, 'Apple', 45000); insert into brands values (2019, 'Apple', 35000); insert into brands values (2020, 'Apple', 75000); insert into brands values (2018, 'Samsung', 15000); insert into brands values (2019, 'Samsung', 20000); insert into brands values (2020, 'Samsung', 25000); insert into brands values (2018, 'Nokia', 21000); insert into brands values (2019, 'Nokia', 17000); insert into brands values (2020, 'Nokia', 14000); Write a query to fetch the records of brands whose Revenue is increasing every year? Altough the question you have solved is similar. But differrence is it is a single table and don't have id. I tried to solve the approach you have said. But I can't resolve it. Please make a video or help me in the solution..
@Code-Con
@Code-Con 3 месяца назад
ok will make a video on this soon
@chandanpatra1053
@chandanpatra1053 3 месяца назад
bro why don't you explain first logic in excel then jump to sql editor to write. can you explain the logic behind total_sales_revenue>nxt>rvn in excel. because there are for product_id 2 every row doesn't satisfy the codition. but how it is working I didn't get it. Please explain in excel first. I have already given feedback related to you previously.
@Code-Con
@Code-Con 3 месяца назад
It should not satisfy the condition, i am selecting those ids that are not satisfying the condition caz total_sales_revenue>nxt_rvn means next year revenue is less which goes against the condition in the question, so we will not consider those ids hence i have used "not in" in where clause. I hope it's clear now. and i will try to first show in excel the move to editor in coming videos.
@anime_763
@anime_763 Месяц назад
My solution with cte as ( SELECT * ,LEAD(total_sales1_revenue,1) OVER(PARTITION BY product_id ORDER BY year) Year1 ,LEAD(total_sales1_revenue,2) OVER(PARTITION BY product_id ORDER BY year) year2 ,ROW_NUMBER() OVER (PARTITION BY product_id ORDER BY year) as RW FROM sales1 ) ,cte2 as( SELECT *, CASE WHEN (total_sales1_revenue < Year1 ) AND (Year1 < Year2) THEN 1 ELSE 0 END flag FROM cte WHERE RW = 1 ) SELECT P.* FROM products1 P JOIN cte2 C ON P.product_id=C.product_id WHERE C.flag = 1
Далее
Olive can see you 😱
01:00
Просмотров 9 млн
Спасибо Анджилишка, попил😂
00:19
SQL Intermediate Question 1
10:21
Просмотров 378
MySQL: PRIMARY KEYS are easy
5:25
Просмотров 40 тыс.
Practice SQL Interview Query | Big 4 Interview Question
14:47