~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
drop table if exists t1,t2;
2
set @sav_dpi= @@div_precision_increment;
3
set div_precision_increment= 5;
4
show variables like 'div_precision_increment';
5
Variable_name	Value
6
div_precision_increment	5
7
create table t1 (grp int, a bigint unsigned, c char(10) not null);
8
insert into t1 values (1,1,"a");
9
insert into t1 values (2,2,"b");
10
insert into t1 values (2,3,"c");
11
insert into t1 values (3,4,"E");
12
insert into t1 values (3,5,"C");
13
insert into t1 values (3,6,"D");
14
select a,c,sum(a) from t1 group by a;
15
a	c	sum(a)
16
1	a	1
17
2	b	2
18
3	c	3
19
4	E	4
20
5	C	5
21
6	D	6
22
select a,c,sum(a) from t1 where a > 10 group by a;
23
a	c	sum(a)
24
select sum(a) from t1 where a > 10;
25
sum(a)
26
NULL
27
select a from t1 order by rand(10);
28
a
29
2
30
6
31
1
32
3
33
5
34
4
35
select distinct a from t1 order by rand(10);
36
a
37
2
38
6
39
1
40
3
41
5
42
4
43
select count(distinct a),count(distinct grp) from t1;
44
count(distinct a)	count(distinct grp)
45
6	3
46
insert into t1 values (null,null,'');
47
select count(distinct a),count(distinct grp) from t1;
48
count(distinct a)	count(distinct grp)
49
6	3
50
select sum(all a),count(all a),avg(all a),std(all a),variance(all a),bit_or(all a),bit_and(all a),min(all a),max(all a),min(all c),max(all c) from t1;
51
sum(all a)	count(all a)	avg(all a)	std(all a)	variance(all a)	bit_or(all a)	bit_and(all a)	min(all a)	max(all a)	min(all c)	max(all c)
52
21	6	3.50000	1.70783	2.91667	7	0	1	6		E
53
select grp, sum(a),count(a),avg(a),std(a),variance(a),bit_or(a),bit_and(a),min(a),max(a),min(c),max(c) from t1 group by grp;
54
grp	sum(a)	count(a)	avg(a)	std(a)	variance(a)	bit_or(a)	bit_and(a)	min(a)	max(a)	min(c)	max(c)
55
NULL	NULL	0	NULL	NULL	NULL	0	18446744073709551615	NULL	NULL		
56
1	1	1	1.00000	0.00000	0.00000	1	1	1	1	a	a
57
2	5	2	2.50000	0.50000	0.25000	3	2	2	3	b	c
58
3	15	3	5.00000	0.81650	0.66667	7	4	4	6	C	E
59
select grp, sum(a)+count(a)+avg(a)+std(a)+variance(a)+bit_or(a)+bit_and(a)+min(a)+max(a)+min(c)+max(c) as sum from t1 group by grp;
60
grp	sum
61
NULL	NULL
62
1	7
63
2	20.25
64
3	45.48316324759439
65
create table t2 (grp int, a bigint unsigned, c char(10));
66
insert into t2 select grp,max(a)+max(grp),max(c) from t1 group by grp;
67
replace into t2 select grp, a, c from t1 limit 2,1;
68
select * from t2;
69
grp	a	c
70
NULL	NULL	
71
1	2	a
72
2	5	c
73
3	9	E
74
2	3	c
75
drop table t1,t2;
76
CREATE TABLE t1 (id int(11),value1 float(10,2));
77
INSERT INTO t1 VALUES (1,0.00),(1,1.00), (1,2.00), (2,10.00), (2,11.00), (2,12.00);
78
CREATE TABLE t2 (id int(11),name char(20));
79
INSERT INTO t2 VALUES (1,'Set One'),(2,'Set Two');
80
select id, avg(value1), std(value1), variance(value1) from t1 group by id;
81
id	avg(value1)	std(value1)	variance(value1)
82
1	1.0000000	0.816497	0.666667
83
2	11.0000000	0.816497	0.666667
84
select name, avg(value1), std(value1), variance(value1) from t1, t2 where t1.id = t2.id group by t1.id;
85
name	avg(value1)	std(value1)	variance(value1)
86
Set One	1.0000000	0.816497	0.666667
87
Set Two	11.0000000	0.816497	0.666667
88
drop table t1,t2;
89
create table t1 (id int not null);
90
create table t2 (id int not null,rating int null);
91
insert into t1 values(1),(2),(3);
92
insert into t2 values(1, 3),(2, NULL),(2, NULL),(3, 2),(3, NULL);
93
select t1.id, avg(rating) from t1 left join t2 on ( t1.id = t2.id ) group by t1.id;
94
id	avg(rating)
95
1	3.00000
96
2	NULL
97
3	2.00000
98
select sql_small_result t2.id, avg(rating) from t2 group by t2.id;
99
id	avg(rating)
100
1	3.00000
101
2	NULL
102
3	2.00000
103
select sql_big_result t2.id, avg(rating) from t2 group by t2.id;
104
id	avg(rating)
105
1	3.00000
106
2	NULL
107
3	2.00000
108
select sql_small_result t2.id, avg(rating+0.0e0) from t2 group by t2.id;
109
id	avg(rating+0.0e0)
110
1	3
111
2	NULL
112
3	2
113
select sql_big_result t2.id, avg(rating+0.0e0) from t2 group by t2.id;
114
id	avg(rating+0.0e0)
115
1	3
116
2	NULL
117
3	2
118
drop table t1,t2;
119
create table t1 (a smallint(6) primary key, c char(10), b text);
120
INSERT INTO t1 VALUES (1,'1','1');
121
INSERT INTO t1 VALUES (2,'2','2');
122
INSERT INTO t1 VALUES (4,'4','4');
123
select count(*) from t1;
124
count(*)
125
3
126
select count(*) from t1 where a = 1;
127
count(*)
128
1
129
select count(*) from t1 where a = 100;
130
count(*)
131
0
132
select count(*) from t1 where a >= 10;
133
count(*)
134
0
135
select count(a) from t1 where a = 1;
136
count(a)
137
1
138
select count(a) from t1 where a = 100;
139
count(a)
140
0
141
select count(a) from t1 where a >= 10;
142
count(a)
143
0
144
select count(b) from t1 where b >= 2;
145
count(b)
146
2
147
select count(b) from t1 where b >= 10;
148
count(b)
149
0
150
select count(c) from t1 where c = 10;
151
count(c)
152
0
153
drop table t1;
154
CREATE TABLE t1 (d DATETIME, i INT);
155
INSERT INTO t1 VALUES (NOW(), 1);
156
SELECT COUNT(i), i, COUNT(i)*i FROM t1 GROUP BY i;
157
COUNT(i)	i	COUNT(i)*i 
158
1	1	1
159
SELECT COUNT(i), (i+0), COUNT(i)*(i+0) FROM t1 GROUP BY i;
160
COUNT(i)	(i+0)	COUNT(i)*(i+0)
161
1	1	1
162
DROP TABLE t1;
163
create table t1 (
164
num float(5,2),
165
user char(20)
166
);
167
insert into t1 values (10.3,'nem'),(20.53,'monty'),(30.23,'sinisa');
168
insert into t1 values (30.13,'nem'),(20.98,'monty'),(10.45,'sinisa');
169
insert into t1 values (5.2,'nem'),(8.64,'monty'),(11.12,'sinisa');
170
select sum(num) from t1;
171
sum(num)
172
147.58
173
select sum(num) from t1 group by user;
174
sum(num)
175
50.15
176
45.63
177
51.80
178
drop table t1;
179
create table t1 (a1 int, a2 char(3), key k1(a1), key k2(a2));
180
insert into t1 values(10,'aaa'), (10,null), (10,'bbb'), (20,'zzz');
181
create table t2(a1 char(3), a2 int, a3 real, key k1(a1), key k2(a2, a1));
182
select * from t1;
183
a1	a2
184
10	aaa
185
10	NULL
186
10	bbb
187
20	zzz
188
select min(a2) from t1;
189
min(a2)
190
aaa
191
select max(t1.a1), max(t2.a2) from t1, t2;
192
max(t1.a1)	max(t2.a2)
193
NULL	NULL
194
select max(t1.a1) from t1, t2;
195
max(t1.a1)
196
NULL
197
select max(t2.a2), max(t1.a1) from t1, t2;
198
max(t2.a2)	max(t1.a1)
199
NULL	NULL
200
explain select min(a2) from t1;
201
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
202
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
203
explain select max(t1.a1), max(t2.a2) from t1, t2;
204
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
205
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
206
insert into t2 values('AAA', 10, 0.5);
207
insert into t2 values('BBB', 20, 1.0);
208
select t1.a1, t1.a2, t2.a1, t2.a2 from t1,t2;
209
a1	a2	a1	a2
210
10	aaa	AAA	10
211
10	aaa	BBB	20
212
10	NULL	AAA	10
213
10	NULL	BBB	20
214
10	bbb	AAA	10
215
10	bbb	BBB	20
216
20	zzz	AAA	10
217
20	zzz	BBB	20
218
select max(t1.a1), max(t2.a1) from t1, t2 where t2.a2=9;
219
max(t1.a1)	max(t2.a1)
220
NULL	NULL
221
select max(t2.a1), max(t1.a1) from t1, t2 where t2.a2=9;
222
max(t2.a1)	max(t1.a1)
223
NULL	NULL
224
select t1.a1, t1.a2, t2.a1, t2.a2 from t1 left outer join t2 on t1.a1=10;
225
a1	a2	a1	a2
226
10	aaa	AAA	10
227
10	aaa	BBB	20
228
10	NULL	AAA	10
229
10	NULL	BBB	20
230
10	bbb	AAA	10
231
10	bbb	BBB	20
232
20	zzz	NULL	NULL
233
select max(t1.a2) from t1 left outer join t2 on t1.a1=10;
234
max(t1.a2)
235
zzz
236
select max(t2.a1) from t2 left outer join t1 on t2.a2=10 where t2.a2=20;
237
max(t2.a1)
238
BBB
239
select max(t2.a1) from t2 left outer join t1 on t2.a2=10 where t2.a2=10;
240
max(t2.a1)
241
AAA
242
select max(t2.a1) from t1 left outer join t2 on t1.a2=t2.a1 and 1=0 where t2.a1='AAA';
243
max(t2.a1)
244
NULL
245
select max(t1.a2),max(t2.a1) from t1 left outer join t2 on t1.a1=10;
246
max(t1.a2)	max(t2.a1)
247
zzz	BBB
248
drop table t1,t2;
249
CREATE TABLE t1 (a int, b int);
250
select count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1;
251
count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
252
0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
253
select a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
254
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
255
insert into t1 values (1,null);
256
select a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
257
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
258
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
259
insert into t1 values (1,null);
260
insert into t1 values (2,null);
261
select a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
262
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
263
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
264
2	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
265
select SQL_BIG_RESULT a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
266
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
267
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
268
2	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
269
insert into t1 values (2,1);
270
select a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
271
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
272
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
273
2	1	1	1.00000	0.00000	1	1	1	1
274
select SQL_BIG_RESULT a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
275
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
276
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
277
2	1	1	1.00000	0.00000	1	1	1	1
278
insert into t1 values (3,1);
279
select a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
280
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
281
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
282
2	1	1	1.00000	0.00000	1	1	1	1
283
3	1	1	1.00000	0.00000	1	1	1	1
284
select SQL_BIG_RESULT a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b), bit_xor(b) from t1 group by a;
285
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)	bit_xor(b)
286
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0	0
287
2	1	1	1.00000	0.00000	1	1	1	1	1
288
3	1	1	1.00000	0.00000	1	1	1	1	1
289
explain extended select SQL_BIG_RESULT a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b), bit_xor(b) from t1 group by a;
290
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
291
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	5	100.00	Using filesort
292
Warnings:
293
Note	1003	select sql_big_result "test"."t1"."a" AS "a",count("test"."t1"."b") AS "count(b)",sum("test"."t1"."b") AS "sum(b)",avg("test"."t1"."b") AS "avg(b)",std("test"."t1"."b") AS "std(b)",min("test"."t1"."b") AS "min(b)",max("test"."t1"."b") AS "max(b)",bit_and("test"."t1"."b") AS "bit_and(b)",bit_or("test"."t1"."b") AS "bit_or(b)",bit_xor("test"."t1"."b") AS "bit_xor(b)" from "test"."t1" group by "test"."t1"."a"
294
drop table t1;
295
create table t1 (col int);
296
insert into t1 values (-1), (-2), (-3);
297
select bit_and(col), bit_or(col) from t1;
298
bit_and(col)	bit_or(col)
299
18446744073709551612	18446744073709551615
300
select SQL_BIG_RESULT bit_and(col), bit_or(col) from t1 group by col;
301
bit_and(col)	bit_or(col)
302
18446744073709551613	18446744073709551613
303
18446744073709551614	18446744073709551614
304
18446744073709551615	18446744073709551615
305
drop table t1;
306
create table t1 (a int);
307
select avg(2) from t1;
308
avg(2)
309
NULL
310
drop table t1;
311
create table t1(
312
a1 char(3) primary key,
313
a2 smallint,
314
a3 char(3),
315
a4 real,
316
a5 date,
317
key k1(a2,a3),
318
key k2(a4 desc,a1),
319
key k3(a5,a1)
320
);
321
create table t2(
322
a1 char(3) primary key,
323
a2 char(17),
324
a3 char(2),
325
a4 char(3),
326
key k1(a3, a2),
327
key k2(a4)
328
);
329
insert into t1 values('AME',0,'SEA',0.100,date'1942-02-19');
330
insert into t1 values('HBR',1,'SEA',0.085,date'1948-03-05');
331
insert into t1 values('BOT',2,'SEA',0.085,date'1951-11-29');
332
insert into t1 values('BMC',3,'SEA',0.085,date'1958-09-08');
333
insert into t1 values('TWU',0,'LAX',0.080,date'1969-10-05');
334
insert into t1 values('BDL',0,'DEN',0.080,date'1960-11-27');
335
insert into t1 values('DTX',1,'NYC',0.080,date'1961-05-04');
336
insert into t1 values('PLS',1,'WDC',0.075,date'1949-01-02');
337
insert into t1 values('ZAJ',2,'CHI',0.075,date'1960-06-15');
338
insert into t1 values('VVV',2,'MIN',0.075,date'1959-06-28');
339
insert into t1 values('GTM',3,'DAL',0.070,date'1977-09-23');
340
insert into t1 values('SSJ',null,'CHI',null,date'1974-03-19');
341
insert into t1 values('KKK',3,'ATL',null,null);
342
insert into t1 values('XXX',null,'MIN',null,null);
343
insert into t1 values('WWW',1,'LED',null,null);
344
insert into t2 values('TKF','Seattle','WA','AME');
345
insert into t2 values('LCC','Los Angeles','CA','TWU');
346
insert into t2 values('DEN','Denver','CO','BDL');
347
insert into t2 values('SDC','San Diego','CA','TWU');
348
insert into t2 values('NOL','New Orleans','LA','GTM');
349
insert into t2 values('LAK','Los Angeles','CA','TWU');
350
insert into t2 values('AAA','AAA','AA','AME');
351
select * from t1;
352
a1	a2	a3	a4	a5
353
AME	0	SEA	0.1	1942-02-19
354
HBR	1	SEA	0.085	1948-03-05
355
BOT	2	SEA	0.085	1951-11-29
356
BMC	3	SEA	0.085	1958-09-08
357
TWU	0	LAX	0.08	1969-10-05
358
BDL	0	DEN	0.08	1960-11-27
359
DTX	1	NYC	0.08	1961-05-04
360
PLS	1	WDC	0.075	1949-01-02
361
ZAJ	2	CHI	0.075	1960-06-15
362
VVV	2	MIN	0.075	1959-06-28
363
GTM	3	DAL	0.07	1977-09-23
364
SSJ	NULL	CHI	NULL	1974-03-19
365
KKK	3	ATL	NULL	NULL
366
XXX	NULL	MIN	NULL	NULL
367
WWW	1	LED	NULL	NULL
368
select * from t2;
369
a1	a2	a3	a4
370
TKF	Seattle	WA	AME
371
LCC	Los Angeles	CA	TWU
372
DEN	Denver	CO	BDL
373
SDC	San Diego	CA	TWU
374
NOL	New Orleans	LA	GTM
375
LAK	Los Angeles	CA	TWU
376
AAA	AAA	AA	AME
377
explain 
378
select min(a1) from t1;
379
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
380
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
381
select min(a1) from t1;
382
min(a1)
383
AME
384
explain 
385
select max(a4) from t1;
386
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
387
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
388
select max(a4) from t1;
389
max(a4)
390
0.1
391
explain 
392
select min(a5), max(a5) from t1;
393
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
394
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
395
select min(a5), max(a5) from t1;
396
min(a5)	max(a5)
397
1942-02-19	1977-09-23
398
explain 
399
select min(a3) from t1 where a2 = 2;
400
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
401
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
402
select min(a3) from t1 where a2 = 2;
403
min(a3)
404
CHI
405
explain 
406
select min(a1), max(a1) from t1 where a4 = 0.080;
407
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
408
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
409
select min(a1), max(a1) from t1 where a4 = 0.080;
410
min(a1)	max(a1)
411
BDL	TWU
412
explain 
413
select min(t1.a5), max(t2.a3) from t1, t2;
414
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
415
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
416
select min(t1.a5), max(t2.a3) from t1, t2;
417
min(t1.a5)	max(t2.a3)
418
1942-02-19	WA
419
explain 
420
select min(t1.a3), max(t2.a2) from t1, t2 where t1.a2 = 0 and t2.a3 = 'CA';
421
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
422
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
423
select min(t1.a3), max(t2.a2) from t1, t2 where t1.a2 = 0 and t2.a3 = 'CA';
424
min(t1.a3)	max(t2.a2)
425
DEN	San Diego
426
explain 
427
select min(a1) from t1 where a1 > 'KKK';
428
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
429
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
430
select min(a1) from t1 where a1 > 'KKK';
431
min(a1)
432
PLS
433
explain 
434
select min(a1) from t1 where a1 >= 'KKK';
435
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
436
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
437
select min(a1) from t1 where a1 >= 'KKK';
438
min(a1)
439
KKK
440
explain 
441
select max(a3) from t1 where a2 = 2 and a3 < 'SEA';
442
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
443
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
444
select max(a3) from t1 where a2 = 2 and a3 < 'SEA';
445
max(a3)
446
MIN
447
explain 
448
select max(a5) from t1 where a5 < date'1970-01-01';
449
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
450
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
451
select max(a5) from t1 where a5 < date'1970-01-01';
452
max(a5)
453
1969-10-05
454
explain 
455
select max(a3) from t1 where a2 is null;
456
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
457
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
458
select max(a3) from t1 where a2 is null;
459
max(a3)
460
MIN
461
explain 
462
select max(a3) from t1 where a2 = 0 and a3 between 'K' and 'Q';
463
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
464
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
465
select max(a3) from t1 where a2 = 0 and a3 between 'K' and 'Q';
466
max(a3)
467
LAX
468
explain
469
select min(a1), max(a1) from t1 where a1 between 'A' and 'P';
470
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
471
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
472
select min(a1), max(a1) from t1 where a1 between 'A' and 'P';
473
min(a1)	max(a1)
474
AME	KKK
475
explain 
476
select max(a3) from t1 where a3 < 'SEA' and a2 = 2 and a3 <= 'MIN';
477
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
478
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
479
select max(a3) from t1 where a3 < 'SEA' and a2 = 2 and a3 <= 'MIN';
480
max(a3)
481
MIN
482
explain 
483
select max(a3) from t1 where a3 = 'MIN' and a2 = 2;
484
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
485
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
486
select max(a3) from t1 where a3 = 'MIN' and a2 = 2;
487
max(a3)
488
MIN
489
explain 
490
select max(a3) from t1 where a3 = 'DEN' and a2 = 2;
491
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
492
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
493
select max(a3) from t1 where a3 = 'DEN' and a2 = 2;
494
max(a3)
495
NULL
496
explain
497
select max(t1.a3), min(t2.a2) from t1, t2 where t1.a2 = 2 and t1.a3 < 'MIN' and t2.a3 = 'CA';
498
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
499
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
500
select max(t1.a3), min(t2.a2) from t1, t2 where t1.a2 = 2 and t1.a3 < 'MIN' and t2.a3 = 'CA';
501
max(t1.a3)	min(t2.a2)
502
CHI	Los Angeles
503
explain
504
select max(a3) from t1 where a2 is null and a2 = 2;
505
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
506
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
507
select max(a3) from t1 where a2 is null and a2 = 2;
508
max(a3)
509
NULL
510
explain
511
select max(a2) from t1 where a2 >= 1;
512
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
513
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
514
select max(a2) from t1 where a2 >= 1;
515
max(a2)
516
3
517
explain
518
select min(a3) from t1 where a2 = 2 and a3 < 'SEA';
519
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
521
select min(a3) from t1 where a2 = 2 and a3 < 'SEA';
522
min(a3)
523
CHI
524
explain
525
select min(a3) from t1 where a2 = 4;
526
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
527
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
528
select min(a3) from t1 where a2 = 4;
529
min(a3)
530
NULL
531
explain
532
select min(a3) from t1 where a2 = 2 and a3 > 'SEA';
533
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
534
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
535
select min(a3) from t1 where a2 = 2 and a3 > 'SEA';
536
min(a3)
537
NULL
538
explain
539
select (min(a4)+max(a4))/2 from t1;
540
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
541
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
542
select (min(a4)+max(a4))/2 from t1;
543
(min(a4)+max(a4))/2
544
0.085
545
explain
546
select min(a3) from t1 where 2 = a2;
547
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
548
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
549
select min(a3) from t1 where 2 = a2;
550
min(a3)
551
CHI
552
explain
553
select max(a3) from t1 where a2 = 2 and 'SEA' > a3;
554
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
555
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
556
select max(a3) from t1 where a2 = 2 and 'SEA' > a3;
557
max(a3)
558
MIN
559
explain
560
select max(a3) from t1 where a2 = 2 and 'SEA' < a3;
561
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
562
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
563
select max(a3) from t1 where a2 = 2 and 'SEA' < a3;
564
max(a3)
565
NULL
566
explain
567
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI';
568
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
569
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
570
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI';
571
min(a3)
572
CHI
573
explain
574
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI' and a3 < 'SEA';
575
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
576
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
577
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI' and a3 < 'SEA';
578
min(a3)
579
CHI
580
explain
581
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI' and a3 = 'MIN';
582
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
583
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
584
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI' and a3 = 'MIN';
585
min(a3)
586
MIN
587
explain
588
select min(a3) from t1 where a2 = 2 and a3 >= 'SEA' and a3 = 'MIN';
589
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
590
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
591
select min(a3) from t1 where a2 = 2 and a3 >= 'SEA' and a3 = 'MIN';
592
min(a3)
593
NULL
594
explain
595
select min(t1.a1), min(t2.a4) from t1,t2 where t1.a1 < 'KKK' and t2.a4 < 'KKK';
596
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
597
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
598
select min(t1.a1), min(t2.a4) from t1,t2 where t1.a1 < 'KKK' and t2.a4 < 'KKK';
599
min(t1.a1)	min(t2.a4)
600
AME	AME
601
explain 
602
select min(a1) from t1 where a1 > 'KKK' or a1 < 'XXX';
603
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
604
1	SIMPLE	t1	index	PRIMARY	PRIMARY	3	NULL	15	Using where; Using index
605
explain 
606
select min(a1) from t1 where a1 != 'KKK';
607
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
608
1	SIMPLE	t1	index	PRIMARY	PRIMARY	3	NULL	15	Using where; Using index
609
explain
610
select max(a3) from t1 where a2 < 2 and a3 < 'SEA';
611
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
612
1	SIMPLE	t1	range	k1	k1	3	NULL	6	Using where; Using index
613
explain
614
select max(t1.a3), min(t2.a2) from t1, t2 where t1.a2 = 2 and t1.a3 < 'MIN' and t2.a3 > 'CA';
615
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
616
1	SIMPLE	t1	range	k1	k1	7	NULL	1	Using where; Using index
617
1	SIMPLE	t2	range	k1	k1	3	NULL	4	Using where; Using index; Using join buffer
618
explain
619
select min(a4 - 0.01) from t1;
620
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
621
1	SIMPLE	t1	index	NULL	k2	12	NULL	15	Using index
622
explain
623
select max(a4 + 0.01) from t1;
624
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
625
1	SIMPLE	t1	index	NULL	k2	12	NULL	15	Using index
626
explain
627
select min(a3) from t1 where (a2 +1 ) is null;
628
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
629
1	SIMPLE	t1	index	NULL	k1	7	NULL	15	Using where; Using index
630
explain
631
select min(a3) from t1 where (a2 + 1) = 2;
632
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
633
1	SIMPLE	t1	index	NULL	k1	7	NULL	15	Using where; Using index
634
explain
635
select min(a3) from t1 where 2 = (a2 + 1);
636
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
637
1	SIMPLE	t1	index	NULL	k1	7	NULL	15	Using where; Using index
638
explain
639
select min(a2) from t1 where a2 < 2 * a2 - 8;
640
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
641
1	SIMPLE	t1	index	NULL	k1	7	NULL	15	Using where; Using index
642
explain
643
select min(a1) from t1  where a1 between a3 and 'KKK';
644
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
645
1	SIMPLE	t1	ALL	PRIMARY	NULL	NULL	NULL	15	Using where
646
explain
647
select min(a4) from t1  where (a4 + 0.01) between 0.07 and 0.08;
648
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
649
1	SIMPLE	t1	index	NULL	k2	12	NULL	15	Using where; Using index
650
explain
651
select concat(min(t1.a1),min(t2.a4)) from t1, t2 where t2.a4 <> 'AME';
652
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
653
1	SIMPLE	t2	range	k2	k2	4	NULL	6	Using where; Using index
654
1	SIMPLE	t1	index	NULL	PRIMARY	3	NULL	15	Using index; Using join buffer
655
drop table t1, t2;
656
create table t1 (a char(10));
657
insert into t1 values ('a'),('b'),('c');
658
select coercibility(max(a)) from t1;
659
coercibility(max(a))
660
2
661
drop table t1;
662
create table t1 (a char character set latin2);
663
insert into t1 values ('a'),('b');
664
select charset(max(a)), coercibility(max(a)),
665
charset(min(a)), coercibility(min(a)) from t1;
666
charset(max(a))	coercibility(max(a))	charset(min(a))	coercibility(min(a))
667
latin2	2	latin2	2
668
show create table t1;
669
Table	Create Table
670
t1	CREATE TABLE "t1" (
671
  "a" char(1) CHARACTER SET latin2
672
) ENGINE=MyISAM DEFAULT CHARSET=latin1
673
create table t2 select max(a),min(a) from t1;
674
show create table t2;
675
Table	Create Table
676
t2	CREATE TABLE "t2" (
677
  "max(a)" char(1) CHARACTER SET latin2,
678
  "min(a)" char(1) CHARACTER SET latin2
679
) ENGINE=MyISAM DEFAULT CHARSET=latin1
680
drop table t2;
681
create table t2 select concat(a) from t1;
682
show create table t2;
683
Table	Create Table
684
t2	CREATE TABLE "t2" (
685
  "concat(a)" varchar(1) CHARACTER SET latin2
686
) ENGINE=MyISAM DEFAULT CHARSET=latin1
687
drop table t2,t1;
688
create table t1 (a int);
689
insert into t1 values (1);
690
select max(a) as b from t1 having b=1;
691
b
692
1
693
select a from t1 having a=1;
694
a
695
1
696
drop table t1;
697
create table t1 (a int);
698
select variance(2) from t1;
699
variance(2)
700
NULL
701
select stddev(2) from t1;
702
stddev(2)
703
NULL
704
drop table t1;
705
create table t1 (a int);
706
insert into t1 values (1),(2);
707
SELECT COUNT(*) FROM t1;
708
COUNT(*)
709
2
710
SELECT COUNT(*) FROM t1;
711
COUNT(*)
712
2
713
SELECT COUNT(*) FROM t1;
714
COUNT(*)
715
2
716
drop table t1;
717
create table t1 (a int, primary key(a));
718
insert into t1 values (1),(2);
719
SELECT max(a) FROM t1;
720
max(a)
721
2
722
SELECT max(a) FROM t1;
723
max(a)
724
2
725
SELECT max(a) FROM t1;
726
max(a)
727
2
728
drop table t1;
729
CREATE TABLE t1 (a int primary key);
730
INSERT INTO t1 VALUES (1),(2),(3),(4);
731
SELECT MAX(a) FROM t1 WHERE a > 5;
732
MAX(a)
733
NULL
734
SELECT MIN(a) FROM t1 WHERE a < 0;
735
MIN(a)
736
NULL
737
DROP TABLE t1;
738
CREATE TABLE t1 (
739
id int(10) unsigned NOT NULL auto_increment,
740
val enum('one','two','three') NOT NULL default 'one',
741
PRIMARY KEY  (id)
742
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
743
INSERT INTO t1 VALUES
744
(1,'one'),(2,'two'),(3,'three'),(4,'one'),(5,'two');
745
select val, count(*) from t1 group by val;
746
val	count(*)
747
one	2
748
two	2
749
three	1
750
drop table t1;
751
CREATE TABLE t1 (
752
id int(10) unsigned NOT NULL auto_increment,
753
val set('one','two','three') NOT NULL default 'one',
754
PRIMARY KEY  (id)
755
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
756
INSERT INTO t1 VALUES
757
(1,'one'),(2,'two'),(3,'three'),(4,'one'),(5,'two');
758
select val, count(*) from t1 group by val;
759
val	count(*)
760
one	2
761
two	2
762
three	1
763
drop table t1;
764
create table t1(a int, b datetime);
765
insert into t1 values (1, NOW()), (2, NOW());
766
create table t2 select MAX(b) from t1 group by a;
767
show create table t2;
768
Table	Create Table
769
t2	CREATE TABLE "t2" (
770
  "MAX(b)" datetime
771
) ENGINE=MyISAM DEFAULT CHARSET=latin1
772
drop table t1, t2;
773
create table t1(f1 datetime);
774
insert into t1 values (now());
775
create table t2 select f2 from (select max(now()) f2 from t1) a;
776
show columns from t2;
777
Field	Type	Null	Key	Default	Extra
778
f2	datetime	YES		NULL	
779
drop table t2;
780
create table t2 select f2 from (select now() f2 from t1) a;
781
show columns from t2;
782
Field	Type	Null	Key	Default	Extra
783
f2	datetime	NO		NULL	
784
drop table t2, t1;
785
CREATE TABLE t1(
786
id int PRIMARY KEY,
787
a  int,
788
b  int,
789
INDEX i_b_id(a,b,id),
790
INDEX i_id(a,id)
791
);
792
INSERT INTO t1 VALUES 
793
(1,1,4), (2,2,1), (3,1,3), (4,2,1), (5,1,1);
794
SELECT MAX(id) FROM t1 WHERE id < 3 AND a=2 AND b=6;
795
MAX(id)
796
NULL
797
DROP TABLE t1;
798
CREATE TABLE t1(
799
id int PRIMARY KEY,
800
a  int,
801
b  int,
802
INDEX i_id(a,id),
803
INDEX i_b_id(a,b,id)
804
);
805
INSERT INTO t1 VALUES 
806
(1,1,4), (2,2,1), (3,1,3), (4,2,1), (5,1,1);
807
SELECT MAX(id) FROM t1 WHERE id < 3 AND a=2 AND b=6;
808
MAX(id)
809
NULL
810
DROP TABLE t1;
811
CREATE TABLE t1 (id int PRIMARY KEY, b char(3), INDEX(b));
812
INSERT INTO t1 VALUES (1,'xx'), (2,'aa');
813
SELECT * FROM t1;
814
id	b
815
1	xx
816
2	aa
817
SELECT MAX(b) FROM t1 WHERE b < 'ppppp';
818
MAX(b)
819
aa
820
SHOW WARNINGS;
821
Level	Code	Message
822
SELECT MAX(b) FROM t1 WHERE b < 'pp';
823
MAX(b)
824
aa
825
DROP TABLE t1;
826
CREATE TABLE t1 (id int PRIMARY KEY, b char(16), INDEX(b(4)));
827
INSERT INTO t1 VALUES (1, 'xxxxbbbb'), (2, 'xxxxaaaa');
828
SELECT MAX(b) FROM t1;
829
MAX(b)
830
xxxxbbbb
831
EXPLAIN SELECT MAX(b) FROM t1;
832
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
833
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	2	
834
DROP TABLE t1;
835
CREATE TABLE t1 (id int , b varchar(512), INDEX(b(250))) COLLATE latin1_bin;
836
INSERT INTO t1 VALUES
837
(1,CONCAT(REPEAT('_', 250), "qq")), (1,CONCAT(REPEAT('_', 250), "zz")),
838
(1,CONCAT(REPEAT('_', 250), "aa")), (1,CONCAT(REPEAT('_', 250), "ff"));
839
SELECT MAX(b) FROM t1;
840
MAX(b)
841
__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________zz
842
EXPLAIN SELECT MAX(b) FROM t1;
843
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
844
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	4	
845
DROP TABLE t1;
846
CREATE TABLE t1 (a INT, b INT);
847
INSERT INTO t1 VALUES (1,1),(1,2),(2,3);
848
SELECT (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
849
(SELECT COUNT(DISTINCT t1.b))
850
2
851
1
852
SELECT (SELECT COUNT(DISTINCT 12)) FROM t1 GROUP BY t1.a;
853
(SELECT COUNT(DISTINCT 12))
854
1
855
1
856
SELECT AVG(2), BIT_AND(2), BIT_OR(2), BIT_XOR(2), COUNT(*), COUNT(12),
857
COUNT(DISTINCT 12), MIN(2),MAX(2),STD(2), VARIANCE(2),SUM(2),
858
GROUP_CONCAT(2),GROUP_CONCAT(DISTINCT 2);
859
AVG(2)	BIT_AND(2)	BIT_OR(2)	BIT_XOR(2)	COUNT(*)	COUNT(12)	COUNT(DISTINCT 12)	MIN(2)	MAX(2)	STD(2)	VARIANCE(2)	SUM(2)	GROUP_CONCAT(2)	GROUP_CONCAT(DISTINCT 2)
860
2.00000	2	2	2	1	1	1	2	2	0.00000	0.00000	2	2	2
861
DROP TABLE t1;
862
create table t2 (ff double);
863
insert into t2 values (2.2);
864
select cast(sum(distinct ff) as decimal(5,2)) from t2;
865
cast(sum(distinct ff) as decimal(5,2))
866
2.20
867
select cast(sum(distinct ff) as signed) from t2;
868
cast(sum(distinct ff) as signed)
869
2
870
select cast(variance(ff) as decimal(10,3)) from t2;
871
cast(variance(ff) as decimal(10,3))
872
0.000
873
select cast(min(ff) as decimal(5,2)) from t2;
874
cast(min(ff) as decimal(5,2))
875
2.20
876
create table t1 (df decimal(5,1));
877
insert into t1 values(1.1);
878
insert into t1 values(2.2);
879
select cast(sum(distinct df) as signed) from t1;
880
cast(sum(distinct df) as signed)
881
3
882
select cast(min(df) as signed) from t1;
883
cast(min(df) as signed)
884
0
885
select 1e8 * sum(distinct df) from t1;
886
1e8 * sum(distinct df)
887
330000000
888
select 1e8 * min(df) from t1;
889
1e8 * min(df)
890
110000000.00000001
891
create table t3 (ifl int);
892
insert into t3 values(1), (2);
893
select cast(min(ifl) as decimal(5,2)) from t3;
894
cast(min(ifl) as decimal(5,2))
895
1.00
896
drop table t1, t2, t3;
897
CREATE TABLE t1 (id int(11),value1 float(10,2));
898
INSERT INTO t1 VALUES (1,0.00),(1,1.00), (1,2.00), (2,10.00), (2,11.00), (2,12.00), (2,13.00);
899
select id, stddev_pop(value1), var_pop(value1), stddev_samp(value1), var_samp(value1) from t1 group by id;
900
id	stddev_pop(value1)	var_pop(value1)	stddev_samp(value1)	var_samp(value1)
901
1	0.816497	0.666667	1.000000	1.000000
902
2	1.118034	1.250000	1.290994	1.666667
903
DROP TABLE t1;
904
CREATE TABLE t1 (col1 decimal(16,12));
905
INSERT INTO t1 VALUES (-5.00000000001),(-5.00000000002),(-5.00000000003),(-5.00000000000),(-5.00000000001),(-5.00000000002);
906
insert into t1 select * from t1;
907
select col1,count(col1),sum(col1),avg(col1) from t1 group by col1;
908
col1	count(col1)	sum(col1)	avg(col1)
909
-5.000000000030	2	-10.000000000060	-5.00000000003000000
910
-5.000000000020	4	-20.000000000080	-5.00000000002000000
911
-5.000000000010	4	-20.000000000040	-5.00000000001000000
912
-5.000000000000	2	-10.000000000000	-5.00000000000000000
913
DROP TABLE t1;
914
create table t1 (col1 decimal(16,12));
915
insert into t1 values (-5.00000000001);
916
insert into t1 values (-5.00000000001);
917
select col1,sum(col1),max(col1),min(col1) from t1 group by col1;
918
col1	sum(col1)	max(col1)	min(col1)
919
-5.000000000010	-10.000000000020	-5.000000000010	-5.000000000010
920
delete from t1;
921
insert into t1 values (5.00000000001);
922
insert into t1 values (5.00000000001);
923
select col1,sum(col1),max(col1),min(col1) from t1 group by col1;
924
col1	sum(col1)	max(col1)	min(col1)
925
5.000000000010	10.000000000020	5.000000000010	5.000000000010
926
DROP TABLE t1;
927
CREATE TABLE t1 (a VARCHAR(400));
928
INSERT INTO t1 (a) VALUES ("A"), ("a"), ("a "), ("a   "),
929
("B"), ("b"), ("b "), ("b   ");
930
SELECT COUNT(DISTINCT a) FROM t1;
931
COUNT(DISTINCT a)
932
2
933
DROP TABLE t1;
934
CREATE TABLE t1 (a int, b int, c int);
935
INSERT INTO t1 (a, b, c) VALUES
936
(1,1,1), (1,1,2), (1,1,3),
937
(1,2,1), (1,2,2), (1,2,3),
938
(1,3,1), (1,3,2), (1,3,3),
939
(2,1,1), (2,1,2), (2,1,3),
940
(2,2,1), (2,2,2), (2,2,3),
941
(2,3,1), (2,3,2), (2,3,3),
942
(3,1,1), (3,1,2), (3,1,3),
943
(3,2,1), (3,2,2), (3,2,3),
944
(3,3,1), (3,3,2), (3,3,3);
945
SELECT b/c as v, a FROM t1 ORDER BY v;
946
v	a
947
0.33333	3
948
0.33333	1
949
0.33333	2
950
0.50000	1
951
0.50000	2
952
0.50000	3
953
0.66667	2
954
0.66667	1
955
0.66667	3
956
1.00000	3
957
1.00000	2
958
1.00000	3
959
1.00000	1
960
1.00000	2
961
1.00000	3
962
1.00000	2
963
1.00000	1
964
1.00000	1
965
1.50000	3
966
1.50000	2
967
1.50000	1
968
2.00000	1
969
2.00000	3
970
2.00000	2
971
3.00000	3
972
3.00000	2
973
3.00000	1
974
SELECT b/c as v, SUM(a) FROM t1 GROUP BY v;
975
v	SUM(a)
976
0.33333	6
977
0.50000	6
978
0.66667	6
979
1.00000	18
980
1.50000	6
981
2.00000	6
982
3.00000	6
983
SELECT SUM(a) FROM t1 GROUP BY b/c;
984
SUM(a)
985
6
986
6
987
6
988
18
989
6
990
6
991
6
992
DROP TABLE t1;
993
set div_precision_increment= @sav_dpi;
994
CREATE TABLE t1 (a INT PRIMARY KEY, b INT);
995
INSERT INTO t1 VALUES (1,1), (2,2);
996
CREATE TABLE t2 (a INT PRIMARY KEY, b INT);
997
INSERT INTO t2 VALUES (1,1), (3,3);
998
SELECT SQL_NO_CACHE 
999
(SELECT SUM(c.a) FROM t1 ttt, t2 ccc 
1000
WHERE ttt.a = ccc.b AND ttt.a = t.a GROUP BY ttt.a) AS minid   
1001
FROM t1 t, t2 c WHERE t.a = c.b;
1002
minid
1003
1
1004
DROP TABLE t1,t2;
1005
create table t1 select variance(0);
1006
show create table t1;
1007
Table	Create Table
1008
t1	CREATE TABLE "t1" (
1009
  "variance(0)" double(8,4)
1010
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1011
drop table t1;
1012
create table t1 select stddev(0);
1013
show create table t1;
1014
Table	Create Table
1015
t1	CREATE TABLE "t1" (
1016
  "stddev(0)" double(8,4)
1017
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1018
drop table t1;
1019
create table bug22555 (i smallint primary key auto_increment, s1 smallint, s2 smallint, e decimal(30,10), o double);
1020
insert into bug22555 (s1, s2, e, o) values (53, 78, 11.4276528, 6.828112), (17, 78, 5.916793, 1.8502951), (18, 76, 2.679231, 9.17975591), (31, 62, 6.07831, 0.1), (19, 41, 5.37463, 15.1), (83, 73, 14.567426, 7.959222), (92, 53, 6.10151, 13.1856852), (7, 12, 13.92272, 3.442007), (92, 35, 11.95358909, 6.01376678), (38, 84, 2.572, 7.904571);
1021
select std(s1/s2) from bug22555 group by i;
1022
std(s1/s2)
1023
0.00000000
1024
0.00000000
1025
0.00000000
1026
0.00000000
1027
0.00000000
1028
0.00000000
1029
0.00000000
1030
0.00000000
1031
0.00000000
1032
0.00000000
1033
select std(e) from bug22555 group by i;
1034
std(e)
1035
0.00000000000000
1036
0.00000000000000
1037
0.00000000000000
1038
0.00000000000000
1039
0.00000000000000
1040
0.00000000000000
1041
0.00000000000000
1042
0.00000000000000
1043
0.00000000000000
1044
0.00000000000000
1045
select std(o) from bug22555 group by i;
1046
std(o)
1047
0
1048
0
1049
0
1050
0
1051
0
1052
0
1053
0
1054
0
1055
0
1056
0
1057
drop table bug22555;
1058
create table bug22555 (i smallint, s1 smallint, s2 smallint, o1 double, o2 double, e1 decimal, e2 decimal);
1059
insert into bug22555 values (1,53,78,53,78,53,78),(2,17,78,17,78,17,78),(3,18,76,18,76,18,76);
1060
select i, count(*) from bug22555 group by i;
1061
i	count(*)
1062
1	1
1063
2	1
1064
3	1
1065
select std(s1/s2) from bug22555 where i=1;
1066
std(s1/s2)
1067
0.00000000
1068
select std(s1/s2) from bug22555 where i=2;
1069
std(s1/s2)
1070
0.00000000
1071
select std(s1/s2) from bug22555 where i=3;
1072
std(s1/s2)
1073
0.00000000
1074
select std(s1/s2) from bug22555 where i=1 group by i;
1075
std(s1/s2)
1076
0.00000000
1077
select std(s1/s2) from bug22555 where i=2 group by i;
1078
std(s1/s2)
1079
0.00000000
1080
select std(s1/s2) from bug22555 where i=3 group by i;
1081
std(s1/s2)
1082
0.00000000
1083
select std(s1/s2) from bug22555 group by i order by i;
1084
std(s1/s2)
1085
0.00000000
1086
0.00000000
1087
0.00000000
1088
select i, count(*), std(o1/o2) from bug22555 group by i order by i;
1089
i	count(*)	std(o1/o2)
1090
1	1	0
1091
2	1	0
1092
3	1	0
1093
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1094
i	count(*)	std(e1/e2)
1095
1	1	0.00000000
1096
2	1	0.00000000
1097
3	1	0.00000000
1098
set @saved_div_precision_increment=@@div_precision_increment;
1099
set div_precision_increment=19;
1100
select i, count(*), variance(s1/s2) from bug22555 group by i order by i;
1101
i	count(*)	variance(s1/s2)
1102
1	1	0.000000000000000000000000000000
1103
2	1	0.000000000000000000000000000000
1104
3	1	0.000000000000000000000000000000
1105
select i, count(*), variance(o1/o2) from bug22555 group by i order by i;
1106
i	count(*)	variance(o1/o2)
1107
1	1	0
1108
2	1	0
1109
3	1	0
1110
select i, count(*), variance(e1/e2) from bug22555 group by i order by i;
1111
i	count(*)	variance(e1/e2)
1112
1	1	0.000000000000000000000000000000
1113
2	1	0.000000000000000000000000000000
1114
3	1	0.000000000000000000000000000000
1115
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1116
i	count(*)	std(s1/s2)
1117
1	1	0.000000000000000000000000000000
1118
2	1	0.000000000000000000000000000000
1119
3	1	0.000000000000000000000000000000
1120
select i, count(*), std(o1/o2) from bug22555 group by i order by i;
1121
i	count(*)	std(o1/o2)
1122
1	1	0
1123
2	1	0
1124
3	1	0
1125
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1126
i	count(*)	std(e1/e2)
1127
1	1	0.000000000000000000000000000000
1128
2	1	0.000000000000000000000000000000
1129
3	1	0.000000000000000000000000000000
1130
set div_precision_increment=20;
1131
select i, count(*), variance(s1/s2) from bug22555 group by i order by i;
1132
i	count(*)	variance(s1/s2)
1133
1	1	0.000000000000000000000000000000
1134
2	1	0.000000000000000000000000000000
1135
3	1	0.000000000000000000000000000000
1136
select i, count(*), variance(o1/o2) from bug22555 group by i order by i;
1137
i	count(*)	variance(o1/o2)
1138
1	1	0
1139
2	1	0
1140
3	1	0
1141
select i, count(*), variance(e1/e2) from bug22555 group by i order by i;
1142
i	count(*)	variance(e1/e2)
1143
1	1	0.000000000000000000000000000000
1144
2	1	0.000000000000000000000000000000
1145
3	1	0.000000000000000000000000000000
1146
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1147
i	count(*)	std(s1/s2)
1148
1	1	0.000000000000000000000000000000
1149
2	1	0.000000000000000000000000000000
1150
3	1	0.000000000000000000000000000000
1151
select i, count(*), std(o1/o2) from bug22555 group by i order by i;
1152
i	count(*)	std(o1/o2)
1153
1	1	0
1154
2	1	0
1155
3	1	0
1156
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1157
i	count(*)	std(e1/e2)
1158
1	1	0.000000000000000000000000000000
1159
2	1	0.000000000000000000000000000000
1160
3	1	0.000000000000000000000000000000
1161
set @@div_precision_increment=@saved_div_precision_increment;
1162
insert into bug22555 values (1,53,78,53,78,53,78),(2,17,78,17,78,17,78),(3,18,76,18,76,18,76);
1163
insert into bug22555 values (1,53,78,53,78,53,78),(2,17,78,17,78,17,78),(3,18,76,18,76,18,76);
1164
insert into bug22555 values (1,53,78,53,78,53,78),(2,17,78,17,78,17,78),(3,18,76,18,76,18,76);
1165
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1166
i	count(*)	std(s1/s2)
1167
1	4	0.00000000
1168
2	4	0.00000000
1169
3	4	0.00000000
1170
select i, count(*), round(std(o1/o2), 16) from bug22555 group by i order by i;
1171
i	count(*)	round(std(o1/o2), 16)
1172
1	4	0.0000000000000000
1173
2	4	0.0000000000000000
1174
3	4	0.0000000000000000
1175
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1176
i	count(*)	std(e1/e2)
1177
1	4	0.00000000
1178
2	4	0.00000000
1179
3	4	0.00000000
1180
select std(s1/s2) from bug22555;
1181
std(s1/s2)
1182
0.21325764
1183
select std(o1/o2) from bug22555;
1184
std(o1/o2)
1185
0.2132576358664934
1186
select std(e1/e2) from bug22555;
1187
std(e1/e2)
1188
0.21325764
1189
set @saved_div_precision_increment=@@div_precision_increment;
1190
set div_precision_increment=19;
1191
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1192
i	count(*)	std(s1/s2)
1193
1	4	0.000000000000000000000000000000
1194
2	4	0.000000000000000000000000000000
1195
3	4	0.000000000000000000000000000000
1196
select i, count(*), round(std(o1/o2), 16) from bug22555 group by i order by i;
1197
i	count(*)	round(std(o1/o2), 16)
1198
1	4	0.0000000000000000
1199
2	4	0.0000000000000000
1200
3	4	0.0000000000000000
1201
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1202
i	count(*)	std(e1/e2)
1203
1	4	0.000000000000000000000000000000
1204
2	4	0.000000000000000000000000000000
1205
3	4	0.000000000000000000000000000000
1206
select round(std(s1/s2), 17) from bug22555;
1207
round(std(s1/s2), 17)
1208
0.21325763586649340
1209
select std(o1/o2) from bug22555;
1210
std(o1/o2)
1211
0.2132576358664934
1212
select round(std(e1/e2), 17) from bug22555;
1213
round(std(e1/e2), 17)
1214
0.21325763586649340
1215
set div_precision_increment=20;
1216
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1217
i	count(*)	std(s1/s2)
1218
1	4	0.000000000000000000000000000000
1219
2	4	0.000000000000000000000000000000
1220
3	4	0.000000000000000000000000000000
1221
select i, count(*), round(std(o1/o2), 16) from bug22555 group by i order by i;
1222
i	count(*)	round(std(o1/o2), 16)
1223
1	4	0.0000000000000000
1224
2	4	0.0000000000000000
1225
3	4	0.0000000000000000
1226
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1227
i	count(*)	std(e1/e2)
1228
1	4	0.000000000000000000000000000000
1229
2	4	0.000000000000000000000000000000
1230
3	4	0.000000000000000000000000000000
1231
select round(std(s1/s2), 17) from bug22555;
1232
round(std(s1/s2), 17)
1233
0.21325763586649340
1234
select std(o1/o2) from bug22555;
1235
std(o1/o2)
1236
0.2132576358664934
1237
select round(std(e1/e2), 17) from bug22555;
1238
round(std(e1/e2), 17)
1239
0.21325763586649340
1240
set @@div_precision_increment=@saved_div_precision_increment;
1241
drop table bug22555;
1242
create table bug22555 (s smallint, o double, e decimal);
1243
insert into bug22555 values (1,1,1),(2,2,2),(3,3,3),(6,6,6),(7,7,7);
1244
select var_samp(s), var_pop(s) from bug22555;
1245
var_samp(s)	var_pop(s)
1246
6.7000	5.3600
1247
select var_samp(o), var_pop(o) from bug22555;
1248
var_samp(o)	var_pop(o)
1249
6.7	5.36
1250
select var_samp(e), var_pop(e) from bug22555;
1251
var_samp(e)	var_pop(e)
1252
6.7000	5.3600
1253
drop table bug22555;
1254
create table bug22555 (s smallint, o double, e decimal);
1255
insert into bug22555 values (null,null,null),(null,null,null);
1256
select var_samp(s) as 'null', var_pop(s) as 'null' from bug22555;
1257
null	null
1258
NULL	NULL
1259
select var_samp(o) as 'null', var_pop(o) as 'null' from bug22555;
1260
null	null
1261
NULL	NULL
1262
select var_samp(e) as 'null', var_pop(e) as 'null' from bug22555;
1263
null	null
1264
NULL	NULL
1265
insert into bug22555 values (1,1,1);
1266
select var_samp(s) as 'null', var_pop(s) as '0' from bug22555;
1267
null	0
1268
NULL	0.0000
1269
select var_samp(o) as 'null', var_pop(o) as '0' from bug22555;
1270
null	0
1271
NULL	0
1272
select var_samp(e) as 'null', var_pop(e) as '0' from bug22555;
1273
null	0
1274
NULL	0.0000
1275
insert into bug22555 values (2,2,2);
1276
select var_samp(s) as '0.5', var_pop(s) as '0.25' from bug22555;
1277
0.5	0.25
1278
0.5000	0.2500
1279
select var_samp(o) as '0.5', var_pop(o) as '0.25' from bug22555;
1280
0.5	0.25
1281
0.5	0.25
1282
select var_samp(e) as '0.5', var_pop(e) as '0.25' from bug22555;
1283
0.5	0.25
1284
0.5000	0.2500
1285
drop table bug22555;
1286
create table t1 (a decimal(20));
1287
insert into t1 values (12345678901234567890);
1288
select count(a) from t1;
1289
count(a)
1290
1
1291
select count(distinct a) from t1;
1292
count(distinct a)
1293
1
1294
drop table t1;
1295
CREATE TABLE t1 (a INT, b INT);
1296
INSERT INTO t1 VALUES (1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8);
1297
INSERT INTO t1 SELECT a, b+8       FROM t1;
1298
INSERT INTO t1 SELECT a, b+16      FROM t1;
1299
INSERT INTO t1 SELECT a, b+32      FROM t1;
1300
INSERT INTO t1 SELECT a, b+64      FROM t1;
1301
INSERT INTO t1 SELECT a, b+128     FROM t1;
1302
INSERT INTO t1 SELECT a, b+256     FROM t1;
1303
INSERT INTO t1 SELECT a, b+512     FROM t1;
1304
INSERT INTO t1 SELECT a, b+1024    FROM t1;
1305
INSERT INTO t1 SELECT a, b+2048    FROM t1;
1306
INSERT INTO t1 SELECT a, b+4096    FROM t1;
1307
INSERT INTO t1 SELECT a, b+8192    FROM t1;
1308
INSERT INTO t1 SELECT a, b+16384   FROM t1;
1309
INSERT INTO t1 SELECT a, b+32768   FROM t1;
1310
SELECT a,COUNT(DISTINCT b) AS cnt FROM t1 GROUP BY a HAVING cnt > 50;
1311
a	cnt
1312
1	65536
1313
SELECT a,SUM(DISTINCT b) AS sumation FROM t1 GROUP BY a HAVING sumation > 50;
1314
a	sumation
1315
1	2147516416
1316
SELECT a,AVG(DISTINCT b) AS average FROM t1 GROUP BY a HAVING average > 50;
1317
a	average
1318
1	32768.5000
1319
DROP TABLE t1;
1320
CREATE TABLE t1 ( a INT, b INT, KEY(a) );
1321
INSERT INTO t1 VALUES (NULL, 1), (NULL, 2);
1322
EXPLAIN SELECT MIN(a), MIN(b) FROM t1;
1323
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1324
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	2	
1325
SELECT MIN(a), MIN(b) FROM t1;
1326
MIN(a)	MIN(b)
1327
NULL	1
1328
CREATE TABLE t2( a INT, b INT, c INT, KEY(a, b) );
1329
INSERT INTO t2 ( a, b, c ) VALUES ( 1, NULL, 2 ), ( 1, 3, 4 ), ( 1, 4, 4 );
1330
EXPLAIN SELECT MIN(b), MIN(c) FROM t2 WHERE a = 1;
1331
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1332
1	SIMPLE	t2	ref	a	a	5	const	2	
1333
SELECT MIN(b), MIN(c) FROM t2 WHERE a = 1;
1334
MIN(b)	MIN(c)
1335
3	2
1336
CREATE TABLE t3 (a INT, b INT, c int, KEY(a, b));
1337
INSERT INTO t3 VALUES (1, NULL, 1), (2, NULL, 2),  (2, NULL, 2),  (3, NULL, 3);
1338
EXPLAIN SELECT MIN(a), MIN(b) FROM t3 where a = 2;
1339
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1340
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
1341
SELECT MIN(a), MIN(b) FROM t3 where a = 2;
1342
MIN(a)	MIN(b)
1343
2	NULL
1344
CREATE TABLE t4 (a INT, b INT, c int, KEY(a, b));
1345
INSERT INTO t4 VALUES (1, 1, 1), (2, NULL, 2),  (2, NULL, 2),  (3, 1, 3);
1346
EXPLAIN SELECT MIN(a), MIN(b) FROM t4 where a = 2;
1347
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1348
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
1349
SELECT MIN(a), MIN(b) FROM t4 where a = 2;
1350
MIN(a)	MIN(b)
1351
2	NULL
1352
SELECT MIN(b), min(c) FROM t4 where a = 2;
1353
MIN(b)	min(c)
1354
NULL	2
1355
CREATE TABLE t5( a INT, b INT, KEY( a, b) );
1356
INSERT INTO t5 VALUES( 1, 1 ), ( 1, 2 );
1357
EXPLAIN SELECT MIN(a), MIN(b) FROM t5 WHERE a = 1;
1358
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1359
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
1360
SELECT MIN(a), MIN(b) FROM t5 WHERE a = 1;
1361
MIN(a)	MIN(b)
1362
1	1
1363
SELECT MIN(a), MIN(b) FROM t5 WHERE a = 1 and b > 1;
1364
MIN(a)	MIN(b)
1365
1	2
1366
DROP TABLE t1, t2, t3, t4, t5;
1367
CREATE TABLE t1 (a int, b date NOT NULL, KEY k1 (a,b));
1368
SELECT MIN(b) FROM t1 WHERE a=1 AND b>'2007-08-01';
1369
MIN(b)
1370
NULL
1371
DROP TABLE t1;
1372
CREATE TABLE t1(a DOUBLE);
1373
INSERT INTO t1 VALUES (10), (20);
1374
SELECT AVG(a), CAST(AVG(a) AS DECIMAL) FROM t1;
1375
AVG(a)	CAST(AVG(a) AS DECIMAL)
1376
15	15
1377
DROP TABLE t1;
1378
End of 5.0 tests