🔵Filter Clauses
WHERE clause
Overview
The WHERE clause is used to filter records returned from a SELECT, UPDATE, or DELETE statement. It allows you to specify conditions that rows must meet to be included in the result set.
Syntax
The basic syntax is to follow the SELECT and FROM clauses with a WHERE and a conditional expression.
Rules
The WHERE clause appears after the FROM and before ORDER BY and GROUP BY clauses.
The condition evaluates to True, False or Unknown for each row. Rows resulting in True are included.
Conditions can use comparison, string, logical and other operators.
Multiple conditions can be combined using AND, OR and NOT logical operators.
Operators
Some commonly used operators are:
Comparison operators: =, !=, >, <, >=, <=
BETWEEN ... AND
IN (set of values)
LIKE for pattern matching
IS NULL to check for null values
Examples
Conclusion
The WHERE clause is an essential filtering component in SQL. It allows you to specify precise conditions to target only relevant rows and avoid entire table scans. This results in faster queries and reduced load on the database.
HAVING clause
Overview
The HAVING clause is used to filter groups of rows returned by the GROUP BY clause. It enables filtering based on aggregate functions like SUM(), COUNT(), AVG() etc.
Syntax
The HAVING clause comes after GROUP BY and before ORDER BY.
Rules
HAVING can only be used with a GROUP BY clause.
The condition can use aggregate functions to evaluate against grouped rows.
Logical operators like AND, OR can combine multiple conditions.
WHERE filters rows before aggregation, HAVING filters groups after aggregation.
Operators
HAVING conditions can use the same operators as WHERE:
Comparison operators like =, !=, >, <
BETWEEN, IN
Pattern matching with LIKE
IS NULL
And aggregate functions like:
SUM(), AVG(), COUNT(), MIN(), MAX()
Examples
Conclusion
The HAVING clause allows filtering of groups based on aggregate criteria after the GROUP BY grouping is done. This is more powerful than WHERE which cannot use aggregates. HAVING provides advanced filtering capabilities for aggregated data.
GROUP BY Clause
Overview
The GROUP BY clause is used to group rows that have the same values into summary rows. It is often used with aggregate functions like COUNT, MAX, SUM, AVG to group and summarize data.
Syntax
GROUP BY goes after the FROM and WHERE clauses but before ORDER BY and HAVING.
Rules
GROUP BY groups rows based on one or more columns specified.
Aggregate functions like SUM() perform calculations on the grouped rows.
Columns in the SELECT must either be in the GROUP BY clause or passed into an aggregate function.
Example
Groups rows by department and sums the salaries.
ORDER BY Clause
Overview
ORDER BY sorts the rows in the output result set based on one or more columns.
Syntax
Order directions are ascending (ASC) by default or descending (DESC).
Rules
ORDER BY appears after FROM, WHERE, GROUP BY and HAVING clauses.
Columns can be sorted in ascending or descending order.
Columns in ORDER BY don't have to be selected columns.
Example
Orders rows by state alphabetically, then birth date descending.
So in summary, GROUP BY aggregates rows into groups and ORDER BY sorts the final output rows.
Last updated