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:
copySELECT 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:
copySELECT 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 |
BINARYOF() Function
The BINARYOF() function can only be used in a GROUP BY statement. The function can be used in places where you wish to produce a output that is not case sensitive and considers all the values in a coulumn of a table.
The BINARYOF() function can only be used on columns with VarChar or Text datatype.
For example, consider the following table Zylker_EMP:
Names |
---|
AMELIA BURROWS |
amelia burrows |
amelia Burrows |
Amelia Burrows |
An example ZCQL statement to illustrate the functionality of BINARYOF() and its possible output is given below:
copySELECT Names FROM Zylker_EMP GROUP BY BINARYOF(Names)
Zylker_EMP.Names |
---|
AMELIA BURROWS |
amelia burrows |
amelia Burrows |
Amelia Burrows |
-
This functionality is only available in ZCQL V2.
-
From December 01st, 2024, all your current projects in all your Orgs present in the Development Environment will be automatically mapped to ZCQL V2 Parser.
-
From April 01st, 2025, all the projects present in all Orgs that have already been mapped to ZCQL V2 Parser in Development Environment will be automatically mapped to ZCQL V2 Parser in the Production Environment, if and when production is enabled for the project.
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.
-
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.
-
ZCQL Functions can also be used with ORDER BY statements.
The basic syntax for using a ORDER BY statement with the SELECT statement is as follows:
copySELECT column_name(s) FROM parent_table_name ORDER BY column_name(s) [ASC | DESC]
-
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’.
-
In all columns where you use the ORDER BY clause, the ASC and DESC functionality can now be applied. You can also apply ASC and DESC for individual columns.
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:
copySELECT 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 |
Last Updated 2024-09-03 16:31:06 +0530 +0530
Yes
No
Send your feedback to us