Exercicios Mysql

3

Click here to load reader

Transcript of Exercicios Mysql

Page 1: Exercicios Mysql

Criar Banco “Banco”

1. /* buscar o nome dos clientes que têm uma conta no banco, as cidades

onde eles vivem e a cidade das agências

onde eles têm suas contas. */ select distinct cli.nomecliente, cidadecliente, cidadeagencia from cliente cli, conta con, agencia a where cli.nomecliente=con.nomecliente and a.nomeagencia=con.nomeagencia;

2. /* selecionar o valor do empréstimo do Pedro */ select concat('R$ ',format(valor,2)) as 'Valor do

Empréstimo do Pedro' from emprestimo where nomecliente='Pedro';

3. /* encontrar o nome dos clientes que fizeram empréstimo, mas somente

daqueles que possuem alguma conta */ select distinct emprestimo.nomecliente from emprestimo,conta where emprestimo.nomecliente=conta.nomecliente;

4. /* selecionar a cidade onde mora a “Ana”. */ select cidadecliente from cliente where nomecliente='Ana';

5. /* selecionar o nome dos clientes preferenciais do banco. Clientes

preferenciais são aqueles que possuem saldo

maior que R$ 2.000,00 */ select distinct nomecliente as 'Clientes Preferenciais' from conta where saldo>2000;

6. /* selecionar o nome dos clientes que realizaram os três primeiros

empréstimos. */ select distinct nomecliente from empréstimo limit 3;

7. /* buscar o nome dos clientes que têm saldo ou empréstimo maior ou

igual a 3.000,00 */ select distinct nomecliente from emprestimo where valor>3000 union select nomecliente from conta where saldo>3000;

8. /* buscar o nome dos clientes que têm conta na cidade onde moram */

Page 2: Exercicios Mysql

select cli.nomecliente,cidadecliente,cidadeagencia

from cliente cli,conta con,agencia a where cidadecliente=cidadeagencia and cli.nomecliente=con.nomecliente and con.nomeagencia=a.nomeagencia;

9. /* selecionar o nome dos clientes que moram em Porto Alegre */ select cli.nomecliente from cliente cli where cidadecliente='POA';

10. /* selecionar o valor dos empréstimos feitos na agência “São João” */ select valor from emprestimo where nomeagencia='São João';

11. /* selecionar o nome das agências que têm o mesmo nome das cidades

onde elas estão. */ select nomeagencia from agencia where nomeagencia=cidadeagencia;

12. /* encontrar todos os clientes da agência “São João”, ou seja, podem ser

aqueles que tenham uma conta na

agência ou empréstimo em ambos. */ select distinct nomecliente from conta where nomeagencia='São João' union

select distinct nomecliente from empréstimo where nomeagencia='São João';

13. /* encontrar todos os clientes que têm algum saldo ou débito.*/ select distinct nomecliente from conta union select distinct nomecliente from emprestimo;

14. /* encontrar todos os clientes da agência “São João” que tenham uma

conta, mas não tenham empréstimo. */ select distinct nomecliente from conta

where nomeagencia='São João' and not exists (select distinct nomecliente from emprestimo where conta.nomecliente=emprestimo.nomecliente);

15. /* encontrar os dados dos clientes que não fizeram empréstimo */ select distinct nomecliente from cliente where not exists (select distinct nomecliente

Page 3: Exercicios Mysql

from emprestimo

where cliente.nomecliente=emprestimo.nomecliente);

16. /* encontrar todos os clientes que possuam tanto um empréstimo como

uma conta na agência “São João”. */ select conta.nomecliente from conta, emprestimo where conta.nomeagencia='São João' and emprestimo.nomeagencia='São João';

17. /* encontrar a cidade tanto das agências quanto dos clientes que se

chamam “João” */ select cidadecliente from cliente where cliente.nomecliente='João'

union select cidadeagencia from agencia,conta where conta.nomeagencia=agencia.nomeagencia and conta.nomecliente='João';

18. /* encontre todos os clientes que possuam um empréstimo em alguma

agência e as cidades onde eles moram. */ select distinct cliente.nomecliente, cidadecliente from emprestimo,cliente where emprestimo.nomecliente=cliente.nomecliente;

19. /* encontre o nome dos clientes que possuem algum empréstimo e

alguma conta */ select distinct conta.nomecliente from conta,emprestimo where emprestimo.nomecliente=conta.nomecliente;