-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.sql
More file actions
55 lines (42 loc) · 1006 Bytes
/
queries.sql
File metadata and controls
55 lines (42 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-- =========================
-- BASIC DATA QUERIES
-- =========================
-- Show all clients
SELECT * FROM clientes;
-- Show all orders
SELECT * FROM ordenes;
-- =========================
-- JOIN QUERIES
-- =========================
-- Combine clients with their orders
SELECT
c.Nombre_Cliente,
c.ApellidoP_Cliente,
o.Nombre_Producto,
o.Cantidad_Producto,
o.Precio_Producto
FROM clientes c
INNER JOIN ordenes o
ON c.Id_Cliente = o.Id_Cliente;
-- =========================
-- AGGREGATION QUERIES
-- =========================
-- Count total orders per client
SELECT
Id_Cliente,
COUNT(*) AS total_ordenes
FROM ordenes
GROUP BY Id_Cliente;
-- Calculate total money spent per client
SELECT
Id_Cliente,
SUM(Cantidad_Producto * Precio_Producto) AS total_gastado
FROM ordenes
GROUP BY Id_Cliente;
-- =========================
-- DATE FILTER
-- =========================
-- Show orders from a specific year
SELECT *
FROM ordenes
WHERE YEAR(Fecha) = 2024;