Mondial Database: Select all countries where all cities start with "A" - sql

I would like to get every country out of Mondial database that has only cities that start with "A".
I already have the amount of cities each country has and the amount of cities each country has that start with an "A". My idea was to just compare the numbers of the count statement but I don't know how to compare 2 queries that are grouped.
The following tables are interesting for that task if you don't know about mondial.
City: Name, Country, Province, Population
Country: Name, Code, Capital, Area, Population
My queries were the following:
SELECT Country.Code, Count(City.Name)
FROM City,
Country
WHERE Country.Code = City.Country
AND City.Name LIKE 'A%'
GROUP BY Country.Name
And the query without LIKE = 'A%'
I hope you can help me.

As suggested by #jarlh I would use NOT EXISTS
SELECT Country.Code
FROM Country
WHERE NOT EXISTS (
SELECT 1
FROM City
WHERE Country.Code = City.Country
AND City.Name NOT LIKE 'A%'
)

Related

Show 'MULTIPLE_VALUES' instead of LISTAGG if there are > 1 records after GROUP BY

Imagine a table below:
I want to get the total population in each country but also I'd like to see the name of a city if that city is the only city in the country.
I could run something like
select
min(Country),
listagg(City) within group as City,
sum(Population) as Population
from table1
group by Country
but what i want is ('MULTIPLE' is just an example of text I'd like to see instead of the list of cities)
How can I do that?
I haven't been able to find any solution and my only idea is to use CASE with COUNT but it won't work
P.S. Sorry for the formatting
Just count the cities or compare min and max city:
select
country,
case when min(city) = max(city) then min(city) else 'multiple' as city,
sum(population) as population
from cities
group by country
order by country;
Assuming you won't have same city appearing twice
CODE 1
WITH list_of_cities_ as (
SELECT
COUNTRY,
city,
count(city) over (partition by country) AS TOTAL_Cities,
SUM(POPULATION) over (partition by country) AS TOTAL_POPULATION
FROM table_
)
select
DISTINCT
country,
case when TOTAL_Cities > 1 THEN 'Multiple' Else city end as city,
TOTAL_POPULATION
from list_of_cities_
CODE 2
IF you are interested in getting the list of all cities, instead of keyword
"Multiple"
https://dbfiddle.uk/?rdbms=postgres_9.5&fiddle=68efe08ab72ef63afa5813925e7f99e0
SELECT
COUNTRY,
STRING_AGG(City,',') as LIST_OF_CITIES,
SUM(POPULATION) AS TOTAL_POPULATION
FROM table_
GROUP BY 1

SQL query tutorial

given that there are 2 such entities
city(name, country, population)
country(code, name, capital, population)
and the question is that there exist cities in different countries that have the same name. For instance, paris in texas, usa, and paris in france. we assume, however, that every city in one country has a unique name in that country. find the names of cities that have a unique name.
would this work then
SELECT DISTINCT c1.name
FROM city c1, city c2
WHERE c1.name<>c2.name;
This will find all the cities that are unique in the database.
SELECT name
FROM city
Group by city
Having count(city) = 1

Group By query - any other way?

I have the following table that contains the following data:
http://img513.imageshack.us/img513/9039/mycities.png
The CREATE statement and the inserts are at http://snipt.org/xoKl .
The table is a list of cities and each city belongs to a region and a country and each city has a founding date. The goal here is to get for each "Country / Region" pair a list of the oldest cities. We need the oldest city on the east coast of Canada, the oldest city on the west coast of the U.S and so on ...
The query that I use right now is:
SELECT * FROM MyCities
INNER JOIN
(SELECT Country, Region, MIN(FoundingDate) AS CityFoundingDate
FROM MyCities
GROUP BY Country, Region ) AS subquery
ON subquery.CityFoundingDate = MyCities.FoundingDate
AND MyCities.Country = subquery.Country
AND MyCities.Region = subquery.Region
I just want to know whether there are other ways to write this group by query or not. :-)
Is this query efficient or not?
Looking forward to a discussion.
What about?
select country, region, city from MyCities mc1
where foundingDate <= ALL (
select foundingDate from MyCities as mc2
where mc1.country = mc2.country and mc1.region = mc2.region
)
How about something like this?
Should work in Oracle (although I can't test it right now)
SELECT country, region, city, foundingdate
FROM (
SELECT country, region, city, foundingdate, MIN(founding_date) OVER PARTITION BY (country, region) min_date
FROM mycities) WHERE foundingdate=min_date
But what if there are two cities founded on the same year in the same country/region?

Joining 2 Tables in a Database

I currently am making a dynamic query string that is comprised of whatever a user checks on a html form. There is Country, District, Population, and Language.
When I select Country, District, and Population the query string SELECT Country, District, Population FROM City WHERE name ='Tulsa' is created. The problem is Country is in a different table.
How do I read from the City table and the Country table in the same query string?
Assuming both your City and Country tables have a CountryID; try this:
SELECT Country, District, Population
FROM City
INNER JOIN Country ON Country.CountryID = City.CountryID
WHERE City.name = 'Tulsa';

Select count / duplicates

I have a table with all U.S. zip codes. each row contains the city and state name for the zip code. I'm trying to get a list of cities that show up in multiple states. This wouldn't be a problem if there weren't X amount of zip codes in the same city...
So basically, I just want to the city in a state to count as 1 instead of it counting the city/state 7 times because there are 2+ zip codes in that city/state...
I'm not really sure how to do this. I know I need to use count but how do I tell the mysql to only count a given city/state combo as 1?
SELECT City, Count(City) As theCount
FROM (Select City, State From tblCityStateZips Group By City, State) As C
GROUP By City
HAVING COUNT Count(City) > 1
This would return all cities, with count, that were contained in more than one state.
Greenville 39
Greenwood 2
GreenBriar 3
etc.
First group on state and city, then group the result on city:
select City
from (
select State, City
from ZipCode
group by State, City
) x
group by City
having count(*) > 1
Will this do the trick
Select CityName, Count (Distinct State) as StateCount
From CityStateTable
Group by CityName
HAVING Count (Distinct State) > 1
Try using a select distinct
SELECT DISTINCT city, state FROM table GROUP BY city
You probably should have created a separate table for zip codes then to avoid the duplication.
You want to look into the GROUP BY Aggregate.