Trouble joining tables with UNION

Asked

Viewed 46 times

0

I am trying to bring the top 3 of my table and also the last 2 placed, SITE TO EXECUTE SCRIPT

This is my current progress that doesn’t work

SELECT ('Podium: ' ||TEAM) as name  
FROM LEAGUE
ORDER BY position ASC LIMIT 3;
  UNION ALL
SELECT ('Demoted: ' ||TEAM) as name 
FROM LEAGUE
ORDER BY position DESC LIMIT 2;

I made a base that can be used in the fiddle for anyone who wants to help me.

    CREATE TABLE league
        (
            position INTEGER PRIMARY KEY NOT NULL,
            team VARCHAR(45)
        );
    

   INSERT INTO league VALUES(1, 'The Quack Bats');
    INSERT INTO league VALUES(2, 'The Responsible Hornets');    
    INSERT INTO league VALUES(3, 'The Bawdy Dolphins'); 
    INSERT INTO league VALUES(4, 'The Abstracted Sharks');  
    INSERT INTO league VALUES(5, 'The Nervous Zebras'); 
    INSERT INTO league VALUES(6, 'The Oafish Owls');    
    INSERT INTO league VALUES(7, 'The Unequaled Bison');    
    INSERT INTO league VALUES(8, 'The Keen Kangaroos'); 
    INSERT INTO league VALUES(9, 'The Left Nightingales');  
    INSERT INTO league VALUES(10, 'The Terrific Elks'); 
    INSERT INTO league VALUES(11, 'The Lumpy Frogs');   
    INSERT INTO league VALUES(12, 'The Swift Buffalo'); 
    INSERT INTO league VALUES(13, 'The Big Chargers');  
    INSERT INTO league VALUES(14, 'The Rough Robins');  
    INSERT INTO league VALUES(15, 'The Silver Crocs');
  • 1

    not just take out the semicolon left there after the LIMIT 3?

1 answer

2

Try it this way:

  SELECT position,
         'Podium' AS tipo,
         team
    FROM (SELECT *
            FROM league
           ORDER BY position ASC LIMIT 3) v
   UNION
  SELECT position,
         'Demoted' AS tipo,
         team
    FROM (SELECT *
            FROM league
           ORDER BY position DESC LIMIT 2) v

ORDER BY 1;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.