Only first column in ORDER BY clause is sorted

You are ordering rows of data, not each column separately.

The row (10, 1) comes before the row (1, 10) because of your ORDER BY clause.

The Value2 only comes into play when there is a tie on the first column.

As further explained by Hellion, as far as the database is concerned, the pair (10, 1) is an indivisible unit: it's not two values, it's one set (that happens to contain two values). You can't just break the set into pieces and swap the pieces into different sets willy-nilly. As I said (and as Darko shows in another answer), the ORDER BY clause sorts by the first specified column (Value1), and then if there is more than one row with the same number in Value1, it sorts that sub-set of rows by Value2.

As another illustration, you can also consider the example suggested by A C:

See also the way the dictionary sorts words: AA, AB, AC, BA, BB, BC, CA, CB, CC... Now replace the first letter with your first column value (even if it's more than one digit) and the second letter with the second column value and there you go - same principle. (Yes, I sorted ASCending for clarity -- DESCending sorted dictionaries are hard to find.)


For example, if you have multiple rows with value1 equals to 10, then the second part of ORDER clause will take place as you expected.

Consider the following snippet, where three rows exist that have the value1 equal to 10.

DECLARE @test AS TABLE
( 
                       value1 int, value2 int
);

INSERT INTO @test
VALUES( 10, 1 ), ( 9, 2 ), ( 8, 3 ), ( 7, 4 ), ( 6, 5 ), ( 5, 6 ), ( 4, 7 ), ( 3, 8 ), ( 2, 9 ), ( 1, 10 ), ( 10, 2 ), ( 10, 3 );

SELECT *
FROM @test
ORDER BY value1 DESC, value2 DESC;

The output is shown in the image below.

enter image description here

More info on SELECT - ORDER BY Clause (Transact-SQL)


Let's use a more illustrative example.

Create a table with some dummy data:

CREATE TABLE [PurchaseHistory](
    [CustomerName] VARCHAR(50),
    [Item] VARCHAR(50),
    [Quantity] INTEGER
);

INSERT INTO [PurchaseHistory]
([CustomerName],[Item],[Quantity])
VALUES
('ZZ Top','Shampoo (Beard Wash) 16oz',3),
('ABC Stores','Fruit Punch 8oz',30),
('Nicolas Cage','Beekeeper suit',1);

Dummy data

If you selected this with Item/Quantity DESC, what do you expect to come out?

SELECT [CustomerName],[Item],[Quantity]
FROM [PurchaseHistory]
ORDER BY
    [CustomerName] ASC,
    [Item] DESC,
    [Quantity] DESC
;

With the example in your question, it seems like you expect it to come out like this: Clean beards

Suddenly your records now indicate that ABC stores bought 30 bottles of Beard Wash, Nicolas Cage was kind of thirsty one day, and ZZ Top is sharing a single bee suit between them.

Instead, when using ORDER BY, the values in a row are kept together, resulting in the correct data set where ABC stores is still buying 30 bottles of fruit punch, Nick Cage gets his beekeeper suit, and ZZ Top continue to have clean facial hair.

Boring