Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Order of Statements | Grouping
Intermediate SQL

Order of StatementsOrder of Statements

For statistical analysis, we were tasked with counting the number of stations on each line and arranging them in increasing order of station count for each metro line.

To do this, we need to find the number of stations on each of the metro lines and then sort them from the least number of stations to the most.

This way, the construction company will understand which metro lines they need to prioritize for adding stations.

It's important for us to understand the order of writing clauses, specifically where the GROUP BY clause should be placed.

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.

From this order, it's clear that the GROUP BY statement must be written AFTER the WHERE statement (or after the FROM table if there is no filtering in your query using SELECT) and also BEFORE the ORDER BY statement.

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:

Note:

It's worth noting that the LIMIT clause is always written last. This way, you can easily remember its placement in the query.

Now, let's move on to the task!

Here is the preview of a metro_travel_time table we are working with:

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

Завдання

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, you need to sort the result from smallest to largest.

Note

COUNT(column) is the function that counts the number of rows.

Все було зрозуміло?

Секція 1. Розділ 2
toggle bottom row
course content

Зміст курсу

Intermediate SQL

Order of StatementsOrder of Statements

For statistical analysis, we were tasked with counting the number of stations on each line and arranging them in increasing order of station count for each metro line.

To do this, we need to find the number of stations on each of the metro lines and then sort them from the least number of stations to the most.

This way, the construction company will understand which metro lines they need to prioritize for adding stations.

It's important for us to understand the order of writing clauses, specifically where the GROUP BY clause should be placed.

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.

From this order, it's clear that the GROUP BY statement must be written AFTER the WHERE statement (or after the FROM table if there is no filtering in your query using SELECT) and also BEFORE the ORDER BY statement.

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:

Note:

It's worth noting that the LIMIT clause is always written last. This way, you can easily remember its placement in the query.

Now, let's move on to the task!

Here is the preview of a metro_travel_time table we are working with:

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

Завдання

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, you need to sort the result from smallest to largest.

Note

COUNT(column) is the function that counts the number of rows.

Все було зрозуміло?

Секція 1. Розділ 2
toggle bottom row
some-alt