Home  >  Article  >  Database  >  The difference between where and having in oracle

The difference between where and having in oracle

下次还敢
下次还敢Original
2024-05-02 23:12:18995browse

The difference between the WHERE and HAVING clauses is the scope: WHERE filters the basic rows, and HAVING filters the grouped result set. WHERE is used for single row conditions and HAVING is used for group result conditions. WHERE comes after FROM and before SELECT, HAVING comes after GROUP BY. WHERE can be used alone, HAVING must be used with a group operation.

The difference between where and having in oracle

The difference between WHERE and HAVING clauses in Oracle

WHERE and HAVING are two SQL clauses, use to filter the data set. The main difference between them is their scope:

1. Scope

  • WHERE clause is used to filter the basis Table or view row. It is executed before the grouping operation, so it applies the condition to a single row.
  • HAVING clause is used to filter the grouped result set. It is executed after the grouping operation, so it applies conditions to the group results.

2. Usage scenarios

  • The WHERE clause is used to filter rows that do not meet specific conditions. For example, to find all items with a price over $100:
<code class="sql">SELECT * FROM products WHERE price > 100;</code>
  • HAVING clause is used to filter groups that meet specific criteria. For example, to find a group of items with an average price over $100:
<code class="sql">SELECT category, AVG(price) AS avg_price
FROM products
GROUP BY category
HAVING avg_price > 100;</code>

3. Location

  • WHERE clause is located After the FROM clause and before the SELECT clause.
  • HAVING clause is located after the GROUP BY clause.

4. Example

<code class="sql">-- 使用 WHERE 子句过滤行
SELECT * FROM orders WHERE customer_id = 1;

-- 使用 HAVING 子句过滤组
SELECT product_category, SUM(quantity) AS total_quantity
FROM order_details
GROUP BY product_category
HAVING total_quantity > 100;</code>

Note:

  • HAVING clause can only operate with grouping used together, while the WHERE clause can be used in any SQL statement.
  • The condition used in the HAVING clause must refer to an aggregate function or grouping column.

The above is the detailed content of The difference between where and having in oracle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn