DELOITTE SQL Interview Question | SQL Intermediate Question 19

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 มี.ค. 2024
  • 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

ความคิดเห็น • 11

  • @ardsha
    @ardsha หลายเดือนก่อน +1

    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 หลายเดือนก่อน

    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 2 หลายเดือนก่อน

    First view

  • @shryk0s963
    @shryk0s963 14 วันที่ผ่านมา

    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

  • @suvadipkundu152
    @suvadipkundu152 16 วันที่ผ่านมา

    can we do it without using a CTE? i suspect we can , though unlikely to be optimal, thoughts??

    • @Code-Con
      @Code-Con  15 วันที่ผ่านมา

      Try it out

  • @chandanpatra1053
    @chandanpatra1053 2 หลายเดือนก่อน

    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  2 หลายเดือนก่อน

      ok will make a video on this soon

  • @anime_763
    @anime_763 10 วันที่ผ่านมา

    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

  • @chandanpatra1053
    @chandanpatra1053 2 หลายเดือนก่อน

    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  2 หลายเดือนก่อน

      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.