SELECT C.Nombre AS Cliente, M.Nombre AS Modelo, V.FechaVenta,
V.CantidadFROM VENTA VINNER JOIN CLIENTE C ON V.IdCliente = C.IdClienteINNER JOIN VEHICULO VH ON V.IdVehiculo = VH.IdVehiculoINNER JOIN MODELO M ON VH.IdModelo = M.IdModelo;
WHERE Precio = (SELECT MAX(Precio) FROM VEHICULO);
- 7. Consultas con funciones agregadas
SELECT AVG(Precio) AS PrecioPromedio, SUM(Stock) AS TotalStock,
COUNT(IdVehiculo) AS TotalVehiculos
FROM VEHICULO;
-- 8. Consultas con WHERE y HAVING, utilizando ORDER BY y GROUP
BY
SELECT M.Nombre AS Marca, COUNT(VH.IdVehiculo) AS
TotalVehiculos, SUM(VH.Stock) AS StockTotalFROM MARCA MINNER JOIN MODELO MO ON M.IdMarca= MO.IdMarcaINNER JOIN VEHICULO VH ON MO.IdModelo = VH.IdModeloGROUP BY M.NombreHAVING SUM(VH.Stock) > 5ORDER BY StockTotal DESC;
-- 9. Procedimiento almacenado
CREATE PROCEDURE SP_VEHICULOS_DISPONIBLESAS
BEGIN SELECT M.Nombre AS Marca, MO.Nombre AS Modelo,
VH.Color, VH.Stock FROM VEHICULO VH INNER JOIN MODELO MO ON VH.IdModelo = MO.IdModel INNER JOIN MARCA M ON MO.IdMarca = M.IdMarc WHERE VH.Stock > 0
ORDER BY VH.Stock DESC;END;-- Ejecutar procedimiento
almacenadoEXEC SP_VEHICULOS_DISPONIBLES;
- 10. Vista: Crear una vista de ventas detalladas
CREATE VIEW VW_DETALLE_VENTAS ASSELECT V.IdVenta, C.Nombre AS Cliente, C.Apellido, M.Nombre AS Marca, MO.Nombre AS Modelo, VH.Color, V.Cantidad, V.FechaVentaFROM VENTA VINNER JOIN CLIENTE C ON V.IdCliente = C.IdClienteINNER JOIN VEHICULO VH ON V.IdVehiculo = VH.IdVehiculoINNER JOIN MODELO MO ON VH.IdModelo = MO.IdModeloINNER JOIN MARCA M ON MO.IdMarca = M.IdMarca;