Having vs Where Clause
Machines, computers in particular, are designed as a means of human communication. As such, they are created in such a way that their behaviors are controlled by programs that use artificial language. Natural language like English cannot be used by computers because the vocabulary is too large and ever changing.
Computer language needs to be accurate and precise because computers have no common sense so it is based on algorithm which can calculate and process data as well as enable computers to reason. There are several computer languages that have been created. Each of them has two components, the syntax or form and the semantics or meaning.
There are assembly, concurrent, dataflow, definitional, functional, intermediate, logic, procedural, and query languages. Query languages are used to make inquiries on databases and information systems. One such language is the Structured Query Language (SQL).
SQL is a database computer language with the following functions: data insert, update and delete, access and control, and schema control and modification. It is further subdivided into the following elements:
Expressions
Predicates
Queries
Statements
Clauses
SQL has the following clauses:
Constraint clause
For Update clause
From clause
Group BY clause
Order By clause
Where Current Of clause
Having clause
Where clause
The having clause is always used with the group by clause. It is used to filter records that are returned by a group by clause utilizing a predicate. The results of a group by clause are restricted by using the having clause and only the rows specified are selected.
It is similar to a where clause and a query can have both a having and a where clause. In this case, the where clause is used first and after individual rows are grouped, the having clause is used. The having clause applies to the group as a whole while the where clause applies to individual rows.
The where clause is used to select rows which are to be included in the query and uses a comparison predicate and it excludes rows that are not evaluated by the comparison predicate to be true. It is used in conditions that refer to aggregate functions and to select individual rows in a table which is specified in a from clause.
Without it the resulting set of rows would become very large. It uses the following operators: = or equal, < > or not equal, > or greater than, < or less than, >= or greater than or equal, <= or less than or equal, BETWEEN, LIKE, and IN.
Summary
1.The having clause is used in rows that are grouped while the where clause is used in individual rows.
2.When they are used together, the where clause is used first to select which rows are to be grouped then the having clause is used.
3.The having clause is used with a group by clause while the where clause is sued in rows specified by a from clause.