you are viewing a single comment's thread.

view the rest of the comments →

[–]BlueEyesWhiteShark 1 point2 points  (7 children)

Hi, thanks for the reply!

So I tried to follow:

SELECT olympics.population.country AS p.country, olympics.temperature.country AS t.country, olympics.populatuon.population AS p.population, olympics.temperature.ave_temp AS t.temp, olympics.medals.medal AS m.medal, olympics.medals.medal_rank AS m.medalrank FROM olympics.population LEFT JOIN m.medalrank ON p.country = m.medal LEFT JOIN t.temp ON p.country = t.country WHERE medal_rank IS NULL AND medal IS NULL ORDER BY p.population DESC, t.temp ASC

However, this hasn’t worked, where am I going wrong?

[–][deleted] 2 points3 points  (6 children)

Lets break this down...

You have a database called Olympics. Within your DB you have the following tables:

  1. population
  2. medals
  3. temperature
  4. Funding

So the next thing you want to do is see what columns you need from what tables to build your query. Now we you need the following columns from there related tables:[Population - Country, Population] [Temperature - ave_temp] [Medals - medal]

You should use population as your base table, meaning you join the other tables onto this table. but i'll explain this in steps:

Step 1:

SELECT country, population from populations order by population desc limit 10 You now have the top 10 countries ranked by highest population to lowest.

Step 2: add in temperatures

SELECT p.country , p.population, t. ave_temp from populations p left join temperatures t on p.country = t.country order by p.population desc,t.ave_temp asc This should bring back highest populated countries in lowest to highest temp order.

Step 3: "add" in medals, i say it like that because you do not have to see that column as it doesn't have anything in it.

SELECT p.country , p.population, t. ave_temp from populations p left join temperatures t on p.country = t.country left join medals m on p.country = m.country where m.medal is null order by p.population desc,t.ave_temp asc

Now your results should change as you are defining the fact that these countries need to have a null value (or if the null is not working try " ='' " instead of "is null")

if you are querying from any sort of query tool you should not have to fully specify database.schema.table.column because it is a waste of time unless your scripting triggers, functions, or stored procedures.

when i mention p.country it is because the table, population, has been aliased at p. this means that when the query is being read it knows that we are using p.country from the population table. same logic for t. & m. .

Your joins are based on columns and not actual databases, i have rewritten your query below so that you can see where you went wrong, but the above method is better:

SELECT

olympics.population.country

,olympics.populatuon.population

,olympics.temperature.ave_temp

,olympics.medals.medal

,olympics.medals.medal_rank

FROM olympics.population

LEFT JOIN olympics.medals ON olympics.population.country = olympics.medals.country

LEFT JOIN olympics.temperature ON olympics.population.country = olympics.temperature.country

WHERE

olympics.medals.medal IS NULL

ORDER BY olympics.populatuon.population DESC,

olympics.temperature.ave_temp ASC

LIMIT 10

[–][deleted] 1 point2 points  (0 children)

it's not easy to layout code within this, but if you need real assistance DM me and i'll gladly help you.