Sort Results Part 1
CoddyのSQLジャーニー「基礎」セクションの一部 — レッスン 13/72。
データベースをクエリする際、結果を意味のある順序で整理すると、データ分析がはるかに効率的になります。結果をソートするには、ORDER BY キーワードを使用し、その後にどのフィールドで並べ替えるかを指定します。デフォルトでは、昇順でソートされます。
たとえば、以下の<strong>competition</strong>テーブルを考えてみましょう:
| runner_id | age | avg_speed |
|---|---|---|
| 1 | 47 | 3.65 |
| 2 | 62 | 3.07 |
| 3 | 57 | 6.82 |
| 4 | 56 | 4.34 |
| 5 | 25 | 4.93 |
| 6 | 40 | 3.94 |
| 7 | 23 | 6.58 |
| 8 | 40 | 3.43 |
SELECT *
FROM competition
WHERE age > 50
ORDER BY avg_speedこれは結果です
| runner_id | age | avg_speed |
|---|---|---|
| 2 | 62 | 3.07 |
| 4 | 56 | 4.34 |
| 3 | 57 | 6.82 |
そのデータをソートする方法を指定するには、列の名前の後に DESC または ASC キーワードを追加できます。
ORDER BY avg_speed ASCチャレンジ
簡単利用可能なテーブルと列:
<strong>feathers</strong>:<strong>id</strong>,<strong>weight</strong>
重量の降順で並べ替えたすべての id を返してください
チートシート
ORDER BYを使用してクエリ結果をソートします。デフォルトでは昇順でソートされます:
SELECT *
FROM table_name
ORDER BY column_nameソート方向を指定するには、ASC(昇順)またはDESC(降順)を追加します:
ORDER BY column_name DESC
ORDER BY column_name ASC自分で試してみよう
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
基礎のすべてのレッスン
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() Function3Specific 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