SQL Order By
To sort your result records, use ORDER BY construction. There is a syntax of query:
12345-- do not run this query SELECT col1, col2, ... FROM table WHERE condition ORDER BY some_col1, some_col2, ...
ORDER BY is at the end of the query, and sorts by one or multiple columns in their order. For example, if there is a construction ORDER BY singer, title, the result will be sorted by singer's name, but for multiple songs of the same singer, records will be sorted by title.
By default, sorting is in ascending order, but you can define it. Use ASC and DESC keywords after each column name:
123SELECT id, title, year FROM songs ORDER BY year DESC
ASC or DESC statement relates to the column name before it. It means, that in ORDER BY title ASC, price DESC first sorts by title in ascending order, and then by price in descending order.
Swipe to start coding
Create a rating of songs from the most to the least expensive. Display title, singer, and price of these songs.
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Sammanfatta detta kapitel
Explain code
Explain why doesn't solve task
Awesome!
Completion rate improved to 4.17
SQL Order By
Svep för att visa menyn
To sort your result records, use ORDER BY construction. There is a syntax of query:
12345-- do not run this query SELECT col1, col2, ... FROM table WHERE condition ORDER BY some_col1, some_col2, ...
ORDER BY is at the end of the query, and sorts by one or multiple columns in their order. For example, if there is a construction ORDER BY singer, title, the result will be sorted by singer's name, but for multiple songs of the same singer, records will be sorted by title.
By default, sorting is in ascending order, but you can define it. Use ASC and DESC keywords after each column name:
123SELECT id, title, year FROM songs ORDER BY year DESC
ASC or DESC statement relates to the column name before it. It means, that in ORDER BY title ASC, price DESC first sorts by title in ascending order, and then by price in descending order.
Swipe to start coding
Create a rating of songs from the most to the least expensive. Display title, singer, and price of these songs.
Lösning
Tack för dina kommentarer!
single