Which term describes what the where clause represents in the select-from-where statement?
Show What is the difference between HAVING and WHERE clause is one the most popular question asked in interviews especially to the freshers? Though these two clauses are very similar and they primarily restrict the tuples returned by the SELECT statement, the main difference arises when used with the GROUP BY clause. So, lets first learn about these two clauses and then their differences. WHERE ClauseThe WHERE clause is used to fetch the data which specify the given condition. It is used to filter records and select only necessary records. It is used with SELECT, UPDATE, DELETE, etc. query. The SQL also implements and, or, and not in the WHERE clause which is known as the boolean condition. Example: Take an example of a table Transaction that has ‘Item’, 'Month' and ‘Amount’ as attributes. If we want to calculate the total sale by two products TV and Fridge then our Query statement would be:
The output of the above query will be: HAVING ClauseThe HAVING clause is generally used along with the GROUP BY clause. This clause is used in the column operation and is applied to aggregate rows or groups according to given conditions. Example: Consider the above example. Now, if we want to calculate the Net_amount of the total sale by two products TV and Fridge then our query statement would be:
The output of the above query will be: Now, if we want only those items which have the net_amount greater than 1200. The query can be written as:
The output of the above query will be: This cannot be done in the WHERE clause. It generates an error message when we use the WHERE clause with the aggregate functions. Difference between WHERE and HAVING clause
Example: The following query has the same results. The WHERE clause uses the “age” index but the having clause will scan the full table instead of directly looking at the "age" column.
2. HAVING clause
This was a discussion on the differences between WHERE and HAVING clause. Hope you learned something new today. Do share this blog with your friends to spread the knowledge. Visit our YouTube channel for more content. You can read more blogs from here. Keep Learning :) Team AfterAcademy! What is the use of WHERE clause in SELECT statement?The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition.
What is the WHERE clause in SQL called?As SQL is an implementation of a relational algebra, the WHERE condition is called selection (not to be confused with the SELECT part which implemenets projection and renames).
What does an asterisk (*) do in a structured query language SQL query?It's a wildcard it means return all columns for that table in the result set.
What is the clause of the SELECT statement WHERE in it indicates the tables or views FROM which data will be obtained?In queries where all the data is found in one table, the FROM clause is where we specify the name of the table from which to retrieve rows. In other articles we will use it to retrieve rows from multiple tables. The WHERE clause is used to constrain which rows to retrieve.
|