Questão de Banco de Dados — Consultas e Comandos em SQL — FGV 2022
- Código
- fg152871
- Banca
- FGV
- Órgão
- TCU
- Ano
- 2022
- Cargo
- AUFC ( )
Na questão abaixo, considere as tabelas de banco de dados T, TX e DUAL, exibidas com suas respectivas instâncias a seguir.
T
| sequencia | caracteristica |
| 1 | 23987 |
| 2 | 9845 |
| 3 | NULL |
| 4 | 40983 |
| 6 | 48750 |
| 7 | NULL |
| 8 | NULL |
| 10 | 48750 |
| 12 | 48750 |
TX
| sequencia | caracteristica |
| 2 | 9845 |
| 3 | 998034 |
| 4 | 50932 |
| 5 | 24390 |
| 6 | 48750 |
| 6 | 50296 |
| 7 | NULL |
| 8 | 998746 |
| 9 | 32746 |
| 9 | NULL |
| 9 | 22798 |
DUAL
| x |
| NULL |
Nas colunas das três tabelas, o tipo é o de número inteiro. Em todos os comandos SQL, considera-se o NULL como um valor desconhecido (unknown).
Supondo que a coluna sequencia da tabela T, anteriormente definida, deveria conter números inteiros em sequência contínua, seria preciso descobrir os intervalos de valores faltantes. Um valor é considerado faltante quando a) é um número inteiro n entre o menor e o maior valor da tabela, tal que n não esteja presente na tabela, ou b) é um número presente na tabela T, com valor nulo na coluna caracteristica.
| inicio | fim | faltantes |
| 3 | 3 | 1 |
| 5 | 5 | 1 |
| 7 | 9 | 3 |
| 11 | 11 | 1 |
O comando SQL que produz o resultado acima, a partir da instância inicialmente definida para a tabela T, é:
- Aselect t1.sequencia 'inicio', t2.sequencia 'fim', t2.sequencia - t1.sequencia -1 faltantes from T t1, T t2 where t1.sequencia < t2.sequencia and t1.sequencia <> t2.sequencia -1 and not exists (select * from T t3 where t3.sequencia > t1.sequencia and t3.sequencia < t2.sequencia)
- Bselect t1.sequencia +1 'inicio', t2.sequencia -1 'fim', t2.sequencia - t1.sequencia -1 faltantes from T t1, T t2 where t1.sequencia < t2.sequencia and t1.sequencia <> t2.sequencia -1 and t1.caracteristica is not null and t2.caracteristica is not null and (not exists (select * from T t3 where t3.sequencia > t1.sequencia and t3.sequencia < t2.sequencia and t3.caracteristica is not null))
- Cselect t1.sequencia +1 'inicio', t2.sequencia -1 'fim', t2.sequencia - t1.sequencia -1 faltantes from T t1, T t2 where t1.sequencia <> t2.sequencia -1 and t1.caracteristica is not null and t2.caracteristica is not null and (not exists (select * from T t3 where t3.sequencia > t1.sequencia and t3.sequencia < t2.sequencia and t3.caracteristica is not null) or exists (select * from T t3 where t3.sequencia > t1.sequencia and t3.sequencia < t2.sequencia and t3.caracteristica is null))
- Dselect t1.sequencia +1 'inicio', t2.sequencia -1 'fim', t2.sequencia - t1.sequencia -1 faltantes from T t1, T t2 where t1.sequencia < t2.sequencia and t1.sequencia <> t2.sequencia -1 and t1.caracteristica is not null and t2.caracteristica is not null and exists (select * from T t3 where t3.sequencia > t1.sequencia and t3.sequencia < t2.sequencia)
- Eselect t1.sequencia +1 'inicio', t2.sequencia -1 'fim', t2.sequencia - t1.sequencia -1 faltantes from T t1, T t2 where t1.sequencia < t2.sequencia and t1.sequencia <> t2.sequencia -1 and t1.caracteristica is not null and t2.caracteristica is not null and not exists (select * from T t3 where t3.sequencia > t1.sequencia and t3.sequencia < t2.sequencia)