You are on page 1of 6

12/24/2016 SQL Server Interview Questions: What is the difference between Having and Where clause

5   More    Next Blog» Create Blog   Sign In

SQL Server Interview Questions
C#
Boo MV ASP. C SQL W Written HR Subsc SQL Buy
Program 5
ks C NET # Server CF Test Round ribe Tutorial DVD
s

If you like this website, please share


this site using g+1 button What is the difference between Having and
+5   Recommend this on Google Where clause
Let us understand the difference between HAVING and WHERE clause with an
example. Consider the Employees table below.

Employee Table

Best software training institute in bangalore for SAP ABAP,
SAP BI, .Net, Informatica, Software Testing, Siebel CRM
I am Venkat and this is my website. I did training in Pragim
Technologies and got job in Dell in less than a week. PRAGIM is the
best S/W training institute in Bangalore.
Click here for Hyderabad Center Website
For further details please call 09900113931.

SAP BI Training in Bangalore
Best SAP BI Training institute in Bangalore,
Marathahalli. Real time project based training
provided by working software professionals
having more than 10 years of experience. Use the script below to create and populate the table, so you can follow
Informatica Training in Bangalore along with the examples.
Informatica training in bangalore delivered by
a real time software expert having 10 years CREATE TABLE [dbo].[Employees1]
of experience.  (
Siebel CRM Training in Bangalore   [Id] [int] NOT NULL,
Best software training institute for Siebel CRM   [Name] [nvarchar](50) NULL,
training in Marathahalli, Bangalore.    [Dept] [nvarchar](50) NULL,
Software testing training institute in   [Gender] [nvarchar](50) NULL,
bangalore   [Country] [nvarchar](50) NULL,
  [Salary] [float] NULL,
http://venkatsqlinterview.blogspot.in/2011/07/what­is­difference­between­having­and.html 1/6
12/24/2016 SQL Server Interview Questions: What is the difference between Having and Where clause

Software testing training on real time projects )
and placements. 
MSBI Training Institute in Bangalore
Best MSBI Training in Bangalore by an
expert. MSBI training on real time projects
and placements. 
Basic SQL Server Interview Questions

SQL Server Interview Questions on


Temporary Tables

SQL Server Interview Questions on


Indexes ‐ Part 1

SQL Server Interview Questions on


Indexes ‐ Part 2

What is the difference between


Having and Where clause
Data Inserttion Script:
What is the difference between a Insert into Employees Values(1,'John','IT','Male','UK',5000)
Temporary Table and a Table Variable Insert into Employees Values(2,'Mary','HR','Female','India',3000)
Insert into Employees Values(3,'Todd','IT','Male','UK',3500)
What is the use of COALESCE in SQL Insert into Employees Values(4,'Pam','HR','Female','India',4000)
Server Insert into Employees Values(5,'Tatcher','Payroll','Male','USA',2000)
Insert into Employees Values(6,'Sunil','IT','Male','USA',1400)
SQL Server Interview Questions on Insert into Employees Values(7,'Hari','Payroll','Male','UK',2500)
triggers Insert into Employees Values(8,'Sunitha','HR','Female','India',4000)
Insert into Employees Values(9,'Sneha','IT','Female','India',3000)
Difference between User Defined Insert into Employees Values(10,'Ruby','Payroll','Male','UK',4600)
Function and Stored Procedure
You can use HAVING clause only when you use Group By clause. The following
SQL Server Interview Questions on query will give an error stating "Column 'Employees.Dept' is invalid in the
Views ‐ Part 1 HAVING clause because it is not contained in either an aggregate function or the
GROUP BY clause."
SQL Server Interview Questions on
Views ‐ Part 2 Select * from Employees Having Dept='IT'

Basic SQL Server Interview Questions So to filter the rows as they are selected from the table we use WHERE clause


on Joins as shown below 

Explain Inner Join with an example Select * from Employees Where Dept='IT'

Explain Left Outer Join with an If I want to select, the toal number of employees in IT department I can write the


example query in 2 different ways as shown below.

Explain Right Outer Join with an 1. Select Dept, COUNT(*) as Total from Employees Where Dept='IT' Group By


example Dept
2. Select Dept, COUNT(*) as Total from Employees Group By Dept Having
Explain Full Outer Join with an Dept='IT'
example
The first query runs faster than the second query. This is because, in the first
Explain Self Join with an example query we only select IT department records and then perform the count operation
where as in the second query we perform the count on all the Department
What is the difference between Index records and then select only the IT department and its count using the HAVING
Scan and Index Seek clause. As the second query has more records to process than the first query, it
tends to be relatively slower.
Write a SQL Query to delete from a
table that is involved in a SQL join So, a WHERE clause is used in the select statement to filter the rows as they
are retrieved from the database table. HAVING clause is used in the select
What are the advantages of using statement in conjunction with the Group By clause, to filter the query results
stored procedures after they have been grouped.

What are the different ways to If you have spotted any errors or if you can improve this answer further, please


replace NULL values in SQL Server feel free to do so by submitting the form below.
http://venkatsqlinterview.blogspot.in/2011/07/what­is­difference­between­having­and.html 2/6
12/24/2016 SQL Server Interview Questions: What is the difference between Having and Where clause

+5   Recommend this on Google


SQL Server interview questions on
string manipulation functions

Write a Stored Procedure that takes 13 comments:


column name as a parameter and
returns the result sorted by the
column that is passed Anonymous August 8, 2011 at 6:57 AM
What is deferred name resolution in First Query can still be improved as follows:
SQL Server? 1. Select 'IT' as Dept, COUNT(1) as Total from Employees Where
Dept='IT'
Reply

Replies

Kiran April 19, 2012 at 10:20 PM


Beautiful Query

Reply

asp.net tutorials December 25, 2011 at 6:30 AM


how to write the query to get the count of particular table
columns in a database.
Reply

Replies

tsmith16789 May 18, 2012 at 11:52 AM


hERE IS THE SOLUTION FOR YOUR QUERY IN ms sQL
SERVER:

Select COUNT(*)

From infoschema.Column

Where Table_Name = 'name_your_table'

tsmith16789 May 18, 2012 at 11:54 AM


Select Count(*)

From infoColumn.Column

Where table_name = 'name_your_table'

sirisha sarikonda July 16, 2013 at 2:34 PM


Sorry, when i executed the above two queries i am
getting a error
Incorrect syntax near the keyword 'Column'.

we can use either of the below to get the column


count.
http://venkatsqlinterview.blogspot.in/2011/07/what­is­difference­between­having­and.html 3/6
12/24/2016 SQL Server Interview Questions: What is the difference between Having and Where clause

select count(*) from SYSCOLUMNS where id=(select id


from SYSOBJECTS where name='table name ')

select count(*) from SYSCOLUMNS where id=


object_id('table name')

amitnpatil.blogspot.com August 11, 2015 at 2:21 AM


Select COUNT(*)
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='name_your_table'

Reply

jatin nahar January 4, 2013 at 3:24 AM


1. We can write having clause with only select statement but
we can use where clause in any DML statement.

Reply

Sameer Thakkar September 18, 2013 at 11:23 AM


Hi All,

One more difference is :


We can use aggregate function in a HAVINGclause but not in
WHERE clause.

Eg: Select Dept, COUNT(*) as Total from Employees Group By


Dept Having COUNT(*) > 5
But the same is not used in WHERE clause

Eg: Select Dept, COUNT(*) as Total from Employees Where


COUNT(*) > 5 ‐‐‐‐‐this will give an error
Reply

Anonymous November 19, 2013 at 5:21 AM


how to get the duplicate value in table ?
Reply

Dhans January 6, 2014 at 11:22 AM


WHERE

‐‐‐‐‐‐

‐‐> Where is used in select, insert, update, delete statements

‐‐> WHERE clause works on individual rows

‐‐> if the query has both WHERE and HAVING clauses in the

http://venkatsqlinterview.blogspot.in/2011/07/what­is­difference­between­having­and.html 4/6
12/24/2016 SQL Server Interview Questions: What is the difference between Having and Where clause

statement, WHERE Clause gets applied first. Once the rows that
meet the WHERE criteria are chosen, then they are grouped
and finally HAVING works on those grouped rows.

HAVING

‐‐‐‐‐‐‐‐

‐‐> Having can only be used in select statement

‐‐> Having clause is used generally with GROUP BY statement


and works as a filter on grouped rows, but it can be used
without GROUP BY too. When GROUP BY is not used, HAVING
behaves like a WHERE clause.

Reply

Unknown July 11, 2016 at 11:30 AM


Is it works?
Select id ,name,sal,from table Group by I'd,name,sal having
(sal>avg(sal))
Reply

Replies

Anonymous August 23, 2016 at 7:06 AM


Sorry it won't works since the aggregate function is
not applied on the column id.It is not possible to use
the having clause as a conditional.

Reply

Enter your comment...

Comment as:  Select profile...

Publish
  Preview

http://venkatsqlinterview.blogspot.in/2011/07/what­is­difference­between­having­and.html 5/6
12/24/2016 SQL Server Interview Questions: What is the difference between Having and Where clause

If you are aware of any other sql server questions asked in an


interview, please post them below. If you find anything missing or
wrong, please feel free to correct by submitting the form below.

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

 
Disclaimer ­ Terms of use ­ Contact Us

http://venkatsqlinterview.blogspot.in/2011/07/what­is­difference­between­having­and.html 6/6

You might also like