SQL Tutorial 4 : “GROUP BY” & “ORDER BY” Functions

“GROUP BY” Function

The “GROUP BY” statement is used with the aggregate functions such as SUM and COUNT to group the result-set by one or more columns.

Again, let’s use the same ”world_data” dataset to illustrate this.

Consider that we want to find the total gdp of each continent from this dataset.
We will write the following query:

SELECT continent, SUM(gdp)
FROM world_data
GROUP BY continent

The output will be:

“ORDER BY” Function

The ORDER BY Function is used to sort our output in ascending or descending order.
For instance, let’s look at the case where we need to query the gdp of the countries, ranked in descending order.

We will use the following query:

SELECT name, gdp
FROM world_data
ORDER BY gdp DESC

*Note the use of DESC here. Ascending order is assumed in Hive SQL unless the DESC keyword is used.

The output will be :

Next : SQL Tutorial 5 : “HAVING” Function

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s