kasivitonen
kasivitonen

Reputation: 5

SQL sorting duplicates different than no duplicate values

My task is (I am studying):

Write a query that lists AuthorID and title for each book (in that order), but so that the books are sorted by their AuthorID values in normal, increasing order. Books that share the same AuthorID value should be ordered by their title alphabetically.

My solution:

select authorid, title from book
 order by authorid asc, title asc;

Is it really working always, like meant?

Result was correct but I just doubt it:

authorid | title
---------+-----------------------------------------
201      | Let's Play Poker and Chess!
202      | The Crime that never was
202      | The Ghost of the Moor
202      | Three Bearded Men and the Sea
204      | My life as I see it
204      | The Hound and other short stories
204      | The Winter Everlasting
204      | Upside-down and other children's stories
205      | How Computers Work
206      | There and Never Back Again
207      | Learn to Knit

Upvotes: 0

Views: 313

Answers (1)

Atif
Atif

Reputation: 2210

Yes this is fine, but asc is default in order by so even if you dont explicilty write it system will assume it to be asc.

So even below will give you same result.

select authorid, title from book
 order by authorid , title ;

Upvotes: 1

Related Questions