Sunteți pe pagina 1din 10

INFORME NRO.

: 5
CONSULTAS MULTITABLAS EN SQL
En la base de datos Venta creamos una tabla con ProductoNuevo, con
los campos codigoProducto, Descripcion y PreUnt de manera grfica o
manualmente.
Tabla Producto
ProductoNuevo

Tabla

--UNA POR LOS SIGUIENTES CAMPOS DE LA TABLA PRODUCTO Y PRODUCTONUEVO


select * from producto
where DesPro='memoria 8GB' union
select * from ProductoNuevo
where Descripcion='Memoria 8GB'
go

/* Unir las tablas por el precio unitario donde el cual debe ser mayor a
20 soles; por el campo
Descripcion de "Teclado" de la tabla ProductoNuevo*/
select * from producto
where PreUnt >= 20 union

select * from ProductoNuevo


where Descripcion ='TECLADO'
go

/*Unir las tablas por el precio unitario por el cual debe


ser mayor a 100 soles;
por el campo
Descripcion "Parlantes" de la tabla ProductoNuevo*/
Select * from producto
where PreUnt >100 union
select * from ProductoNuevo
where Descripcion='PARLANTES'
go

Select * from producto


where DesPro ='procesador' union
select * from ProductoNuevo
go

--uso el UNION ALL, muestra todos los registros incluso repetidos


select * from producto
where DesPro = 'procesador' union all
select * from ProductoNuevo
go

Luego hacemos uso del INNER JOIN.


Para ello insertamos los registros para la tabla Factura de manera
grafica.

--INNER JOIN
select NomCli,DisCli,CodFac,FecFac
from cliente
inner join
factura on cliente.CodCli=factura.CodCli
go

Select NomCli,DisCli,CodFac,FecFac, cliente.CodCli


from cliente
inner join
factura
on cliente.CodCli=factura.CodCli

go

Ahora realizamos el uso de un ALIAS


/* cuando no queremos escribir el nombre de la tabla, podemos suprimir por una
letra y en ese caso hacemos uso de un ALIAS */
select a.CodCli, a.NomCli, b.CodFac, b.FecFac
from cliente a
inner join
factura b
on a.CodCli=b.CodCli
go

Select x1.CodFac, x1.FecFac, x2.CodPro, x2.CanPro, x2.SubPro


from factura x1
inner join
factura_detalle x2
on x1.CodFac=x2.CodFac
go

--Ahora quiero que me muestre solo el codigo de factura, es decir, de los que tengan F02
select x1.CodFac, x1.FecFac, x2.CodPro, x2.CanPro, x2.SubPro
from factura x1
inner join
factura_detalle x2
on x1.CodFac=x2.CodFac
where x1.CodFac='F02'
go

/*Uso de right join: Mostrar en el lado derecho los campos Codigo,


nombre, Telefono, Distrito
en el lado derecho de la tabla resultante, seguido de Codigo factura,
codigo cliente, Fecha de factura y Total facturacion */
select a.*, b.*
from cliente a
right join
factura b

on a.CodCli=b.CodCli
go

/*En el lado izquierdo se mostrara los que no tienen ningun


detalle porque las filas de la primera tabla y la segunda tabla no tienen
los mismos valores, es decir en cuanto a datos*/
select a.*, b.*
from cliente a
left join
factura b
on a.CodCli= b.CodCli
go

--CUANDO SE DESEA PONER UN NOMBRE DIFERENTE A LA COLUMNA, no se altera la


estructura solo es para mostrar
select CodCli CODIGO, NomCli NOMBRES

from cliente
go

--TAMBIEN SE PUEDE REPRESENTAR CON "AS"


select CodCli AS CODIGO, TelCli AS TELEFONO
from cliente
go

--Para nombres compuestos


select CodCli AS CODIGO, NomCli AS "NOMBRE DEL CLIENTE"
from cliente
go

--HACIENDO ALGUNAS OPERACIONES... SE PUEDEN HACER OPERACIONES DE CALCULO


select 5+15 AS SUMA, 3*2 AS MULT
go

--SI SE MULTIPLICA UN VALOR


select CodPro, DesPro, PreUnt, PreUnt* 1.10 as CALCULO
from producto
go

/*HACIENDO USO DE FUNCIONES ESCALARES: SE PUEDE REALIZAR SUMA, HALLAR EL


MAXIMO,
EL MINIMO Y EL PROMEDIO */
select SUM(PreUnt) as SUMA, MAX(PreUnt) AS MAXIMO,MIN(PreUnt) AS MINIMO,
AVG(PreUnt) AS PROMEDIO
from producto

go

--mostrar el numero de registro de la tabla ProductoNuevo


select count(*)
from ProductoNuevo
go

/*mostrar el numero de registro de la tabla ProductoNuevo, pero debe mostrarse


con un titulo Total_Mouse*/
select count(*) as Total_Mouse
from ProductoNuevo
where Descripcion = 'Mouse'
go

--Crear la tabla EMPLEADO


create table EMPLEADO
(
codigo char(3) not null,
nombres varchar(30) null,
apellidos varchar(30) null,
fecha datetime null,
sueldo decimal(9,2) null
)
go

Luego de haber creado la tabla ingresamos los registros:

--getdate

select GETDATE() as 'Fecha_Nacimiento' from EMPLEADO


go

1.Hacemos una consulta entre los nacidos 1980 y 1997

select * from EMPLEADO


where fecha>='1980' and fecha<='1998'
go

S-ar putea să vă placă și