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

single

bookGROUP BY Clause

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

Let's understand what "grouping data" means using a simple example of an employees table:

Grouping Data

We have a task to find out the number of employees in each department. To do this, we will group the data by the department column and use aggregation with the COUNT(*) function.

Here's what the implementation will look like:

123
SELECT department, COUNT(*) AS number_of_employees FROM employees GROUP BY department
copy

So, as you can see, the syntax for grouping data looks like this:

SELECT column1, AGG_FUNC(column2)
FROM table
GROUP BY column1
Note
Note

AGG_FUNC means aggregate functions like MAX, MIN, COUNT, etc.

This syntax exists to find certain values using aggregate functions in specific columns.

Let's consider another example: we've been tasked with finding the department with the highest average salary.

To retrieve such data, we need to group the data by the department column and then use the AVG() function to calculate the average salary:

123
SELECT department, AVG(salary) as average_salary FROM employees GROUP BY department
copy

In this part of the section, we will work with the Montreal Metro system database, which contains the metro_travel_time table.

This table will contain information about the station line(line_name), its name(station_name), and the amount of time it takes for a train to travel from one station to the next one(time_to_next_station).

Here is what this table looks like and the data preview in it:

As you can see, this is not a complex table. Let's think about where we can use grouping here.

The most obvious option is grouping by the colors of metro lines. That means we can aggregate the data, grouping it by the color of the metro line.

Alias

In the assignments, you’ll often use a concept called an alias. An alias is essentially a "nickname" for a column you retrieve with a SELECT statement. It’s specified using the following syntax:

SELECT column AS alias

An alias only affects how the column appears in the response.

For example, instead of MAX(time), the column could be called max_time if you assign that alias. This makes the output more readable and clear.

タスク

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

Your task is to find the longest time until the next station on each line. This will allow us to determine the longest travel time between stations for each metro line. To do this, use the MAX() function and alias it as max_time, grouping the data by the line_name column.

Brief Instructions

  • Retrieve the line_name column and the maximum value of the time_to_next_station column from the metro_travel_time table.
  • Add the alias max_time for the maximum value.
  • Group the data by the line_name column.

解答

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

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

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

セクション 1.  1
single

single

AIに質問する

expand

AIに質問する

ChatGPT

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

some-alt