GROUP BY Statement

The GROUP BY statement is used to group the records fetched in a search query results by one or more columns. GROUP BY enables identical data to be grouped together and displayed sequentially. When there are duplicate values in a column, the GROUP BY statement displays the duplicates together. The records are then ordered based on other columns.

The GROUP BY statement is associated with the SELECT statement and is often used with ZCQL functions . It is used towards the end of the query and should therefore satisfy the JOIN statements and follow the WHERE conditions.

The basic syntax for using a GROUP BY statement along with the SELECT statement is as follows:

    
copy
SELECT column_name(s) FROM parent_table_name GROUP BY column_name(s)

Example:

To view a list of theaters and their locations from the Theaters table, grouped by the location of the theaters, execute the following query:

    
copy
SELECT TheaterName, Location FROM Theaters GROUP BY Location

This will generate the following output:

TheaterName Location
Cosmos Theater Albany
FunTime Cinemas Buffalo
The Express Cinemas New York City
ANC Cinemas Rochester

ORDER BY Statement

The ORDER BY statement is used to sort the records fetched in a search query results in an ascending or a descending order, based on one or more columns. When the ORDER BY statement is used to sort textual data, it sorts the records in the alphabetical order.

Similar to the GROUP BY statement, the ORDER BY statement is associated with the SELECT statement and is often used after the GROUP BY statement. It is used towards the end of the query, after the JOIN clause statements or the WHERE conditions, if present, but before the LIMIT clause. The ORDER BY statement should then satisfy the JOIN statements and follow the WHERE conditions.

Note: If the ORDER BY statement follows a GROUP BY statement, the ORDER BY statement takes precedence and the results are ordered first based on the ORDER BY statement, and then grouped together based on the GROUP BY statement.

If there are duplicate values in a column, the ORDER BY statement will display the duplicates together. The records are then ordered based on other columns.

The basic syntax for using a GROUP BY statement with the SELECT statement is as follows:

    
copy
SELECT column_name(s) FROM parent_table_name ORDER BY column_name(s) [ASC | DESC]
Note: By default, the ORDER BY statement sorts the records in ascending order without using the keyword 'ASC'. To sort the records in a descending order, you will have to use the keyword 'DESC'.

Example:

To view a list of the ticket prices from the Pricing table in descending order for each movie from the Movies table execute the following query:

    
copy
SELECT Movies.MovieName, Pricing.Price FROM Movies INNER JOIN Pricing ON Movies.MovieID = Pricing.MovieID ORDER BY Pricing.Price DESC

This will generate the following output:

MovieName Price
Hotel Transylvania 3: Summer Vacation 11.50
The First Purge 9.20
Ant-Man and the Wasp 8.64
Skyscraper 7.44