Beginning SQL How do I sum and pick columns in the same line? -
i started learning sql on w3schools.com today, , want make sure i'm on right track.
i'm trying solve problem:
write sql statement finding combined population of u.s. , mexico (in database).
i can't post table here because of lack of reputation, simple. given mexico's country id 2 , u.s. id 5.
the table has 4 columns, city_id, name, country_id, , population. not know how many rows there are. need add population columns have corresponding '2' or '5' country id.
this have far:
//this statement gives result-set cities in mexico , u.s. select * city country_id=’5’ or country_id=’2’ //here, don't know how reference result-set select sum(population) result-set
the question says in 1 statement, there simpler way this? thanks.
you put sum(population)
expression query..
select sum(population) totalpopulation city country_id='5' or country_id='2'
note can write x=a or x=b
x in (a,b)
, i.e.
country_id in (5,2)
(you can drop quotes if country_id integer although won't fail them)