-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_data_manipulation.sql
More file actions
48 lines (42 loc) · 1.13 KB
/
02_data_manipulation.sql
File metadata and controls
48 lines (42 loc) · 1.13 KB
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
-- o = Orders, c = Customers, p = Products
-- 1. All customers from Nigeria
SELECT *
FROM Customers
WHERE Country = 'Nigeria';
-- 2. Show all products ordered, including customer and product name
SELECT
o.OrderID,
c.Name AS CustomerName,
p.ProductName,
o.Quantity,
o.OrderDate
FROM Orders AS o
INNER JOIN Customers AS c ON o.CustomerID = c.CustomerID
INNER JOIN Products AS p ON o.ProductID = p.ProductID;
-- 3. Total amount spent by each customer
SELECT
c.Name,
SUM(o.Quantity * p.Price) AS TotalSpent
FROM Orders AS o
INNER JOIN Customers AS c ON o.CustomerID = c.CustomerID
INNER JOIN Products AS p ON o.ProductID = p.ProductID
GROUP BY c.Name
ORDER BY TotalSpent DESC;
-- 4. Orders placed after April 2024
SELECT *
FROM Orders
WHERE OrderDate > '2024-04-30';
-- 5. Count orders per customer
SELECT
c.Name,
COUNT(o.OrderID) AS OrderCount
FROM Orders AS o
INNER JOIN Customers AS c ON o.CustomerID = c.CustomerID
GROUP BY c.Name;
-- 6. Customers who have NOT placed any orders
SELECT
c.Name,
o.OrderID
FROM Customers AS c
LEFT JOIN Orders AS o ON c.CustomerID = o.CustomerID
WHERE o.OrderID IS NULL;