SQL server 2012, ex fix

Asked

Viewed 39 times

1

inserir a descrição da imagem aquicheck if the answer of the exercise below is correct:

List the names of customers (Cli_Nome) that have NO NULL BONUS
and bought products with code (pro_codigo) greater than 5 and less than 10.

select c.cli_nome
from cliente c, pedido p, item i, produto pr 
where c.cli_codigo = p.cli_codigo 
and p.ped_numero = i.ped_numero
and i.pro_codigo = pr.pro_codigo 
and cli_bonus is not null
and pr.pro_codigo > '5' and pr.pro_codigo < '10' 

Is there any possibility of performing a "real proof" to identify on its own if the question is correct?

  • 2

    Yes, there is. Meet the SQL Fiddle.

  • Thank you, but how would you check this SQL Fiddle? @Ciganomorrisonmendez

  • 1

    You specify the tables and data in the Schema part (left side), and execute your command on the right side.

  • 1

    It’s a good one, but I’m still starting. I don’t know how to create tables, just run commands with some difficulty. But thanks, @Ciganomorrisonmendez, you help a lot here, man.

  • in that answer above the not was used correctly, or is it incorrect because it is not necessary? @Ciganomorrisonmendez

  • 1

    I’m riding a Fiddle for you. I’ll ask you for a few minutes.

Show 1 more comment

1 answer

2


I rode this Fiddle for you. Returned the following:

cli_nome
--------
Beltrano
Ciclano
Ciclano

As it had repetition because of the products, we need to use DISTINCT to bring each name only once, so:

select distinct c.cli_nome
from cliente c, pedido p, item i, produto pr 
where c.cli_codigo = p.cli_codigo 
and p.ped_numero = i.ped_numero
and i.pro_codigo = pr.pro_codigo 
and cli_bonus is not null
and pr.pro_codigo > '5' and pr.pro_codigo < '10';

That brings:

cli_nome
--------
Beltrano
Ciclano

The final Fiddle, therefore, gets like this.

The data I used was:

create table Grupo (
  GRP_Codigo int primary key identity,
  GRP_Descricao char(30) not null
);

create table Produto (
  PRO_Codigo int primary key identity,
  PRO_Descricao char(50) not null,
  PRO_Preco numeric(9,2) not null,
  PRO_Apresentacao char(2),
  PRO_Quantidade int default 0,
  GRP_Codigo int foreign key references Grupo(GRP_Codigo)
);

create table Cliente(
  CLI_Codigo int primary key identity,
  CLI_Nome char(30) not null,
  CLI_Endereco char(40),
  CLI_Cidade char(20),
  CLI_Bonus numeric(9,2)
);

create table Pedido (
  PED_Numero int primary key identity,
  CLI_Codigo int foreign key references Cliente(CLI_Codigo),
  PED_Data datetime default getdate(),
  PED_ValorTotal numeric(9,2) default 0
);

create table Item (
  ITE_Numero int primary key identity,
  PED_Numero int foreign key references Pedido(PED_Numero),
  PRO_Codigo int foreign key references Produto(PRO_Codigo),
  ITE_PrecoUnitario numeric(9,2) default 0,
  ITE_Quantidade int default 0
);

-- Dados

insert into Grupo values ('Produtos de Limpeza');
insert into Grupo values ('Alimentos');
insert into Grupo values ('Bebidas');

insert into Produto values ('Detergente', 1.00, null, 50, 1);
insert into Produto values ('Pão Integral', 4.00, null, 50, 2);
insert into Produto values ('Refrigerante', 2.50, null, 50, 3);
insert into Produto values ('Sabão em Pó', 10.00, null, 50, 1);
insert into Produto values ('Queijo', 8.00, null, 50, 2);
insert into Produto values ('Vinho', 20.00, null, 50, 3);
insert into Produto values ('Saponáceo', 5.00, null, 50, 1);
insert into Produto values ('Biscoitos', 2.00, null, 50, 2);
insert into Produto values ('Água Mineral', 1.50, null, 50, 3);
insert into Produto values ('Limpa Vidros', 7.00, null, 50, 1);
insert into Produto values ('Chocolate', 6.00, null, 50, 2);
insert into Produto values ('Cerveja', 3.50, null, 50, 3);

insert into Cliente values ('Fulano', 'Rua Tal', 'Cidade Tal', null);
insert into Cliente values ('Beltrano', 'Outra Rua Tal', 'Tal do Sul', 12);
insert into Cliente values ('Ciclano', 'Uma Terceira Rua Tal', 'Tal do Oeste', 9);

insert into Pedido values (1, getdate(), 7.50);
insert into Pedido values (2, getdate(), 38.00);
insert into Pedido values (3, getdate(), 10.50);

insert into Item values (1, 1, 1.00, 1);
insert into Item values (1, 2, 4.00, 1);
insert into Item values (1, 3, 2.50, 1);

insert into Item values (2, 4, 10.00, 1);
insert into Item values (2, 5, 8.00, 1);
insert into Item values (2, 6, 20.00, 1);

insert into Item values (3, 7, 5.00, 1);
insert into Item values (3, 8, 2.00, 1);
insert into Item values (3, 12, 3.50, 1);
  • 1

    Now it’s much simpler. Thank you so much for your help, brother.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.