9.5. Sorting

So far, we’ve only looked at rows of data in the order of the query is returning to us. What if we want to see the rows in a certain sorting order? We use the ORDER BY command to sort them by some other criteria.

For example, to see the bike trips in the order of the duration in seconds:

%%sql

SELECT
  member_type, start_date, duration
FROM
  trip_data
ORDER BY
  duration
LIMIT
  10
 * sqlite:///bikeshare.db
Done.
member_type start_date duration
Member 2011-01-08 12:28:59.000000 60
Member 2011-01-10 19:46:08.000000 60
Member 2011-01-17 03:59:57.000000 60
Member 2011-01-26 12:03:06.000000 60
Member 2011-01-29 14:01:28.000000 60
Member 2011-02-04 22:01:04.000000 60
Member 2011-02-23 09:23:36.000000 60
Member 2011-02-25 14:02:05.000000 60
Member 2011-02-27 14:03:44.000000 60
Member 2011-03-21 21:47:30.000000 60

Well, it turns out by default the sorting order is ascending. To sort the rows in descending order, add the keyword DESC.

%%sql

SELECT
  member_type, start_date, duration
FROM
  trip_data
ORDER BY
  duration DESC
LIMIT
  10
 * sqlite:///bikeshare.db
Done.
member_type start_date duration
Member 2011-06-09 19:18:26.000000 86355
Casual 2011-04-29 10:37:47.000000 86337
Member 2011-11-16 18:32:05.000000 86336
Casual 2011-05-14 10:12:06.000000 86187
Member 2011-07-07 11:34:45.000000 85679
Casual 2011-06-05 23:40:33.000000 85674
Casual 2011-06-05 23:40:53.000000 85666
Casual 2011-10-08 14:22:29.000000 85657
Member 2011-01-28 10:15:55.000000 85518
Casual 2011-07-02 14:38:06.000000 85505

Of course, we can mix WHERE and ORDER BY, to get only the bike trips from Member type of Casual in the order of the duration.

%%sql

SELECT
  member_type, start_date, duration
FROM
  trip_data
WHERE
  member_type = "Casual"
ORDER BY
  duration
LIMIT
  10
 * sqlite:///bikeshare.db
Done.
member_type start_date duration
Casual 2011-12-28 09:47:29.000000 61
Casual 2011-03-05 15:48:04.000000 62
Casual 2011-07-29 04:08:15.000000 62
Casual 2011-08-29 12:39:15.000000 64
Casual 2011-09-01 10:40:15.000000 65
Casual 2011-10-28 02:30:20.000000 70
Casual 2011-12-18 16:15:28.000000 71
Casual 2011-09-03 23:51:53.000000 72
Casual 2011-02-14 12:52:52.000000 73
Casual 2011-08-29 13:58:16.000000 73

9.5.1. Practice Exercises

Q-1: Get the start and end station IDs for bike trips that are longer 60 minutes or longer, in the order of largest number of seconds first and display the top 40 results. What is the duration of the last ride what is the ending station?

Q-2: On which bike was longest bike ride? How many seconds long was that ride?

Q-3: What is the duration and starting station of the longest ride starting and ending at the same station?

Lesson Feedback

    During this lesson I was primarily in my...
  • Comfort Zone
  • Learning Zone
  • Panic Zone
    Completing this lesson took...
  • Very little time
  • A reasonable amount of time
  • More time than is reasonable
    Based on my own interests and needs, the things taught in this lesson...
  • Don't seem worth learning
  • May be worth learning
  • Are definitely worth learning
    For me to master the things taught in this lesson feels...
  • Definitely within reach
  • Within reach if I try my hardest
  • Out of reach no matter how hard I try
Next Section - 9.6. Aggregation or Group By