Having Clause

Description

The HAVING clause specifies an intermediate result table that consists of those groups of R for which the search-condition is true.

Click here to navigate to the IBM DB2 docs page for this syntax.

Grammar Syntax

Sample Source Patterns

Note that the following code is valid in both Db2 and Snowflake.

In order to show an example of the Having Clause, we are going to need a table

CREATE TABLE TestTableHaving
(
    COL1 CHAR(20), 
    COL2 INT
);

Now, let's insert some values into the table.

INSERT INTO TestTableHaving VALUES('FistValue', 1),('SecondValue', 2),('ThirdValue', 2),('FourthValue', 3),('FifthValue', 4);

Suppose we wish to get those values where COL2 equals to 2. We can do it with the having clause

SELECT * FROM TestTableHaving GROUP BY COL1, COL2 HAVING COL2 = 2;

Let's compare the result that this query would return in Db2 and Snowflake.

IBM DB2Snowflake

As you can see, both return the same results.

Last updated