HAVING语句用于在分组之后再进行筛选

    SELECT
    client_id,
    SUM(invoice_total) AS total_sales,
    COUNT() AS number_of_invoices,
    FROM invoices
    GROUP BY client_id
    *HAVING
    total_sales > 500

    复合条件查询
    SELECT
    client_id,
    SUM(invoice_total) AS total_sales,
    COUNT() AS number_of_invoices,
    FROM invoices
    GROUP BY client_id
    HAVING total_sales > 500 *AND
    number_of_invoices > 5

    获取坐落于VG地区的客户,并且花费超过100美元
    SELECT
    c.state,
    c.customer_id,
    c.first_name,
    c.second_name,
    SUM(oi.quantity * oi.unit_price) AS total_sales
    FROM customers c
    JOIN orders o USING (customer_id)
    JOIN order_items oi USING(order_id)
    WHERE state = ‘VA’
    GROUP BY
    c.customer_id,
    c.first_name,
    c.second_name,
    HAVING total_sales > 100