MySQL - SELECT the name that comes first alphabetically

后端 未结 11 1388
悲&欢浪女
悲&欢浪女 2020-12-24 08:50

I have started to learn MySQL.

Here is the table world:

+-------------+-----------+---------+
|    name     | continent |  area   |
+--         


        
相关标签:
11条回答
  • 2020-12-24 08:55

    If it's an Exercise from SQLZoo, than IMO it should look something like this:

    select continent, name from world x
    where name = (select name 
                      from world y 
                      where  x.continent =  y.continent 
                      order by name asc 
                      limit 1)
    

    P.S. I study SQL from there now and this post helped me. Thanks to @Parado!)

    Update: I've found this site with answers. Useful if stack.

    0 讨论(0)
  • 2020-12-24 08:57
    SELECT continent,
           name
    FROM world x
    WHERE name=
        (SELECT name
         FROM world y
         WHERE x.continent=y.continent
         ORDER BY name
         LIMIT 1)
    

    This is correlated/ synchronous query.

    0 讨论(0)
  • 2020-12-24 08:58

    Try this

    select distinct  w.continent, 
                     (select w2.name 
                      from world w2 
                      where  w.continent =  w2.continent 
                      order by name asc 
                      limit 1) name 
    from world w
    order by w.continent
    
    0 讨论(0)
  • 2020-12-24 09:01

    Try this

    SELECT continent, name FROM world ORDER BY name ASC;
    
    0 讨论(0)
  • 2020-12-24 09:02
    SELECT  distinct x.continent , x.name
    FROM world x ,world y 
    WHERE x.name = (SELECT y.name FROM world y
     WHERE y.continent=x.continent order by y.name asc limit 1 ) ;
    
    0 讨论(0)
  • 2020-12-24 09:11
    select continent, name from world group by continent order by name
    
    0 讨论(0)
提交回复
热议问题