Group By query - any other way? - sql

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?

Related

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

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%'
)

How do I get city with highest population in SQL Server (Express Edition)?

So I'm using SQL Server with two columns named City and Population. How do I find the name of the city with the highest population (in syntax)? I've heard SQL Server is a little bit different from MySQL.
Current Code:
Select MAX(Population) from City
In this case, the code is pretty similar between T-SQL and MySQL.
There are other approaches that would work, but this might be the easiest to understand based on the code you've already written.
select
city,
population
from
city
where
population = (
select max(population) from city
)
You may use max(population) over (order by population desc) syntax (do not neglect to alias (here by q) inner query in SQL Server):
SELECT city
FROM
(SELECT MAX(population) OVER (ORDER BY population desc) max_pop, *
FROM city) q
WHERE population = q.max_pop;
SQL Fiddle Demo
You need to find the maximum population in a subquery and then find all cities with that population:
select city
from my_table
where population = (
select max(population) from my_table
)
This query will also give you multiple cities, in the (rare) case where two cities share the maximum population.
You can sort the records by Population in descending order and then pick out the first record only:
SELECT TOP 1 City FROM t
ORDER BY Population DESC

SQL find repeated values

I need to identify rows where a certain value is repeated. Here is a sample table:
COUNTRY CITY
Italy Milan
Englad London
USA New York
Canada London
USA Atlanta
The query should return...
COUNTRY CITY
Englad London
Canada London
...because London is repeated. Thank you in advance for your help.
The easiest way is to use a subquery that counts the number of times each city appears (and filter to those values that appear more than once):
SELECT * FROM Cities
WHERE City in
(
SELECT City FROM Cities
GROUP BY City
HAVING COUNT(*) > 1
)
If your DBMS supports windowed aggregates.
SELECT COUNTRY,
CITY
FROM (SELECT COUNTRY,
CITY,
COUNT(*) OVER (PARTITION BY CITY) AS Cnt
FROM Cities) T
WHERE Cnt > 1
SQL Fiddle
select country, city
from aTable
where city in
(
select city
from aTable
group by city
HAVING count(1) > 1
)
Try it here: http://sqlfiddle.com/#!3/e9b1a/1
Or if the same city & country combo appears twice and you're only interested where the countries are different:
select distinct country, city
from aTable
where city in
(
select city
from aTable
group by city
HAVING count(distinct country) > 1
)
Try it here: http://sqlfiddle.com/#!3/2dfaa/2
This one works. Got it from my wife (she finally had time to look into this). Thought you might be interested.
SELECT * FROM Cities
WHERE City in ( select city
from (SELECT City,
count(distinct country)
FROM Cities
GROUP BY City
HAVING count(distinct country) > 1) a )

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.