SQL Coding Interview Question Using A Window Function (PARTITION BY) | Data Science Interviews

Поделиться
HTML-код
  • Опубликовано: 11 дек 2024

Комментарии • 155

  • @stratascratch
    @stratascratch  4 года назад +5

    Timestamps:
    Intro: (0:00)
    Interview Question: (0:11)
    Explore the Dataset: (0:38)
    Solution Approach: (1:27)
    Coding - JOIN The Tables: (3:05)
    Coding - Explanation of the SQL Window Function: (4:22)
    Coding - Finishing up the Solution: (6:37)

  • @ProdbyTrehnt
    @ProdbyTrehnt 3 года назад +32

    legit thought I sucked at SQL but doing some questions and reading deeper I was able to teach myself a lot of concepts just by practicing and seeing what I got wrong.... Thanks for the content Nate 👍

    • @stratascratch
      @stratascratch  3 года назад +1

      That's great! Keep on improving! Glad you've found the content helpful.

  • @mohitupadhayay1439
    @mohitupadhayay1439 2 года назад +2

    2 months ago I was so afraid to even walk through an SQL video on RUclips. But look at me now! I am diving deeper and learning so much every day. Thankfully i didn't gave up!

  • @DaniMrtini
    @DaniMrtini 2 года назад +5

    I get the overall gist of solving questions like this but breaking jt down so nicely like you is something I'm strugglong with. Got final interviews with Amazon coming up and just looking around

  • @AdvikSSC2023
    @AdvikSSC2023 3 года назад +1

    Really heart touching .
    With few seconds you will soled this query with beter understanding

  • @adhirajmajumder
    @adhirajmajumder 2 года назад

    You're the best SQL/Data science RUclipsr.

    • @stratascratch
      @stratascratch  2 года назад +1

      Thank you! And thanks for watching my videos!

  • @aashishmalhotra
    @aashishmalhotra 2 года назад +2

    Ok i write steps on my own by breaking down question. But how to implement it helped me. Also converting to float is learnt thanku

  • @mercantilism954
    @mercantilism954 2 года назад

    This is amazing intro for windows function. Love the video.
    For those who want to replicate his code, "order_cost" has been replaced with "total_rder_cost"

  • @ShivaniPandhare
    @ShivaniPandhare 3 года назад +27

    Very well explained. I am amazed how the difficult question seemed easy after the explanation!! 😁 I am going to try and use the approach you've used here. Thanks!!

    • @stratascratch
      @stratascratch  3 года назад +3

      Definitely give the framework a try! It'll help break the more complex portions of the problem into simpler bite size pieces.

  • @alfredama
    @alfredama 3 года назад +1

    That was really neat , i love the clear and concise train of thought before delving into code

  • @marinamondadorigessinger6390
    @marinamondadorigessinger6390 3 года назад +2

    thanks a million, it helped me a lot with the understanding of Partition by clause!

  • @okygy2125
    @okygy2125 3 года назад +2

    Very clean explanation. Short and sweet :-) Thanks a lot!

  • @angelomartinez9843
    @angelomartinez9843 3 года назад

    Great explanation of partition by. I was so lost before watching this video! Thank you!

  • @yasoram8007
    @yasoram8007 3 года назад +1

    Simple and excellent explanation Nate! . Thank you

  • @bachlam9841
    @bachlam9841 3 года назад

    Thank for your vid, I didn’t think about partitioning method, and intend to use sub query with group by id for calculate the percentage before selecting it

  • @subhashgn1775
    @subhashgn1775 3 года назад

    Very well explained.
    Thank you for providing the great resource.

  • @thulanidendere3986
    @thulanidendere3986 2 года назад

    You have explained this well sir,

  • @madhurasawarkar8373
    @madhurasawarkar8373 3 года назад +3

    Really Good tutorial Nate!!! Thanks for uploading such a good content

    • @stratascratch
      @stratascratch  3 года назад +2

      Thank you for watching. I'm glad you enjoyed the video and hope you take a look at the other sql videos I've posted.

  • @oscarherediamiranda616
    @oscarherediamiranda616 2 года назад

    Hi guy. Thanks you for this video. You have a new subscriber now. I am studying software engineering here in peru and I would like to have a master at data engineering or data scientist.

  • @sauravkumar9454
    @sauravkumar9454 3 года назад

    Your explanatory videos provide a great deal of knowledge. Thanks.

    • @stratascratch
      @stratascratch  3 года назад +1

      Thanks so much and thanks for watching! Let me know if you have any recommendations for topics and I can try to make a video.

    • @sauravkumar9454
      @sauravkumar9454 3 года назад

      @@stratascratch It would help If you can cover what questions are asked in the interview from other topics as well and then you can keep adding videos to that section just like SQL.

    • @stratascratch
      @stratascratch  3 года назад

      @@sauravkumar9454 Thanks for the suggestion. This is something I do plan on creating in the near future in addition to doing some python videos. Thanks for keeping engaged.

    • @sauravkumar9454
      @sauravkumar9454 3 года назад

      @@stratascratch Great. Will be tuned for the upcoming stuff.

  • @jmmmmmmmmmk
    @jmmmmmmmmmk 2 года назад

    Very very good video and well explained. Thank you

    • @stratascratch
      @stratascratch  2 года назад

      Thank you. Glad you find it helpful.

  • @bakkster29
    @bakkster29 3 года назад +2

    Amazing tutorial Nate!! Keep the awesome content coming!

    • @stratascratch
      @stratascratch  3 года назад +1

      Thank you for watching! Will keep churning out SQL videos. And will jump into some python stuff soon!

  • @cabaymau5132
    @cabaymau5132 3 года назад

    very well-explained, Thank you and big subcribed

  • @IrakliChitishvili
    @IrakliChitishvili 3 года назад +3

    Great. One question: Using the same window function, how to show total spent by customer? Like Eva: 180, Farida 260 etc without repeating sum over each order_cost?

    • @observer698
      @observer698 2 года назад

      I think we need to use group by then over name field using the SUM(order_cost)

    • @keifer7813
      @keifer7813 2 года назад

      @@observer698 Would there even be a point in the window function if we just used a group by?

  • @dollajihukum
    @dollajihukum 3 года назад

    so nice 🙏....

  • @EminentJade
    @EminentJade 2 года назад

    lifesaver before interview, tyvm.

  • @chriskeo392
    @chriskeo392 2 года назад

    Content 🔥

  • @Daro2023
    @Daro2023 10 месяцев назад

    woaw super helpful

  • @halildurmaz7827
    @halildurmaz7827 2 года назад +1

    Do interviewers allow us to create example tables (2 rows + Vertical), on paper? Because, it really helps me to think about the solution.

    • @stratascratch
      @stratascratch  2 года назад

      I think that is okay. Everyone has their own process of coming up with a solution. Talk to your interviewer.

  • @RobertoSolanoM
    @RobertoSolanoM 3 года назад

    great video man!! you won a follower

  • @ashutoshanand4040
    @ashutoshanand4040 3 года назад

    You got a new subscriber 🙂

  • @HowardLinca
    @HowardLinca 2 года назад

    GENIUS...

  • @alexlawson2750
    @alexlawson2750 4 года назад +4

    This is really well made and well explained. Thank you so much for this! Quick question: is this possible with MySQL? (I would check, but I don't want to sit in front of my computer anymore today lol)

    • @stratascratch
      @stratascratch  4 года назад +4

      Hey! It is possible in MySQL. You'd use the same over() PARTITION BY functions. The only difference between postgres and MySQL is how one would cast data types. In postgres I used a ::FLOAT to change an integer to a decimal but in MySQL, I think you do something like cast(column_name as decimal). But you only need to do this if you're trying to convert it to a percentage. If you're going to keep it as a ratio (e.g., 0.55 for 55%), then no need to do any of that.

  • @Holu54
    @Holu54 2 года назад

    @ Nate, thanks for the effort in putting these stuffs together. I observed that we were silent on an assumption that was made in the question "Assume each customer has a unique first name (i.e., there is only 1 customer named Karen in the dataset) and that customers place at most only 1 order a day". Don't you think this assumption would change the dynamics of the solution? Thanks

    • @stratascratch
      @stratascratch  2 года назад

      Yes, those are assumptions that would definitely change the solution. We were silent in the video but on an interview, I would definitely mention those assumptions. Thanks for watching!

  • @yusufbas035
    @yusufbas035 2 года назад

    waow
    that was so nice
    nice work dude

    • @yusufbas035
      @yusufbas035 2 года назад

      it looks like simple but it is helpful

  • @Fux704
    @Fux704 2 года назад

    I was less sophisticated than you on my attempt (btw the parameters of the challenged have changed a bit). Guess I just love subqueries 😅. Sum over partition was genius.
    SELECT c.first_name
    , o.order_details
    , round(total_order_cost/sub.total_spend,2) as perc_total_spend
    from orders o
    join
    (select cust_id, sum(total_order_cost) as total_spend
    from orders group by 1) sub
    on o.cust_id = sub.cust_id
    join customers c
    on o.cust_id = c.id
    order by 1,2,3

  • @keifer7813
    @keifer7813 2 года назад +1

    I'm really starting to like window functions. Question though - could this have been done using a group by?

    • @stratascratch
      @stratascratch  2 года назад +1

      Yeah, you could use a group by but window functions are a bit more flexible. Depending on the question, sometimes it's just more clear to use a window function rather than a group by. It really depends on the question!

    • @keifer7813
      @keifer7813 2 года назад

      @@stratascratch Ahh got it. Thank you. Keep the videos coming!

  • @meghasyam427
    @meghasyam427 3 года назад

    That's a cool function

  • @rakibraihanrimon8784
    @rakibraihanrimon8784 4 года назад +1

    Well explained Bro, thanks

    • @stratascratch
      @stratascratch  4 года назад

      Thanks Rakib. If there's any videos you'd like me to create, let me know. I have more windowing functions videos coming up, case whens, and some data pivoting too.

  • @rohankhubchandani3694
    @rohankhubchandani3694 3 года назад

    you made this so simple omg

  • @manuelsojan9093
    @manuelsojan9093 3 года назад

    Hi Nate...I did it like this, but I'm getting different values for the percentages for Eva:
    SELECT first_name, order_details, ROUND((ORDER_COST/TOTAL_PER_CUSTOMER) * 100,0) AS Percent_of_total_spend FROM
    (select *, SUM(order_cost) OVER(PARTITION BY cust_id) AS TOTAL_PER_CUSTOMER from orders
    INNER JOIN CUSTOMERS
    ON ORDERS.CUST_ID = CUSTOMERS.ID) AS D1
    ORDER BY First_name

    • @stratascratch
      @stratascratch  3 года назад +1

      I like this solution. Nothing obvious right now but post this in the user discussion in the question. Someone from my team will help you.

    • @manuelsojan9093
      @manuelsojan9093 3 года назад

      @@stratascratch ok sure

  • @Vintagetube310
    @Vintagetube310 3 года назад

    Thank you for that clear explanation. For someone who wouldn’t know to use the partition clause, would there be another way to solve this?

    • @stratascratch
      @stratascratch  3 года назад +1

      The only other way I know is to create CTEs that would create views like the one the partition clause creates. Then you can join or use the CTE view so that it gets you the output you want.

  • @anmolchawla616
    @anmolchawla616 3 года назад

    Thank you so much.

  • @gowthamjeevanantham6144
    @gowthamjeevanantham6144 3 года назад

    Excellent explanation thanks

  • @gustavmoller8988
    @gustavmoller8988 3 года назад +1

    Nice tutorial :)

  • @kli9005
    @kli9005 3 года назад

    Nice explanation!! Thank you. However I was a little distracted by “eVa”.

  • @amazhobner
    @amazhobner 3 года назад

    Clear explanation but I used to think order by was mandatory, partition was optional when using over clause, so can you please explain in which scenario order by is mandatory when using the over clause?

    • @localmartian9047
      @localmartian9047 2 года назад

      Order by is mandatory when using ranking functions like rank dense_rank row_number lead lag, but for normal aggregation it can be skipped

  • @margimehta9609
    @margimehta9609 3 года назад

    Thanks for explaining it really well. I had a quick question: what if there are 2+ identical entries for order_details? In this example, what if Evan purchased a coat twice and slippers twice, still we wanted to sum both of those entries to give just one row per person per order_detail?

    • @stratascratch
      @stratascratch  3 года назад

      That's a great edge case. Right now there's no way of really knowing that without another column to make each row unique. It would be nice to have a date column to be able to differentiate these rows. That's something that I'd put in there. Or you can add an ID row in the very beginning to separate out all orders.

    • @Sam-zg4vc
      @Sam-zg4vc 3 года назад

      Should we create another partition by which will by on both columns first_name and order_details .
      That can be then decided with total cost

  • @asholkkumar4685
    @asholkkumar4685 3 года назад

    Very good explanation .thanks for that.
    if u don't mind share the tables source code ..

  • @msvrk123
    @msvrk123 3 года назад +1

    Hey Nate! Great problem, I found another solution to the problem:
    with sum_of_cost_per_cust(cust_id,tot_sum) as
    (select cust_id, sum(order_cost) from orders group by cust_id order by cust_id)
    select c.first_name,o.order_details, round(o.order_cost / s.tot_sum::FLOAT * 100) "Percentage"
    from customers c
    join orders o
    on c.id = o.cust_id
    join sum_of_cost_per_cust s
    on o.cust_id = s.cust_id
    order by c.first_name;
    By any chance could this query be optimized further?

    • @stratascratch
      @stratascratch  3 года назад

      I like it! I like to use CTEs like you've done as well in my code. I find that it really helps separate out logic. Thanks for sharing.

  • @GoktugAsc123
    @GoktugAsc123 2 года назад

    That is a really clear explanation but aren't we supposed to use the order_quantity column to calculate the total cost per person, too?

  • @SandyCoco1
    @SandyCoco1 2 года назад

    The way you explain these sql queries are a 💯
    Please what's the symbol used in converting int to float? Is it colon (:)

    • @stratascratch
      @stratascratch  2 года назад +1

      It's a double colon (::) for postgres. But for other db engines like MySQL it's cast() -- e.g., cast(column_name as int). You can use cast() for postgres too but most just use :: to save time.

    • @stratascratch
      @stratascratch  2 года назад +1

      Also thanks for the kind words!

    • @SandyCoco1
      @SandyCoco1 2 года назад

      @@stratascratch Thank you for the extra tip. I'm learning on MySQL ❤

  • @pratikparbhane8677
    @pratikparbhane8677 2 года назад

    Thanks A Lot !!!!!!!!!!!!!!

  • @nikilkvn
    @nikilkvn 3 года назад +1

    I am curious. why is that 'order_quantity' not included. It would not change the relative percentages. But, definitely changes the percentage value individually. The code when executed was a success by the system. can someone please explain. :( - Nice explanation BTW. Thanks Nate. :)

    • @stratascratch
      @stratascratch  3 года назад +1

      Thanks for watching! Would you be able to ask this in the discussion forum in the platform? Someone from my team or myself will help to answer it! Sorry about that, it's easier to handle these questions if they're logged on our side!

  • @manishbolbanda9872
    @manishbolbanda9872 3 года назад

    Well explained 👍

  • @nargisparvin4267
    @nargisparvin4267 4 года назад +1

    Hello sir, can you pls make videos on nodejs developer?

    • @stratascratch
      @stratascratch  4 года назад

      I wish I could =(. But I don't know anything about nodejs...

  • @chrislin6308
    @chrislin6308 4 года назад +1

    Hi great video explanation on this question, I tried to use ROUND( order_cost / sum(order_cost) over (partition by first_name)::FLOAT , 2 ) to round the percentage to 2 decimal places but it doesn't work. Is there another way to do it?

    • @stratascratch
      @stratascratch  4 года назад +3

      It's because of a data type mismatch. Round() expects a decimal dtype but we're passing a FLOAT. If you run this query into the platform (link to the question in the description):
      SELECT
      c.first_name,
      o.order_details,
      round(((o.order_cost / sum(o.order_cost) over (PARTITION BY c.first_name)::decimal)*100),2) AS percentage_total_cost
      FROM orders o
      JOIN customers c ON c.id = o.cust_id
      This will return what you want.

    • @stratascratch
      @stratascratch  4 года назад +2

      Also, any feedback for me? Things I can do better? Is the code viewable?
      Do you want to see any other type of concepts? I have more window function videos coming soon and some CASE WHENs starting in Jan.

    • @chrislin6308
      @chrislin6308 4 года назад +1

      @@stratascratch You explained it very clearly, very easy to follow.
      I would love it if you can do a video that categorizes different type of sql questions that frequently appear in the interview. (what are the must-knows)

    • @stratascratch
      @stratascratch  4 года назад +1

      @@chrislin6308 Good idea. I actually have that in my queue of videos to make. I'll make that after a few other videos users have requested so stay tuned. I think I can make this video in Jan or Feb. Thanks for the great idea.

  • @immanuelsuleiman7550
    @immanuelsuleiman7550 3 года назад

    Very cool

    • @stratascratch
      @stratascratch  3 года назад

      Thanks so much man! Let me know if you have any requests.

  • @thepogchamp7783
    @thepogchamp7783 2 года назад

    So I did the same thing without a window function by just having a nested select in the from... is that better or worse? And if I can do that, when do I really need a window function?

    • @stratascratch
      @stratascratch  2 года назад

      You can choose when to apply a window function vs another method. The nested select is slower because it's another operation inside a bigger operation that SQL will need to perform. It's more of an optimization and performance question when you compare the 2

  • @sophiema1433
    @sophiema1433 3 года назад

    Just thinking, what if there are customers with the same first name, is it better to partition by customer id?

    • @stratascratch
      @stratascratch  3 года назад

      Yup it is better. I think in a later version of the question, I had an assumption that all names are unique. In reality, partitioning by id is the best practice.

  • @mangoout8683
    @mangoout8683 2 года назад

    Why we didn’t take order_quantity into calculations?
    Apart from this solution is really easy if you know how to use partition by function

  • @VishalKumar-iv8gl
    @VishalKumar-iv8gl 2 года назад

    Which platform you are using?

    • @stratascratch
      @stratascratch  2 года назад +1

      This is StrataScratch. The coding is in postgres but MySQL is also available. Python is also available on the platform.

    • @VishalKumar-iv8gl
      @VishalKumar-iv8gl 2 года назад

      @@stratascratch Thanks..I will start practice on your site..😅

  • @mohit4902
    @mohit4902 3 года назад

    There are 2 kinds of people - 1) That use rank over partition by 2) That create a subquery

    • @stratascratch
      @stratascratch  3 года назад

      Ranking is sometimes faster so if someone asks you about query optimization...use ranking > subquery =)

  • @jaychopra8932
    @jaychopra8932 2 года назад

    what is the syntax for :: in mysql

    • @stratascratch
      @stratascratch  2 года назад +1

      It's a cast function in mysql. So something like cast(data_column as datatype)

  • @gursimarkaur9340
    @gursimarkaur9340 2 года назад

    Can this be done using a simple group by?

    • @stratascratch
      @stratascratch  2 года назад

      the edge here is that a group by will de-dup while a partition by will preserve all rows. So it might work on this dataset but a group by might fail with another dataset.

  • @saivarunkolluru
    @saivarunkolluru 3 года назад

    Which one is better and faster, group by solution or window func?

    • @stratascratch
      @stratascratch  3 года назад

      Probably the group by I would imagine. The window function is a bit like a group by but it might be more complicated if you have functions and partitions in there and would take more time to execute compared to a simple group by.

  • @rollinas1
    @rollinas1 4 года назад +1

    God damn MVP you are :)

    • @stratascratch
      @stratascratch  4 года назад

      Thank you Rolandas =). Let me know if there's other concepts/topics you want me to cover! Happy to make them.

  • @sharanchakradhar
    @sharanchakradhar 3 года назад

    Where can I find such questions more to crack ??

    • @stratascratch
      @stratascratch  3 года назад

      There's over 600+ coding questions on StrataScratch!

  • @NotFound-iu8wx
    @NotFound-iu8wx 4 года назад

    Hello Nate,
    I have a question, is it possible to 'loop' in sql

    • @stratascratch
      @stratascratch  4 года назад +2

      Yes you can. Here are 2 resources to help you write loops.
      1. www.wiseowl.co.uk/blog/s348/loops-in-sql.htm
      2. www.sqlshack.com/sql-while-loop-understanding-while-loops-in-sql-server/
      But I would recommend just porting the loops to python and doing them there. It's much easier to write loops in python and you can even add automation to anything you're trying to do. I would stick to data manipulation for SQL and do all automation, loops, advanced stuff in python.

  • @malice112
    @malice112 2 года назад

    can this be solved with a GROUP BY instead?

    • @stratascratch
      @stratascratch  2 года назад +1

      Yes you can but there might be duplicate rows that effect the outcome. That's just something to keep top of mind while coding it up

  • @MashiroRedo
    @MashiroRedo 2 года назад

    I think the hard part is understanding the data and what they mean by total customer spend. If someone misreads this part it would be difficult.

    • @stratascratch
      @stratascratch  2 года назад +1

      Agreed and it's hard to write the question in a clear manner without giving away too much of the solution. On an interview, you can at least ask clarifying questions.

  • @atharnayab3547
    @atharnayab3547 3 года назад

    why not use group by first_name?

  • @96merluzzo
    @96merluzzo 3 года назад

    Why did I discover your channel just today? I have an interview tomorrow! :( :'(

    • @96merluzzo
      @96merluzzo 3 года назад

      Great content by the way

    • @stratascratch
      @stratascratch  3 года назад +1

      Good luck! You should binge watch all of this! Try out the platform too to get an idea to see if you're ready for the coding portion!

    • @96merluzzo
      @96merluzzo 3 года назад

      @@stratascratch Thanks

  • @alexandersfetcu1121
    @alexandersfetcu1121 2 года назад

    CAST(order_cost AS NUMERIC) / SUM(order_cost) OVER( PARTITION BY first_name) * 100 AS Ratio

  • @teaspells9994
    @teaspells9994 3 года назад

    omg this is pretty hard! I can write some simple queries, but how can I learn the advanced material?

    • @stratascratch
      @stratascratch  3 года назад

      Keep practicing! Try the StrataScratch platform and others like Leetcode to get better. There's hundreds of hard questions on both platforms. And there's many free questions to try out.

  • @rakeshorrikan
    @rakeshorrikan 3 года назад +1

    Why don't we use group by?

    • @stratascratch
      @stratascratch  3 года назад +2

      If you use a group by with all those columns in the SELECT clause, it will cut the data by all those columns. We're just interested in summing the order cost by user name so we use a window function to bypass the order details and order cost columns in the SELECT. Give it a try using the link in the description. You can code up your solution and see if the solution matches mine. Thanks for watching and appreciate the comments.

    • @stratascratch
      @stratascratch  3 года назад

      @@hugomichaelnunezmejia3384 Yes that's true. So you'd group by first name and then the order details and percent of total purchases are aggregated by the group by. But to take the percentage you need the granularity of the order details (the numerator of the %) and then the overall total that is found by summing and grouping by first name (the denominator). So you lose the numerator by doing a groupby first name.

  • @tarushikhasharma5328
    @tarushikhasharma5328 2 года назад

    where can we practice SQL?

    • @stratascratch
      @stratascratch  2 года назад

      stratascratch.com is what you see in this video. Give it a try!

  • @fabio336ful
    @fabio336ful 2 года назад

    Why didnt you use group by?

  • @Reemo3
    @Reemo3 Год назад

    What is this using? :: does not convert the data type

    • @stratascratch
      @stratascratch  Год назад +1

      :: is a cast function in postgres. If you're using other sql engines, the function is usually cast(column_name as data_type)

    • @Reemo3
      @Reemo3 Год назад

      @@stratascratch very cool and simple, seen it in Python. Hopefully will have some Postgres exposure soon

  • @egor.okhterov
    @egor.okhterov 3 года назад

    Why PARTITION BY instead of GROUP BY?

    • @stratascratch
      @stratascratch  3 года назад

      Mainly because I wanted to sum all the orders by the user's name with still preserving the information in order details and order cost. Window partition was the only way to do it. Great question.

  • @jtanakala2976
    @jtanakala2976 2 года назад

    Hi,
    This is the table called comments_and_translations
    id comment translation
    1 very good
    2 good
    3 bad
    4 ordinary
    5 cdcdcdcd very bad
    6 excellent
    7 ababab not satisfied
    8 satisfied
    9 aabbaabb extraordinary
    10 ccddccbb medium
    The output should be
    comment
    very good
    good
    bad
    ordinary
    very bad
    excellent
    not satisfied
    satisfied
    extraordinary
    medium
    please help me out with the query.

  • @emiliogarza6446
    @emiliogarza6446 2 года назад +1

    Edit: Just saw the data changed, mine appears as 20, 125, 60 and yours appears as 20, 100, 60.
    I tried solving this with a correlated subquery but it gave me different percentages than yours.
    For example, for Eva your percentages are 56, 11, and 33. Mine appear as 60.98, 9.76, 29.27. If you do manual verification, you'll see that Eva spent $205 total, so the percentages (125, 20, 60) should be 60.98, 9.76, 29.27. Weird thing is that I'm getting the same percentages as you in Farida, what could be the mistake here?
    This is my solution:
    SELECT c.first_name, c.id, o1.order_details, ROUND(o1.total_order_cost / (SELECT SUM(total_order_cost)
    FROM orders o2
    WHERE o2.cust_id = o1.cust_id)*100, 2) AS per_total_spent
    FROM orders o1
    JOIN customers c ON c.id = o1.cust_id
    ORDER BY c.first_name, 100

    • @stratascratch
      @stratascratch  2 года назад

      You're right; the question and the datasets have been updated in the meantime. There's no error; the data for Farida stayed the same. From what I see, your code should work OK and return the correct solution on the platform.