github.com/dolthub/go-mysql-server@v0.18.0/enginetest/sqllogictest/testdata/join/subquery_correlated.txt (about) 1 # Copyright 2023 Dolthub, Inc. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # 15 # TEST ADAPTED FROM COCKROACHDB; HEAVILY MODIFIED TO WORK WITH MYSQL 16 # SOURCE https://github.com/cockroachdb/cockroach/blob/78f7da402bec149b403cd27479a3af38bed6d69b/pkg/sql/logictest/testdata/logic_test/subquery_correlated 17 18 # ------------------------------------------------------------------------------ 19 # Create a simple schema that models customers and orders. Each customer has an 20 # id (c_id), and has zero or more orders that are related via a foreign `key` of 21 # the same name. A customer has a billing state and an order has a shipping 22 # state, either of which could be NULL. This schema, while simple, is rich 23 # enough to provide many interesting correlated subquery variations. 24 # ------------------------------------------------------------------------------ 25 statement ok 26 CREATE TABLE c (c_id INT PRIMARY KEY, bill TEXT); 27 28 statement ok 29 CREATE TABLE o (o_id INT PRIMARY KEY, c_id INT, ship TEXT); 30 31 statement ok 32 INSERT INTO c VALUES 33 (1, 'CA'), 34 (2, 'TX'), 35 (3, 'MA'), 36 (4, 'TX'), 37 (5, NULL), 38 (6, 'FL'); 39 40 statement ok 41 INSERT INTO o VALUES 42 (10, 1, 'CA'), (20, 1, 'CA'), (30, 1, 'CA'), 43 (40, 2, 'CA'), (50, 2, 'TX'), (60, 2, NULL), 44 (70, 4, 'WY'), (80, 4, NULL), 45 (90, 6, 'WA'); 46 47 # ------------------------------------------------------------------------------ 48 # Subqueries in select filters. 49 # ------------------------------------------------------------------------------ 50 51 # Customers with orders. 52 query IT nosort 53 SELECT * FROM c WHERE EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id); 54 ---- 55 1 56 CA 57 2 58 TX 59 4 60 TX 61 6 62 FL 63 64 # Customers with no orders. 65 query IT nosort 66 SELECT * FROM c WHERE NOT EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id); 67 ---- 68 3 69 MA 70 5 71 NULL 72 73 # Customers with orders or with no orders (should return all customers). 74 query IT nosort 75 SELECT * 76 FROM c 77 WHERE 78 EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id) 79 OR NOT EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id); 80 ---- 81 1 82 CA 83 2 84 TX 85 3 86 MA 87 4 88 TX 89 5 90 NULL 91 6 92 FL 93 94 # Customers with billing address in TX that have orders. 95 query IT nosort 96 SELECT * FROM c WHERE EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id AND c.bill='TX'); 97 ---- 98 2 99 TX 100 4 101 TX 102 103 # Customers that have at least one order shipped to WY. 104 query IT nosort 105 SELECT * FROM c WHERE 'WY' IN (SELECT ship FROM o WHERE o.c_id=c.c_id); 106 ---- 107 4 108 TX 109 110 # Customers that have at least one order shipped to WY or to WA. 111 query IT nosort 112 SELECT * 113 FROM c 114 WHERE 115 'WY' IN (SELECT ship FROM o WHERE o.c_id=c.c_id) 116 OR 'WA' IN (SELECT ship FROM o WHERE o.c_id=c.c_id); 117 ---- 118 4 119 TX 120 6 121 FL 122 123 # Customers that have at least one order shipped to CA, but none to TX. 124 query IT nosort 125 SELECT * 126 FROM c 127 WHERE 128 'CA' IN (SELECT ship FROM o WHERE o.c_id=c.c_id) 129 AND 'TX' NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id); 130 ---- 131 1 132 CA 133 134 # Customers with at least one order with billing addr = shipping addr. 135 query IT nosort 136 SELECT * FROM c WHERE bill IN (SELECT ship FROM o WHERE o.c_id=c.c_id); 137 ---- 138 1 139 CA 140 2 141 TX 142 143 # Customers with all orders with billing addr = shipping addr. 144 query IT nosort 145 SELECT * FROM c WHERE bill = ALL(SELECT ship FROM o WHERE o.c_id=c.c_id); 146 ---- 147 1 148 CA 149 3 150 MA 151 5 152 NULL 153 154 # Customers with no order with billing addr = shipping addr (with NULL ship). 155 query IT nosort 156 SELECT * FROM c WHERE bill NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id); 157 ---- 158 3 159 MA 160 5 161 NULL 162 6 163 FL 164 165 # Customers with no order with billing addr = shipping addr (no NULL ship). 166 query IT nosort 167 SELECT * FROM c WHERE bill NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NOT NULL); 168 ---- 169 3 170 MA 171 4 172 TX 173 5 174 NULL 175 6 176 FL 177 178 # Customers with no order with billing addr = shipping addr (only NULL ship). 179 query IT nosort 180 SELECT * FROM c WHERE bill NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NULL); 181 ---- 182 1 183 CA 184 3 185 MA 186 5 187 NULL 188 6 189 FL 190 191 # Customers with bill state < any ship state. 192 query IT nosort 193 SELECT * FROM c WHERE bill < ANY(SELECT ship FROM o WHERE o.c_id=c.c_id); 194 ---- 195 4 196 TX 197 6 198 FL 199 200 # Customers where bill state < any ship state is null result. This prevents 201 # normalizing ANY into EXISTS. 202 query IT nosort 203 SELECT * FROM c WHERE (bill < ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NULL; 204 ---- 205 2 206 TX 207 208 # Customers where bill state < any ship state is not null result. This prevents 209 # normalizing ANY into EXISTS. 210 query IT nosort 211 SELECT * FROM c WHERE (bill < ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL; 212 ---- 213 1 214 CA 215 3 216 MA 217 4 218 TX 219 5 220 NULL 221 6 222 FL 223 224 # Customers with bill state > any ship state. 225 query IT nosort 226 SELECT * FROM c WHERE bill > ANY(SELECT ship FROM o WHERE o.c_id=c.c_id); 227 ---- 228 2 229 TX 230 231 # Customers where bill state > any ship state is null result. This prevents 232 # normalizing ANY into EXISTS. 233 query IT nosort 234 SELECT * FROM c WHERE (bill > ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NULL; 235 ---- 236 4 237 TX 238 239 # Customers where bill state > any ship state is not null result. This prevents 240 # normalizing ANY into EXISTS. 241 query IT nosort 242 SELECT * FROM c WHERE (bill > ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL; 243 ---- 244 1 245 CA 246 2 247 TX 248 3 249 MA 250 5 251 NULL 252 6 253 FL 254 255 # Customers where bill state matches any ship state. 256 query IT nosort 257 SELECT * FROM c WHERE bill = ANY(SELECT ship FROM o); 258 ---- 259 1 260 CA 261 2 262 TX 263 4 264 TX 265 266 # Customers where bill state matches any ship state or is null. 267 query IT nosort 268 SELECT * FROM c WHERE bill = ANY(SELECT ship FROM o) OR bill IS NULL; 269 ---- 270 1 271 CA 272 2 273 TX 274 4 275 TX 276 5 277 NULL 278 279 # Test NULL IN case. Use IS NOT NULL to prevent normalize ANY into EXISTS. 280 query IT nosort 281 SELECT * FROM c WHERE (NULL IN (SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL; 282 ---- 283 3 284 MA 285 5 286 NULL 287 288 # Test NULL NOT IN case. Use IS NOT NULL to prevent normalize ANY into EXISTS. 289 query IT nosort 290 SELECT * FROM c WHERE (NULL NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL; 291 ---- 292 3 293 MA 294 5 295 NULL 296 297 # Customers where it is unknown whether a replaced bill state is one of the ship 298 # states. This tests a more complex scalar expression as argument to IN. 299 query IT nosort 300 SELECT * FROM c WHERE (replace(bill, 'TX', 'WY') IN (SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NULL; 301 ---- 302 2 303 TX 304 305 # Customers with all orders with billing addr = shipping addr, or with at least 306 # one order shipped to WY. 307 query IT nosort 308 SELECT * 309 FROM c 310 WHERE 311 bill = ALL(SELECT ship FROM o WHERE o.c_id=c.c_id) 312 OR EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id AND ship='WY'); 313 ---- 314 1 315 CA 316 3 317 MA 318 4 319 TX 320 5 321 NULL 322 323 # Customers with all orders with billing addr = shipping addr, but with at least 324 # one order. 325 query IT nosort 326 SELECT * 327 FROM c 328 WHERE 329 bill = ALL(SELECT ship FROM o WHERE o.c_id=c.c_id) 330 AND EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id); 331 ---- 332 1 333 CA 334 335 # Customers with more than one order. 336 query IT nosort 337 SELECT * FROM c WHERE (SELECT count(*) FROM o WHERE o.c_id=c.c_id) > 1; 338 ---- 339 1 340 CA 341 2 342 TX 343 4 344 TX 345 346 # Customers with more than one order shipped to a known state (i.e. NOT NULL). 347 query IT nosort 348 SELECT * FROM c WHERE (SELECT count(ship) FROM o WHERE o.c_id=c.c_id) > 1; 349 ---- 350 1 351 CA 352 2 353 TX 354 355 # For each customer, orders shipped to lowest state (alphabetically). 356 query IIT nosort 357 SELECT c.c_id, o.o_id, o.ship 358 FROM c 359 INNER JOIN o 360 ON c.c_id=o.c_id AND o.ship = (SELECT min(o.ship) FROM o WHERE o.c_id=c.c_id) 361 ORDER BY c.c_id, o.o_id, o.ship; 362 ---- 363 1 364 10 365 CA 366 1 367 20 368 CA 369 1 370 30 371 CA 372 2 373 40 374 CA 375 4 376 70 377 WY 378 6 379 90 380 WA 381 382 # Customers who have shipped more orders to a particular state than all other 383 # customers have shipped to that state, combined. 384 query ITI nosort 385 SELECT c.c_id, o.ship, count(*) 386 FROM c 387 INNER JOIN o 388 ON c.c_id=o.c_id 389 WHERE 390 (SELECT count(*) FROM o AS o2 WHERE o2.ship = o.ship AND o2.c_id = o.c_id) > 391 (SELECT count(*) FROM o AS o2 WHERE o2.ship = o.ship AND o2.c_id <> o.c_id) 392 GROUP BY c.c_id, o.ship; 393 ---- 394 1 395 CA 396 3 397 2 398 TX 399 1 400 4 401 WY 402 1 403 6 404 WA 405 1 406 407 # Customers with more than one order and with the highest state = 'CA'. 408 query IT nosort 409 SELECT * 410 FROM c 411 WHERE 412 (SELECT count(*) FROM o WHERE o.c_id=c.c_id) > 1 413 AND (SELECT max(ship) FROM o WHERE o.c_id=c.c_id) = 'CA'; 414 ---- 415 1 416 CA 417 418 # Customers with more than one order or with an unknown ship state. 419 query IT nosort 420 SELECT * 421 FROM c 422 WHERE 423 (SELECT count(*) FROM o WHERE o.c_id=c.c_id) > 1 424 OR EXISTS(SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NULL); 425 ---- 426 1 427 CA 428 2 429 TX 430 4 431 TX 432 433 # Customers that have a bill state equal to the max ship state of all their 434 # orders (alphabetically). 435 query IT nosort 436 SELECT c_id, bill 437 FROM c AS c2 438 WHERE EXISTS 439 ( 440 SELECT * FROM c WHERE bill=(SELECT max(ship) FROM o WHERE c_id=c2.c_id AND c_id=c.c_id) 441 ) 442 ---- 443 1 444 CA 445 2 446 TX 447 448 # Customers that have at least one order shipped to their billing state (or if 449 # the ship state is null). 450 query IT nosort 451 SELECT c_id, bill 452 FROM c AS c2 453 WHERE EXISTS 454 ( 455 SELECT * 456 FROM (SELECT c_id, coalesce(ship, bill) AS state FROM o WHERE c_id=c2.c_id) AS o 457 WHERE state=bill 458 ) 459 ---- 460 1 461 CA 462 2 463 TX 464 4 465 TX 466 467 # Customers with each of their orders numbered. 468 # query II nosort 469 # SELECT c_id, generate_series(1, (SELECT count(*) FROM o WHERE o.c_id=c.c_id)) FROM c 470 # ---- 471 # 1 472 # 1 473 # 1 474 # 2 475 # 1 476 # 3 477 # 2 478 # 1 479 # 2 480 # 2 481 # 2 482 # 3 483 # 4 484 # 1 485 # 4 486 # 2 487 # 6 488 # 1 489 490 # Customers that have no orders with a NULL ship state. 491 query IT nosort 492 SELECT * 493 FROM c 494 WHERE (SELECT ship FROM o WHERE o.c_id=c.c_id ORDER BY ship LIMIT 1) IS NOT NULL 495 ---- 496 1 497 CA 498 6 499 FL 500 501 # Customers that have first order shipping to 'CA' or 'WY' (no NULL ship). 502 query IT nosort 503 SELECT * 504 FROM c 505 WHERE 506 (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NOT NULL ORDER BY ship LIMIT 1)='CA' 507 OR (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NOT NULL ORDER BY ship LIMIT 1)='WY' 508 ORDER BY c_id 509 ---- 510 1 511 CA 512 2 513 TX 514 4 515 TX 516 517 # Apply 518 query IT nosort 519 SELECT * 520 FROM c 521 WHERE (SELECT o_id FROM o WHERE o.c_id=c.c_id AND ship='WY')=4; 522 ---- 523 524 # Try to find customers other than customer #2 that have at most one order that 525 # is shipping to 'CA'. However, since there is more than one order shipping to 526 # 'CA' corresponding to customers other than #2, this attempt fails with an 527 # error. 528 # TODO: gosql driver ignores error for some reason 529 # statement error 530 # SELECT * FROM c WHERE c_id=(SELECT c_id FROM o WHERE ship='CA' AND c.c_id<>2) 531 532 # Find customers other than customer #1 that have at most one order that is 533 # shipping to 'CA' and a billing state equal to 'TX'. Since there is only one 534 # other customer who is shipping to 'CA', and this customer has only a single 535 # order, this attempt is successful. 536 query IT nosort 537 SELECT * FROM c WHERE c_id=(SELECT c_id FROM o WHERE ship='CA' AND c_id<>1 AND bill='TX') 538 ---- 539 2 540 TX 541 542 # Find customers with billing state equal to 'FL' that have at most one order 543 # that is shipping to 'WA'. Since there is only one order shipping to 'WA', this 544 # attempt is successful. 545 query IT nosort 546 SELECT * FROM c WHERE c_id=(SELECT c_id FROM o WHERE ship='WA' AND bill='FL') 547 ---- 548 6 549 FL 550 551 # Try to find customers that have at most one order that is also shipping to 552 # 'WA'. However, since there are customers that have more than one order, this 553 # attempt fails with an error. 554 # TODO: gosql driver ignores error for some reason 555 # statement error 556 # SELECT * FROM c WHERE (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NOT NULL)='WA' 557 558 # Add clause to filter out customers that have more than one order. Find 559 # remaining customers with at least one order shipping to 'WA'. 560 query IT nosort 561 SELECT * 562 FROM c 563 WHERE ( 564 SELECT ship 565 FROM o 566 WHERE o.c_id=c.c_id AND ship IS NOT NULL AND (SELECT count(*) FROM o WHERE o.c_id=c.c_id)<=1 567 )='WA' 568 ---- 569 6 570 FL 571 572 # ------------------------------------------------------------------------------ 573 # Subqueries in projection lists. 574 # Although the queries are similar to those above, they are often compiled 575 # differently in the context of a projection list, due to different null 576 # result handling rules. 577 # ------------------------------------------------------------------------------ 578 579 # Customers with orders. 580 query IB nosort 581 SELECT c_id, EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id; 582 ---- 583 1 584 1 585 2 586 1 587 3 588 0 589 4 590 1 591 5 592 0 593 6 594 1 595 596 # Customers with no orders. 597 query IB nosort 598 SELECT c_id, NOT EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id; 599 ---- 600 1 601 0 602 2 603 0 604 3 605 1 606 4 607 0 608 5 609 1 610 6 611 0 612 613 # Customers with orders or with no orders (should be all customers). 614 query IB nosort 615 SELECT 616 c_id, 617 EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id) 618 OR NOT EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id) 619 FROM c 620 ORDER BY c_id; 621 ---- 622 1 623 1 624 2 625 1 626 3 627 1 628 4 629 1 630 5 631 1 632 6 633 1 634 635 # Customers with billing address in TX that have orders. 636 query IB nosort 637 SELECT c_id, EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id AND c.bill='TX') FROM c ORDER BY c_id; 638 ---- 639 1 640 0 641 2 642 1 643 3 644 0 645 4 646 1 647 5 648 0 649 6 650 0 651 652 # Customers that have at least one order shipped to WY. 653 query IB nosort 654 SELECT c_id, 'WY' IN (SELECT ship FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id; 655 ---- 656 1 657 0 658 2 659 NULL 660 3 661 0 662 4 663 1 664 5 665 0 666 6 667 0 668 669 # Customers that have at least one order shipped to WY or to WA. 670 query IB nosort 671 SELECT 672 c_id, 673 'WY' IN (SELECT ship FROM o WHERE o.c_id=c.c_id) 674 OR 'WA' IN (SELECT ship FROM o WHERE o.c_id=c.c_id) 675 FROM c 676 ORDER BY c_id; 677 ---- 678 1 679 0 680 2 681 NULL 682 3 683 0 684 4 685 1 686 5 687 0 688 6 689 1 690 691 # Customers that have at least one order shipped to CA, but none to TX. 692 query IB nosort 693 SELECT 694 c_id, 695 'CA' IN (SELECT ship FROM o WHERE o.c_id=c.c_id) 696 AND 'TX' NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id) 697 FROM c 698 ORDER BY c_id; 699 ---- 700 1 701 1 702 2 703 0 704 3 705 0 706 4 707 NULL 708 5 709 0 710 6 711 0 712 713 # Customers with at least one order with billing addr = shipping addr. 714 query IB nosort 715 SELECT c_id, bill IN (SELECT ship FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id; 716 ---- 717 1 718 1 719 2 720 1 721 3 722 0 723 4 724 NULL 725 5 726 0 727 6 728 0 729 730 # Customers with all orders with billing addr = shipping addr. 731 query IB nosort 732 SELECT c_id, bill = ALL(SELECT ship FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id; 733 ---- 734 1 735 1 736 2 737 0 738 3 739 1 740 4 741 0 742 5 743 1 744 6 745 0 746 747 # Customers with no order with billing addr = shipping addr (with NULL ship). 748 query IB nosort 749 SELECT c_id, bill NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id; 750 ---- 751 1 752 0 753 2 754 0 755 3 756 1 757 4 758 NULL 759 5 760 1 761 6 762 1 763 764 # Customers with no order with billing addr = shipping addr (no NULL ship). 765 query IB nosort 766 SELECT c_id, bill NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NOT NULL) 767 FROM c 768 ORDER BY c_id; 769 ---- 770 1 771 0 772 2 773 0 774 3 775 1 776 4 777 1 778 5 779 1 780 6 781 1 782 783 # Customers with no order with billing addr = shipping addr (only NULL ship). 784 query IB nosort 785 SELECT c_id, bill NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NULL) 786 FROM c 787 ORDER BY c_id; 788 ---- 789 1 790 1 791 2 792 NULL 793 3 794 1 795 4 796 NULL 797 5 798 1 799 6 800 1 801 802 # Customers with bill state < any ship state. 803 query IB nosort 804 SELECT c_id, bill < ANY(SELECT ship FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id; 805 ---- 806 1 807 0 808 2 809 NULL 810 3 811 0 812 4 813 1 814 5 815 0 816 6 817 1 818 819 # Customers where bill state < any ship state is null result. 820 query IB nosort 821 SELECT c_id, (bill < ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NULL FROM c ORDER BY c_id; 822 ---- 823 1 824 0 825 2 826 1 827 3 828 0 829 4 830 0 831 5 832 0 833 6 834 0 835 836 # Customers where bill state < any ship state is not null result. 837 query IB nosort 838 SELECT c_id, (bill < ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL FROM c ORDER BY c_id; 839 ---- 840 1 841 1 842 2 843 0 844 3 845 1 846 4 847 1 848 5 849 1 850 6 851 1 852 853 # Customers with bill state > any ship state. 854 query IB nosort 855 SELECT c_id, bill > ANY(SELECT ship FROM o WHERE o.c_id=c.c_id) FROM c ORDER BY c_id; 856 ---- 857 1 858 0 859 2 860 1 861 3 862 0 863 4 864 NULL 865 5 866 0 867 6 868 0 869 870 # Customers where bill state > any ship state is null result. 871 query IB nosort 872 SELECT c_id, (bill > ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NULL FROM c ORDER BY c_id; 873 ---- 874 1 875 0 876 2 877 0 878 3 879 0 880 4 881 1 882 5 883 0 884 6 885 0 886 887 # Customers where bill state > any ship state is not null result. 888 query IB nosort 889 SELECT c_id, (bill > ANY(SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL FROM c ORDER BY c_id; 890 ---- 891 1 892 1 893 2 894 1 895 3 896 1 897 4 898 0 899 5 900 1 901 6 902 1 903 904 # Customers where bill state matches any non-null ship state. 905 query IB nosort 906 SELECT c_id, bill = ANY(SELECT ship FROM o WHERE ship IS NOT NULL) FROM c; 907 ---- 908 1 909 1 910 2 911 1 912 3 913 0 914 4 915 1 916 5 917 NULL 918 6 919 0 920 921 # Customers where bill state matches any non-null ship state or is null. 922 query IB nosort 923 SELECT c_id, bill = ANY(SELECT ship FROM o WHERE ship IS NOT NULL) OR bill IS NULL FROM c; 924 ---- 925 1 926 1 927 2 928 1 929 3 930 0 931 4 932 1 933 5 934 1 935 6 936 0 937 938 # Test NULL IN case. 939 query IB nosort 940 SELECT c_id, (NULL IN (SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL 941 FROM c 942 ORDER BY c_id; 943 ---- 944 1 945 0 946 2 947 0 948 3 949 1 950 4 951 0 952 5 953 1 954 6 955 0 956 957 # Test NULL NOT IN case. Use IS NOT NULL to prevent normalize ANY into EXISTS. 958 query IB nosort 959 SELECT c_id, (NULL NOT IN (SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NOT NULL 960 FROM c 961 ORDER BY c_id; 962 ---- 963 1 964 0 965 2 966 0 967 3 968 1 969 4 970 0 971 5 972 1 973 6 974 0 975 976 # Customers where it is unknown whether a replaced bill state is one of the ship 977 # states. This tests a more complex scalar expression as argument to IN. 978 query IB nosort 979 SELECT c_id, (replace(bill, 'TX', 'WY') IN (SELECT ship FROM o WHERE o.c_id=c.c_id)) IS NULL 980 FROM c 981 ORDER BY c_id; 982 ---- 983 1 984 0 985 2 986 1 987 3 988 0 989 4 990 0 991 5 992 0 993 6 994 0 995 996 # Customers with all orders with billing addr = shipping addr, or with at least 997 # one order shipped to WY. 998 query IB nosort 999 SELECT 1000 c_id, 1001 bill = ALL(SELECT ship FROM o WHERE o.c_id=c.c_id) 1002 OR EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id AND ship='WY') 1003 FROM c 1004 ORDER BY c_id; 1005 ---- 1006 1 1007 1 1008 2 1009 0 1010 3 1011 1 1012 4 1013 1 1014 5 1015 1 1016 6 1017 0 1018 1019 # Customers with all orders with billing addr = shipping addr, but with at least 1020 # one order. 1021 query IB nosort 1022 SELECT 1023 c_id, 1024 bill = ALL(SELECT ship FROM o WHERE o.c_id=c.c_id) 1025 AND EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id) 1026 FROM c 1027 ORDER BY c_id; 1028 ---- 1029 1 1030 1 1031 2 1032 0 1033 3 1034 0 1035 4 1036 0 1037 5 1038 0 1039 6 1040 0 1041 1042 # Apply. 1043 query IT nosort 1044 SELECT * 1045 FROM c 1046 WHERE (SELECT min(ship) FROM o WHERE o.c_id=c.c_id) IN (SELECT ship FROM o WHERE o.c_id=c.c_id); 1047 ---- 1048 1 1049 CA 1050 2 1051 TX 1052 4 1053 TX 1054 6 1055 FL 1056 1057 # Customers with at least one shipping address = minimum shipping address. 1058 query IB nosort 1059 SELECT 1060 c_id, 1061 (SELECT min(ship) FROM o WHERE o.c_id=c.c_id) IN (SELECT ship FROM o WHERE o.c_id=c.c_id) 1062 FROM c 1063 ORDER BY c_id; 1064 ---- 1065 1 1066 1 1067 2 1068 1 1069 3 1070 0 1071 4 1072 1 1073 5 1074 0 1075 6 1076 1 1077 1078 # Maximum number of orders for a customer. Use subquery in aggregate function. 1079 query I nosort 1080 SELECT max((SELECT count(*) FROM o WHERE o.c_id=c.c_id)) FROM c; 1081 ---- 1082 3 1083 1084 # Order count by customer. 1085 query II nosort 1086 SELECT 1087 c_id, 1088 (SELECT count(*) FROM o WHERE o.c_id=c.c_id) 1089 FROM c 1090 ORDER BY c_id; 1091 ---- 1092 1 1093 3 1094 2 1095 3 1096 3 1097 0 1098 4 1099 2 1100 5 1101 0 1102 6 1103 1 1104 1105 # Count bill/ship addresses in each state. 1106 query TI nosort 1107 SELECT 1108 s.st, 1109 (SELECT count(*) FROM c WHERE c.bill=s.st) + (SELECT count(*) FROM o WHERE o.ship=s.st) 1110 FROM (SELECT c.bill AS st FROM c UNION SELECT o.ship AS st FROM o) s 1111 ORDER BY s.st; 1112 ---- 1113 NULL 1114 0 1115 CA 1116 5 1117 FL 1118 1 1119 MA 1120 1 1121 TX 1122 3 1123 WA 1124 1 1125 WY 1126 1 1127 1128 # Customer orders grouped by ship state, compared with count of all orders 1129 # shipped to that state by all other customers combined. 1130 query ITII nosort 1131 SELECT c.c_id, o.ship, count(*) AS cust, 1132 (SELECT count(*) FROM o AS o2 WHERE o2.ship = o.ship AND o2.c_id <> c.c_id) AS other 1133 FROM c 1134 INNER JOIN o 1135 ON c.c_id=o.c_id 1136 GROUP BY c.c_id, o.ship; 1137 ---- 1138 1 1139 CA 1140 3 1141 1 1142 2 1143 CA 1144 1 1145 3 1146 2 1147 TX 1148 1 1149 0 1150 2 1151 NULL 1152 1 1153 0 1154 4 1155 WY 1156 1 1157 0 1158 4 1159 NULL 1160 1 1161 0 1162 6 1163 WA 1164 1 1165 0 1166 1167 # Customers with their orders (even if no orders), plus max of bill and ship 1168 # states for that customer (alphabetically). 1169 query IIT nosort 1170 SELECT 1171 c.c_id, 1172 o.o_id, 1173 ( 1174 SELECT max(CASE WHEN c2.bill > o2.ship THEN c2.bill ELSE o2.ship END) 1175 FROM c AS c2, o AS o2 1176 WHERE c2.c_id=o2.c_id AND c2.c_id=c.c_id 1177 ) 1178 FROM c 1179 LEFT JOIN o 1180 ON c.c_id=o.c_id 1181 ORDER BY c.c_id, o.o_id 1182 ---- 1183 1 1184 10 1185 CA 1186 1 1187 20 1188 CA 1189 1 1190 30 1191 CA 1192 2 1193 40 1194 TX 1195 2 1196 50 1197 TX 1198 2 1199 60 1200 TX 1201 3 1202 NULL 1203 NULL 1204 4 1205 70 1206 WY 1207 4 1208 80 1209 WY 1210 5 1211 NULL 1212 NULL 1213 6 1214 90 1215 WA 1216 1217 # Customers, with boolean indicating whether they have at least one order with a 1218 # NULL ship state. 1219 query IB nosort 1220 SELECT 1221 c.c_id, 1222 (SELECT ship FROM o WHERE o.c_id=c.c_id ORDER BY ship LIMIT 1) IS NOT NULL 1223 FROM c 1224 ORDER BY c.c_id 1225 ---- 1226 1 1227 1 1228 2 1229 0 1230 3 1231 0 1232 4 1233 0 1234 5 1235 0 1236 6 1237 1 1238 1239 # Customers, with boolean indicating whether their first order shipped to 'CA' 1240 # or 'WY' (no NULL ship). 1241 query IB nosort 1242 SELECT 1243 c.c_id, 1244 (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NOT NULL ORDER BY ship LIMIT 1)='CA' 1245 OR (SELECT ship FROM o WHERE o.c_id=c.c_id AND ship IS NOT NULL ORDER BY ship LIMIT 1)='WY' 1246 FROM c 1247 ORDER BY c_id 1248 ---- 1249 1 1250 1 1251 2 1252 1 1253 3 1254 NULL 1255 4 1256 1 1257 5 1258 NULL 1259 6 1260 0 1261 1262 # query T nosort 1263 # SELECT (SELECT concat_agg(ship || ' ') 1264 # FROM 1265 # (SELECT c_id AS o_c_id, ship FROM o ORDER BY ship) sq1 1266 # WHERE o_c_id=c.c_id) 1267 # FROM c ORDER BY c_id 1268 # ---- 1269 # CA 1270 # CA 1271 # CA 1272 # CA 1273 # TX 1274 # NULL 1275 # WY 1276 # NULL 1277 # WA 1278 1279 # query T nosort 1280 # SELECT (SELECT string_agg(ship, ', ') 1281 # FROM 1282 # (SELECT c_id AS o_c_id, ship FROM o ORDER BY ship) 1283 # WHERE o_c_id=c.c_id) 1284 # FROM c ORDER BY c_id 1285 # ---- 1286 # CA, 1287 # CA, 1288 # CA 1289 # CA, 1290 # TX 1291 # NULL 1292 # WY 1293 # NULL 1294 # WA 1295 1296 # query T nosort 1297 # SELECT (SELECT string_agg(DISTINCT ship, ', ') 1298 # FROM 1299 # (SELECT c_id AS o_c_id, ship FROM o ORDER BY ship) 1300 # WHERE o_c_id=c.c_id) 1301 # FROM c ORDER BY c_id 1302 # ---- 1303 # CA 1304 # CA, 1305 # TX 1306 # NULL 1307 # WY 1308 # NULL 1309 # WA 1310 1311 query ITI nosort 1312 SELECT 1313 * 1314 FROM 1315 (SELECT c_id AS c_c_id, bill FROM c) sq1, 1316 LATERAL (SELECT row_number() OVER () AS rownum FROM o WHERE c_id = c_c_id) sq2 1317 ORDER BY c_c_id, bill, rownum 1318 ---- 1319 1 1320 CA 1321 1 1322 1 1323 CA 1324 2 1325 1 1326 CA 1327 3 1328 2 1329 TX 1330 1 1331 2 1332 TX 1333 2 1334 2 1335 TX 1336 3 1337 4 1338 TX 1339 1 1340 4 1341 TX 1342 2 1343 6 1344 FL 1345 1 1346 1347 query TI nosort 1348 SELECT 1349 * 1350 FROM 1351 (SELECT bill FROM c) sq1, 1352 LATERAL (SELECT row_number() OVER (PARTITION BY bill) AS rownum FROM o WHERE ship = bill) sq2 1353 ORDER BY bill, rownum 1354 ---- 1355 CA 1356 1 1357 CA 1358 2 1359 CA 1360 3 1361 CA 1362 4 1363 TX 1364 1 1365 TX 1366 1 1367 1368 # ------------------------------------------------------------------------------ 1369 # Subqueries in other interesting locations. 1370 # ------------------------------------------------------------------------------ 1371 1372 # Group by order count by customer, and order by that order count as well. 1373 query II nosort 1374 SELECT 1375 (SELECT count(*) FROM o WHERE o.c_id=c.c_id) AS order_cnt, 1376 count(*) AS cust_cnt 1377 FROM c 1378 GROUP BY (SELECT count(*) FROM o WHERE o.c_id=c.c_id) 1379 ORDER BY (SELECT count(*) FROM o WHERE o.c_id=c.c_id) DESC; 1380 ---- 1381 3 1382 2 1383 2 1384 1 1385 1 1386 1 1387 0 1388 2 1389 1390 # Subquery in VALUES clause. 1391 query III nosort 1392 SELECT c_cnt, o_cnt, c_cnt + o_cnt AS total 1393 FROM (VALUES ROW((SELECT count(*) FROM c), (SELECT count(*) FROM o))) AS v(c_cnt, o_cnt) 1394 WHERE c_cnt > 0 AND o_cnt > 0; 1395 ---- 1396 6 1397 9 1398 15 1399 1400 # Subquery in JOIN condition. 1401 query II nosort 1402 SELECT c.c_id, o.o_id 1403 FROM c 1404 INNER JOIN o 1405 ON c.c_id=o.c_id AND EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id AND ship IS NULL); 1406 ---- 1407 2 1408 40 1409 2 1410 50 1411 2 1412 60 1413 4 1414 70 1415 4 1416 80 1417 1418 # statement error 1419 # TODO: gosql driver ignores error for some reason 1420 # SELECT c.c_id, o.o_id 1421 # FROM c 1422 # INNER JOIN o 1423 # ON c.c_id=o.c_id AND o.ship = (SELECT o.ship FROM o WHERE o.c_id=c.c_id); 1424 1425 statement error 1426 SELECT (SELECT c_id FROM o AS OF SYSTEM TIME '-1us') 1427 FROM c 1428 WHERE EXISTS(SELECT * FROM o WHERE o.c_id=c.c_id) 1429 1430 # Subquery in ARRAY(...) 1431 # query ITT nosort 1432 # SELECT 1433 # c_id, 1434 # ARRAY(SELECT o_id FROM o WHERE o.c_id = c.c_id ORDER BY o_id), 1435 # ARRAY(SELECT o_id FROM o WHERE o.ship = c.bill ORDER BY o_id) 1436 # FROM c ORDER BY c_id 1437 # ---- 1438 # 1 1439 # {10,20,30} 1440 # {10,20,30,40} 1441 # 2 1442 # {40,50,60} 1443 # {50} 1444 # 3 1445 # {} 1446 # {} 1447 # 4 1448 # {70,80} 1449 # {50} 1450 # 5 1451 # {} 1452 # {} 1453 # 6 1454 # {90} 1455 # {} 1456 1457 # query IT nosort 1458 # SELECT 1459 # c_id, 1460 # ARRAY(SELECT o_id FROM o WHERE o.c_id = c.c_id ORDER BY o_id) 1461 # FROM c ORDER BY c_id 1462 # ---- 1463 # 1 1464 # {10,20,30} 1465 # 2 1466 # {40,50,60} 1467 # 3 1468 # {} 1469 # 4 1470 # {70,80} 1471 # 5 1472 # {} 1473 # 6 1474 # {90} 1475 1476 # Regression for issue #24676: missing support for correlated subquery in JSON 1477 # operator. 1478 statement ok 1479 CREATE TABLE `groups`( 1480 id SERIAL PRIMARY KEY, 1481 data JSON 1482 ); 1483 1484 statement ok 1485 INSERT INTO `groups`(data) VALUES('{"name": "Group 1", "members": [{"name": "admin", "type": "USER"}, {"name": "user", "type": "USER"}]}'); 1486 1487 statement ok 1488 INSERT INTO `groups`(data) VALUES('{"name": "Group 2", "members": [{"name": "admin2", "type": "USER"}]}'); 1489 1490 # query TT nosort 1491 # SELECT 1492 # g.data->>'name' AS group_name, 1493 # jsonb_array_elements( (SELECT gg.data->'members' FROM `groups` gg WHERE gg.data->>'name' = g.data->>'name') ) 1494 # FROM 1495 # `groups` g 1496 # ORDER BY g.data->>'name' 1497 # ---- 1498 # Group 1499 # 1 1500 # {"name": 1501 # "admin", 1502 # "type": 1503 # "USER"} 1504 # Group 1505 # 1 1506 # {"name": 1507 # "user", 1508 # "type": 1509 # "USER"} 1510 # Group 1511 # 2 1512 # {"name": 1513 # "admin2", 1514 # "type": 1515 # "USER"} 1516 1517 # query TT nosort 1518 # SELECT 1519 # data->>'name', 1520 # members 1521 # FROM 1522 # `groups` AS g, 1523 # jsonb_array_elements( 1524 # ( 1525 # SELECT 1526 # gg.data->'members' AS members 1527 # FROM 1528 # `groups` AS gg 1529 # WHERE 1530 # gg.data->>'name' = g.data->>'name' 1531 # ) 1532 # ) AS members 1533 # ORDER BY g.data->>'name' 1534 # ---- 1535 # Group 1536 # 1 1537 # {"name": 1538 # "admin", 1539 # "type": 1540 # "USER"} 1541 # Group 1542 # 1 1543 # {"name": 1544 # "user", 1545 # "type": 1546 # "USER"} 1547 # Group 1548 # 2 1549 # {"name": 1550 # "admin2", 1551 # "type": 1552 # "USER"} 1553 1554 # ------------------------------------------------------------------------------ 1555 # Regression test cases. 1556 # ------------------------------------------------------------------------------ 1557 1558 # Regression for issue 32786. 1559 1560 statement ok 1561 CREATE TABLE t32786 (id VARCHAR(36) PRIMARY KEY, parent_id VARCHAR(36), parent_path text) 1562 1563 statement ok 1564 INSERT INTO t32786 VALUES ('3AAA2577-DBC3-47E7-9E85-9CC7E19CF48A', null, null) 1565 1566 # statement ok 1567 # UPDATE t32786 as node 1568 # SET parent_path=concat((SELECT parent.parent_path 1569 # FROM t32786 parent 1570 # WHERE parent.id=node.parent_id), 1571 # node.id::varchar, '/') 1572 1573 statement ok 1574 INSERT INTO t32786 VALUES ('5AE7EAFD-8277-4F41-83DE-0FD4B4482169', '3AAA2577-DBC3-47E7-9E85-9CC7E19CF48A', null) 1575 1576 # statement ok 1577 # UPDATE t32786 as node 1578 # SET parent_path=concat((SELECT parent.parent_path 1579 # FROM t32786 parent 1580 # WHERE parent.id=node.parent_id), 1581 # node.id::varchar, '/') 1582 1583 # query T nosort 1584 # SELECT parent_path FROM t32786 ORDER BY id 1585 # ---- 1586 # 3aaa2577-dbc3-47e7-9e85-9cc7e19cf48a/ 1587 # 3aaa2577-dbc3-47e7-9e85-9cc7e19cf48a/5ae7eafd-8277-4f41-83de-0fd4b4482169/ 1588 1589 # Regression test for #32723. 1590 # query I nosort 1591 # SELECT 1592 # generate_series(a + 1, a + 1) 1593 # FROM 1594 # (SELECT a FROM ((SELECT 1 AS a, 1) EXCEPT ALL (SELECT 0, 0))) 1595 # ---- 1596 # 2 1597 1598 # Regression for issue 35437. 1599 1600 statement ok 1601 CREATE TABLE users ( 1602 id INT8 NOT NULL, 1603 name VARCHAR(50), 1604 PRIMARY KEY (id) 1605 ); 1606 1607 statement ok 1608 INSERT INTO users(id, name) VALUES (1, 'user1'); 1609 1610 statement ok 1611 INSERT INTO users(id, name) VALUES (2, 'user2'); 1612 1613 statement ok 1614 INSERT INTO users(id, name) VALUES (3, 'user3'); 1615 1616 statement ok 1617 CREATE TABLE stuff ( 1618 id INT8 NOT NULL, 1619 date DATE, 1620 user_id INT8, 1621 PRIMARY KEY (id), 1622 FOREIGN KEY (user_id) REFERENCES users (id) 1623 ); 1624 1625 statement ok 1626 INSERT INTO stuff(id, date, user_id) VALUES (1, '2007-10-15', 1); 1627 1628 statement ok 1629 INSERT INTO stuff(id, date, user_id) VALUES (2, '2007-12-15', 1); 1630 1631 statement ok 1632 INSERT INTO stuff(id, date, user_id) VALUES (3, '2007-11-15', 1); 1633 1634 statement ok 1635 INSERT INTO stuff(id, date, user_id) VALUES (4, '2008-01-15', 2); 1636 1637 statement ok 1638 INSERT INTO stuff(id, date, user_id) VALUES (5, '2007-06-15', 3); 1639 1640 statement ok 1641 INSERT INTO stuff(id, date, user_id) VALUES (6, '2007-03-15', 3); 1642 1643 # query ITITI nosort 1644 # SELECT 1645 # users.id AS users_id, 1646 # users.name AS users_name, 1647 # stuff_1.id AS stuff_1_id, 1648 # stuff_1.date AS stuff_1_date, 1649 # stuff_1.user_id AS stuff_1_user_id 1650 # FROM 1651 # users 1652 # LEFT JOIN stuff AS stuff_1 1653 # ON 1654 # users.id = stuff_1.user_id 1655 # AND stuff_1.id 1656 # = ( 1657 # SELECT 1658 # stuff_2.id 1659 # FROM 1660 # stuff AS stuff_2 1661 # WHERE 1662 # stuff_2.user_id = users.id 1663 # ORDER BY 1664 # stuff_2.date DESC 1665 # LIMIT 1666 # 1 1667 # ) 1668 # ORDER BY 1669 # users.name; 1670 # ---- 1671 # 1 1672 # user1 1673 # 2 1674 # 2007-12-15 1675 # 00:00:00 1676 # +0000 1677 # +0000 1678 # 1 1679 # 2 1680 # user2 1681 # 4 1682 # 2008-01-15 1683 # 00:00:00 1684 # +0000 1685 # +0000 1686 # 2 1687 # 3 1688 # user3 1689 # 5 1690 # 2007-06-15 1691 # 00:00:00 1692 # +0000 1693 # +0000 1694 # 3 1695 1696 statement ok 1697 DROP TABLE stuff; 1698 1699 statement ok 1700 DROP TABLE users; 1701 1702 # Regression test for #38867. 1703 # query T nosort 1704 # SELECT ( 1705 # SELECT 1706 # ARRAY ( 1707 # SELECT c.relname 1708 # FROM pg_inherits AS i JOIN pg_class AS c ON c.oid = i.inhparent 1709 # WHERE i.inhrelid = rel.oid 1710 # ORDER BY inhseqno 1711 # ) 1712 # ) 1713 # FROM pg_class AS rel 1714 # LIMIT 5; 1715 # ---- 1716 # {} 1717 # {} 1718 # {} 1719 # {} 1720 # {} 1721 1722 # Customers, their billing address, and all orders not going to their billing address 1723 # query ITT nosort 1724 # SELECT 1725 # c_id, bill, states 1726 # FROM 1727 # c 1728 # JOIN LATERAL ( 1729 # SELECT 1730 # COALESCE(array_agg(o.ship), '{}') AS states 1731 # FROM 1732 # o 1733 # WHERE 1734 # o.c_id = c.c_id AND o.ship != c.bill 1735 # ) ON true; 1736 # ---- 1737 # 1 1738 # CA 1739 # {} 1740 # 3 1741 # MA 1742 # {} 1743 # 4 1744 # TX 1745 # {WY} 1746 # 5 1747 # NULL 1748 # {} 1749 # 6 1750 # FL 1751 # {WA} 1752 # 2 1753 # TX 1754 # {CA} 1755 1756 # Customers that have billing addresses and all orders not going to their billing address 1757 # query IT nosort 1758 # SELECT 1759 # c_id, states 1760 # FROM 1761 # c 1762 # LEFT JOIN LATERAL ( 1763 # SELECT 1764 # COALESCE(array_agg(o.ship), '{}') AS states 1765 # FROM 1766 # o 1767 # WHERE 1768 # o.c_id = c.c_id AND o.ship != c.bill 1769 # ) ON true 1770 # WHERE 1771 # bill IS NOT NULL; 1772 # ---- 1773 # 1 1774 # {} 1775 # 3 1776 # {} 1777 # 2 1778 # {CA} 1779 # 4 1780 # {WY} 1781 # 6 1782 # {WA} 1783 1784 # Regression test for #48638. 1785 statement ok 1786 CREATE TABLE IF NOT EXISTS t_48638 ( 1787 `key` INT NOT NULL, 1788 `value` INTEGER NOT NULL, 1789 PRIMARY KEY (`key`, `value`)) 1790 1791 statement ok 1792 INSERT INTO t_48638 values (1, 4); 1793 1794 statement ok 1795 INSERT INTO t_48638 values (4, 3); 1796 1797 statement ok 1798 INSERT INTO t_48638 values (3, 2); 1799 1800 statement ok 1801 INSERT INTO t_48638 values (4, 1); 1802 1803 statement ok 1804 INSERT INTO t_48638 values (1, 2); 1805 1806 statement ok 1807 INSERT INTO t_48638 values (6, 5); 1808 1809 statement ok 1810 INSERT INTO t_48638 values (7, 8); 1811 1812 query II nosort 1813 SELECT * 1814 FROM t_48638 1815 WHERE `key` IN ( 1816 WITH v AS ( 1817 SELECT 1818 level1.`value` AS `value`, level1.`key` AS level1, level2.`key` AS level2, level3.`key` AS level3 1819 FROM 1820 t_48638 AS level2 1821 RIGHT JOIN (SELECT * FROM t_48638 WHERE `value` = 4) AS level1 ON level1.`value` = level2.`key` 1822 LEFT JOIN (SELECT * FROM t_48638) AS level3 ON level3.`key` = level2.`value` 1823 ) 1824 SELECT v.level1 FROM v WHERE v.level1 IS NOT NULL 1825 UNION ALL SELECT v.level2 FROM v WHERE v.level2 IS NOT NULL 1826 UNION ALL SELECT v.level3 FROM v WHERE v.level3 IS NOT NULL 1827 ) 1828 ---- 1829 1 1830 2 1831 1 1832 4 1833 3 1834 2 1835 4 1836 1 1837 4 1838 3 1839