-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomer_analysis.sql
More file actions
102 lines (92 loc) · 2.38 KB
/
Copy pathcustomer_analysis.sql
File metadata and controls
102 lines (92 loc) · 2.38 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
-- ================================================
-- SQL Customer Analysis Project
-- Author: nmujtaba01-debug
-- GitHub: github.com/nmujtaba01-debug
-- Platform: W3Schools Northwind Database
-- Description: Real business analysis using SQL
-- ================================================
-- Q1: Total number of customers
SELECT COUNT(*) AS Total_customers
FROM Customers;
-- Q2: Top 5 countries with most customers
SELECT TOP 5 Country,
COUNT(*) AS Total_customers
FROM Customers
GROUP BY Country
ORDER BY Total_customers DESC;
-- Q3: European countries grouped together
SELECT Country,
COUNT(*) AS Total_customers
FROM Customers
WHERE Country = 'Germany'
OR Country = 'France'
OR Country = 'UK'
OR Country = 'Spain'
OR Country = 'Italy'
GROUP BY Country
ORDER BY Total_customers DESC;
-- Q4: Cities with more than 2 customers
SELECT Country,
COUNT(*) AS Total_customers
FROM Customers
GROUP BY Country
HAVING COUNT(*) > 2
ORDER BY Total_customers DESC;
-- Q5: Customers with names starting A to D
SELECT CustomerName
FROM Customers
WHERE CustomerName LIKE 'A%'
OR CustomerName LIKE 'B%'
OR CustomerName LIKE 'C%'
OR CustomerName LIKE 'D%'
ORDER BY CustomerName ASC;
-- Q6: Customers with more than 3 orders
SELECT Customers.CustomerName,
COUNT(*) AS Total_orders
FROM Customers
JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
GROUP BY Customers.CustomerName
HAVING COUNT(*) > 3
ORDER BY Total_orders DESC;
-- Q7: Customers who ordered in 1997
SELECT Customers.CustomerName,
Orders.OrderDate
FROM Customers
JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.OrderDate LIKE '%1997%'
ORDER BY CustomerName DESC;
-- Q8: Orders placed each year
SELECT '1996' AS Year,
COUNT(*) AS Total_orders
FROM Orders
WHERE OrderDate LIKE '1996%'
UNION
SELECT '1997' AS Year,
COUNT(*) AS Total_orders
FROM Orders
WHERE OrderDate LIKE '1997%'
UNION
SELECT '1998' AS Year,
COUNT(*) AS Total_orders
FROM Orders
WHERE OrderDate LIKE '1998%'
ORDER BY Year ASC;
-- Q9: Shipper with most deliveries
SELECT Shippers.ShipperName,
COUNT(*) AS Total_orders
FROM Shippers
JOIN Orders
ON Shippers.ShipperID = Orders.ShipperID
GROUP BY ShipperName
ORDER BY COUNT(*) DESC;
-- Q10: German customers order count
SELECT Customers.CustomerName,
COUNT(*) AS Total_orders
FROM Customers
JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
WHERE Customers.Country = 'Germany'
GROUP BY CustomerName
ORDER BY Total_orders DESC;