How to divide a value in row by the value in the previous row in 1 table

I am doing the Warby Parker project and question 3 requires calculating the percentage of users who answer each question in the quiz funnel.

The hint says:

We divide the number of people completing each step by the number of people completing the previous step:

In SQL is there a way in to rather divide a value in row by the value in the previous row in 1 table?

Like in this example

Divide question 2’s responses (475) by question 1’s responses (500) : 475/500 * 100 = 95%
Divide question 3’s responses (380) by question 2’s responses (475) : 380/475 * 100 = 80%
Divide question 4’s responses (361) by question 3’s responses (380) : 361/380 * 100 = 95%
Divide question 5’s responses (270) by question 3’s responses (361) : 270/361 * 100 = 74,79%

Note: I realize the hint is incorrect and what should actually be done is to divide each question’s responses by the responses of question 1 (500). But I am curious is dividing by rows in 1 table in SQL possible?

Hello!
Consider writing out what you would do on paper. It looks like you’ve done that here:

Now, consider how you would make this more like SQL code. (This is called pseudo-code).

Once you’ve done that, you can then consider how to translate your pseudo code into actual SQL code.

1 Like