1
drop table if exists t1,t2,t3;
2
SELECT 1 FROM (SELECT 1) as a GROUP BY SUM(1);
3
ERROR HY000: Invalid use of group function
11
INSERT INTO t1 VALUES (1,1,1,'',NULL);
12
INSERT INTO t1 VALUES (2,2,2,'',NULL);
13
INSERT INTO t1 VALUES (2,1,1,'',NULL);
14
INSERT INTO t1 VALUES (3,3,3,'',NULL);
16
userID int NOT NULL auto_increment,
20
isAukt enum('N','Y') DEFAULT 'N',
29
INSERT INTO t2 VALUES (1,'name','pass','mail','Y','v','n','adr','1','1','1');
30
INSERT INTO t2 VALUES (2,'name','pass','mail','Y','v','n','adr','1','1','1');
31
INSERT INTO t2 VALUES (3,'name','pass','mail','Y','v','n','adr','1','1','1');
32
INSERT INTO t2 VALUES (4,'name','pass','mail','Y','v','n','adr','1','1','1');
33
INSERT INTO t2 VALUES (5,'name','pass','mail','Y','v','n','adr','1','1','1');
34
SELECT t2.userid, MIN(t1.score) FROM t1, t2 WHERE t1.userID=t2.userID GROUP BY t2.userid;
39
SELECT t2.userid, MIN(t1.score) FROM t1, t2 WHERE t1.userID=t2.userID GROUP BY t2.userid ORDER BY NULL;
44
SELECT t2.userid, MIN(t1.score) FROM t1, t2 WHERE t1.userID=t2.userID AND t1.spID=2 GROUP BY t2.userid;
48
SELECT t2.userid, MIN(t1.score+0.0) FROM t1, t2 WHERE t1.userID=t2.userID AND t1.spID=2 GROUP BY t2.userid;
49
userid MIN(t1.score+0.0)
52
SELECT t2.userid, MIN(t1.score+0.0) FROM t1, t2 WHERE t1.userID=t2.userID AND t1.spID=2 GROUP BY t2.userid ORDER BY NULL;
53
userid MIN(t1.score+0.0)
56
EXPLAIN SELECT t2.userid, MIN(t1.score+0.0) FROM t1, t2 WHERE t1.userID=t2.userID AND t1.spID=2 GROUP BY t2.userid ORDER BY NULL;
57
id select_type table type possible_keys key key_len ref rows Extra
58
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using where; Using temporary
59
1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.userID 1 Using index
62
PID int NOT NULL auto_increment,
65
URID int DEFAULT '0' NOT NULL,
66
CRID int DEFAULT '0' NOT NULL,
67
amount int DEFAULT '0' NOT NULL,
69
method enum('unknown','cash','dealer','check','card','lazy','delayed','test') DEFAULT 'unknown' NOT NULL,
71
reason char(1) binary DEFAULT '' NOT NULL,
73
qty int DEFAULT '0' NOT NULL,
80
INSERT INTO t1 VALUES (1,'1970-01-01','1997-10-17 00:00:00',2529,1,21000,11886,'check',0,'F',16200,6);
81
SELECT COUNT(P.URID),SUM(P.amount),P.method, MIN(PP.recdate+0) > 19980501000000 AS IsNew FROM t1 AS P JOIN t1 as PP WHERE P.URID = PP.URID GROUP BY method,IsNew;
82
ERROR 42000: Can't group on 'IsNew'
85
cid int NOT NULL auto_increment,
86
firstname varchar(32) DEFAULT '' NOT NULL,
87
surname varchar(32) DEFAULT '' NOT NULL,
90
INSERT INTO t1 VALUES (1,'That','Guy');
91
INSERT INTO t1 VALUES (2,'Another','Gent');
93
call_id int NOT NULL auto_increment,
94
contact_id int DEFAULT '0' NOT NULL,
95
PRIMARY KEY (call_id),
96
KEY contact_id (contact_id)
98
INSERT INTO t2 VALUES (10,2);
99
INSERT INTO t2 VALUES (18,2);
100
INSERT INTO t2 VALUES (62,2);
101
INSERT INTO t2 VALUES (91,2);
102
INSERT INTO t2 VALUES (92,2);
103
SELECT cid, CONCAT(firstname, ' ', surname), COUNT(call_id) FROM t1 LEFT JOIN t2 ON cid=contact_id WHERE firstname like '%foo%' GROUP BY cid;
104
cid CONCAT(firstname, ' ', surname) COUNT(call_id)
105
SELECT cid, CONCAT(firstname, ' ', surname), COUNT(call_id) FROM t1 LEFT JOIN t2 ON cid=contact_id WHERE firstname like '%foo%' GROUP BY cid ORDER BY NULL;
106
cid CONCAT(firstname, ' ', surname) COUNT(call_id)
107
SELECT cid, CONCAT(firstname, ' ', surname), COUNT(call_id) FROM t1 LEFT JOIN t2 ON cid=contact_id WHERE firstname like '%foo%' GROUP BY cid ORDER BY surname, firstname;
108
cid CONCAT(firstname, ' ', surname) COUNT(call_id)
111
create table t1 (foo int);
112
insert into t1 values (1);
113
select 1+1, "a",count(*) from t1 where foo in (2);
116
insert into t1 values (1);
117
select 1+1,"a",count(*) from t1 where foo in (2);
128
INSERT INTO t1 VALUES (1,1,1),(2,2,2),(2,1,1),(3,3,3),(4,3,3),(5,3,3),(6,3,3),(7,3,3);
129
explain select userid,count(*) from t1 group by userid desc;
130
id select_type table type possible_keys key key_len ref rows Extra
131
1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using temporary; Using filesort
132
explain select userid,count(*) from t1 group by userid desc order by null;
133
id select_type table type possible_keys key key_len ref rows Extra
134
1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using temporary
135
select userid,count(*) from t1 group by userid desc;
140
select userid,count(*) from t1 group by userid desc having (count(*)+1) IN (4,3);
143
select userid,count(*) from t1 group by userid desc having 3 IN (1,COUNT(*));
145
explain select spid,count(*) from t1 where spid between 1 and 2 group by spid desc;
146
id select_type table type possible_keys key key_len ref rows Extra
147
1 SIMPLE t1 range spID spID 5 NULL 1 Using where; Using index
148
explain select spid,count(*) from t1 where spid between 1 and 2 group by spid;
149
id select_type table type possible_keys key key_len ref rows Extra
150
1 SIMPLE t1 range spID spID 5 NULL 1 Using where; Using index
151
explain select spid,count(*) from t1 where spid between 1 and 2 group by spid order by null;
152
id select_type table type possible_keys key key_len ref rows Extra
153
1 SIMPLE t1 range spID spID 5 NULL 1 Using where; Using index
154
select spid,count(*) from t1 where spid between 1 and 2 group by spid;
158
select spid,count(*) from t1 where spid between 1 and 2 group by spid desc;
162
explain extended select sql_big_result spid,sum(userid) from t1 group by spid desc;
163
id select_type table type possible_keys key key_len ref rows filtered Extra
164
1 SIMPLE t1 ALL NULL NULL NULL NULL 8 100.00 Using filesort
166
Note 1003 select sql_big_result `test`.`t1`.`spID` AS `spid`,sum(`test`.`t1`.`userID`) AS `sum(userid)` from `test`.`t1` group by `test`.`t1`.`spID` desc
167
explain select sql_big_result spid,sum(userid) from t1 group by spid desc order by null;
168
id select_type table type possible_keys key key_len ref rows Extra
169
1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using filesort
170
select sql_big_result spid,sum(userid) from t1 group by spid desc;
179
explain select sql_big_result score,count(*) from t1 group by score desc;
180
id select_type table type possible_keys key key_len ref rows Extra
181
1 SIMPLE t1 index NULL score 5 NULL 8 Using index; Using filesort
182
explain select sql_big_result score,count(*) from t1 group by score desc order by null;
183
id select_type table type possible_keys key key_len ref rows Extra
184
1 SIMPLE t1 index NULL score 5 NULL 8 Using index; Using filesort
185
select sql_big_result score,count(*) from t1 group by score desc;
191
create table t1 (a date default null, b date default null);
192
insert t1 values ('1999-10-01','2000-01-10'), ('1997-01-01','1998-10-01');
193
select a,min(b) c,count(distinct rand()) from t1 group by a having c<a + interval 1 day;
194
a c count(distinct rand())
196
CREATE TABLE t1 (a char(1));
197
INSERT INTO t1 VALUES ('A'),('B'),('A'),('B'),('A'),('B'),(NULL),('a'),('b'),(NULL),('A'),('B'),(NULL);
198
SELECT a FROM t1 GROUP BY a;
203
SELECT a,count(*) FROM t1 GROUP BY a;
208
SELECT a FROM t1 GROUP BY binary a;
215
SELECT a,count(*) FROM t1 GROUP BY binary a;
222
SELECT binary a FROM t1 GROUP BY 1;
229
SELECT binary a,count(*) FROM t1 GROUP BY 1;
236
SELECT a FROM t1 GROUP BY a;
241
SELECT a,count(*) FROM t1 GROUP BY a;
246
SELECT a FROM t1 GROUP BY binary a;
253
SELECT a,count(*) FROM t1 GROUP BY binary a;
260
SELECT binary a FROM t1 GROUP BY 1;
267
SELECT binary a,count(*) FROM t1 GROUP BY 1;
276
`a` char(193) default NULL,
277
`b` char(63) default NULL
279
INSERT INTO t1 VALUES ('abc','def'),('hij','klm');
280
SELECT CONCAT(a, b) FROM t1 GROUP BY 1;
284
SELECT CONCAT(a, b),count(*) FROM t1 GROUP BY 1;
285
CONCAT(a, b) count(*)
288
SELECT CONCAT(a, b),count(distinct a) FROM t1 GROUP BY 1;
289
CONCAT(a, b) count(distinct a)
292
SELECT 1 FROM t1 GROUP BY CONCAT(a, b);
296
INSERT INTO t1 values ('hij','klm');
297
SELECT CONCAT(a, b),count(*) FROM t1 GROUP BY 1;
298
CONCAT(a, b) count(*)
302
create table t1 (One int, Two int, Three int, Four int);
303
insert into t1 values (1,2,1,4),(1,2,2,4),(1,2,3,4),(1,2,4,4),(1,1,1,4),(1,1,2,4),(1,1,3,4),(1,1,4,4),(1,3,1,4),(1,3,2,4),(1,3,3,4),(1,3,4,4);
304
select One, Two, sum(Four) from t1 group by One,Two;
310
create table t1 (id integer primary key not null auto_increment, gender char(1));
311
insert into t1 values (NULL, 'M'), (NULL, 'F'),(NULL, 'F'),(NULL, 'F'),(NULL, 'M');
312
create table t2 (user_id integer not null, date date);
313
insert into t2 values (1, '2002-06-09'),(2, '2002-06-09'),(1, '2002-06-09'),(3, '2002-06-09'),(4, '2002-06-09'),(4, '2002-06-09');
314
select u.gender as gender, count(distinct u.id) as dist_count, (count(distinct u.id)/5*100) as percentage from t1 u, t2 l where l.user_id = u.id group by u.gender;
315
gender dist_count percentage
318
select u.gender as gender, count(distinct u.id) as dist_count, (count(distinct u.id)/5*100) as percentage from t1 u, t2 l where l.user_id = u.id group by u.gender order by percentage;
319
gender dist_count percentage
323
CREATE TABLE t1 (ID1 int, ID2 int, ID int NOT NULL AUTO_INCREMENT,PRIMARY KEY(ID
325
insert into t1 values (1,244,NULL),(2,243,NULL),(134,223,NULL),(185,186,NULL);
326
select S.ID as xID, S.ID1 as xID1 from t1 as S left join t1 as yS on S.ID1 between yS.ID1 and yS.ID2;
338
select S.ID as xID, S.ID1 as xID1, repeat('*',count(distinct yS.ID)) as Level from t1 as S left join t1 as yS on S.ID1 between yS.ID1 and yS.ID2 group by xID order by xID1;
345
CREATE TEMPORARY TABLE t1 (
346
pid int NOT NULL default '0',
347
c1id int default NULL,
348
c2id int default NULL,
349
value int NOT NULL default '0',
350
UNIQUE KEY pid2 (pid,c1id,c2id),
351
UNIQUE KEY pid (pid,value)
353
INSERT INTO t1 VALUES (1, 1, NULL, 1),(1, 2, NULL, 2),(1, NULL, 3, 3),(1, 4, NULL, 4),(1, 5, NULL, 5);
354
CREATE TEMPORARY TABLE t2 (
355
id int NOT NULL default '0',
356
active enum('Yes','No') NOT NULL default 'Yes',
359
INSERT INTO t2 VALUES (1, 'Yes'),(2, 'No'),(4, 'Yes'),(5, 'No');
361
id int NOT NULL default '0',
362
active enum('Yes','No') NOT NULL default 'Yes',
365
INSERT INTO t3 VALUES (3, 'Yes');
366
select * from t1 AS m LEFT JOIN t2 AS c1 ON m.c1id =
367
c1.id AND c1.active = 'Yes' LEFT JOIN t3 AS c2 ON m.c2id = c2.id AND
368
c2.active = 'Yes' WHERE m.pid=1 AND (c1.id IS NOT NULL OR c2.id IS NOT NULL);
369
pid c1id c2id value id active id active
370
1 1 NULL 1 1 Yes NULL NULL
371
1 NULL 3 3 NULL NULL 3 Yes
372
1 4 NULL 4 4 Yes NULL NULL
373
select max(value) from t1 AS m LEFT JOIN t2 AS c1 ON
374
m.c1id = c1.id AND c1.active = 'Yes' LEFT JOIN t3 AS c2 ON m.c2id =
375
c2.id AND c2.active = 'Yes' WHERE m.pid=1 AND (c1.id IS NOT NULL OR c2.id IS
380
create table t1 (a blob null);
381
insert into t1 values (NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(""),(""),(""),("b");
382
select a,count(*) from t1 group by a;
387
select a,count(*) from t1 group by a;
393
create table t1 (a int not null, b int not null);
394
insert into t1 values (1,1),(1,2),(3,1),(3,2),(2,2),(2,1);
395
create table t2 (a int not null, b int not null, key(a));
396
insert into t2 values (1,3),(3,1),(2,2),(1,1);
397
select t1.a,t2.b from t1,t2 where t1.a=t2.a group by t1.a,t2.b;
403
select t1.a,t2.b from t1,t2 where t1.a=t2.a group by t1.a,t2.b ORDER BY NULL;
409
explain select t1.a,t2.b from t1,t2 where t1.a=t2.a group by t1.a,t2.b;
410
id select_type table type possible_keys key key_len ref rows Extra
411
1 SIMPLE t1 ALL NULL NULL NULL NULL 6 Using temporary; Using filesort
412
1 SIMPLE t2 ALL a NULL NULL NULL 4 Using where; Using join buffer
413
explain select t1.a,t2.b from t1,t2 where t1.a=t2.a group by t1.a,t2.b ORDER BY NULL;
414
id select_type table type possible_keys key key_len ref rows Extra
415
1 SIMPLE t1 ALL NULL NULL NULL NULL 6 Using temporary
416
1 SIMPLE t2 ALL a NULL NULL NULL 4 Using where; Using join buffer
418
create table t1 (a int, b int);
419
insert into t1 values (1, 4),(10, 40),(1, 4),(10, 43),(1, 4),(10, 41),(1, 4),(10, 43),(1, 4);
420
select a, MAX(b), INTERVAL (MAX(b), 1,3,10,30,39,40,50,60,100,1000) from t1 group by a;
421
a MAX(b) INTERVAL (MAX(b), 1,3,10,30,39,40,50,60,100,1000)
424
select a, MAX(b), CASE MAX(b) when 4 then 4 when 43 then 43 else 0 end from t1 group by a;
425
a MAX(b) CASE MAX(b) when 4 then 4 when 43 then 43 else 0 end
428
select a, MAX(b), FIELD(MAX(b), '43', '4', '5') from t1 group by a;
429
a MAX(b) FIELD(MAX(b), '43', '4', '5')
432
select a, MAX(b), CONCAT_WS(MAX(b), '43', '4', '5') from t1 group by a;
433
a MAX(b) CONCAT_WS(MAX(b), '43', '4', '5')
436
select a, MAX(b), ELT(MAX(b), 'a', 'b', 'c', 'd', 'e', 'f') from t1 group by a;
437
a MAX(b) ELT(MAX(b), 'a', 'b', 'c', 'd', 'e', 'f')
440
select a, MAX(b), MAKE_SET(MAX(b), 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h') from t1 group by a;
441
a MAX(b) MAKE_SET(MAX(b), 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
445
create table t1 (id int not null, qty int not null);
446
insert into t1 values (1,2),(1,3),(2,4),(2,5);
447
select id, sum(qty) as sqty, count(qty) as cqty from t1 group by id having sum(qty)>2 and cqty>1;
451
select id, sum(qty) as sqty from t1 group by id having sqty>2 and count(qty)>1;
455
select id, sum(qty) as sqty, count(qty) as cqty from t1 group by id having sqty>2 and cqty>1;
459
select id, sum(qty) as sqty, count(qty) as cqty from t1 group by id having sum(qty)>2 and count(qty)>1;
463
select count(*), case interval(qty,2,3,4,5,6,7,8) when -1 then NULL when 0 then "zero" when 1 then "one" when 2 then "two" end as category from t1 group by category;
468
select count(*), interval(qty,2,3,4,5,6,7,8) as category from t1 group by category;
480
INSERT INTO t1 VALUES (1,1),(2,2),(1,1),(3,3),(3,3),(3,3),(3,3),(3,3);
481
SELECT userid,count(*) FROM t1 GROUP BY userid DESC;
486
EXPLAIN SELECT userid,count(*) FROM t1 GROUP BY userid DESC;
487
id select_type table type possible_keys key key_len ref rows Extra
488
1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using temporary; Using filesort
494
INSERT INTO t1 VALUES (1,2),(2,3),(4,5),(3,5),(1,5),(23,5);
495
SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL;
500
explain SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL;
501
id select_type table type possible_keys key key_len ref rows Extra
502
1 SIMPLE t1 ALL NULL NULL NULL NULL 6 Using filesort
504
create table t1 (a int);
505
insert into t1 values(null);
506
select min(a) is null from t1;
509
select min(a) is null or null from t1;
510
min(a) is null or null
512
select 1 and min(a) is null from t1;
516
create table t1 ( col1 int, col2 int );
517
insert into t1 values (1,1),(1,2),(1,3),(2,1),(2,2);
518
select group_concat( distinct col1 ) as alias from t1
519
group by col2 having alias like '%';
525
create table t1 (a integer, b integer, c integer);
526
insert into t1 (a,b) values (1,2),(1,3),(2,5);
527
select a, 0.1*0+1 r2, sum(1) r1 from t1 where a = 1 group by a having r1>1 and r2=1;
530
select a, round(rand(100)*10) r2, sum(1) r1 from t1 where a = 1 group by a having r1>1 and r2<=2;
533
select a,sum(b) from t1 where a=1 group by c;
536
select a*sum(b) from t1 where a=1 group by c;
539
select sum(a)*sum(b) from t1 where a=1 group by c;
542
select a,sum(b) from t1 where a=1 group by c having a=1;
545
select a as d,sum(b) from t1 where a=1 group by c having d=1;
548
select sum(a)*sum(b) as d from t1 where a=1 group by c having d > 0;
552
create table t1(a int);
553
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(8),(9);
556
b varchar(200) NOT NULL,
557
c varchar(50) NOT NULL,
558
d varchar(100) NOT NULL,
559
primary key (a,b(132),c,d),
562
insert into t2 select
564
concat('val-', x3.a + 3*x4.a), -- 12
565
concat('val-', @a:=x3.a + 3*x4.a + 12*C.a), -- 120
566
concat('val-', @a + 120*D.a)
567
from t1 x3, t1 x4, t1 C, t1 D where x3.a < 3 and x4.a < 4 and D.a < 4;
568
delete from t2 where a = 2 and b = 'val-2' order by a,b,c,d limit 30;
569
explain select c from t2 where a = 2 and b = 'val-2' group by c;
570
id select_type table type possible_keys key key_len ref rows Extra
571
1 SIMPLE t2 ref PRIMARY,a PRIMARY 534 const,const 1 Using where
572
select c from t2 where a = 2 and b = 'val-2' group by c;
577
create table t1 (b int4 not null);
578
insert into t1 values(300000);
582
select min(b) from t1;
586
CREATE TABLE t1 (id int PRIMARY KEY, user_id int, hostname longtext);
587
INSERT INTO t1 VALUES
588
(1, 7, 'cache-dtc-af05.proxy.aol.com'),
589
(2, 3, 'what.ever.com'),
590
(3, 7, 'cache-dtc-af05.proxy.aol.com'),
591
(4, 7, 'cache-dtc-af05.proxy.aol.com');
592
SELECT hostname, COUNT(DISTINCT user_id) as no FROM t1
593
WHERE hostname LIKE '%aol%'
596
cache-dtc-af05.proxy.aol.com 1
598
CREATE TABLE t1 (a int, b int);
599
INSERT INTO t1 VALUES (1,2), (1,3);
600
SELECT a, b FROM t1 GROUP BY 'const';
603
SELECT DISTINCT a, b FROM t1 GROUP BY 'const';
607
CREATE TABLE t1 (id INT, dt DATETIME);
608
INSERT INTO t1 VALUES ( 1, '2005-05-01 12:30:00' );
609
INSERT INTO t1 VALUES ( 1, '2005-05-01 12:30:00' );
610
INSERT INTO t1 VALUES ( 1, '2005-05-01 12:30:00' );
611
INSERT INTO t1 VALUES ( 1, '2005-05-01 12:30:00' );
612
SELECT dt DIV 1 AS f, id FROM t1 GROUP BY f;
616
CREATE TABLE t1 (id varchar(20) NOT NULL);
617
INSERT INTO t1 VALUES ('trans1'), ('trans2');
618
CREATE TABLE t2 (id varchar(20) NOT NULL, err_comment blob NOT NULL);
619
INSERT INTO t2 VALUES ('trans1', 'a problem');
620
SELECT COUNT(DISTINCT(t1.id)), LEFT(err_comment, 256) AS comment
621
FROM t1 LEFT JOIN t2 ON t1.id=t2.id GROUP BY comment;
622
COUNT(DISTINCT(t1.id)) comment
626
create table t1 (f1 date);
627
insert into t1 values('2005-06-06');
628
insert into t1 values('2005-06-06');
629
select date(left(f1+0,8)) from t1 group by 1;
633
CREATE TABLE t1 (n int);
634
INSERT INTO t1 VALUES (1);
635
SELECT n+1 AS n FROM t1 GROUP BY n;
639
Warning 1052 Column 'n' in group statement is ambiguous
641
create table t1(f1 varchar(5) key);
642
insert into t1 values (1),(2);
643
select sql_buffer_result max(f1) is null from t1;
646
select sql_buffer_result max(f1)+1 from t1;
650
CREATE TABLE t1(a INT);
651
INSERT INTO t1 VALUES (1),(2);
652
SELECT a FROM t1 GROUP BY 'a';
655
SELECT a FROM t1 GROUP BY "a";
658
SELECT a FROM t1 GROUP BY `a`;
662
SELECT a FROM t1 GROUP BY "a";
665
SELECT a FROM t1 GROUP BY 'a';
668
SELECT a FROM t1 GROUP BY `a`;
672
SELECT a FROM t1 HAVING 'a' > 1;
675
Warning 1292 Truncated incorrect DOUBLE value: 'a'
676
SELECT a FROM t1 HAVING "a" > 1;
679
Warning 1292 Truncated incorrect DOUBLE value: 'a'
680
SELECT a FROM t1 HAVING `a` > 1;
683
SELECT a FROM t1 ORDER BY 'a' DESC;
687
SELECT a FROM t1 ORDER BY "a" DESC;
691
SELECT a FROM t1 ORDER BY `a` DESC;
697
f1 int NOT NULL auto_increment primary key,
698
f2 varchar(100) NOT NULL default ''
701
f1 varchar(10) NOT NULL default '',
702
f2 char(3) NOT NULL default '',
706
INSERT INTO t1 values(NULL, '');
707
INSERT INTO `t2` VALUES ('486878','WDT'),('486910','WDT');
708
SELECT SQL_BUFFER_RESULT avg(t2.f1) FROM t1, t2 where t2.f2 = 'SIR' GROUP BY t1.f1;
710
SELECT avg(t2.f1) FROM t1, t2 where t2.f2 = 'SIR' GROUP BY t1.f1;
713
create table t1 (c1 char(3), c2 char(3));
714
create table t2 (c3 char(3), c4 char(3));
715
insert into t1 values ('aaa', 'bb1'), ('aaa', 'bb2');
716
insert into t2 values ('aaa', 'bb1'), ('aaa', 'bb2');
717
select t1.c1 as c2 from t1, t2 where t1.c2 = t2.c4
723
Warning 1052 Column 'c2' in group statement is ambiguous
726
Warning 1052 Column 'c2' in group statement is ambiguous
727
select t1.c1 as c2 from t1, t2 where t1.c2 = t2.c4
734
CREATE TABLE t1 (a INT PRIMARY KEY, b INT, key (b));
735
INSERT INTO t1 VALUES (1, 1);
736
INSERT INTO t1 SELECT a + 1 , MOD(a + 1 , 20) FROM t1;
737
INSERT INTO t1 SELECT a + 2 , MOD(a + 2 , 20) FROM t1;
738
INSERT INTO t1 SELECT a + 4 , MOD(a + 4 , 20) FROM t1;
739
INSERT INTO t1 SELECT a + 8 , MOD(a + 8 , 20) FROM t1;
740
INSERT INTO t1 SELECT a + 16, MOD(a + 16, 20) FROM t1;
741
INSERT INTO t1 SELECT a + 32, MOD(a + 32, 20) FROM t1;
742
INSERT INTO t1 SELECT a + 64, MOD(a + 64, 20) FROM t1;
743
SELECT MIN(b), MAX(b) from t1;
746
EXPLAIN SELECT b, sum(1) FROM t1 GROUP BY b;
747
id select_type table type possible_keys key key_len ref rows Extra
748
1 SIMPLE t1 index NULL b 5 NULL 128 Using index
749
EXPLAIN SELECT SQL_BIG_RESULT b, sum(1) FROM t1 GROUP BY b;
750
id select_type table type possible_keys key key_len ref rows Extra
751
1 SIMPLE t1 index NULL b 5 NULL 128 Using index; Using filesort
752
SELECT b, sum(1) FROM t1 GROUP BY b;
774
SELECT SQL_BIG_RESULT b, sum(1) FROM t1 GROUP BY b;
797
CREATE TABLE t1 (a INT PRIMARY KEY, b INT);
798
INSERT INTO t1 VALUES (1,1),(2,1),(3,2),(4,2),(5,3),(6,3);
799
SELECT MAX(a)-MIN(a) FROM t1 GROUP BY b;
804
SELECT CEILING(MIN(a)) FROM t1 GROUP BY b;
809
SELECT CASE WHEN AVG(a)>=0 THEN 'Positive' ELSE 'Negative' END FROM t1
811
CASE WHEN AVG(a)>=0 THEN 'Positive' ELSE 'Negative' END
815
SELECT a + 1 FROM t1 GROUP BY a;
823
SELECT a + b FROM t1 GROUP BY b;
828
SELECT (SELECT t1_outer.a FROM t1 AS t1_inner GROUP BY b LIMIT 1)
830
(SELECT t1_outer.a FROM t1 AS t1_inner GROUP BY b LIMIT 1)
837
SELECT 1 FROM t1 as t1_outer GROUP BY a
838
HAVING (SELECT t1_outer.a FROM t1 AS t1_inner GROUP BY b LIMIT 1);
846
SELECT (SELECT t1_outer.a FROM t1 AS t1_inner LIMIT 1)
847
FROM t1 AS t1_outer GROUP BY t1_outer.b;
848
(SELECT t1_outer.a FROM t1 AS t1_inner LIMIT 1)
852
SELECT 1 FROM t1 as t1_outer GROUP BY a
853
HAVING (SELECT t1_outer.b FROM t1 AS t1_inner LIMIT 1);
854
ERROR 42S22: Unknown column 'test.t1_outer.b' in 'field list'
855
SELECT (SELECT SUM(t1_inner.a) FROM t1 AS t1_inner LIMIT 1)
856
FROM t1 AS t1_outer GROUP BY t1_outer.b;
857
(SELECT SUM(t1_inner.a) FROM t1 AS t1_inner LIMIT 1)
861
SELECT (SELECT SUM(t1_inner.a) FROM t1 AS t1_inner GROUP BY t1_inner.b LIMIT 1)
863
(SELECT SUM(t1_inner.a) FROM t1 AS t1_inner GROUP BY t1_inner.b LIMIT 1)
870
SELECT (SELECT SUM(t1_outer.a) FROM t1 AS t1_inner LIMIT 1)
871
FROM t1 AS t1_outer GROUP BY t1_outer.b;
872
(SELECT SUM(t1_outer.a) FROM t1 AS t1_inner LIMIT 1)
876
SELECT 1 FROM t1 as t1_outer
877
WHERE (SELECT t1_outer.b FROM t1 AS t1_inner GROUP BY t1_inner.b LIMIT 1);
885
SELECT b FROM t1 GROUP BY b HAVING CEILING(b) > 0;
890
SELECT 1 FROM t1 GROUP BY b HAVING b = 2 OR b = 3 OR SUM(a) > 12;
894
SELECT 1 FROM t1 GROUP BY b HAVING ROW (b,b) = ROW (1,1);
897
SELECT 1 FROM t1 GROUP BY b HAVING a = 2;
898
ERROR 42S22: Unknown column 'a' in 'having clause'
899
SELECT 1 FROM t1 GROUP BY SUM(b);
900
ERROR HY000: Invalid use of group function
901
SELECT b FROM t1 AS t1_outer GROUP BY a HAVING t1_outer.a IN
902
(SELECT SUM(t1_inner.b)+t1_outer.b FROM t1 AS t1_inner GROUP BY t1_inner.a
903
HAVING SUM(t1_inner.b)+t1_outer.b > 5);
907
create table t1(f1 int, f2 int);
908
select * from t1 group by f1;
910
select * from t1 group by f2;
912
select * from t1 group by f1, f2;
914
select t1.f1,t.* from t1, t1 t group by 1;
918
id INT AUTO_INCREMENT PRIMARY KEY,
922
INSERT INTO t1(c1,c2) VALUES (5,1), (4,1), (3,5), (2,3), (1,3);
923
SELECT * FROM t1 ORDER BY c1;
930
SELECT * FROM t1 GROUP BY id ORDER BY c1;
937
SELECT * FROM t1 GROUP BY id ORDER BY id DESC;
944
SELECT * FROM t1 GROUP BY c2 ,c1, id ORDER BY c2, c1;
951
SELECT * FROM t1 GROUP BY c2, c1, id ORDER BY c2 DESC, c1;
958
SELECT * FROM t1 GROUP BY c2, c1, id ORDER BY c2 DESC, c1 DESC;
965
SELECT * FROM t1 GROUP BY c2 ORDER BY c2, c1;
970
SELECT * FROM t1 GROUP BY c2 ORDER BY c2 DESC, c1;
975
SELECT * FROM t1 GROUP BY c2 ORDER BY c2 DESC, c1 DESC;
982
# Bug#27219: Aggregate functions in ORDER BY.
984
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT 0);
985
INSERT INTO t1 (a, b) VALUES (3,3), (2,2), (3,3), (2,2), (3,3), (4,4);
986
CREATE TABLE t2 SELECT * FROM t1;
987
SELECT 1 FROM t1 ORDER BY COUNT(*);
990
SELECT 1 FROM t1 ORDER BY COUNT(*) + 1;
993
SELECT 1 FROM t1 ORDER BY COUNT(*) + a;
996
SELECT 1 FROM t1 ORDER BY COUNT(*), 1;
999
SELECT 1 FROM t1 ORDER BY COUNT(*), a;
1002
SELECT 1 FROM t1 ORDER BY SUM(a);
1005
SELECT 1 FROM t1 ORDER BY SUM(a + 1);
1008
SELECT 1 FROM t1 ORDER BY SUM(a) + 1;
1011
SELECT 1 FROM t1 ORDER BY SUM(a), b;
1014
SELECT a FROM t1 ORDER BY COUNT(b);
1017
SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a) FROM t2);
1025
SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a) FROM t2 ORDER BY t2.a);
1033
SELECT t1.a FROM t1 ORDER BY (SELECT t2.a FROM t2 ORDER BY SUM(t2.b) LIMIT 1);
1042
WHERE t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t2.b) LIMIT 1);
1047
SELECT t1.a FROM t1 GROUP BY t1.a
1048
HAVING t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
1051
SELECT t1.a FROM t1 GROUP BY t1.a
1052
HAVING t1.a IN (SELECT t2.a FROM t2 ORDER BY SUM(t1.b));
1057
SELECT t1.a FROM t1 GROUP BY t1.a
1058
HAVING t1.a IN (SELECT t2.a FROM t2 ORDER BY t2.a, SUM(t2.b));
1061
SELECT t1.a FROM t1 GROUP BY t1.a
1062
HAVING t1.a > ANY (SELECT t2.a FROM t2 ORDER BY t2.a, SUM(t2.b));
1066
WHERE t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t1.b));
1071
SELECT 1 FROM t1 GROUP BY t1.a
1072
HAVING (SELECT AVG(SUM(t1.b) + 1) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
1077
SELECT 1 FROM t1 GROUP BY t1.a
1078
HAVING (SELECT AVG(SUM(t1.b) + t2.b) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
1083
SELECT 1 FROM t1 GROUP BY t1.a
1084
HAVING (SELECT AVG(t1.b + t2.b) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
1089
SELECT 1 FROM t1 GROUP BY t1.a
1090
HAVING (SELECT AVG(SUM(t1.b) + 1) FROM t2 ORDER BY t2.a LIMIT 1);
1095
SELECT 1 FROM t1 GROUP BY t1.a
1096
HAVING (SELECT AVG(SUM(t1.b) + t2.b) FROM t2 ORDER BY t2.a LIMIT 1);
1101
SELECT 1 FROM t1 GROUP BY t1.a
1102
HAVING (SELECT AVG(t1.b + t2.b) FROM t2 ORDER BY t2.a LIMIT 1);
1108
WHERE t1.a = (SELECT t2.a FROM t2 GROUP BY t2.a
1109
ORDER BY SUM(t2.b), SUM(t1.b) LIMIT 1);
1112
SELECT t1.a, SUM(t1.b) FROM t1
1113
WHERE t1.a = (SELECT SUM(t2.b) FROM t2 GROUP BY t2.a
1114
ORDER BY SUM(t2.b), SUM(t1.b) LIMIT 1)
1118
SELECT t1.a, SUM(t1.b) FROM t1
1119
WHERE t1.a = (SELECT SUM(t2.b) FROM t2
1120
ORDER BY SUM(t2.b) + SUM(t1.b) LIMIT 1)
1123
SELECT t1.a, SUM(t1.b) FROM t1
1124
WHERE t1.a = (SELECT SUM(t2.b) FROM t2
1125
ORDER BY SUM(t2.b + t1.a) LIMIT 1)
1128
SELECT t1.a FROM t1 GROUP BY t1.a
1129
HAVING (1, 1) = (SELECT SUM(t1.a), t1.a FROM t2 LIMIT 1);
1133
(select sum(outr.a + innr.a) from t1 as innr limit 1) as tt
1134
from t1 as outr order by outr.a limit 1))
1135
from t1 as most_outer;
1138
(select sum(outr.a + innr.a) from t1 as innr limit 1) as tt
1139
from t1 as outr order by outr.a limit 1))
1143
(select sum(outr.a + innr.a) from t1 as innr limit 1)) as tt
1144
from t1 as outr order by count(outr.a) limit 1)) as tt
1145
from t1 as most_outer;
1148
select (select sum(outr.a + t1.a) from t1 limit 1) as tt from t1 as outr order by outr.a;
1158
CREATE TABLE t1 (a INT, b INT,
1161
INSERT INTO t1 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8);
1162
INSERT INTO t1 SELECT a + 8,b FROM t1;
1163
INSERT INTO t1 SELECT a + 16,b FROM t1;
1164
INSERT INTO t1 SELECT a + 32,b FROM t1;
1165
INSERT INTO t1 SELECT a + 64,b FROM t1;
1166
INSERT INTO t1 SELECT a + 128,b FROM t1 limit 16;
1168
Table Op Msg_type Msg_text
1169
test.t1 analyze status OK
1170
EXPLAIN SELECT a FROM t1 WHERE a < 2;
1171
id select_type table type possible_keys key key_len ref rows Extra
1172
1 SIMPLE t1 range PRIMARY,i2 PRIMARY 4 NULL 1 Using where; Using index
1173
EXPLAIN SELECT a FROM t1 WHERE a < 2 ORDER BY a;
1174
id select_type table type possible_keys key key_len ref rows Extra
1175
1 SIMPLE t1 range PRIMARY,i2 PRIMARY 4 NULL 1 Using where; Using index
1176
EXPLAIN SELECT a FROM t1 WHERE a < 2 GROUP BY a;
1177
id select_type table type possible_keys key key_len ref rows Extra
1178
1 SIMPLE t1 range PRIMARY,i2 PRIMARY 4 NULL 1 Using where; Using index for group-by
1179
EXPLAIN SELECT a FROM t1 IGNORE INDEX (PRIMARY,i2);
1180
id select_type table type possible_keys key key_len ref rows Extra
1181
1 SIMPLE t1 ALL NULL NULL NULL NULL 144
1182
EXPLAIN SELECT a FROM t1 IGNORE INDEX FOR JOIN (PRIMARY,i2);
1183
id select_type table type possible_keys key key_len ref rows Extra
1184
1 SIMPLE t1 ALL NULL NULL NULL NULL 144
1185
EXPLAIN SELECT a FROM t1 IGNORE INDEX FOR GROUP BY (PRIMARY,i2) GROUP BY a;
1186
id select_type table type possible_keys key key_len ref rows Extra
1187
1 SIMPLE t1 index NULL PRIMARY 4 NULL 144 Using index
1188
EXPLAIN SELECT a FROM t1 IGNORE INDEX FOR ORDER BY (PRIMARY,i2) ORDER BY a;
1189
id select_type table type possible_keys key key_len ref rows Extra
1190
1 SIMPLE t1 index NULL PRIMARY 4 NULL 144 Using index
1191
SELECT a FROM t1 IGNORE INDEX FOR ORDER BY (PRIMARY,i2) ORDER BY a;
1337
EXPLAIN SELECT a FROM t1 IGNORE INDEX FOR ORDER BY (PRIMARY)
1338
IGNORE INDEX FOR GROUP BY (i2) GROUP BY a;
1339
id select_type table type possible_keys key key_len ref rows Extra
1340
1 SIMPLE t1 index NULL PRIMARY 4 NULL 144 Using index
1341
EXPLAIN SELECT a FROM t1 IGNORE INDEX (PRIMARY) IGNORE INDEX FOR ORDER BY (i2);
1342
id select_type table type possible_keys key key_len ref rows Extra
1343
1 SIMPLE t1 index NULL i2 9 NULL 144 Using index
1344
EXPLAIN SELECT a FROM t1 FORCE INDEX (i2);
1345
id select_type table type possible_keys key key_len ref rows Extra
1346
1 SIMPLE t1 index NULL i2 9 NULL 144 Using index
1347
EXPLAIN SELECT a FROM t1 USE INDEX ();
1348
id select_type table type possible_keys key key_len ref rows Extra
1349
1 SIMPLE t1 ALL NULL NULL NULL NULL 144
1350
EXPLAIN SELECT a FROM t1 USE INDEX () USE INDEX (i2);
1351
id select_type table type possible_keys key key_len ref rows Extra
1352
1 SIMPLE t1 ALL NULL NULL NULL NULL 144
1353
EXPLAIN SELECT a FROM t1
1354
FORCE INDEX (PRIMARY)
1355
IGNORE INDEX FOR GROUP BY (i2)
1356
IGNORE INDEX FOR ORDER BY (i2)
1358
ERROR HY000: Incorrect usage of USE INDEX and FORCE INDEX
1359
EXPLAIN SELECT a FROM t1 USE INDEX (i2) USE INDEX ();
1360
id select_type table type possible_keys key key_len ref rows Extra
1361
1 SIMPLE t1 index NULL i2 9 NULL 144 Using index
1362
EXPLAIN SELECT a FROM t1 FORCE INDEX ();
1363
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near ')' at line 1
1364
EXPLAIN SELECT a FROM t1 IGNORE INDEX ();
1365
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near ')' at line 1
1366
EXPLAIN SELECT a FROM t1 USE INDEX FOR JOIN (i2)
1367
USE INDEX FOR GROUP BY (i2) GROUP BY a;
1368
id select_type table type possible_keys key key_len ref rows Extra
1369
1 SIMPLE t1 # NULL i2 # NULL # #
1370
EXPLAIN SELECT a FROM t1 FORCE INDEX FOR JOIN (i2)
1371
FORCE INDEX FOR GROUP BY (i2) GROUP BY a;
1372
id select_type table type possible_keys key key_len ref rows Extra
1373
1 SIMPLE t1 range NULL i2 4 NULL 145 Using index for group-by
1374
EXPLAIN SELECT a FROM t1 USE INDEX () IGNORE INDEX (i2);
1375
id select_type table type possible_keys key key_len ref rows Extra
1376
1 SIMPLE t1 ALL NULL NULL NULL NULL 144
1377
EXPLAIN SELECT a FROM t1 IGNORE INDEX (i2) USE INDEX ();
1378
id select_type table type possible_keys key key_len ref rows Extra
1379
1 SIMPLE t1 ALL NULL NULL NULL NULL 144
1380
EXPLAIN SELECT a FROM t1
1381
USE INDEX FOR GROUP BY (i2)
1382
USE INDEX FOR ORDER BY (i2)
1383
USE INDEX FOR JOIN (i2);
1384
id select_type table type possible_keys key key_len ref rows Extra
1385
1 SIMPLE t1 index NULL i2 9 NULL 144 Using index
1386
EXPLAIN SELECT a FROM t1
1387
USE INDEX FOR JOIN (i2)
1388
USE INDEX FOR JOIN (i2)
1389
USE INDEX FOR JOIN (i2,i2);
1390
id select_type table type possible_keys key key_len ref rows Extra
1391
1 SIMPLE t1 index NULL i2 9 NULL 144 Using index
1392
EXPLAIN SELECT 1 FROM t1 WHERE a IN
1393
(SELECT a FROM t1 USE INDEX (i2) IGNORE INDEX (i2));
1394
id select_type table type possible_keys key key_len ref rows Extra
1395
1 PRIMARY t1 index NULL PRIMARY 4 NULL 144 Using where; Using index
1396
2 SUBQUERY t1 ALL NULL NULL NULL NULL 144
1397
CREATE TABLE t2 (a INT, b INT, KEY(a));
1398
INSERT INTO t2 VALUES (1, 1), (2, 2), (3,3), (4,4);
1399
EXPLAIN SELECT a, SUM(b) FROM t2 GROUP BY a LIMIT 2;
1400
id select_type table type possible_keys key key_len ref rows Extra
1401
1 SIMPLE t2 index NULL a 5 NULL 2
1402
EXPLAIN SELECT a, SUM(b) FROM t2 IGNORE INDEX (a) GROUP BY a LIMIT 2;
1403
id select_type table type possible_keys key key_len ref rows Extra
1404
1 SIMPLE t2 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
1405
EXPLAIN SELECT 1 FROM t2 WHERE a IN
1406
(SELECT a FROM t1 USE INDEX (i2) IGNORE INDEX (i2));
1407
id select_type table type possible_keys key key_len ref rows Extra
1408
1 PRIMARY t2 index NULL a 5 NULL 4 Using where; Using index
1409
2 SUBQUERY t1 ALL NULL NULL NULL NULL 144
1418
INSERT INTO t1 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4);
1424
INSERT INTO t2 VALUES (NULL, NULL), (NULL, NULL), (NULL, 1), (1, NULL), (1, 1), (1,2);
1425
EXPLAIN SELECT c,b,d FROM t1 GROUP BY c,b,d;
1426
id select_type table type possible_keys key key_len ref rows Extra
1427
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using filesort
1428
SELECT c,b,d FROM t1 GROUP BY c,b,d;
1433
EXPLAIN SELECT c,b,d FROM t1 GROUP BY c,b,d ORDER BY NULL;
1434
id select_type table type possible_keys key key_len ref rows Extra
1435
1 SIMPLE t1 ALL NULL NULL NULL NULL 3
1436
SELECT c,b,d FROM t1 GROUP BY c,b,d ORDER BY NULL;
1441
EXPLAIN SELECT c,b,d FROM t1 ORDER BY c,b,d;
1442
id select_type table type possible_keys key key_len ref rows Extra
1443
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using filesort
1444
SELECT c,b,d FROM t1 ORDER BY c,b,d;
1449
EXPLAIN SELECT c,b,d FROM t1 GROUP BY c,b;
1450
id select_type table type possible_keys key key_len ref rows Extra
1451
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using filesort
1452
SELECT c,b,d FROM t1 GROUP BY c,b;
1457
EXPLAIN SELECT c,b FROM t1 GROUP BY c,b;
1458
id select_type table type possible_keys key key_len ref rows Extra
1459
1 SIMPLE t1 index NULL c 8 NULL 3 Using index
1460
SELECT c,b FROM t1 GROUP BY c,b;
1465
EXPLAIN SELECT a,b from t2 ORDER BY a,b;
1466
id select_type table type possible_keys key key_len ref rows Extra
1467
1 SIMPLE t2 index NULL a 10 NULL 6 Using index
1468
SELECT a,b from t2 ORDER BY a,b;
1476
EXPLAIN SELECT a,b from t2 GROUP BY a,b;
1477
id select_type table type possible_keys key key_len ref rows Extra
1478
1 SIMPLE t2 index NULL a 10 NULL 6 Using index
1479
SELECT a,b from t2 GROUP BY a,b;
1486
EXPLAIN SELECT a from t2 GROUP BY a;
1487
id select_type table type possible_keys key key_len ref rows Extra
1488
1 SIMPLE t2 index NULL a 10 NULL 6 Using index
1489
SELECT a from t2 GROUP BY a;
1493
EXPLAIN SELECT b from t2 GROUP BY b;
1494
id select_type table type possible_keys key key_len ref rows Extra
1495
1 SIMPLE t2 index NULL a 10 NULL 6 Using index; Using temporary; Using filesort
1496
SELECT b from t2 GROUP BY b;
1502
CREATE TABLE t1 ( a INT, b INT );
1503
SELECT b c, (SELECT a FROM t1 WHERE b = c)
1505
c (SELECT a FROM t1 WHERE b = c)
1506
SELECT b c, (SELECT a FROM t1 WHERE b = c)
1509
c (SELECT a FROM t1 WHERE b = c)
1510
SELECT MAX(b) c, (SELECT a FROM t1 WHERE b = c)
1513
ERROR 42S22: Reference 'c' not supported (reference to group function)
1514
SELECT b c, (SELECT a FROM t1 WHERE b = c)
1516
c (SELECT a FROM t1 WHERE b = c)
1517
SELECT b c, (SELECT a FROM t1 WHERE b = c)
1520
c (SELECT a FROM t1 WHERE b = c)
1521
SELECT MAX(b) c, (SELECT a FROM t1 WHERE b = c)
1524
ERROR 42S22: Reference 'c' not supported (reference to group function)
1525
INSERT INTO t1 VALUES (1, 1);
1526
SELECT b c, (SELECT a FROM t1 WHERE b = c)
1528
c (SELECT a FROM t1 WHERE b = c)
1530
INSERT INTO t1 VALUES (2, 1);
1531
SELECT b c, (SELECT a FROM t1 WHERE b = c)
1533
ERROR 21000: Subquery returns more than 1 row