ID | Nome | Supervisor_ID | Nível |
---|---|---|---|
1 | João CEO | NULL | 1 |
2 | Maria | 1 | 2 |
3 | Pedro | 1 | 2 |
4 | Ana | 2 | 3 |
5 | Carlos | 2 | 3 |
6 | Fernanda | 3 | 3 |
7 | Roberto | 3 | 3 |
8 | Juliana | 4 | 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH vendas_2023 AS ( | |
SELECT * FROM vendas WHERE YEAR(data_venda) = 2023 | |
) | |
SELECT produto_id, SUM(valor) AS receita_total | |
FROM vendas_2023 | |
GROUP BY produto_id; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH RECURSIVE hierarquia_empregados AS ( | |
-- Caso base: Seleciona o CEO | |
SELECT id, nome, supervisor_id, 1 AS nivel | |
FROM empregados | |
WHERE supervisor_id IS NULL | |
UNION ALL | |
-- Caso recursivo: Seleciona os subordinados | |
SELECT e.id, e.nome, e.supervisor_id, h.nivel + 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH clientes_vips AS ( | |
SELECT c.cliente_id, c.nome, SUM(v.valor) AS total_gasto | |
FROM clientes c | |
JOIN vendas v ON c.cliente_id = v.cliente_id | |
GROUP BY c.cliente_id, c.nome | |
HAVING SUM(v.valor) > 5000 | |
) | |
SELECT * FROM clientes_vips; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH vendas_por_produto AS ( | |
SELECT produto_id, SUM(valor) AS receita_total | |
FROM vendas | |
GROUP BY produto_id | |
) | |
SELECT * FROM vendas_por_produto; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH nome_da_cte AS ( | |
-- Consulta SQL | |
) | |
SELECT * FROM nome_da_cte; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT c.Nome, SUM(v.Valor) AS Total_Vendas, SUM(d.Valor) AS Total_Devolucoes | |
FROM Vendas v | |
JOIN Cliente c ON v.Cliente_ID = c.Cliente_ID | |
LEFT JOIN Devolucoes d ON v.Cliente_ID = d.Cliente_ID | |
GROUP BY c.Nome; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT c.Nome, ci.Nome AS Cidade, SUM(v.Valor) AS Total_Gasto | |
FROM Vendas v | |
JOIN Cliente c ON v.Cliente_ID = c.Cliente_ID | |
JOIN Cidade ci ON c.Cidade_ID = ci.Cidade_ID | |
GROUP BY c.Nome, ci.Nome; |
Cliente_ID | Nome | Cidade_ID |
---|---|---|
101 | João Silva | 201 |
102 | Maria Souza | 202 |
Cidade_ID | Nome | Estado |
---|---|---|
201 | São Paulo | SP |
202 | Rio de Janeiro | RJ |