【优达学城测评】SQL-INSERT(9)

Insert into table values(42,'stuff');

Insert into table (col2,col1) values('stuff',42)

【优达学城测评】SQL-INSERT(9)

【优达学城测评】SQL-INSERT(9)

insert into animals values ('wibble','opossum','2014-11-14');

insert into animals (name,species,birthdate) values ('wibble','opossum','2014-11-14')

【优达学城测评】SQL-INSERT(9)

 


# Find the names of the individual animals that eat fish.
# The animals table has columns (name, species, birthdate) for each individual.
# The diet table has columns (species, food) for each food that a species eats.
 

QUERY = '''
select animals.name
from animals join diet on animals.species=diet.species
where food='fish'
'''

【优达学城测评】SQL-INSERT(9)

【优达学城测评】SQL-INSERT(9)

another question:

Which species does the zoo have only one of?

wrong way:  select species,count(*) as num from animals group by species where num=1;

REMEMBER :The value of num come from count and group by.But where always runs before aggregations

【优达学城测评】SQL-INSERT(9)

BECAUSE: There's no num column in the animals table.And you can't use where after a group by anyway.(而且你不能在 group by 后面使用where) If you tried this query out,you'd get a big error ,but if we change just one word,we can make this right,whereas where filters(筛选) the source table,animals,having filters the result table.So having applies after the group BI aggregation.

 

 

 

转载于:https://my.oschina.net/Bettyty/blog/757874

相关文章:

  • 2021-12-20
  • 2021-11-09
  • 2022-12-23
  • 2021-09-25
  • 2021-07-20
  • 2021-06-12
  • 2021-12-04
  • 2021-07-06
猜你喜欢
  • 2021-08-11
  • 2021-04-20
  • 2021-05-08
  • 2021-04-19
  • 2022-01-05
  • 2021-11-12
  • 2021-10-15
相关资源
相似解决方案