user180825
user180825

Reputation:

SELECT from SQL

I have a table which contains the names of a country, the population of the country and the countries GDP, how would I display the names of the country and their per capita GDP

Upvotes: 1

Views: 2047

Answers (3)

ManiacZX
ManiacZX

Reputation: 867

To deal with the 0 population division by zero, I'd suggest using a CASE statement

SELECT
    Name,
    CASE IsNull(population,0)
        WHEN 0
            THEN 0
        ELSE
            THEN gdp/population
    END AS 'PerCapitaGDP'
FROM Countries

This will work on MS SQL Server, you may need to look up syntax for which DBMS you are using (for example, it looks like MySQL uses END CASE instead of just END)

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415735

SELECT name, gdp/NullIf(population,0) AS PerCapitaGDP FROM "MyCountryTable"

Upvotes: 12

Rowland Shaw
Rowland Shaw

Reputation: 38130

SQL allows calculations to be performed inline, like:

SELECT Name, GDP / Population AS [Per Capita GDP] FROM YourTable

Upvotes: 6

Related Questions