Key Difference – where vs having clause in SQL
Data is important for every organization. Therefore, it is necessary to store the data in an organized way to retrieve them. The data is stored in tables. A database consists of a collection of tables. One common database type is relational databases. In a relational database, the tables are related to each other. For example, the table customer is connected to order table. A Relational Database Management System (RDBMS) is a database management system that is based on relational model. It is used to manage Relational Databases. Some examples of RDBMS are MySQL, MSSQL, and Oracle. The Structured Query Language (SQL) is the language used for manipulating and retrieving data in a relational database. There are various clauses in SQL to perform different tasks. Two of them are where and having. This article discusses the difference between where and having clause in SQL. The key difference between where and having clause in SQL is that where clause is used to filter records before a grouping or an aggregation occurs while having clause is used to filter records after a grouping, or an aggregation occurs.
What is where clause in SQL?
It helps to retrieve, update or delete a particular set of data from the table according to the given condition. The programmer can use where clause to restrict and fetch only the required data. The query executes only on the records where the condition specified by the where clause is true. It can be used with select, update and delete.
Refer the below student table,
What is having clause in SQL?
What are the Similarities Between where and having clause in SQL?
What is the Difference Between where and having clause in SQL?
where vs having clause in SQL | |
The where is an SQL clause that is used to specify a condition while fetching the data from a single table or by joining with multiple tables. | The having is an SQL clause that specifies that an SQL select statement should only return rows where aggregate values meet the specified conditions. |
Purpose | |
The where clause is used to filter rows. | The having clause is used to filter groups. |
Aggregation | |
The where clause cannot be used with aggregation functions unless it is in a subquery contained in a Having clause. | The aggregation functions can be used with the having clause. |
Filtering Method | |
The where clause behaves as a pre-filter. | The having clause behaves as a post filter. |
Group By Clause Order | |
The where clause is used before the Group By clause. | The having clause is used after the Group By clause. |
Used With | |
The where clause can be used with select, update and delete. | The having clause is used only with select. |
0 comments:
Post a Comment