0% found this document useful (0 votes)
10 views4 pages

98 Difference Between Where and Having in SQL Server

The document is a video tutorial discussing the differences between the WHERE and HAVING clauses in SQL Server. It explains that WHERE cannot be used with aggregate functions, while HAVING can, and highlights their respective positions in a SQL query. Additionally, it notes that using HAVING is generally slower than using WHERE and provides examples to illustrate these concepts.

Uploaded by

realayoola007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

98 Difference Between Where and Having in SQL Server

The document is a video tutorial discussing the differences between the WHERE and HAVING clauses in SQL Server. It explains that WHERE cannot be used with aggregate functions, while HAVING can, and highlights their respective positions in a SQL query. Additionally, it notes that using HAVING is generally slower than using WHERE and provides examples to illustrate these concepts.

Uploaded by

realayoola007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

3/12/2023 Sql server, .

net and c# video tutorial: Difference between where and having in sql server
The Wayback Machine - https://web.archive.org/web/20210805080838/https://csharp-video-tutorials.blogspot.com/2015/09/difference-between…

Sql server, .net and c# video tutorial


Free C#, .Net and Sql server video tutorial for beginners and intermediate programmers.

Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Difference between where and having in sql server

Suggested Videos
Part 95 - Audit table changes in sql server
Part 96 - Logon triggers in sql server
Part 97 - Select into in sql server

Pragim Technologies - Best software


training and placements in marathahalli,
bangalore. For further details please call
09945699393.

Complete Tutorials
How to become a full stack web
developer

Cloud computing complete tutorial

In this video we will discuss the difference between where and having clauses in SQL Healthy food for healthy mind and
Server. body

JavaScript tutorial

Bootstrap tutorial

Angular tutorial for beginners

Angular 5 Tutorial for beginners

https://web.archive.org/web/20210805080838/https://csharp-video-tutorials.blogspot.com/2015/09/difference-between-where-and-having-in.html 1/4
3/12/2023 Sql server, .net and c# video tutorial: Difference between where and having in sql server

Important Videos
The Gift of Education

Web application for your business

How to become .NET developer

Resources available to help you

Dot Net Video Tutorials


Blazor tutorial

C tutorial

ASP.NET Core Tutorial


Let us understand the difference with an example. For the examples in this video we will
use the following Sales table. ASP.NET Core Razor Pages Tutorial

Angular 6 Tutorial

Angular CRUD Tutorial

Angular CLI Tutorial

Angular 2 Tutorial

Design Patterns

SOLID Principles
SQL Script to create and populate Sales table with test data
Create table Sales ASP.NET Web API
(
Product nvarchar(50), Bootstrap
SaleAmount int
) AngularJS Tutorial
Go
jQuery Tutorial
Insert into Sales values ('iPhone', 500)
JavaScript with ASP.NET Tutorial
Insert into Sales values ('Laptop', 800)
Insert into Sales values ('iPhone', 1000) JavaScript Tutorial
Insert into Sales values ('Speakers', 400)
Insert into Sales values ('Laptop', 600) Charts Tutorial
Go
LINQ
To calculate total sales by product, we would write a GROUP BY query as shown below
LINQ to SQL
SELECT Product, SUM(SaleAmount) AS TotalSales
FROM Sales
LINQ to XML
GROUP BY Product
Entity Framework
The above query produces the following result
WCF

ASP.NET Web Services

Dot Net Basics

C#
Now if we want to find only those products where the total sales amount is greater
SQL Server
than $1000, we will use HAVING clause to filter products
SELECT Product, SUM(SaleAmount) AS TotalSales
ADO.NET
FROM Sales
GROUP BY Product ASP.NET
HAVING SUM(SaleAmount) > 1000
GridView
Result :
ASP.NET MVC

Visual Studio Tips and Tricks

Dot Net Interview Questions

If we use WHERE clause instead of HAVING clause, we will get a syntax error. This is
Slides
https://web.archive.org/web/20210805080838/https://csharp-video-tutorials.blogspot.com/2015/09/difference-between-where-and-having-in.html 2/4
3/12/2023 Sql server, .net and c# video tutorial: Difference between where and having in sql server
because the WHERE clause doesn’t work with aggregate functions like sum, min, max, Entity Framework
avg, etc.
SELECT Product, SUM(SaleAmount) AS TotalSales WCF
FROM Sales
GROUP BY Product ASP.NET Web Services
WHERE SUM(SaleAmount) > 1000
Dot Net Basics
So in short, the difference is WHERE clause cannot be used with aggregates where
C#
as HAVING can.
SQL Server
However, there are other differences as well that we need to keep in mind when using
WHERE and HAVING clauses. WHERE clause filters rows before aggregate ADO.NET
calculations are performed where as HAVING clause filters rows after aggregate
calculations are performed. Let us understand this with an example. ASP.NET

Total sales of iPhone and Speakers can be calculated by using either WHERE or GridView
HAVING clause
ASP.NET MVC

Calculate Total sales of iPhone and Speakers using WHERE clause : In this Visual Studio Tips and Tricks
example the WHERE clause retrieves only iPhone and Speaker products and then
performs the sum.
SELECT Product, SUM(SaleAmount) AS TotalSales Java Video Tutorials
FROM Sales Part 1 : Video | Text | Slides
WHERE Product in ('iPhone', 'Speakers')
GROUP BY Product Part 2 : Video | Text | Slides

Part 3 : Video | Text | Slides


Result :

Interview Questions
C#

SQL Server

Calculate Total sales of iPhone and Speakers using HAVING clause : This example Written Test
retrieves all rows from Sales table, performs the sum and then removes all products
except iPhone and Speakers.
SELECT Product, SUM(SaleAmount) AS TotalSales
FROM Sales
GROUP BY Product
HAVING Product in ('iPhone', 'Speakers')

Result :

So from a performance standpoint, HAVING is slower than WHERE and should be


avoided when possible.

Another difference is WHERE comes before GROUP BY and HAVING comes after
GROUP BY.

Difference between WHERE and Having


1. WHERE clause cannot be used with aggregates where as HAVING can. This means
WHERE clause is used for filtering individual rows where as HAVING clause is used to
filter groups.

2. WHERE comes before GROUP BY. This means WHERE clause filters rows before
aggregate calculations are performed. HAVING comes after GROUP BY. This means
HAVING clause filters rows after aggregate calculations are performed. So from a
performance standpoint, HAVING is slower than WHERE and should be avoided when
possible.

3. WHERE and HAVING can be used together in a SELECT query. In this case WHERE
clause is applied first to filter individual rows. The rows are then grouped and aggregate
calculations are performed, and then the HAVING clause filters the groups.

https://web.archive.org/web/20210805080838/https://csharp-video-tutorials.blogspot.com/2015/09/difference-between-where-and-having-in.html 3/4
3/12/2023 Sql server, .net and c# video tutorial: Difference between where and having in sql server

2 comments:

Sheetal October 7, 2015 at 12:16 AM


i am a student and learning sql. i am looking for such tutorials.
Reply

jessicaray4 February 24, 2016 at 1:06 AM


i am an understudy and learning sql. i am searching for such instructional exercises.
Reply

It would be great if you can help share these free resources

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

Powered by Blogger.

https://web.archive.org/web/20210805080838/https://csharp-video-tutorials.blogspot.com/2015/09/difference-between-where-and-having-in.html 4/4

You might also like