Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do SQL order-by with multiple-columns work?

Tags:

sql

I'm studying this SQL statement :

SELECT * FROM Customers
ORDER BY Country,CustomerName;

From this

How does it work that it orders by both country and CustomerName ? In my mind it just doesn't seem intuitive, i.e you order by either Country OR CustomerName but not both

like image 218
Caffeinated Avatar asked Mar 02 '14 20:03

Caffeinated


People also ask

Can we use ORDER BY for multiple columns?

You can also ORDER BY two or more columns, which creates a nested sort . The default is still ascending, and the column that is listed first in the ORDER BY clause takes precedence. The following query and Figure 3 and the corresponding query results show nested sorts.

How does GROUP BY work with multiple columns in SQL?

The GROUP BY clause is used along with some aggregate functions to group columns with the same values in different rows. The group by multiple columns technique retrieves grouped column values from one or more database tables by considering more than one column as grouping criteria.

Can we sort multiple columns by ORDER BY clause in a single query?

The ORDER BY clause applies to the final result set from the FULLSELECT statement. You can sort on either a single column or multiple columns. To sort on columns in a FULLSELECT statement that have the same ordinal value but are in different subordinate SELECT statements, the columns must have the same name.

Can I GROUP BY 3 columns in SQL?

Yes, it is possible to use MySQL GROUP BY clause with multiple columns just as we can use MySQL DISTINCT clause.


1 Answers

It orders by Country, but if some rows have the same Country, it orders them by CustomerName.

like image 196
Blorgbeard Avatar answered Oct 13 '22 05:10

Blorgbeard