Cassic

Cláusula WHERE com Condições Simples

Autor: Tadeu Pereira


  Podemos filtrar colunas para nos mostrar apenas os dados que nos interessa através da cláusula where em conjunto com os operadores comparativos.

Sintaxe :

select  Coluna, Coluna, ..., Coluna from  Tabela
where Condição


Tabela
Nome da tabela
Coluna
Nome de uma coluna – Para mostrar todas as colunas pode-se colocar apenas a máscara "*" no lugar do nome das colunas
Condição
Cria uma condição para filtrar os dados utilizando os operadores comparativos

Operador Comparativo "=" (Igual)

Exemplo :

select Codigo, Nome from Funcion
where Nome = 'Tadeu'

Resultado


Codigo
Nome
1
Tadeu

Operador Comparativo "<>" (Diferente)

Exemplo :

select Codigo, Nome from Funcion
where Nome <> 'Tadeu'

Resultado


Codigo
Nome
2
Ylane
3
Julian
4
Ewerton
5
João
6
Geraldo
7
Maria

Operador Comparativo ">" (Maior que)

Exemplo :

select Codigo, Nome from Funcion
where Nome > 'Tadeu'

Resultado


Codigo
Nome
2
Ylane

Operador Comparativo ">=" (Maior que ou Igual)

Exemplo :

select Codigo, Nome from Funcion
where Nome >= 'Tadeu'

Resultado


Codigo
Nome
1
Tadeu
2
Ylane

Operador Comparativo "<" (Menor que)

Exemplo :

select Codigo, Nome from Funcion
where Nome < 'Tadeu'

Resultado


Codigo
Nome
3
Julian
4
Ewerton
5
João
6
Geraldo
7
Maria

Operador Comparativo "<=" (Menor que ou Igual)

Exemplo :

select Codigo, Nome from Funcion
where Nome <= 'Tadeu'

Resultado


Codigo
Nome
1
Tadeu
3
Julian
4
Ewerton
5
João
6
Geraldo
7
Maria

Operador Comparativo "between ... and ..." (Entre dois valores)

Exemplo :

select Codigo, Nome from Funcion
where Nome between 'João' and 'Tadeu'

Resultado


Codigo
Nome
1
Tadeu
3
Julian
5
João
7
Maria

Operador Comparativo "not between ... and ..." (Não está entre dois valores)

Exemplo :

select Codigo, Nome from Funcion
where Nome not between 'João' and 'Tadeu'

Resultado


Codigo
Nome
2
Ylane
4
Ewerton
6
Geraldo

Operador Comparativo "in(lista)" (Igual a qualquer valor da lista)

Exemplo :

select Codigo, Nome from Funcion
where Nome in ('João','Tadeu')

Resultado


Codigo
Nome
1
Tadeu
5
João

Operador Comparativo "not in(lista)" (Diferente de qualquer valor da lista)

Exemplo :

select Codigo, Nome from Funcion
where Nome not in ('João','Tadeu')

Resultado


Codigo
Nome
2
Ylane
3
Julian
4
Ewerton
6
Geraldo
7
Maria

Operador Comparativo "like" (Pesquisa uma cadeia de caractere)

Exemplo :

select Codigo, Nome from Funcion
where Nome like'J%'
Obs.: A máscara no operador like usada foi "%", porém ele pode mudar de um SGBDR para outro.

Resultado


Codigo
Nome
3
Julian
5
João

Operador Comparativo "is null" (Valor nulo)

Exemplo :

select Codigo, Nome from Funcion
where Setor is null

Resultado


Codigo
Nome
7
Maria

Operador Comparativo "is not null" (Valor não nulo)

Exemplo :

select Codigo, Nome from Funcion
where Setor is not null

Resultado


Codigo
Nome
1
Tadeu
2
Ylane
3
Julian
4
Ewerton
5
João
6
Geraldo