-4

I have this table,

CREATE TABLE Person 
    (
     id int auto_increment primary key, 
     name varchar(20), 
     Age varchar(30)
    );

INSERT INTO Person
(name, Age)
VALUES
('Ganesan', '23'),
('Prasanna', '30'),
('Karthikeyan', '27'),
('RParthiban', '33');

And this query:

select Name, Age from Person HAVING Age > 30;

select Name, Age from Person WHERE Age > 30;

Both queries produces the same output:

NAME    AGE
RParthiban  33

Why ? Whats the difference or significance between using Having and Where ?

I am sure that there must be any. Any examples for each ?

Thanks.

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105

3 Answers3

2

A HAVING clause is equivalent to a WHERE clause for a group or aggregate.

http://technet.microsoft.com/en-us/library/ms180199.aspx

The syntax is SELECT ... FROM ... GROUP BY ... HAVING ... ORDER BY

e.g.

SELECT ID, SUM(whatever) AS [Total]
FROM table
GROUP BY ID
HAVING SUM(whatever) > 100000.00
ORDER BY ID
Tom Chantler
  • 14,753
  • 4
  • 48
  • 53
1

If you search about this question, you will get the answer. Here is one of them

HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause.

Reference

Sachin
  • 40,216
  • 7
  • 90
  • 102
1

WHERE is for filtering query results based on condition.

HAVING is for applying a filter on results of an aggregate function. In absence of aggregate function it functions same as WHERE.

naveen goyal
  • 4,571
  • 2
  • 16
  • 26