SQL Programming I am having trouble writing the correct code, any help would be
ID: 3585368 • Letter: S
Question
SQL Programming
I am having trouble writing the correct code, any help would be great.
Using subqueries
1. This query uses only the countryLanguage table. Create a query that lists the countrycode, number of distinct languages spoken in that country, along with the % of languages spoken to total languages.
My code so far
SELECT CountryCode, (Count(Language) * 100 / (SELECT COUNT(*) FROM country))AS Language%
WHERE country.countrycode=language.countrycode;
Explanation / Answer
Query for the question:
SELECT CountryCode, COUNT(DISTINCT Language) as no_of_langauges, (no_of_langauges * 100 / sum(no_of_langauges)) AS Language%
FROM countryLanguage
GROUP BY CountryCode;
percentage for each language is given in the same table, so i think summing the percentage column for each country is the right answer.
SELECT CountryCode, COUNT(DISTINCT Language) as no_of_langauges, sum(percentage) AS tot_percentage
FROM countryLanguage
GROUP BY CountryCode;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.