From the course: PostgreSQL: Advanced Queries
Unlock the full course today
Join today to access over 24,400 courses taught by industry experts.
Solution: Calculations across rows - PostgreSQL Tutorial
From the course: PostgreSQL: Advanced Queries
Solution: Calculations across rows
(bright music) - [Instructor] In this final challenge, we were asked to sort people by height and then figure out how much taller each person is over the next person in the sequence. Let's run the starting query, and we'll see that we get a list of names and each person's height in US inches. Now the very first thing that we need to do is to sort everyone tallest to shortest. And that just needs the addition of an order by clause to the end of the query here. So we'll say order by height_inches. And I want to make sure everybody is ordered descending so we get the tallest person here at the very top. Okay, so we can now see that Marcellus is the tallest person at 76.87, followed by Steph at 74.74. And I want to focus on these two people here. It'll help us out as we move through the rest of the challenge. Okay, so now everyone is in height order. Now we need to add in a column that says that Marcellus is taller than Steph. And we can do that by pulling out the name column and then…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Output row numbers with query results2m 29s
-
(Locked)
Cast values to a different data type2m 15s
-
(Locked)
Move rows within a result with LEAD and LAG5m 47s
-
(Locked)
Use an IN function with a subquery4m 7s
-
(Locked)
Define WHERE criteria with a series4m 30s
-
(Locked)
Solution: Calculations across rows4m 19s
-
(Locked)
Pattern matching with regular expressions6m 57s
-
(Locked)
-