Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Order of Statements | Section
Intermediate SQL
セクション 1.  2
single

single

bookOrder of Statements

メニューを表示するにはスワイプしてください

In our statistical analysis, we need to count the number of stations on each metro line and sort them in ascending order based on the number of stations.

This means we should first determine the number of stations for each metro line and then arrange them from the line with the fewest stations to the one with the most.

This information will help the construction company decide which metro lines should be prioritized for adding more stations.

For this, it's crucial to understand the sequence of SQL clauses, especially where to place the GROUP BY clause.

So, the order looks like this:

  1. SELECT statement;
  2. FROM table;
  3. WHERE clause;
  4. GROUP BY clause;
  5. ORDER BY clause;
  6. LIMIT clause.

Let's consider an example of such statement order using our employee table. Suppose we need to retrieve the number of employees in each department whose salary is above 70000 and sort them from smallest to largest:

12345
SELECT department, COUNT(employee_id) AS number_of_employees FROM employees WHERE salary > 70000 GROUP BY department ORDER BY number_of_employees
copy
タスク

スワイプしてコーディングを開始

Using the metro_travel_time table, find the number of stations (create a new column, named number_of_stations using station_name and COUNT() function) for each of the lines (line_name). Next, sort the result from smallest to largest.

Brief Instructions

  • Retrieve line_name and the count of rows in the station_name column.
  • Add the alias number_of_stations to the second column.
  • Group the data by line_name.
  • Sort the result by number_of_stations.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  2
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt