GROUP BY
Привіт! Ласкаво просимо на курс Intermediate SQL! У першому розділі ми будемо працювати з базою даних метрополітену Монреаля, що містить три таблиці: station
, line
і relation
. Перша таблиця містить назви станцій метро. Друга таблиця містить лінії монреальського метрополітену. Остання таблиця містить час для кожної станції, який потрібно витратити, щоб дістатися до неї.
Hi there! Welcome to the Intermediate SQL course!
In the first section, we're diving into how we can group and aggregate data within our tables.
Picture this: you've got a table of employees
laid out like this:
station_id | station_name |
---|---|
1 | Angrignon |
2 | Monk |
3 | Verdun |
4 | Charlevoix |
... | ... |
21 | Longueuil |
Next, we have the line
table, which contains the lines of the Montreal metro. This table helps us understand the various metro lines in Montreal.
Lastly, we have the relation
table, which holds information about the time it takes to reach each station. This table allows us to analyze the connections and travel times between stations.
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:
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:
First, we have the station
table, which contains the names of metro stations. It provides information about individual stations
In this course, 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:
id | line_name | station_name | time_to_next_station |
---|---|---|---|
1 | Green | Angrignon | 10 |
2 | Green | Monk | 16 |
3 | Green | Verdun | 9 |
4 | Green | Charlevoix | 17 |
... | ... | ... | ... |
21 | Yellow | Longueuil | 10 |
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.
Now, let's practice grouping by completing a task.
Завдання
Ваше завдання полягає в тому, щоб знайти найдовший час до наступної станції на кожній лінії. Це дозволить нам визначити найдовший час у дорозі між станціями для кожної лінії метро. Для цього використайте функцію MAX()
та задайте їй псевдонім max_time
, групуючи дані за стовпцем line_name
.
Після завершення цього завдання натисніть кнопку , що знаходиться нижче коду, щоб перевірити ваше рішення.
Все було зрозуміло?
Зміст курсу
Розширений рівень SQL
Розширений рівень SQL
GROUP BY
Привіт! Ласкаво просимо на курс Intermediate SQL! У першому розділі ми будемо працювати з базою даних метрополітену Монреаля, що містить три таблиці: station
, line
і relation
. Перша таблиця містить назви станцій метро. Друга таблиця містить лінії монреальського метрополітену. Остання таблиця містить час для кожної станції, який потрібно витратити, щоб дістатися до неї.
Hi there! Welcome to the Intermediate SQL course!
In the first section, we're diving into how we can group and aggregate data within our tables.
Picture this: you've got a table of employees
laid out like this:
station_id | station_name |
---|---|
1 | Angrignon |
2 | Monk |
3 | Verdun |
4 | Charlevoix |
... | ... |
21 | Longueuil |
Next, we have the line
table, which contains the lines of the Montreal metro. This table helps us understand the various metro lines in Montreal.
Lastly, we have the relation
table, which holds information about the time it takes to reach each station. This table allows us to analyze the connections and travel times between stations.
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:
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:
First, we have the station
table, which contains the names of metro stations. It provides information about individual stations
In this course, 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:
id | line_name | station_name | time_to_next_station |
---|---|---|---|
1 | Green | Angrignon | 10 |
2 | Green | Monk | 16 |
3 | Green | Verdun | 9 |
4 | Green | Charlevoix | 17 |
... | ... | ... | ... |
21 | Yellow | Longueuil | 10 |
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.
Now, let's practice grouping by completing a task.
Завдання
Ваше завдання полягає в тому, щоб знайти найдовший час до наступної станції на кожній лінії. Це дозволить нам визначити найдовший час у дорозі між станціями для кожної лінії метро. Для цього використайте функцію MAX()
та задайте їй псевдонім max_time
, групуючи дані за стовпцем line_name
.
Після завершення цього завдання натисніть кнопку , що знаходиться нижче коду, щоб перевірити ваше рішення.
Все було зрозуміло?