SQL HAVING Clause
Having clause is used to filter data based on the group functions. This is similar to WHERE condition but is used with group
functions. Group functions cannot be used in WHERE Clause but can be used in HAVING clause.
For Example: If you want to select the department that has total salary paid for its employees more than 25000, the sql
query would be like;
SELECT departmet_id, SUM (salary)
FROM employees
GROUP BY departmets
HAVING SUM(salary) > 25000
When WHERE, GROUP BY and HAVING clauses are used together in a SELECT statement,
the WHERE clause is processed first, then the rows that are returned after the WHERE clause is executed are grouped
based on the GROUP BY clause. Finally, any conditions on the group functions in the HAVING clause are applied to the
grouped rows before the final output is displayed.
SQL ORDER BY
The ORDER BY clause is used in a SELECT statement to sort results either in ascending or descending order. Oracle sorts
query results in ascending order by default.
For Example: If you want to sort the employee table by salary of the employee, the sql query would be.
SELECT first_name, salary FROM employee ORDER BY salary;
The query first sorts the result according to name and then displays it.
You can also use more than one column in the ORDER BY clause.
he query first sorts the result according to name and then displays it.
You can also use more than one column in the ORDER BY clause.
0 comments:
Post a Comment