Oracle 23c - SQL

GROUP BY and HAVING Clauses with Column Aliases and Positions (23c)

Oracle Database 23c introduces enhancements to GROUP BY and HAVING clauses, empowering developers to write more concise, readable, and maintainable SQL queries. These improvements include:

  • Column Aliases in GROUP BY and HAVING: The ability to reference column aliases directly within GROUP BY and HAVING clauses, eliminating the need for repetitive expressions and improving code clarity.
  • Column Positions in GROUP BY: The option to use column positions instead of explicit column names in the GROUP BY clause, making queries more adaptable to changes in column order.

Pre-23c Restrictions:

  • Repetition in GROUP BY: Previously, column expressions had to be repeated in both the SELECT and GROUP BY clauses, leading to potential maintenance challenges and reduced readability.
  • No Alias Support in HAVING: Column aliases defined in the SELECT clause could not be used directly in the HAVING clause, requiring repeated expressions.

Oracle 23c Empowers Simplicity:

Column Aliases in Action:

-- Pre-23c:
SELECT department_id, INITCAP(department_name) AS department, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id, INITCAP(department_name);

-- Oracle 23c: Using the alias "department"
SELECT department_id, INITCAP(department_name) AS department, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id, department;

Column Positions in GROUP BY:

SELECT INITCAP(department_name) AS department, COUNT(*) AS employee_count
FROM employees
GROUP BY 2;  -- Using the position of "department" in the SELECT list

HAVING with Aliases:

SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id
HAVING employee_count > 10;

Conclusion

The advancements in GROUP BY and HAVING clauses within Oracle 23c offer benefits for query development and maintenance. Developers can create more readable, manageable, and portable SQL code, ultimately leading to more efficient and robust database applications.

Scroll to Top