-
Notifications
You must be signed in to change notification settings - Fork 0
/
Group By and Having.sql
208 lines (160 loc) · 4.14 KB
/
Group By and Having.sql
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
DROP TABLE Transaction;
DROP TABLE Store;
CREATE TABLE Store
(
StoreID NUMBER,
City VARCHAR2(20) NOT NULL,
State CHAR(2) NOT NULL,
CONSTRAINT Store_PK
PRIMARY KEY (StoreID)
);
CREATE TABLE Transaction
(
StoreID NUMBER,
TransID NUMBER,
TDate DATE,
Amount NUMBER(*, 2),
CONSTRAINT DateCheck
CHECK (TDate > TO_DATE('01-Jan-2010')),
CONSTRAINT AmountCheck
CHECK (Amount > 0.00),
CONSTRAINT Transaction_PK
PRIMARY KEY (StoreID, TransID),
CONSTRAINT Transaction_FK
FOREIGN KEY (StoreID)
REFERENCES Store(StoreID)
);
INSERT INTO Store VALUES (100, 'Chicago', 'IL');
INSERT INTO Store VALUES (200, 'Chicago', 'IL');
INSERT INTO Store VALUES (300, 'Schaumburg', 'IL');
INSERT INTO Store VALUES (400, 'Boston', 'MA');
INSERT INTO Store VALUES (500, 'Boston', 'MA');
INSERT INTO Store VALUES (600, 'Portland', 'ME');
INSERT INTO Transaction VALUES(100, 1, '10-Oct-2011', 100.00);
INSERT INTO Transaction VALUES(100, 2, '11-Oct-2011', 120.00);
INSERT INTO Transaction VALUES(200, 1, '11-Oct-2011', 50.00);
INSERT INTO Transaction VALUES(200, 2, '11-Oct-2011', 70.00);
INSERT INTO Transaction VALUES(300, 1, '12-Oct-2011', 20.00);
INSERT INTO Transaction VALUES(400, 1, '10-Oct-2011', 10.00);
INSERT INTO Transaction VALUES(400, 2, '11-Oct-2011', 20.00);
INSERT INTO Transaction VALUES(400, 3, '12-Oct-2011', 30.00);
INSERT INTO Transaction VALUES(500, 1, '10-Oct-2011', 10.00);
INSERT INTO Transaction VALUES(500, 2, '10-Oct-2011', 110.00);
INSERT INTO Transaction VALUES(500, 3, '11-Oct-2011', 90.00);
INSERT INTO Transaction VALUES(600, 1, '11-Oct-2011', 300.00);
INSERT INTO Transaction VALUES(600, 2, NULL, 300.00);
SELECT *
FROM Store;
SELECT *
FROM Transaction;
SELECT *
FROM Transaction
WHERE TDate IS NULL;
SELECT DISTINCT City
FROM Store;
SELECT DISTINCT City, State
FROM Store;
SELECT *
FROM Store
WHERE StoreID >= 500;
SELECT *
FROM Store
WHERE StoreID != 600;
SELECT *
FROM Store
WHERE State = 'IL';
SELECT StoreID, Amount, TDate
FROM Transaction
WHERE TDate >= TO_DATE('11-Oct-2011');
SELECT *
FROM Store
WHERE State LIKE '__';
SELECT *
FROM Store
WHERE City LIKE '_h%';
SELECT DISTINCT City, State
FROM Store
WHERE State = 'IL' OR State = 'ME';
SELECT *
FROM Transaction
WHERE Amount < 50 OR Amount > 100;
SELECT *
FROM Transaction
WHERE Amount > 50 AND Amount < 100;
SELECT *
FROM Transaction
WHERE (TDate = TO_DATE('11-Oct-2011') OR Amount > 100)
AND Amount < 150;
SELECT TDate, Amount
FROM Transaction
WHERE (Amount >= 20 AND Amount < 35)
OR (Amount > 100 AND Amount <= 120);
SELECT *
FROM Transaction
WHERE (Amount >= 20 AND Amount < 35)
OR (Amount > 100 AND Amount <= 120)
ORDER BY Amount ASC;
SELECT *
FROM Store
ORDER BY State;
SELECT *
FROM Store
ORDER BY State, City DESC;
SELECT COUNT(*)
FROM Store;
SELECT COUNT(*)
FROM Transaction;
SELECT COUNT(State)
FROM Store;
SELECT AVG(Amount)
FROM Transaction;
SELECT SUM(Amount) / COUNT(Amount)
FROM Transaction;
SELECT SUM(Amount), COUNT(StoreID), COUNT(Amount)
FROM Transaction;
/*
Below will generate an error
SELECT SUM(Amount), StoreID
FROM Transaction;
*/
SELECT COUNT(*), COUNT(Amount), COUNT(TDate)
FROM Transaction;
SELECT COUNT(*) - COUNT(TDate), COUNT(*) - COUNT(Amount)
FROM Transaction;
SELECT COUNT(*), COUNT(State), COUNT(DISTINCT State)
FROM Store;
SELECT SUM(Amount)
FROM Transaction
WHERE Amount <= 20 OR Amount >= 100;
SELECT State, COUNT(StoreID)
FROM Store
GROUP BY State;
SELECT State, COUNT(DISTINCT City)
FROM Store
GROUP BY State;
SELECT State, City, COUNT(StoreID)
FROM Store
GROUP BY State, City
ORDER BY COUNT(StoreID);
SELECT TDate, SUM(Amount)
FROM Transaction
GROUP BY TDate;
SELECT StoreID, MAX(Amount)
FROM Transaction
GROUP BY StoreID
ORDER BY StoreID;
SELECT MAX(Amount)
FROM Transaction
GROUP BY StoreID;
SELECT StoreID, 0.9 * MIN(Amount)
FROM Transaction
GROUP BY StoreID
ORDER BY MAX(Amount);
SELECT MAX(Amount)
FROM Transaction
GROUP BY StoreID
HAVING MAX(Amount) > 100;
SELECT State, COUNT(City)
FROM Store
GROUP BY State
HAVING COUNT(City) >= 3;