Select other columns when using functions in SQL

Thanks to INF 551, I finally got a chance to practice the SQL skillset that I learned on my own. In this class, the instructor is recommending us students to use MySQL, which is, not bad but it is not as familiar to me as SQLite.

SQL is straightforward in most ways, but I found a tricky problem when I want to select another column while using functions such as sum(), count() in SELECT clauses. In excel or R, I can easily specify which column to show up when specifying some conditions. However, it seems that SQL does not provide such user-friendly methods to do that.

For example, when I want to select the column next to the maximum of the sum of “sales_amount” column, I ended up inserting two “SELECT” clauses into the chunk, and used a where clause to clarify one last condition. Mind that two clauses in brackets are kind of the same except for the letter alias.

I have done some search on the Internet but I could not find a more elegant way to solve this issue. Another method is to create a new table or view in the software to serve as a substitute for clauses in brackets.

Anyone know better solutions?

One Comment

  1. CyrusBNolton says:

    Appreciate this post. Allow me to try it out.

Leave a Reply

Your email address will not be published. Required fields are marked *