How to write the SQL QUERY for this question - sql

Given the CITY and COUNTRY tables, query the names of all the continents (COUNTRY.Continent) and their respective average city populations (CITY.Population) rounded down to the nearest integer.
Note: CITY.CountryCode and COUNTRY.Code are matching key columns.

Try the following
select
c.continent,
floor(avg(ci.population))
from country c
join city ci
on c.Code = ci.countrycode
group by
c.continent;

Related

Join two tables and select a row using foreign key SQL query

I have two tables Cities and Country. I am using the query
SELECT *
FROM citiesTable
WHERE cityName LIKE 'F%'
LIMIT 30
Each cityName has their respective country joined with foreign key in country table.
I want to select each city with and their respective country like Fayzabad , Afghanistan. Which query should I use? Please mention must since I am new to SQL
Better use left join to get all city table data
select city.*, ctry.countryName
from citytable city
left join country ctry on city.countryid = ctry.countryid
Use join query
SELECT *
FROM citiesTable, country
WHERE country.country_id = citiesTable.country_id
AND cityName LIKE 'F%' LIMIT 30
Try this query:
SELECT *
FROM CitiesTable a, Country b
WHERE a.country_id = b.id
AND a.cityName LIKE 'F%'
LIMIT 30

Sql query for extracting info from one table based on other

I have two tables
Country with countryid and countryname
City with cityid and cityname
I want to extract city names based on the countryid I select.
I'm very new to sql database and please help me with direct query if you can.
Thank you very much.
According to the table structure which you showed, this query is not possible. You need to add a 'country_id' to the cities table so that you know in which country is each city. Once this has been done, your query would be
select cities.cityname, countries.countryname
from cities inner join countries on countries.country_id = cities.country_id
order by countries.countryname, cities.cityname
First you should have countryid in city table as a country can have many cities. Then you can write.
select A.cityname from city A
where
A.countryid in (select B.countryid from country B)
Please check this link if you want to find out more.
SQL: Select from one table matching criteria in another?
You have to add a countryid in City table and write the following query
select * from city c left outer join country co on c.countryid=co.countryid;
You need to have the countryid in the city table.
SELECT cityid, cityname FROM city WHERE countryid = $CounrtyID

SQL Query Not Working, Returning Nothing Back

The goal of my query is to return the country, capital, and number of languages spoken. It also needs to be ordered by descending number of languages spoken, and then by capital. Finally, the number of languages must be at least 5 and 10 or less.
Here is my query:
SELECT country.name AS Country,
city.name AS Capital,
Count(countrylanguage.language) AS NumLanguages
FROM country,
city,
countrylanguage
WHERE city.id = country.capital
GROUP BY city.name,
country.name
HAVING ( Count(countrylanguage.language) BETWEEN 5 AND 10 );
It returns nothing. The where clause is necessary in order to get the city name to display. In the country table is just an id number, and then the city table holds the id number and name.
If anyone could spot my error I"d be very grateful!
You are missing the relationship with countrylanguage. Without it, you have a cartesian product, so Count(countrylanguage.language) is equal the number of records in countrylanguage, which is most likely to be greater then 10.
Here's a proposed solution (adjust with field names/DB structure accordingly):
SELECT country.name AS Country,
city.name AS Capital,
Count(countrylanguage.language) AS NumLanguages
FROM country,
city,
countrylanguage
WHERE city.id = country.capital
AND countrylanguage.language_id = country.language_id
GROUP BY city.name,
country.name
HAVING ( Count(countrylanguage.language) BETWEEN 5 AND 10 )
ORDER BY NumLanguages desc, city.Name
That said, you should always try to avoid joins in the WHERE clause of the query (implicit joins). Favoring explicit (declarative) joins will give you more readability and also more flexibility.
Update
As per comments suggestion, here is the query's version using ANSI-92 join syntax:
SELECT country.name AS Country,
city.name AS Capital,
Count(countrylanguage.language) AS NumLanguages
FROM country
INNER JOIN city on city.id = country.capital
INNER JOIN countrylanguage on countrylanguage.language_id = country.language_id
GROUP BY city.name,
country.name
HAVING ( Count(countrylanguage.language) BETWEEN 5 AND 10 );
ORDER BY NumLanguages desc, city.Name

SQL Selecting from two tables

I have the following two tables...
I am trying to select all the cities belonging to a country using the country name, or using the country id but displaying only the city name and country name.
I am using the following statement but is not working, this is my first time doing SQL
SELECT CI.CITY_NAME, CO.COUNTRY_NAME
FROM CITY CI INNER JOIN COUNTRY CO
ON CI.CITY_ID = CO.COUNTRY_ID
WHERE CO.COUNTRY_ID = 1;
You're comparing a country id with a city id, seems like you'd really want to do;
SELECT CI.CITY_NAME, CO.COUNTRY_NAME
FROM CITY CI INNER JOIN COUNTRY CO
ON CI.COUNTRY_ID = CO.COUNTRY_ID
WHERE CO.COUNTRY_ID = 1;

Help forming a SQL statement

I have two tables: attractions and cities. Attractions contains a column called city, which is a reference to the id in the cities table. I want to form a MySQL statement that will show me which cities have the most attractions.
I know I can do:
SELECT COUNT(*) as `number`
FROM `attractions`
WHERE `city` = XX
...to get a count of how many attractions are in one city, but is there one statement that can count all of the attractions in every city, and return the results in descending order by number of attractions?
SELECT City.Name, COUNT(Attractions.City) as AttractionCount
FROM City
LEFT OUTER JOIN Attractions ON City.City = Attractions.City
GROUP BY City.Name
ORDER BY COUNT(Attractions.City) DESC