SQL query - IS NULL - sql

Tables:
Country
-------
PK CountryID
Name
City
-------
PK CityID
FK CountryID
Name
Airport
--------
PK AirportID
FK CityID
Name
My task is to select names of countries that have no airport.
I can imagine only one solution with EXCEPT (or MINUS)
SELECT Country.Name
FROM Country EXCEPT (SELECT DISTINCT Country.Name FROM Country, City, Airport
WHERE City.CountryID = Country.CountryID AND Airport.CityID = City.CityID);
But is it possible not to use EXCEPT but something like IS NULL?

SELECT cn.CountryID
FROM Country cn
LEFT JOIN City ct ON cn.CountryID = ct.CountryID
LEFT JOIN Airport ar on ar.CityID=ct.CityID
WHERE ar.AirportID is null

If you need to make this query with IS NULL try following query:
SQLFiddle demo
select ct.CountryId,max(ct.Name) from Country ct
left join City c on ct.CountryId=c.CountryId
left join Airport a on a.CityId=c.CityID
group by ct.CountryId
HAVING max(a.AirportID) IS NULL

You can use this, but it's basically the same thing you have with different syntax.
SELECT Country.Name
FROM Country
Where Country.Name Not IN
(
SELECT DISTINCT Country.Name FROM Country, City, Airport
WHERE City.CountryID = Country.CountryID AND Airport.CityID = City.CityID
);

Related

Count the repeats of foreign key of two tables

I have two tables Countries( id, country) and Cities (id, city, countryId ) I want to select countries and to show how much cities AS locations .
I tried this :
SELECT countryid, COUNT(*) AS locations
FROM Cities
GROUP BY countryid
but it shows me countryid instead of country
You need to join the tables as follows:
select c.country, count(*) as locations
from Country c
inner join Cities ci
on c.id = ci.countryId
group by c.id, c.country
I also included the country in the group by since it appears in the select clause

SQL, choosing maximum values from another table

This one I've been spending the last hours on. Still haven't found an answer so I figured I'd ask.
I have two tables:
Country - Code, Name, Populaion.
City - CountryCode, Name, Population.
The Foreign key is the CountryCode which response to country.Code.
I'm trying to find the most populated city in each country, where the output is the name of the country and the city. I know this could be done with Max(), but I'm having a struggle limiting my table to showing all the countries name and showing onle the name of the most populated city.
SELECT country.name, city.name, MAX(city.Population)
FROM city
LEFT JOIN country
ON city.CountryCode=Country.Code
GROUP BY city.name, country.name, city.population
ORDER BY city.population DESC;
This only gives me all the countries and all the cities. Could anyone help me narrow it down so it only shows every countries name but with their largest city?
You want to first the max population in each country and then join it with the city table to know which city it belongs to. After that, join it with the country table to get the required result.
select co.name,
ct.name,
ct.population
from (
select c1.*
from city c1
join (
select countryCode,
max(population) population
from city
group by countryCode
) c2 on c1.countryCode = c2.countryCode
and c1.population = c2.population
) ct
join country co on ct.countryCode = co.code;
Another way of finding max in group using left join:
select co.name,
ct.name,
ct.population
from (
select c1.*
from city c1
left join city c2 on c1.countryCode = c2.countryCode
and c1.population < c2.population
where c2.countryCode is null
) ct
join country co on ct.countryCode = co.code;

Group By SQL Statement - Get countries with more than 5 cities

I am trying to pull the countries that have more than 5 cities.
Tables:
City city_id, city, country_id, last_update
Country country_id, country, last_update
I think I am very close to getting this figured out, but I'm not quite there. Any pointers?
SELECT DISTINCT country
FROM country C, city O
WHERE O.country_id = C.country_id AND O.country_id
IN (SELECT country_id FROM city group by country_id having count(country_id) > 5);
select country
from country inner join city on city.country_id = country.country_id
group by country
having count(distinct city) > 5
Use the below query..
SELECT country
FROM country C
JOIN city O
ON O.country_id = C.country_id
GROUP BY country
HAVING count(distinct O.city)>5
you can try like this
select countryid, count(distinct city_id) from country c join city ct c.country_id=ct.country_id
group by countryid
having count(distinct city_id) > 5
SELECT DISTINCT
country
FROM country C
WHERE EXISTS (SELECT COUNT(DISTINCT city_id)
FROM city CITY
WHERE CITY.country_id = C.country_id
GROUP BY CITY.country_id
HAVING COUNT(DISTINCT city_id) > 5);
The below query works for your requirement.
SELECT C.country
FROM country C
INNER JOIN
city O
ON O.country_id = C.country_id
GROUP BY C.country
HAVING count(distinct O.city)>5;

Using Oracle in select query

Question: Given the CITY and COUNTRY tables, query the sum of the populations of all cities where the CONTINENT is 'Asia'.
City table contains fields: CountryCode, Population
Country table contains fields: Code, Continent
(CITY.CountryCode and COUNTRY.Code are matching key columns.)
I tried the following query: (I know this can be solved using Inner join)
Select sum(city.population) from city
where city.countrycode in (Select code from Country where continent = 'Asia')
Hacker rank gives following error:
ERROR at line 3:
ORA-00933: SQL command not properly ended
Do you need a semi colon?
Something along these lines......
Per city..........
SELECT City.Name, SUM(City.Population)
FROM City INNER JOIN Country ON Country.Code = City.CountryCode
WHERE
Country.Continent = 'ASIA'
GROUP BY
City.Name;
Per Country & City
SELECT Country.Name, City.Name, SUM(City.Population)
FROM City INNER JOIN Country ON Country.Code = City.CountryCode
WHERE
Country.Continent = 'ASIA'
GROUP BY
Country.Name, City.Name;
Just the Total for ASIA
SELECT SUM(City.Population)
FROM City INNER JOIN Country ON Country.Code = City.CountryCode
WHERE
Country.Continent = 'ASIA';
What we are doing here is querying the city table and finding all matches in the country table by using the identifying field in both those tables:
country.code in your country table
city.countrycode in your city table
Whenever country.code = city.countrycode and country.continent= 'Asia', you will be returned the sum of that population.
Select sum(city.population) from city inner join country on country.code = city.countrycode
where country.continent= 'Asia';
I also recommend you select the city the population count belongs to:
Select city.name, sum(city.population) from city inner join country on country.code = city.countrycode
where country.continent= 'Asia';
This is the right code
Select sum(city.population)
from city inner join country on country.code = city.countrycode
where country.continent= 'Asia';
Select SUM(City.POPULATION) from City join Country ON city.countrycode=country.code
where country.continent='Asia'
Use ; at the end of the code.
Select sum(city.population) from city
where city.countrycode in (Select code from Country where continent = 'Asia');

Fetch corresponding States of Countries table

We have two tables Country and CountryStates with the following fields
Country
- CountryId
- CountryName
CountryStates
- StateId
- StateName
- CountryId
There are some Countries without any states added in the database.
Now we have to fetch only those countries where states are added.
You can use this query to retrieve only countries that have a state:
SELECT *
FROM Country AS C
WHERE EXISTS (SELECT TOP 1 1
FROM CountryStates CS
WHERE CS.CountryId = C.CountryId
)
Use Exists like this
SELECT CountryID,CountryName
FROM Country C
WHERE EXISTS
(
SELECT 1
FROM CountryStates S
WHERE S.CountryID = C.CountryID
)
You can perform a JOIN (also known as INNER JOIN) on CountryStates, using the CountryId.
This will leave you with only the details of Countries which have a state added for them.
SELECT *
FROM Country
INNER JOIN CountryStates ON Country.CountryId = CountryStates.CountryId
Same Result can be obtained by using sample INNER JOIN..
SELECT CountryID,CountryName
FROM Country C
INNER JOIN CountryStates S
ON C.CountryID = S.CountryID