-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL JOINS.sql
More file actions
147 lines (121 loc) · 3 KB
/
SQL JOINS.sql
File metadata and controls
147 lines (121 loc) · 3 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Question 1. Retrieve all customers who have placed at least one order.
CREATE DATABASE sql_joins;
use sql_joins;
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
City VARCHAR(50)
);
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
Amount INT
);
INSERT INTO Customers VALUES
(1, 'John Smith', 'New York'),
(2, 'Mary Johnson', 'Chicago'),
(3, 'Peter Adams', 'Los Angeles'),
(4, 'Robert White', 'Houston'),
(5, 'Nancy Miller', 'Miami');
INSERT INTO Orders VALUES
(101, 1, '2024-10-01', 250),
(102, 2, '2024-10-05', 300),
(103, 1, '2024-10-07', 150),
(104, 3, '2024-10-10', 450);
SELECT DISTINCT c.CustomerID, c.CustomerName, c.City
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID;
#Question 2. Retrieve all customers and their orders, including customers who have not placed any orders.
SELECT
c.CustomerID,
c.CustomerName,
c.City,
o.OrderID,
o.OrderDate,
o.Amount
FROM Customers c
LEFT JOIN Orders o
ON c.CustomerID = o.CustomerID;
#Question 3. Retrieve all orders and their corresponding customers, including orders placed by unknown
#customers.
SELECT
o.OrderID,
o.CustomerID,
o.OrderDate,
o.Amount,
c.CustomerName,
c.City
FROM Customers c
RIGHT JOIN Orders o
ON c.CustomerID = o.CustomerID;
#Question 4. Display all customers and orders, whether matched or not.
SELECT
c.CustomerID,
c.CustomerName,
o.OrderID,
o.OrderDate,
o.Amount
FROM Customers c
LEFT JOIN Orders o
ON c.CustomerID = o.CustomerID
UNION
SELECT
c.CustomerID,
c.CustomerName,
o.OrderID,
o.OrderDate,
o.Amount
FROM Customers c
RIGHT JOIN Orders o
ON c.CustomerID = o.CustomerID;
#Question 5. Find customers who have not placed any orders.
SELECT
c.CustomerID,
c.CustomerName,
c.City
FROM Customers c
LEFT JOIN Orders o
ON c.CustomerID = o.CustomerID
WHERE o.OrderID IS NULL;
#Question 6. Retrieve customers who made payments but did not place any orders.
SELECT DISTINCT
p.CustomerID,c.CustomerName
FROM Payments p
LEFT JOIN Orders o
ON p.CustomerID = o.CustomerID
LEFT JOIN Customers c
ON p.CustomerID = c.CustomerID
WHERE o.OrderID IS NULL;
#Question 7. Generate a list of all possible combinations between Customers and Orders.
SELECT
c.CustomerID,
c.CustomerName,
o.OrderID,
o.OrderDate,
o.Amount
FROM Customers c
CROSS JOIN Orders o;
# Question 8. Show all customers along with order and payment amounts in one table.
SELECT
c.CustomerID,
c.CustomerName,
o.OrderID,
o.Amount AS OrderAmount,
p.PaymentID,
p.Amount AS PaymentAmount
FROM Customers c
LEFT JOIN Orders o
ON c.CustomerID = o.CustomerID
LEFT JOIN Payments p
ON c.CustomerID = p.CustomerID;
# Question 9. Retrieve all customers who have both placed orders and made payments.
SELECT DISTINCT
c.CustomerID,
c.CustomerName
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID
INNER JOIN Payments p
ON c.CustomerID = p.CustomerID;