Which SQL clause is used to filter rows based on a condition ?
AORDER BY
BWHERE
CGROUP BY
DHAVING
Answer:
B. WHERE
Read Explanation:
The SQL WHERE Clause
- The WHERE clause is fundamental in SQL for data retrieval, enabling the filtering of records that meet specific criteria defined by the user.
- It is used in conjunction with SELECT, UPDATE, and DELETE statements to restrict operations to a subset of the dataset.
- Syntax:
SELECT column1, column2 FROM table_name WHERE condition;.
Key Operators used with WHERE:
- Comparison Operators: =, <>, !=, <, <=, >, >=.
- Logical Operators: AND, OR, NOT, which allow for combining multiple conditions to narrow down results.
- Pattern Matching: The LIKE operator is used for pattern matching within string columns (e.g., using
%or_wildcards). - Range Operators: BETWEEN is used to filter values within a specific range, including the boundary values.
- Set Membership: IN is used to specify multiple possible values for a column.
- Null Check: IS NULL or IS NOT NULL is required to identify missing data, as standard comparison operators do not function correctly with
NULLvalues.
Execution Order
- In the logical processing of a SQL query, the WHERE clause is evaluated before the GROUP BY clause.
- Rows that do not satisfy the condition in the WHERE clause are filtered out before any aggregation (such as SUM, COUNT, or AVG) takes place.
- To filter data after aggregation, the HAVING clause must be used instead.
