ORDER BY criterion
Part of the Fundamentals section of Coddy's SQL journey — lesson 58 of 72.
One of the OVER () options are the ORDER BY
For example:
ROW_NUMBER() OVER (ORDER BY year DESC) as row_numThis will generate a column that will number the rows following the descending year order.
Challenge
EasyAvailable tables and columns:
<strong>plants</strong>:<strong>id</strong>,<strong>hue</strong>,<strong>rough</strong>
Select all plants with a hue greater than 0.34 and add two numbering columns:
row_num_hue that will number the rows according to hue in ascending order
row_num_score that will number the rows by the following formula: rough*hue in descending order
Return only the id and the two numbering columns. Order the result by the plant id in ascending order
Cheat sheet
Use ROW_NUMBER() OVER (ORDER BY column) to generate row numbers based on a specific ordering:
ROW_NUMBER() OVER (ORDER BY year DESC) as row_numThis creates a numbered column following the descending year order. You can use ASC for ascending or DESC for descending order, and can also order by calculated expressions.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4More Keywords
The IN keywordThe BETWEEN keywordThe LIKE keywordThe AS keywordRecap - Cellphone Models2Conditions
Conditions BasicsThe AND keywordThe OR keywordThe NOT keywordMultiple Conditions CombinedParenthesisBooleans5Arithmetic Operations
Mathematical OperatorsMathematical ColumnsThe Modulo OperationThe ROUND() Function8Statistics
Built-In Aggregate Part 1Built-In Aggregate Part 2Grouping Part 1Grouping Part 2Subqueries Part 1Subqueries Part 2Recap - Total Gain ShopRecap - Scooter ShopRecap - Coffee Shop11Window Functions part 1
ROW_NUMBER functionORDER BY criterionPARTITION BY criterionPARTITION & ORDERLEAD & LAG FunctionsRecap - LEAD & LAGRecap - PicturesRecap - Boxes3Specific Return Format
Null valuesSort Results Part 1Sort Results Part 2Recap - Cyber Security FirmLimit number of recordsRecap - Vehicle Factory6Intro Challenges
Recap - Parliamentary ElectionRecap - Police Criminal ArrestRecap - Bar Beverage ContainerRecap - Engineer new columns