-7

I am learning BigQuery in Coursera. I am trying to sort a table but I am confused about how to use ORDER BY.

My table has columns like name and age. I want to sort the age from highest to lowest. What is the correct SQL query to do this?

4
  • 2
    What exactly are you confused by? It's very straightforward and the documentation is clear Commented Mar 13 at 22:57
  • You should also show us what you tried and why it didn't work? Commented Mar 13 at 22:59
  • 1
    docs.cloud.google.com/bigquery/docs/reference/standard-sql/… Commented Mar 13 at 23:31
  • 3
    1) Just read documentation to answer your question, 2) Do not use a single column name, better separate columns for first name and last name. 3) Do not use a column age because you need to update it quite often or the age will be incorrect. Avoid this trouble and rather use a column date_of_birth. Commented Mar 14 at 4:07

1 Answer 1

1

Simply use it like this:

SELECT name, age
FROM your_table
ORDER BY age DESC;

The syntax is to first do ORDER BY, then mention the column. Finally, if you want descending order, use DESC. Ascending is default.

Sign up to request clarification or add additional context in comments.

2 Comments

You can also order by multiple columns or no columns, but expressions/conditions.
There is no benefit in just, partially, reproducing official documentation - you need to add additional clarification, link to the source, and mention that this is only one way to use it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.