At first, it is worth mentioning that Oracle is presented here as a collecting SQL language. Aggregate functions and manner of their application fit the entire SQL family and are applicable to all queries considering the syntax of each language.

We’ll try to briefly and quickly explain all the details in two parts. The article will most likely be useful for beginners.

Part 1

Order by, Group by, Having

ORDER BY

The ORDER BY clause allows sorting the output values, i.e., sorting the retrieved value by a specific column. Sorting can also be applied by a column alias that is defined with a clause.

The advantage of ORDER BY is that it can be applied to both numeric and string columns. String columns are usually sorted alphabetically.

By default, the ascending (ASC) sorting is applied. For the sorting to be descending, an additional DESC clause is used.

Syntax:

SELECT column1, column2,... (indicates the name
FROM table_name
ORDER BY column1, column2... ASC | DESC;

Let’s consider the examples:

Image for post

In the first table, we get all the data and sort it in ascending order by the ID column.

In the second, we also get all the data sorted by the ID column in descending order using DESC.

The third table uses several fields for sorting. First, we sort by the department. If the first statement is equal for the fields with the same department, the second sorting condition is applied; in our case, it’s salary.

It’s pretty simple. We can specify more than one sorting condition, which allows us to sort the output lists more efficiently.

#oracle #aggregation #sql #window-functions #programming

Oracle: Order by/Group by/Having clauses and Window Functions
1.35 GEEK