|
The WHERE clause is an optional clause of the SELECT statement
The WHERE clause specifies the search condition for the rows returned by the SELECT statement.
The following shows the syntax of the WHERE clause in the SELECT statement:
A cláusula WHERE é uma cláusula opcional da instrução SELECT.
A cláusula WHERE especifica a condição de pesquisa para as linhas retornadas pela instrução SELECT.
O seguinte mostra a sintaxe da cláusula WHERE na instrução SELECT:
SELECT column1
, column2
, .......
FROM table_name
WHERE condition;
|
In this syntax, the condition like a filter that defines a condition for the returned rows.
The rows that cause the condition evaluate to true will be included in the result set.
The condition may consist of one or many logical expressions that evaluate to true, false, or unknown.
The logical expressions in the WHERE clause are also known as predicates.
The WHERE clause appears after the FROM clause and before the ORDER BY clause.
When evaluating the SELECT statement, Db2 evaluates the clauses in the following sequence:
FROM, WHERE, SELECT, and ORDER BY.
Besides the SELECT statement, the WHERE clause are used in the UPDATE or DELETE statement to specify rows to be updated or deleted.
Nessa sintaxe, CONDITION é semelhante a um filtro que define uma condição para as linhas retornadas.
As linhas que fazem com que a avaliação CONDITION seja verdadeira serão incluídas no conjunto de resultados.
O CONDITION pode consistir em uma ou mais expressões lógicas avaliadas como verdadeiras, falsas ou desconhecidas.
As expressões lógicas na cláusula WHERE também são conhecidas como predicados.
A cláusula WHERE aparece após a cláusula FROM e antes da cláusula ORDER BY.
Ao avaliar a afirmação SELECT, o DB2 avalia as cláusulas na seguinte seqüência:
FROM, WHERE, SELECT, e ORDER BY.
Além da instrução SELECT, a cláusula WHERE é usada na instrução UPDATE ou DELETE para especificar as linhas a serem atualizadas ou excluídas.
DB2 Database - Banco de dados DB2:
Below is a selection from the "Product" table in the DB2 database.
Abaixo está uma seleção da tabela "Produto" no banco de dados DB2.
| ProductID |
ProductDesc |
Category |
SRP |
QtyOnHand |
TotalValue |
| 7001 | Mouse | Accessories | 75.00 | | |
| 7002 | Harddrive | | 65.00 | 20 | 1,300 |
| 7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
| 7004 | RAM | Components | 23.50 | 16 | 376.00 |
| 7005 | Honda | Bikes | 1,200 | | |
| 7006 | PEN | | 7.45 | 10 | 74.50 |
| 7007 | Cddrive | Accessories | 75.00 | | |
| 7008 | Speaker | Accessories | 75.00 | | |
Example - Exemplo:
The below SQL statement retrieves two columns from the "Product" table, but instead of returning all rows, only rows with a SRP value of 75.00 are returned.
A instrução SQL a seguir recupera duas colunas da tabela "Produto", mas em vez de retornar todas as linhas, apenas as linhas com um valor SRP de 75,00 são
retornadas.
SELECT ProductID
, ProductDesc
FROM Product
WHERE SRP = 75.00;
|
Result - Resultado:
ProductID ProductDesc
7001 Mouse
7007 Cddrive
7008 Speaker
|
Numeric Fields vs Text Fields - Campos numéricos vs campos de texto
SQL requires single quotes around text values (most database systems will also allow double quotes).
However, numeric fields should not be enclosed in quotes.
O SQL requer aspas simples em torno dos valores de texto (a maioria dos sistemas de banco de dados também permite aspas duplas).
No entanto, os campos numéricos não devem ser colocados entre aspas.
SELECT ProductID
, ProductDesc
, SRP
FROM Product
WHERE Category = 'Accessories';
|
Result - Resultado:
ProductID ProductDesc SRP
7001 Mouse 75.00
7003 Keyboard 36.00
7007 Cddrive 75.00
7008 Speaker 75.00
|
WHERE Clause Operators - Operadores da cláusula WHERE::
SQL supports a whole range of conditional operators in the WHERE clause as listed.
O SQL oferece suporte a uma ampla gama de operadores condicionais na cláusula WHERE conforme listado.
| Operator |
Description |
| = |
Equality |
| <> |
Non-equality |
| != |
Non-equality |
| < |
Less than |
| <= |
Less than or equal to |
| !< |
Not less than |
| > |
Greater than |
| >= |
Greater than or equal to |
| !> |
Not greater than |
| BETWEEN |
Between two specified values including the specified start and end value |
| IS NULL |
Is a NULL value |
Example 1 - Exemplo 1:
To List all products that cost less than $10
Para listar todos os produtos que custam menos de $ 10
SELECT ProductID
, ProductDesc
FROM Product
WHERE SRP < 10;
|
Result - Resultado:
ProductID ProductDesc
7006 PEN
|
Example 2 - Exemplo 2:
To List all Category products other than 'Accessories'
Para listar todos os produtos da categoria, exceto 'Acessórios'
SELECT ProductID
, ProductDesc
, SRP
FROM Product
WHERE Category <> 'Accessories';
|
Result - Resultado:
ProductID ProductDesc SRP
7002 Harddrive 65.00
7004 RAM 23.50
7005 Honda 1,200
7006 PEN 7.45
|
Example 3 - Exemplo 3:
To retrieve all products with a price between $5 and $10, including the specified start and end values.
Para recuperar todos os produtos com um preço entre $ 5 e $ 10, incluindo os valores inicial e final especificados.
SELECT ProductID
, ProductDesc
FROM Product
WHERE SRP BETWEEN 5 AND 25;
|
Result - Resultado:
ProductID ProductDesc
7004 RAM
7006 RAM
|
Example 4 - Exemplo 4:
To return a list of all products that have no Category(i.e. null value)
Para retornar uma lista de todos os produtos que não têm categoria (ou seja, valor nulo)
SELECT ProductID
, ProductDesc
, SRP
FROM Product
WHERE Category IS NULL;
|
Result - Resultado:
ProductID ProductDesc SRP
7002 Harddrive 65.00
7006 PEN 7.45
|
|