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';
6
div_precision_increment 5
7
create table t1 (grp int, a bigint, 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;
22
select a,c,sum(a) from t1 where a > 10 group by a;
24
select sum(a) from t1 where a > 10;
27
select a from t1 order by rand(10);
35
select distinct a from t1 order by rand(10);
43
select count(distinct a),count(distinct grp) from t1;
44
count(distinct a) count(distinct grp)
46
insert into t1 values (null,null,'');
47
select count(distinct a),count(distinct grp) from t1;
48
count(distinct a) count(distinct grp)
50
create table t2 (grp int, a bigint, c char(10));
51
insert into t2 select grp,max(a)+max(grp),max(c) from t1 group by grp;
52
replace into t2 select grp, a, c from t1 limit 2,1;
61
CREATE TABLE t1 (id int,value1 float(10,2));
62
INSERT INTO t1 VALUES (1,0.00),(1,1.00), (1,2.00), (2,10.00), (2,11.00), (2,12.00);
63
CREATE TABLE t2 (id int,name char(20));
64
INSERT INTO t2 VALUES (1,'Set One'),(2,'Set Two');
65
select id, avg(value1), std(value1), variance(value1) from t1 group by id;
66
id avg(value1) std(value1) variance(value1)
67
1 1.0000000 0.816497 0.666667
68
2 11.0000000 0.816497 0.666667
69
select name, avg(value1), std(value1), variance(value1) from t1, t2 where t1.id = t2.id group by t1.id;
70
name avg(value1) std(value1) variance(value1)
71
Set One 1.0000000 0.816497 0.666667
72
Set Two 11.0000000 0.816497 0.666667
74
create table t1 (id int not null);
75
create table t2 (id int not null,rating int null);
76
insert into t1 values(1),(2),(3);
77
insert into t2 values(1, 3),(2, NULL),(2, NULL),(3, 2),(3, NULL);
78
select t1.id, avg(rating) from t1 left join t2 on ( t1.id = t2.id ) group by t1.id;
83
select sql_small_result t2.id, avg(rating) from t2 group by t2.id;
88
select sql_big_result t2.id, avg(rating) from t2 group by t2.id;
93
select sql_small_result t2.id, avg(rating+0.0e0) from t2 group by t2.id;
98
select sql_big_result t2.id, avg(rating+0.0e0) from t2 group by t2.id;
104
create table t1 (a int primary key, c char(10), b text);
105
INSERT INTO t1 VALUES (1,'1','1');
106
INSERT INTO t1 VALUES (2,'2','2');
107
INSERT INTO t1 VALUES (4,'4','4');
108
select count(*) from t1;
111
select count(*) from t1 where a = 1;
114
select count(*) from t1 where a = 100;
117
select count(*) from t1 where a >= 10;
120
select count(a) from t1 where a = 1;
123
select count(a) from t1 where a = 100;
126
select count(a) from t1 where a >= 10;
129
select count(b) from t1 where b >= 2;
132
select count(b) from t1 where b >= 10;
135
select count(c) from t1 where c = 10;
139
CREATE TABLE t1 (d DATETIME, i INT);
140
INSERT INTO t1 VALUES (NOW(), 1);
141
SELECT COUNT(i), i, COUNT(i)*i FROM t1 GROUP BY i;
142
COUNT(i) i COUNT(i)*i
144
SELECT COUNT(i), (i+0), COUNT(i)*(i+0) FROM t1 GROUP BY i;
145
COUNT(i) (i+0) COUNT(i)*(i+0)
152
insert into t1 values (10.3,'nem'),(20.53,'monty'),(30.23,'sinisa');
153
insert into t1 values (30.13,'nem'),(20.98,'monty'),(10.45,'sinisa');
154
insert into t1 values (5.2,'nem'),(8.64,'monty'),(11.12,'sinisa');
155
select sum(num) from t1;
158
select sum(num) from t1 group by user;
164
create table t1 (a1 int, a2 char(3), key k1(a1), key k2(a2));
165
insert into t1 values(10,'aaa'), (10,null), (10,'bbb'), (20,'zzz');
166
create table t2(a1 char(3), a2 int, a3 real, key k1(a1), key k2(a2, a1));
173
select min(a2) from t1;
176
select max(t1.a1), max(t2.a2) from t1, t2;
177
max(t1.a1) max(t2.a2)
179
select max(t1.a1) from t1, t2;
182
select max(t2.a2), max(t1.a1) from t1, t2;
183
max(t2.a2) max(t1.a1)
185
explain select min(a2) from t1;
186
id select_type table type possible_keys key key_len ref rows Extra
187
1 SIMPLE t1 ALL NULL NULL NULL NULL 4
188
explain select max(t1.a1), max(t2.a2) from t1, t2;
189
id select_type table type possible_keys key key_len ref rows Extra
190
1 SIMPLE t2 ALL NULL NULL NULL NULL 0
191
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using join buffer
192
insert into t2 values('AAA', 10, 0.5);
193
insert into t2 values('BBB', 20, 1.0);
194
select t1.a1, t1.a2, t2.a1, t2.a2 from t1,t2;
204
select max(t1.a1), max(t2.a1) from t1, t2 where t2.a2=9;
205
max(t1.a1) max(t2.a1)
207
select max(t2.a1), max(t1.a1) from t1, t2 where t2.a2=9;
208
max(t2.a1) max(t1.a1)
210
select t1.a1, t1.a2, t2.a1, t2.a2 from t1 left outer join t2 on t1.a1=10;
219
select max(t1.a2) from t1 left outer join t2 on t1.a1=10;
222
select max(t2.a1) from t2 left outer join t1 on t2.a2=10 where t2.a2=20;
225
select max(t2.a1) from t2 left outer join t1 on t2.a2=10 where t2.a2=10;
228
select max(t2.a1) from t1 left outer join t2 on t1.a2=t2.a1 and 1=0 where t2.a1='AAA';
231
select max(t1.a2),max(t2.a1) from t1 left outer join t2 on t1.a1=10;
232
max(t1.a2) max(t2.a1)
235
create table t1 (a int);
236
select avg(2) from t1;
241
a1 char(3) primary key,
251
a1 char(3) primary key,
258
insert into t1 values('AME',0,'SEA',0.100,date'1942-02-19');
259
insert into t1 values('HBR',1,'SEA',0.085,date'1948-03-05');
260
insert into t1 values('BOT',2,'SEA',0.085,date'1951-11-29');
261
insert into t1 values('BMC',3,'SEA',0.085,date'1958-09-08');
262
insert into t1 values('TWU',0,'LAX',0.080,date'1969-10-05');
263
insert into t1 values('BDL',0,'DEN',0.080,date'1960-11-27');
264
insert into t1 values('DTX',1,'NYC',0.080,date'1961-05-04');
265
insert into t1 values('PLS',1,'WDC',0.075,date'1949-01-02');
266
insert into t1 values('ZAJ',2,'CHI',0.075,date'1960-06-15');
267
insert into t1 values('VVV',2,'MIN',0.075,date'1959-06-28');
268
insert into t1 values('GTM',3,'DAL',0.070,date'1977-09-23');
269
insert into t1 values('SSJ',null,'CHI',null,date'1974-03-19');
270
insert into t1 values('KKK',3,'ATL',null,null);
271
insert into t1 values('XXX',null,'MIN',null,null);
272
insert into t1 values('WWW',1,'LED',null,null);
273
insert into t2 values('TKF','Seattle','WA','AME');
274
insert into t2 values('LCC','Los Angeles','CA','TWU');
275
insert into t2 values('DEN','Denver','CO','BDL');
276
insert into t2 values('SDC','San Diego','CA','TWU');
277
insert into t2 values('NOL','New Orleans','LA','GTM');
278
insert into t2 values('LAK','Los Angeles','CA','TWU');
279
insert into t2 values('AAA','AAA','AA','AME');
282
AME 0 SEA 0.1 1942-02-19
283
BDL 0 DEN 0.08 1960-11-27
284
BMC 3 SEA 0.085 1958-09-08
285
BOT 2 SEA 0.085 1951-11-29
286
DTX 1 NYC 0.08 1961-05-04
287
GTM 3 DAL 0.07 1977-09-23
288
HBR 1 SEA 0.085 1948-03-05
290
PLS 1 WDC 0.075 1949-01-02
291
SSJ NULL CHI NULL 1974-03-19
292
TWU 0 LAX 0.08 1969-10-05
293
VVV 2 MIN 0.075 1959-06-28
295
XXX NULL MIN NULL NULL
296
ZAJ 2 CHI 0.075 1960-06-15
301
LAK Los Angeles CA TWU
302
LCC Los Angeles CA TWU
303
NOL New Orleans LA GTM
307
select min(a1) from t1;
308
id select_type table type possible_keys key key_len ref rows Extra
309
1 SIMPLE t1 ALL NULL NULL NULL NULL 15
310
select min(a1) from t1;
314
select max(a4) from t1;
315
id select_type table type possible_keys key key_len ref rows Extra
316
1 SIMPLE t1 ALL NULL NULL NULL NULL 15
317
select max(a4) from t1;
321
select min(a5), max(a5) from t1;
322
id select_type table type possible_keys key key_len ref rows Extra
323
1 SIMPLE t1 ALL NULL NULL NULL NULL 15
324
select min(a5), max(a5) from t1;
326
1942-02-19 1977-09-23
328
select min(a3) from t1 where a2 = 2;
329
id select_type table type possible_keys key key_len ref rows Extra
330
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
331
select min(a3) from t1 where a2 = 2;
335
select min(a1), max(a1) from t1 where a4 = 0.080;
336
id select_type table type possible_keys key key_len ref rows Extra
337
1 SIMPLE t1 ref k2 k2 9 const 1 Using where
338
select min(a1), max(a1) from t1 where a4 = 0.080;
342
select min(t1.a5), max(t2.a3) from t1, t2;
343
id select_type table type possible_keys key key_len ref rows Extra
344
1 SIMPLE t2 ALL NULL NULL NULL NULL 7
345
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using join buffer
346
select min(t1.a5), max(t2.a3) from t1, t2;
347
min(t1.a5) max(t2.a3)
350
select min(t1.a3), max(t2.a2) from t1, t2 where t1.a2 = 0 and t2.a3 = 'CA';
351
id select_type table type possible_keys key key_len ref rows Extra
352
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
353
1 SIMPLE t2 ref k1 k1 11 const 1 Using where
354
select min(t1.a3), max(t2.a2) from t1, t2 where t1.a2 = 0 and t2.a3 = 'CA';
355
min(t1.a3) max(t2.a2)
358
select min(a1) from t1 where a1 > 'KKK';
359
id select_type table type possible_keys key key_len ref rows Extra
360
1 SIMPLE t1 ALL PRIMARY NULL NULL NULL 15 Using where
361
select min(a1) from t1 where a1 > 'KKK';
365
select min(a1) from t1 where a1 >= 'KKK';
366
id select_type table type possible_keys key key_len ref rows Extra
367
1 SIMPLE t1 ALL PRIMARY NULL NULL NULL 15 Using where
368
select min(a1) from t1 where a1 >= 'KKK';
372
select max(a3) from t1 where a2 = 2 and a3 < 'SEA';
373
id select_type table type possible_keys key key_len ref rows Extra
374
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
375
select max(a3) from t1 where a2 = 2 and a3 < 'SEA';
379
select max(a5) from t1 where a5 < date'1970-01-01';
380
id select_type table type possible_keys key key_len ref rows Extra
381
1 SIMPLE t1 ALL k3 NULL NULL NULL 15 Using where
382
select max(a5) from t1 where a5 < date'1970-01-01';
386
select max(a3) from t1 where a2 is null;
387
id select_type table type possible_keys key key_len ref rows Extra
388
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
389
select max(a3) from t1 where a2 is null;
393
select max(a3) from t1 where a2 = 0 and a3 between 'K' and 'Q';
394
id select_type table type possible_keys key key_len ref rows Extra
395
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
396
select max(a3) from t1 where a2 = 0 and a3 between 'K' and 'Q';
400
select min(a1), max(a1) from t1 where a1 between 'A' and 'P';
401
id select_type table type possible_keys key key_len ref rows Extra
402
1 SIMPLE t1 ALL PRIMARY NULL NULL NULL 15 Using where
403
select min(a1), max(a1) from t1 where a1 between 'A' and 'P';
407
select max(a3) from t1 where a3 < 'SEA' and a2 = 2 and a3 <= 'MIN';
408
id select_type table type possible_keys key key_len ref rows Extra
409
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
410
select max(a3) from t1 where a3 < 'SEA' and a2 = 2 and a3 <= 'MIN';
414
select max(a3) from t1 where a3 = 'MIN' and a2 = 2;
415
id select_type table type possible_keys key key_len ref rows Extra
416
1 SIMPLE t1 ref k1 k1 20 const,const 1 Using where
417
select max(a3) from t1 where a3 = 'MIN' and a2 = 2;
421
select max(a3) from t1 where a3 = 'DEN' and a2 = 2;
422
id select_type table type possible_keys key key_len ref rows Extra
423
1 SIMPLE t1 ref k1 k1 20 const,const 1 Using where
424
select max(a3) from t1 where a3 = 'DEN' and a2 = 2;
428
select max(t1.a3), min(t2.a2) from t1, t2 where t1.a2 = 2 and t1.a3 < 'MIN' and t2.a3 = 'CA';
429
id select_type table type possible_keys key key_len ref rows Extra
430
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
431
1 SIMPLE t2 ref k1 k1 11 const 1 Using where
432
select max(t1.a3), min(t2.a2) from t1, t2 where t1.a2 = 2 and t1.a3 < 'MIN' and t2.a3 = 'CA';
433
max(t1.a3) min(t2.a2)
436
select max(a3) from t1 where a2 is null and a2 = 2;
437
id select_type table type possible_keys key key_len ref rows Extra
438
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
439
select max(a3) from t1 where a2 is null and a2 = 2;
443
select max(a2) from t1 where a2 >= 1;
444
id select_type table type possible_keys key key_len ref rows Extra
445
1 SIMPLE t1 ALL k1 NULL NULL NULL 15 Using where
446
select max(a2) from t1 where a2 >= 1;
450
select min(a3) from t1 where a2 = 2 and a3 < 'SEA';
451
id select_type table type possible_keys key key_len ref rows Extra
452
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
453
select min(a3) from t1 where a2 = 2 and a3 < 'SEA';
457
select min(a3) from t1 where a2 = 4;
458
id select_type table type possible_keys key key_len ref rows Extra
459
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
460
select min(a3) from t1 where a2 = 4;
464
select min(a3) from t1 where a2 = 2 and a3 > 'SEA';
465
id select_type table type possible_keys key key_len ref rows Extra
466
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
467
select min(a3) from t1 where a2 = 2 and a3 > 'SEA';
471
select (min(a4)+max(a4))/2 from t1;
472
id select_type table type possible_keys key key_len ref rows Extra
473
1 SIMPLE t1 ALL NULL NULL NULL NULL 15
474
select (min(a4)+max(a4))/2 from t1;
478
select min(a3) from t1 where 2 = a2;
479
id select_type table type possible_keys key key_len ref rows Extra
480
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
481
select min(a3) from t1 where 2 = a2;
485
select max(a3) from t1 where a2 = 2 and 'SEA' > a3;
486
id select_type table type possible_keys key key_len ref rows Extra
487
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
488
select max(a3) from t1 where a2 = 2 and 'SEA' > a3;
492
select max(a3) from t1 where a2 = 2 and 'SEA' < a3;
493
id select_type table type possible_keys key key_len ref rows Extra
494
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
495
select max(a3) from t1 where a2 = 2 and 'SEA' < a3;
499
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI';
500
id select_type table type possible_keys key key_len ref rows Extra
501
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
502
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI';
506
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI' and a3 < 'SEA';
507
id select_type table type possible_keys key key_len ref rows Extra
508
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
509
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI' and a3 < 'SEA';
513
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI' and a3 = 'MIN';
514
id select_type table type possible_keys key key_len ref rows Extra
515
1 SIMPLE t1 ref k1 k1 20 const,const 1 Using where
516
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI' and a3 = 'MIN';
520
select min(a3) from t1 where a2 = 2 and a3 >= 'SEA' and a3 = 'MIN';
521
id select_type table type possible_keys key key_len ref rows Extra
522
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
523
select min(a3) from t1 where a2 = 2 and a3 >= 'SEA' and a3 = 'MIN';
527
select min(t1.a1), min(t2.a4) from t1,t2 where t1.a1 < 'KKK' and t2.a4 < 'KKK';
528
id select_type table type possible_keys key key_len ref rows Extra
529
1 SIMPLE t2 ALL k2 NULL NULL NULL 7 Using where
530
1 SIMPLE t1 ALL PRIMARY NULL NULL NULL 15 Using where; Using join buffer
531
select min(t1.a1), min(t2.a4) from t1,t2 where t1.a1 < 'KKK' and t2.a4 < 'KKK';
532
min(t1.a1) min(t2.a4)
535
select min(a1) from t1 where a1 > 'KKK' or a1 < 'XXX';
536
id select_type table type possible_keys key key_len ref rows Extra
537
1 SIMPLE t1 ALL PRIMARY NULL NULL NULL 15 Using where
539
select min(a1) from t1 where a1 != 'KKK';
540
id select_type table type possible_keys key key_len ref rows Extra
541
1 SIMPLE t1 ALL PRIMARY NULL NULL NULL 15 Using where
543
select max(a3) from t1 where a2 < 2 and a3 < 'SEA';
544
id select_type table type possible_keys key key_len ref rows Extra
545
1 SIMPLE t1 ALL k1 NULL NULL NULL 15 Using where
547
select max(t1.a3), min(t2.a2) from t1, t2 where t1.a2 = 2 and t1.a3 < 'MIN' and t2.a3 > 'CA';
548
id select_type table type possible_keys key key_len ref rows Extra
549
1 SIMPLE t1 ref k1 k1 5 const 1 Using where
550
1 SIMPLE t2 ALL k1 NULL NULL NULL 7 Using where; Using join buffer
552
select min(a4 - 0.01) from t1;
553
id select_type table type possible_keys key key_len ref rows Extra
554
1 SIMPLE t1 ALL NULL NULL NULL NULL 15
556
select max(a4 + 0.01) from t1;
557
id select_type table type possible_keys key key_len ref rows Extra
558
1 SIMPLE t1 ALL NULL NULL NULL NULL 15
560
select min(a3) from t1 where (a2 +1 ) is null;
561
id select_type table type possible_keys key key_len ref rows Extra
562
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using where
564
select min(a3) from t1 where (a2 + 1) = 2;
565
id select_type table type possible_keys key key_len ref rows Extra
566
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using where
568
select min(a3) from t1 where 2 = (a2 + 1);
569
id select_type table type possible_keys key key_len ref rows Extra
570
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using where
572
select min(a2) from t1 where a2 < 2 * a2 - 8;
573
id select_type table type possible_keys key key_len ref rows Extra
574
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using where
576
select min(a1) from t1 where a1 between a3 and 'KKK';
577
id select_type table type possible_keys key key_len ref rows Extra
578
1 SIMPLE t1 ALL PRIMARY NULL NULL NULL 15 Using where
580
select min(a4) from t1 where (a4 + 0.01) between 0.07 and 0.08;
581
id select_type table type possible_keys key key_len ref rows Extra
582
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using where
584
select concat(min(t1.a1),min(t2.a4)) from t1, t2 where t2.a4 <> 'AME';
585
id select_type table type possible_keys key key_len ref rows Extra
586
1 SIMPLE t2 ALL k2 NULL NULL NULL 7 Using where
587
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using join buffer
589
create table t1 (a char);
590
insert into t1 values ('a'),('b');
591
show create table t1;
593
t1 CREATE TABLE `t1` (
594
`a` VARCHAR(1) COLLATE utf8_general_ci DEFAULT NULL
595
) ENGINE=DEFAULT COLLATE = utf8_general_ci
596
create table t2 select max(a),min(a) from t1;
597
show create table t2;
599
t2 CREATE TABLE `t2` (
600
`max(a)` VARCHAR(1) COLLATE utf8_general_ci DEFAULT NULL,
601
`min(a)` VARCHAR(1) COLLATE utf8_general_ci DEFAULT NULL
602
) ENGINE=DEFAULT COLLATE = utf8_general_ci
604
create table t2 select concat(a) from t1;
605
show create table t2;
607
t2 CREATE TABLE `t2` (
608
`concat(a)` VARCHAR(1) COLLATE utf8_general_ci DEFAULT NULL
609
) ENGINE=DEFAULT COLLATE = utf8_general_ci
611
create table t1 (a int);
612
insert into t1 values (1);
613
select max(a) as b from t1 having b=1;
616
select a from t1 having a=1;
620
create table t1 (a int);
621
select variance(2) from t1;
624
select stddev(2) from t1;
628
create table t1 (a int);
629
insert into t1 values (1),(2);
630
SELECT COUNT(*) FROM t1;
633
SELECT COUNT(*) FROM t1;
636
SELECT COUNT(*) FROM t1;
640
create table t1 (a int, primary key(a));
641
insert into t1 values (1),(2);
642
SELECT max(a) FROM t1;
645
SELECT max(a) FROM t1;
648
SELECT max(a) FROM t1;
652
CREATE TABLE t1 (a int primary key);
653
INSERT INTO t1 VALUES (1),(2),(3),(4);
654
SELECT MAX(a) FROM t1 WHERE a > 5;
657
SELECT MIN(a) FROM t1 WHERE a < 0;
661
CREATE TEMPORARY TABLE t1 (
662
id int NOT NULL auto_increment,
663
val enum('one','two','three') NOT NULL default 'one',
666
INSERT INTO t1 VALUES
667
(1,'one'),(2,'two'),(3,'three'),(4,'one'),(5,'two');
668
select val, count(*) from t1 group by val;
674
create table t1(a int, b datetime);
675
insert into t1 values (1, NOW()), (2, NOW());
676
create table t2 select MAX(b) from t1 group by a;
677
show create table t2;
679
t2 CREATE TABLE `t2` (
680
`MAX(b)` DATETIME DEFAULT NULL
681
) ENGINE=DEFAULT COLLATE = utf8_general_ci
683
create table t1(f1 datetime);
684
insert into t1 values (now());
685
create table t2 select f2 from (select max(now()) f2 from t1) a;
686
show columns from t2;
687
Field Type Null Default Default_is_NULL On_Update
690
create table t2 select f2 from (select now() f2 from t1) a;
691
show columns from t2;
692
Field Type Null Default Default_is_NULL On_Update
699
INDEX i_b_id(a,b,id),
702
INSERT INTO t1 VALUES
703
(1,1,4), (2,2,1), (3,1,3), (4,2,1), (5,1,1);
704
SELECT MAX(id) FROM t1 WHERE id < 3 AND a=2 AND b=6;
715
INSERT INTO t1 VALUES
716
(1,1,4), (2,2,1), (3,1,3), (4,2,1), (5,1,1);
717
SELECT MAX(id) FROM t1 WHERE id < 3 AND a=2 AND b=6;
721
CREATE TABLE t1 (id int PRIMARY KEY, b char(3), INDEX(b));
722
INSERT INTO t1 VALUES (1,'xx'), (2,'aa');
727
SELECT MAX(b) FROM t1 WHERE b < 'ppppp';
732
SELECT MAX(b) FROM t1 WHERE b < 'pp';
736
CREATE TABLE t1 (id int PRIMARY KEY, b char(16), INDEX(b(4)));
737
INSERT INTO t1 VALUES (1, 'xxxxbbbb'), (2, 'xxxxaaaa');
738
SELECT MAX(b) FROM t1;
741
EXPLAIN SELECT MAX(b) FROM t1;
742
id select_type table type possible_keys key key_len ref rows Extra
743
1 SIMPLE t1 ALL NULL NULL NULL NULL 2
745
CREATE TABLE t1 (a INT, b INT);
746
INSERT INTO t1 VALUES (1,1),(1,2),(2,3);
747
SELECT (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
748
(SELECT COUNT(DISTINCT t1.b))
751
SELECT (SELECT COUNT(DISTINCT 12)) FROM t1 GROUP BY t1.a;
752
(SELECT COUNT(DISTINCT 12))
755
SELECT AVG(2), COUNT(*), COUNT(12),
756
COUNT(DISTINCT 12), MIN(2),MAX(2),STD(2), VARIANCE(2),SUM(2),
757
GROUP_CONCAT(2),GROUP_CONCAT(DISTINCT 2);
758
AVG(2) COUNT(*) COUNT(12) COUNT(DISTINCT 12) MIN(2) MAX(2) STD(2) VARIANCE(2) SUM(2) GROUP_CONCAT(2) GROUP_CONCAT(DISTINCT 2)
759
2.00000 1 1 1 2 2 0.00000 0.00000 2 2 2
761
create table t2 (ff double);
762
insert into t2 values (2.2);
763
select cast(sum(distinct ff) as decimal(5,2)) from t2;
764
cast(sum(distinct ff) as decimal(5,2))
766
select sum(distinct ff) from t2;
769
select cast(variance(ff) as decimal(10,3)) from t2;
770
cast(variance(ff) as decimal(10,3))
772
select cast(min(ff) as decimal(5,2)) from t2;
773
cast(min(ff) as decimal(5,2))
775
create table t1 (df decimal(5,1));
776
insert into t1 values(1.1);
777
insert into t1 values(2.2);
778
select sum(distinct df) from t1;
781
select min(df) from t1;
784
select 1e8 * sum(distinct df) from t1;
785
1e8 * sum(distinct df)
787
select 1e8 * min(df) from t1;
790
create table t3 (ifl int);
791
insert into t3 values(1), (2);
792
select cast(min(ifl) as decimal(5,2)) from t3;
793
cast(min(ifl) as decimal(5,2))
795
drop table t1, t2, t3;
796
CREATE TABLE t1 (id int,value1 float(10,2));
797
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);
798
select id, stddev_pop(value1), var_pop(value1), stddev_samp(value1), var_samp(value1) from t1 group by id;
799
id stddev_pop(value1) var_pop(value1) stddev_samp(value1) var_samp(value1)
800
1 0.816497 0.666667 1.000000 1.000000
801
2 1.118034 1.250000 1.290994 1.666667
803
CREATE TABLE t1 (col1 decimal(16,12));
804
INSERT INTO t1 VALUES (-5.00000000001),(-5.00000000002),(-5.00000000003),(-5.00000000000),(-5.00000000001),(-5.00000000002);
805
insert into t1 select * from t1;
806
select col1,count(col1),sum(col1),avg(col1) from t1 group by col1;
807
col1 count(col1) sum(col1) avg(col1)
808
-5.000000000030 2 -10.000000000060 -5.00000000003000000
809
-5.000000000020 4 -20.000000000080 -5.00000000002000000
810
-5.000000000010 4 -20.000000000040 -5.00000000001000000
811
-5.000000000000 2 -10.000000000000 -5.00000000000000000
813
create table t1 (col1 decimal(16,12));
814
insert into t1 values (-5.00000000001);
815
insert into t1 values (-5.00000000001);
816
select col1,sum(col1),max(col1),min(col1) from t1 group by col1;
817
col1 sum(col1) max(col1) min(col1)
818
-5.000000000010 -10.000000000020 -5.000000000010 -5.000000000010
820
insert into t1 values (5.00000000001);
821
insert into t1 values (5.00000000001);
822
select col1,sum(col1),max(col1),min(col1) from t1 group by col1;
823
col1 sum(col1) max(col1) min(col1)
824
5.000000000010 10.000000000020 5.000000000010 5.000000000010
826
CREATE TABLE t1 (a VARCHAR(400));
827
INSERT INTO t1 (a) VALUES ("A"), ("a"), ("a "), ("a "),
828
("B"), ("b"), ("b "), ("b ");
829
SELECT COUNT(DISTINCT a) FROM t1;
833
CREATE TABLE t1 (a int, b int, c int);
834
INSERT INTO t1 (a, b, c) VALUES
835
(1,1,1), (1,1,2), (1,1,3),
836
(1,2,1), (1,2,2), (1,2,3),
837
(1,3,1), (1,3,2), (1,3,3),
838
(2,1,1), (2,1,2), (2,1,3),
839
(2,2,1), (2,2,2), (2,2,3),
840
(2,3,1), (2,3,2), (2,3,3),
841
(3,1,1), (3,1,2), (3,1,3),
842
(3,2,1), (3,2,2), (3,2,3),
843
(3,3,1), (3,3,2), (3,3,3);
844
SELECT b/c as v, a FROM t1 ORDER BY v;
873
SELECT b/c as v, SUM(a) FROM t1 GROUP BY v;
881
SELECT SUM(a) FROM t1 GROUP BY b/c;
890
set div_precision_increment= @sav_dpi;
891
CREATE TABLE t1 (a INT PRIMARY KEY, b INT);
892
INSERT INTO t1 VALUES (1,1), (2,2);
893
CREATE TABLE t2 (a INT PRIMARY KEY, b INT);
894
INSERT INTO t2 VALUES (1,1), (3,3);
896
(SELECT SUM(c.a) FROM t1 ttt, t2 ccc
897
WHERE ttt.a = ccc.b AND ttt.a = t.a GROUP BY ttt.a) AS minid
898
FROM t1 t, t2 c WHERE t.a = c.b;
902
create table t1 select variance(0);
903
show create table t1;
905
t1 CREATE TABLE `t1` (
906
`variance(0)` DOUBLE(8,4) DEFAULT NULL
907
) ENGINE=DEFAULT COLLATE = utf8_general_ci
909
create table t1 select stddev(0);
910
show create table t1;
912
t1 CREATE TABLE `t1` (
913
`stddev(0)` DOUBLE(8,4) DEFAULT NULL
914
) ENGINE=DEFAULT COLLATE = utf8_general_ci
916
create table bug22555 (i int primary key auto_increment, s1 int, s2 int, e decimal(30,10), o double);
917
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);
918
select std(s1/s2) from bug22555 group by i;
930
select std(e) from bug22555 group by i;
942
select std(o) from bug22555 group by i;
955
create table bug22555 (i int, s1 int, s2 int, o1 double, o2 double, e1 decimal, e2 decimal);
956
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);
957
select i, count(*) from bug22555 group by i;
962
select std(s1/s2) from bug22555 where i=1;
965
select std(s1/s2) from bug22555 where i=2;
968
select std(s1/s2) from bug22555 where i=3;
971
select std(s1/s2) from bug22555 where i=1 group by i;
974
select std(s1/s2) from bug22555 where i=2 group by i;
977
select std(s1/s2) from bug22555 where i=3 group by i;
980
select std(s1/s2) from bug22555 group by i order by i;
985
select i, count(*), std(o1/o2) from bug22555 group by i order by i;
986
i count(*) std(o1/o2)
990
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
991
i count(*) std(e1/e2)
995
set @saved_div_precision_increment=@@div_precision_increment;
996
set div_precision_increment=19;
997
select i, count(*), variance(s1/s2) from bug22555 group by i order by i;
998
i count(*) variance(s1/s2)
999
1 1 0.000000000000000000000000000000
1000
2 1 0.000000000000000000000000000000
1001
3 1 0.000000000000000000000000000000
1002
select i, count(*), variance(o1/o2) from bug22555 group by i order by i;
1003
i count(*) variance(o1/o2)
1007
select i, count(*), variance(e1/e2) from bug22555 group by i order by i;
1008
i count(*) variance(e1/e2)
1009
1 1 0.000000000000000000000000000000
1010
2 1 0.000000000000000000000000000000
1011
3 1 0.000000000000000000000000000000
1012
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1013
i count(*) std(s1/s2)
1014
1 1 0.000000000000000000000000000000
1015
2 1 0.000000000000000000000000000000
1016
3 1 0.000000000000000000000000000000
1017
select i, count(*), std(o1/o2) from bug22555 group by i order by i;
1018
i count(*) std(o1/o2)
1022
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1023
i count(*) std(e1/e2)
1024
1 1 0.000000000000000000000000000000
1025
2 1 0.000000000000000000000000000000
1026
3 1 0.000000000000000000000000000000
1027
set div_precision_increment=20;
1028
select i, count(*), variance(s1/s2) from bug22555 group by i order by i;
1029
i count(*) variance(s1/s2)
1030
1 1 0.000000000000000000000000000000
1031
2 1 0.000000000000000000000000000000
1032
3 1 0.000000000000000000000000000000
1033
select i, count(*), variance(o1/o2) from bug22555 group by i order by i;
1034
i count(*) variance(o1/o2)
1038
select i, count(*), variance(e1/e2) from bug22555 group by i order by i;
1039
i count(*) variance(e1/e2)
1040
1 1 0.000000000000000000000000000000
1041
2 1 0.000000000000000000000000000000
1042
3 1 0.000000000000000000000000000000
1043
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1044
i count(*) std(s1/s2)
1045
1 1 0.000000000000000000000000000000
1046
2 1 0.000000000000000000000000000000
1047
3 1 0.000000000000000000000000000000
1048
select i, count(*), std(o1/o2) from bug22555 group by i order by i;
1049
i count(*) std(o1/o2)
1053
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1054
i count(*) std(e1/e2)
1055
1 1 0.000000000000000000000000000000
1056
2 1 0.000000000000000000000000000000
1057
3 1 0.000000000000000000000000000000
1058
set @@div_precision_increment=@saved_div_precision_increment;
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
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);
1061
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);
1062
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1063
i count(*) std(s1/s2)
1067
select i, count(*), round(std(o1/o2), 16) from bug22555 group by i order by i;
1068
i count(*) round(std(o1/o2), 16)
1069
1 4 0.0000000000000000
1070
2 4 0.0000000000000000
1071
3 4 0.0000000000000000
1072
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1073
i count(*) std(e1/e2)
1077
select std(s1/s2) from bug22555;
1080
select std(o1/o2) from bug22555;
1083
select std(e1/e2) from bug22555;
1086
set @saved_div_precision_increment=@@div_precision_increment;
1087
set div_precision_increment=19;
1088
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1089
i count(*) std(s1/s2)
1090
1 4 0.000000000000000000000000000000
1091
2 4 0.000000000000000000000000000000
1092
3 4 0.000000000000000000000000000000
1093
select i, count(*), round(std(o1/o2), 16) from bug22555 group by i order by i;
1094
i count(*) round(std(o1/o2), 16)
1095
1 4 0.0000000000000000
1096
2 4 0.0000000000000000
1097
3 4 0.0000000000000000
1098
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1099
i count(*) std(e1/e2)
1100
1 4 0.000000000000000000000000000000
1101
2 4 0.000000000000000000000000000000
1102
3 4 0.000000000000000000000000000000
1103
select round(std(s1/s2), 17) from bug22555;
1104
round(std(s1/s2), 17)
1106
select std(o1/o2) from bug22555;
1109
select round(std(e1/e2), 17) from bug22555;
1110
round(std(e1/e2), 17)
1112
set div_precision_increment=20;
1113
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1114
i count(*) std(s1/s2)
1115
1 4 0.000000000000000000000000000000
1116
2 4 0.000000000000000000000000000000
1117
3 4 0.000000000000000000000000000000
1118
select i, count(*), round(std(o1/o2), 16) from bug22555 group by i order by i;
1119
i count(*) round(std(o1/o2), 16)
1120
1 4 0.0000000000000000
1121
2 4 0.0000000000000000
1122
3 4 0.0000000000000000
1123
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1124
i count(*) std(e1/e2)
1125
1 4 0.000000000000000000000000000000
1126
2 4 0.000000000000000000000000000000
1127
3 4 0.000000000000000000000000000000
1128
select round(std(s1/s2), 17) from bug22555;
1129
round(std(s1/s2), 17)
1131
select std(o1/o2) from bug22555;
1134
select round(std(e1/e2), 17) from bug22555;
1135
round(std(e1/e2), 17)
1137
set @@div_precision_increment=@saved_div_precision_increment;
1138
drop table bug22555;
1139
create table bug22555 (s int, o double, e decimal);
1140
insert into bug22555 values (1,1,1),(2,2,2),(3,3,3),(6,6,6),(7,7,7);
1141
select var_samp(s), var_pop(s) from bug22555;
1142
var_samp(s) var_pop(s)
1144
select var_samp(o), var_pop(o) from bug22555;
1145
var_samp(o) var_pop(o)
1147
select var_samp(e), var_pop(e) from bug22555;
1148
var_samp(e) var_pop(e)
1150
drop table bug22555;
1151
create table bug22555 (s int, o double, e decimal);
1152
insert into bug22555 values (null,null,null),(null,null,null);
1153
select var_samp(s) as 'null', var_pop(s) as 'null' from bug22555;
1156
select var_samp(o) as 'null', var_pop(o) as 'null' from bug22555;
1159
select var_samp(e) as 'null', var_pop(e) as 'null' from bug22555;
1162
insert into bug22555 values (1,1,1);
1163
select var_samp(s) as 'null', var_pop(s) as '0' from bug22555;
1166
select var_samp(o) as 'null', var_pop(o) as '0' from bug22555;
1169
select var_samp(e) as 'null', var_pop(e) as '0' from bug22555;
1172
insert into bug22555 values (2,2,2);
1173
select var_samp(s) as '0.5', var_pop(s) as '0.25' from bug22555;
1176
select var_samp(o) as '0.5', var_pop(o) as '0.25' from bug22555;
1179
select var_samp(e) as '0.5', var_pop(e) as '0.25' from bug22555;
1182
drop table bug22555;
1183
create table t1 (a decimal(20));
1184
insert into t1 values (12345678901234567890);
1185
select count(a) from t1;
1188
select count(distinct a) from t1;
1192
CREATE TABLE t1 (a INT, b INT);
1193
INSERT INTO t1 VALUES (1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8);
1194
INSERT INTO t1 SELECT a, b+8 FROM t1;
1195
INSERT INTO t1 SELECT a, b+16 FROM t1;
1196
INSERT INTO t1 SELECT a, b+32 FROM t1;
1197
INSERT INTO t1 SELECT a, b+64 FROM t1;
1198
INSERT INTO t1 SELECT a, b+128 FROM t1;
1199
INSERT INTO t1 SELECT a, b+256 FROM t1;
1200
INSERT INTO t1 SELECT a, b+512 FROM t1;
1201
INSERT INTO t1 SELECT a, b+1024 FROM t1;
1202
INSERT INTO t1 SELECT a, b+2048 FROM t1;
1203
INSERT INTO t1 SELECT a, b+4096 FROM t1;
1204
INSERT INTO t1 SELECT a, b+8192 FROM t1;
1205
INSERT INTO t1 SELECT a, b+16384 FROM t1;
1206
INSERT INTO t1 SELECT a, b+32768 FROM t1;
1207
SELECT a,COUNT(DISTINCT b) AS cnt FROM t1 GROUP BY a HAVING cnt > 50;
1210
SELECT a,SUM(DISTINCT b) AS sumation FROM t1 GROUP BY a HAVING sumation > 50;
1213
SELECT a,AVG(DISTINCT b) AS average FROM t1 GROUP BY a HAVING average > 50;
1217
CREATE TABLE t1 ( a INT, b INT, KEY(a) );
1218
INSERT INTO t1 VALUES (NULL, 1), (NULL, 2);
1219
EXPLAIN SELECT MIN(a), MIN(b) FROM t1;
1220
id select_type table type possible_keys key key_len ref rows Extra
1221
1 SIMPLE t1 ALL NULL NULL NULL NULL 2
1222
SELECT MIN(a), MIN(b) FROM t1;
1225
CREATE TABLE t2( a INT, b INT, c INT, KEY(a, b) );
1226
INSERT INTO t2 ( a, b, c ) VALUES ( 1, NULL, 2 ), ( 1, 3, 4 ), ( 1, 4, 4 );
1227
EXPLAIN SELECT MIN(b), MIN(c) FROM t2 WHERE a = 1;
1228
id select_type table type possible_keys key key_len ref rows Extra
1229
1 SIMPLE t2 ref a a 5 const 1 Using where
1230
SELECT MIN(b), MIN(c) FROM t2 WHERE a = 1;
1233
CREATE TABLE t3 (a INT, b INT, c int, KEY(a, b));
1234
INSERT INTO t3 VALUES (1, NULL, 1), (2, NULL, 2), (2, NULL, 2), (3, NULL, 3);
1235
EXPLAIN SELECT MIN(a), MIN(b) FROM t3 where a = 2;
1236
id select_type table type possible_keys key key_len ref rows Extra
1237
1 SIMPLE t3 ref a a 5 const 1 Using where
1238
SELECT MIN(a), MIN(b) FROM t3 where a = 2;
1241
CREATE TABLE t4 (a INT, b INT, c int, KEY(a, b));
1242
INSERT INTO t4 VALUES (1, 1, 1), (2, NULL, 2), (2, NULL, 2), (3, 1, 3);
1243
EXPLAIN SELECT MIN(a), MIN(b) FROM t4 where a = 2;
1244
id select_type table type possible_keys key key_len ref rows Extra
1245
1 SIMPLE t4 ref a a 5 const 1 Using where
1246
SELECT MIN(a), MIN(b) FROM t4 where a = 2;
1249
SELECT MIN(b), min(c) FROM t4 where a = 2;
1252
CREATE TABLE t5( a INT, b INT, KEY( a, b) );
1253
INSERT INTO t5 VALUES( 1, 1 ), ( 1, 2 );
1254
EXPLAIN SELECT MIN(a), MIN(b) FROM t5 WHERE a = 1;
1255
id select_type table type possible_keys key key_len ref rows Extra
1256
1 SIMPLE t5 ref a a 5 const 1 Using where
1257
SELECT MIN(a), MIN(b) FROM t5 WHERE a = 1;
1260
SELECT MIN(a), MIN(b) FROM t5 WHERE a = 1 and b > 1;
1263
DROP TABLE t1, t2, t3, t4, t5;
1264
CREATE TABLE t1 (a int, b date NOT NULL, KEY k1 (a,b));
1265
SELECT MIN(b) FROM t1 WHERE a=1 AND b>'2007-08-01';
1269
CREATE TABLE t1(a DOUBLE);
1270
INSERT INTO t1 VALUES (10), (20);
1271
SELECT AVG(a), CAST(AVG(a) AS DECIMAL) FROM t1;
1272
AVG(a) CAST(AVG(a) AS DECIMAL)