Difference between a WHERE clause and HAVING clause?

  • WHERE clause used to filter the physical rows in your table(s) whereas the HAVING clause operates on groups of a result-set (usually aggregate) and specifies which of the summary rows are returned in your resultset.  
  • A WHERE clause is independent of GROUP BY clause but the HAVING clause does depend on this, although it can be used without this as well.
  •  First SQL Server filters the physical rows using WHERE conditions and then performs GROUP BY on filtered data in memory and then filters the aggregated rows again with HAVING clause.

Example:
List of all the employee with basic greater than 3000.

Now I want to calculate the Dept codes for which total of basic is greater than 20,000 for those employees who are getting basic more than 3000.

T-SQL Clauses

Most commonly used T-SQL command is SELECT statement. Below are the basic T-SQL clauses every SQL beginner must know:

SELECT <Column_List>
FROM <Table_Name>
WHERE <Conditional_Statement>
GROUP BY <Column_List>
HAVING <Conditional_Statement>
ORDER BY <Column_List>


Lets take an example of Employee table to understand these clauses:

EmployeeID
FirstName
MiddleName
LastName
DOB
Basic
Department
1
Hari
N
Sharma
10/10/1980
2500
Sales
2
John
D
Howell
11/11/1981
1900
Sales
3
Vijay
NULL
Sharma
10/20/1982
1500
Engineering
4
Paul
NULL
Catlin
7/25/1976
2600
Engineering
5
Romil
F
Driscoll
1/13/1979
2400
Sales
6
Karthik
P
Nagarajan
8/10/1969
2800
Engineering
7
Hany
H
Rodriguez
4/10/1977
2200
Management
8
William
NULL
Green
1/21/1981
1800
Management
9
Michelle
NULL
Jones
5/19/1974
2300
Management
10
Thomas
D
Lee
3/31/1984
1600
Sales

SELECT statement is used to pull the data from a table, view, or table valued function.
FROM clause is used to specify the name of the source table which we pull the data from.

For instance, we want to pull FirstName. LastName of all the employees along with Department name then we will use following query:

SELECT  FirstName,LastName,Department
FROM    dbo.Employee

Here is output:


WHERE clause is used to filter the data coming from a table. For instance, you want to pull FirstName, MiddleName, LastName, and Basic of all employees who belong to Sales department:

SELECT  FirstName,MiddleName,LastName,Basic
FROM    dbo.Employee
WHERE   Department = 'Sales'

 
Here is output:






 

GROUP BY clause is used to aggregate the data based on certain group of columns. For instance, find out the TOTAL of basic amount for each department.

SELECT  Department, SUM(Basic) BasicTotal
FROM    dbo.Employee
GROUP BY Department

Here is output:





 

HAVING clause is used to filter out the data based on aggregated values. For instance, find out the list of Departments for which Total of basic is greater than 6500:

SELECT  Department, SUM(Basic) BasicTotal
FROM    dbo.Employee
GROUP BY Department
HAVING   SUM(Basic) > 6500

I will explain the difference between WHERE and HAVING in my next post.


ORDER BY clause is used to arrange the result set in desired order, We can order the result in DESC or ASC order. For instance, get the list of all the employees in the order of First Name and then Last Name:

SELECT  FirstName,MiddleName,LastName,Basic
FROM    dbo.Employee
ORDER BY FirstName,LastName

You can also write this query in following way (1 means first column in SELECT statement and 2 means second column the SELECT statement):

SELECT FirstName,MiddleName,LastName,Basic
FROM dbo.Employee
ORDER BY 1,2
 

What is View in SQL Server?

A view can be thought of as either a virtual table or a stored query. The data accessible through a view is not stored in the database as a distinct object. Only SELECT statement is stored in the database. The result set of the SELECT statement forms the virtual table returned by the view.
 
Views are called virtual tables because the result set of a view is us not usually saved in the database. The result set for a view is dynamically incorporated into the logic of the statement and the result set is built dynamically at run time.

Views are created by defining the SELECT statement that retrieves the data to be presented by the view. The data tables referenced by the SELECT statement are known as the base tables for the view.

Views can be created to hide data complexity as well data security. For instance you have three tables, Employee, Department, and BasicPay and you want to display only specific columns from these table.

Table / Entity
Fields / Attributes
Comments
Employee
(EmployeeID, EmployeeName, DOB, DepartmentID)
User can see all the attributes.
Department
(DepartmentID, DepartmentName, ManagerEmployeeID)
User can see all the attributes.
BasicPay
(EmployeeID, StartDate, EndDate, PayScale, BasicAmt, BonusAmt, SalaryCurreny)
User can see only PayScale and should not have any access to BasicAmt and BonusAmout.

 

Suppose user is interested in following reporting data:


SELECT       e.EmployeeName

            ,d.DepartmentName
            ,mgr.FirstName AS DeptManagerName
            ,b.PayScale
FROM        dbo.Employee e
LEFT JOIN   Department d
ON          d.DepartmentID = e.DepartmentID
LEFT JOIN   dbo.Employee mgr
ON          mgr.EmployeeID = d.ManagerEmployeeID
LEFT JOIN   dbo.BasicPay b
ON          b.EmployeeID = e.EmployeeID
 

Now you can create view a view to store the above query and grant access to the view instead of granting access to all three tables.

 

IF OBJECT_ID('dbo.vwEmployeeDetails') IS NOT NULL
BEGIN
    PRINT 'Dropping view dbo.vwEmployeeDetails'
    DROP VIEW dbo.vwEmployeeDetails
END
GO
 
PRINT 'Creating view dbo.vwEmployeeDetails'
GO
 
CREATE VIEW dbo.vwEmployeeDetails
AS
/***********************************************
Created By      :   Author Name
Created Date    :   MM/DD/YYYY
 
History
-----------------------------------------------
MM/DD/YYYY      Comments
 
***********************************************/
SELECT      e.EmployeeName
            ,d.DepartmentName
            ,mgr.FirstName AS DeptManagerName
            ,b.PayScale
FROM        dbo.Employee e
LEFT JOIN   Department d
ON          d.DepartmentID = e.DepartmentID
LEFT JOIN   dbo.Employee mgr
ON          mgr.EmployeeID = d.ManagerEmployeeID
LEFT JOIN   dbo.BasicPay b
ON          b.EmployeeID = e.EmployeeID
GO

 

Now you can pull the data directly from the view instead of using complex query:

SELECT   EmployeeName
        ,DepartmentName
        ,DeptManagerName
        ,PayScale
FROM    dbo.vwEmployeeDetails

 

Advantages of View

There are several advantages of views as mentioned below:

·         We can hide data complexity in a view. Instead of forcing users to learn the T-SQL syntax, you might wish to provide a view that runs a commonly requested SQL statement.

·         View is an ideal object to protect your data. If you have a table containing sensitive data in certain columns, you might wish to hide those columns from certain groups of users. For instance allow employees who do not work in payroll to see the name, work phone, and department columns in an employee table, but do not allow them to see any columns with salary information or personal information, so you can create a view that only displays required columns. You can grant permissions to that view without allowing users to query the underlying tables.

·         Join columns from multiple tables so that they look like a single table.

·         Enforcing some simple business rules. For example, if you wish to generate a list of customers that need to receive the fall catalog, you can create a view of customers that have previously bought your shirts during the fall.

·         Aggregate information instead of supplying details. For example, present the sum of a column, or the maximum or minimum value from a column.

·         Data exports with BCP. If you are using BCP to export your SQL Server data into text files, you can format the data through views since BCP's formatting ability is quite limited.

·         Customizing data. If you wish to display some computed values or column names formatted differently than the base table columns, you can do so by creating views.

 

Disadvantages of View

·         Even though views can be a great tool for securing and customizing data, they can be performance bottleneck. Indeed, they are not any faster than the query that defines them.

Note: From SQL Server 2000 onwards, indexed views (also referred to as "materialized" views) are supported to overcome this limitation.

·         Views can especially degrade the performance if they are based on other views. Therefore, it is recommended NOT to create views based on other views.