sql tutorial

SQL HAVING


  SQL > SQL Commands > Having

Another thing people may want to do is to limit the output based on the corresponding sum (or any other aggregate functions). For example, we might want to see only the stores with sales over $1,500. Instead of using the WHERE clause in the SQL statement, though, we need to use the HAVING clause, which is reserved for aggregate functions. The HAVING clause is typically placed near the end of the SQL statement, and a SQL statement with the HAVING clause may or may not include the GROUP BY clause. The syntax for HAVING is,

SELECT "column_name1", SUM("column_name2")
FROM "table_name"
GROUP BY "column_name1"
HAVING (arithmetic function condition)

Note: the GROUP BY clause is optional.

In our example, table Store_Information,

Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999

we would type,

SELECT store_name, SUM(sales)
FROM Store_Information
GROUP BY store_name
HAVING SUM(sales) > 1500

Result:

store_name   SUM(Sales)
Los Angeles   $1800

SQL Alias >>

Link to this page: If you find this page useful, we encourage you to link to this page. Simply copy and paste the code below to your website, blog, or profile.



Copyright 1999-2009 1keydata.com.   All Rights Reserved.     Privacy Policy  








SQL SELECT
SQL DISTINCT
SQL WHERE
SQL AND OR
SQL IN
SQL BETWEEN
SQL Wildcard
SQL LIKE
SQL ORDER BY
SQL Functions
SQL Average
SQL COUNT
SQL MAX
SQL MIN
SQL SUM
SQL GROUP BY
SQL HAVING
SQL ALIAS
SQL AS
SQL JOIN
SQL OUTER JOIN
SQL CONCATENATE
SQL SUBSTRING
SQL TRIM
SQL LENGTH
SQL REPLACE

SQL CREATE TABLE
SQL CONSTRAINT
SQL NOT NULL
SQL DEFAULT
SQL UNIQUE
SQL CHECK
SQL PRIMARY KEY
SQL FOREIGN KEY
SQL View
SQL CREATE VIEW
SQL Index
SQL CREATE INDEX
SQL ALTER TABLE
SQL DROP TABLE
SQL TRUNCATE TABLE
SQL USE
SQL INSERT INTO
SQL UPDATE
SQL DELETE FROM

SQL Jobs

Site Map
Resources



CSS Tutorial

PHP Tutorial

Data Warehousing & Business Intelligence