SQL ClausesLast Updated : 10 Feb 2026 The following are the various SQL clauses: ![]() 1. GROUP BY
Syntax Sample table: PRODUCT_MAST
Example: Explanation: On execution of the above query the result would be as shown below. Here, the company wise group the rows and calculated the number of items under each company will be counted and displayed. Output: Com1 5 Com2 3 Com3 2 2. HAVINGIf you want to add restriction on the number of groups using the WHERE clause it generates an error message, 'ORA00934: because the group function is not allowed here'. For this purpose we use the concept of Having clause.
Syntax: Explanation: It would further restrict the rows on execution as shown below. In this manner, we can filter the group of rows returned from your group using the HAVING clause. The HAVING clause always contains aggregate SQL functions and it can be used by the group by clause only. Example: Output: Com1 5 Com2 3 3. ORDER BY
Syntax: Where ASC: It is used to sort the result set in ascending order by expression. DESC: It sorts the result set in descending order by expression. Example: Sorting Results in Ascending OrderTable: CUSTOMER
Enter the following SQL statement: Explanation: The output is shown below after execution. Here the column NAME by default is sorted in ascending order. Output:
Example: Sorting Results in Descending OrderUsing the above CUSTOMER table Explanation: On execution the output is shown below. Here the column NAME is sorted in the descending order. Output:
Example: Sorting Results both in Ascending and Descending OrderUsing the above CUSTOMER table Explanation: On execution the output is shown below. Here the column NAME is sorted in the descending order. Output:
Example: Sorting Results both in Ascending and Descending Order Using the above CUSTOMER table SELECT * FROM CUSTOMER ORDER BY NAME ASC, ADDRESS DESC; Explanation: The output is listed below on execution. Here the column NAME is sorted in ascending order first and then column. ADDRESS sorted in descending order. Output:
Example: Sorting Results both in the Ascending and Descending Order by specify columns position instead of column name You also have the option to specify the column positions in place of column names in the order by clause. Using the above CUSTOMER table SELECT * FROM CUSTOMER ORDER BY 2 ASC, 3 DESC; Explanation: On execution the output is shown below. Here the column 2 i.e. NAME is sorted in ascending order first and then column 3 i.e. ADDRESS sorted in descending order. Output:
The following points should be remembered while using ORDER BY clause:
Next TopicDBMS SQL Aggregate function |
We request you to subscribe our newsletter for upcoming updates.