Reto 1: Top 5 clientes por monto total
Solución
SELECT TOP 5 c.nombre AS cliente, SUM(v.total) AS monto_total
FROM clientes c
INNER JOIN ventas v ON v.id_cliente = c.id_cliente
GROUP BY c.nombre
ORDER BY monto_total DESC;Construye reportes reales usando JOIN + GROUP BY + HAVING (SQL Server).
SELECT TOP 5 c.nombre AS cliente, SUM(v.total) AS monto_total
FROM clientes c
INNER JOIN ventas v ON v.id_cliente = c.id_cliente
GROUP BY c.nombre
ORDER BY monto_total DESC;SELECT c.id_cliente, c.nombre
FROM clientes c
LEFT JOIN ventas v ON v.id_cliente = c.id_cliente
WHERE v.id_venta IS NULL;SELECT c.nombre AS cliente,
COUNT(v.id_venta) AS total_ventas,
SUM(v.total) AS monto_total
FROM clientes c
INNER JOIN ventas v ON v.id_cliente = c.id_cliente
GROUP BY c.nombre
ORDER BY monto_total DESC;SELECT c.nombre AS cliente, SUM(v.total) AS monto_total
FROM clientes c
INNER JOIN ventas v ON v.id_cliente = c.id_cliente
GROUP BY c.nombre
HAVING SUM(v.total) >= 500
ORDER BY monto_total DESC;Refuerza con ejercicios y realiza la evaluación del módulo.