Comparison of HAVING Clause to WHERE Clause in SQL

In this article I am going to explain what is difference between HAVING clause to WHERE clause in SQL.
  • 1577

Introduction

In this article I am going to explain  what is difference between HAVING clause to WHERE clause in SQL. GROUP BY clause is used to groups the rows of result set based on one or more columns or expression. GROUP BY clause is used in SELECT statements that include aggregate functions.

To put any condition with group by clause  we use HAVING clause. HAVING clause is used to specify a search condition for a group or an aggregate.

WHERE clause is used before the rows are groped in SELECT statement that uses grouping and aggregate.

Having clause is used after the rows are groped in SELECT statement that uses grouping and aggregate.

Example

select vendorname,sum(invoicetotal) as invo_total

from mcninvoices join mcnvendors

on mcninvoices.vendorid = mcnvendors.vendorid

group by vendorname

having sum(invoicetotal)>500

order by vendorname desc

Output

Clipboard02.jpg

Example

select vendorname,sum(invoicetotal) as invo_total

from mcninvoices join mcnvendors

on mcninvoices.vendorid = mcnvendors.vendorid

where invoicetotal >500

group by vendorname

order by vendorname desc

Output

Clipboard04.jpg


© 2018 DotNetHeaven. All rights reserved.