Data Mastery: SQL — Order By

Written by laurenjglass9 | Published 2018/11/21
Tech Story Tags: sql | data-science | programming | data | interview-questions

TLDRvia the TL;DR App

Data Mastery: SQL — Order By

There are so many exciting projects out there in the Data World. Artificial Intelligence, Machine Learning, Neural Nets, Blockchain, and more are sweeping the technology industry. In order to get to the cutting-edge stuff, first and foremost, data needs to be stored, evaluated, and tested. The best place to do that is SQL (or a library that operates with SQL-like commands, see my article on Python’s Pandas library).

This series Data Mastery: SQL, will teach you the essential subjects. These are not exhaustive tutorials. Instead they are focused preparation guides — with brevity and efficiency in mind. It is meant for:

  • Software Engineers who want to analyze their creation’s data
  • Product Managers, Marketers, and others who want to be data-driven
  • Beginning Data Scientists, Data Engineers, Data Analysts, or Business Intelligence Developers preparing for interviews

See my resource list of the books I used to prepare for my big interview

Each article will contain a brief technical explanation of the subject, an example question, and an answer. Follow up articles will contain challenging questions so you can evaluate your learning.

This series does not come with accompanying data sets. The advantage to this is when you are on the drawing board, whether in an interview or project design, you do not have test data to play with. You have to think abstract.

Order By

The ORDER BY statement is a very simple SQL concept. It goes at the end of the SQL query. Following the ORDER BY keywords, you write a list of columns which will inform the engine how to sort the results. You can specify if you want each column to be sorted ascending or descending with the keywords ASC and DESC.

Recall our daily_user_score table from earlier in the series:

date       | userid    | sessionid | score
------------------------------------------
2018–09–01 | 983489272 | 125       | 112
2018–09–01 | 234342423 | 34        | 112
2018–09–01 | 567584329 | 207       | 618
2018–09–02 | 983489272 | 126       | 410
2018–09–02 | 983489272 | 127       | 339

If we wanted to view scores top to bottom and the users who earned the score in descending order as well, the SQL query would look like this:

SELECT score, 
       userid
FROM daily_user_score
ORDER BY score DESC, userid DESC;

These are the results of the above query:

score | userid 
--------------------
618   | 567584329
410   | 983489272
339   | 983489272
112   | 983489272
112   | 234342423

Notice the score 112 has two users. Since the score is the same, the SQL engine goes on to sort the userids in this set in descending order as well.

Try it yourself

Write a SQL query that lists the userids from smallest to largest and their total score.

Answer

SELECT userid,
       SUM(score) AS total_score
FROM daily_user_score
GROUP BY userid
ORDER BY userid ASC;

This query returns:

userid    | total_score
-----------------------
234342423 | 112
567584329 | 618
983489272 | 861

Notice that both columns are in ascending order, that is just a coincidence. The engine focused on only sorting the results in the userid column in ascending order.

Thanks for reading! If you have questions feel free to comment & I will try to get back to you.

Connect with me on Instagram @lauren__glass

Connect with me on LinkedIn

Check out my essentials list on Amazon

Search for me using my nametag on Instagram!


Published by HackerNoon on 2018/11/21