SELECT City, State, MIN(Zip) AS ZIP
FROM Zips
WHERE City = 'chicago'
GROUP BY City, State
ORDER BY State;
Or, maybe more informatively:
SELECT City, State, MIN(Zip) AS Min_ZIP, MAX(Zip) AS Max_ZIP
FROM Zips
WHERE City = 'chicago'
GROUP BY City, State
ORDER BY State;
Would it also be possible to allow the input search to take a zip code and/or a city?
Search on zip code:
SELECT City, State, MIN(Zip) AS Min_ZIP, MAX(Zip) AS Max_ZIP
FROM Zips
WHERE Zip = '01234'
GROUP BY City, State
ORDER BY State;
Search on zip and city:
SELECT City, State, MIN(Zip) AS Min_ZIP, MAX(Zip) AS Max_ZIP
FROM Zips
WHERE City = 'chicago'
AND Zip = '01234'
GROUP BY City, State
ORDER BY State;
Search on zip or city:
SELECT City, State, MIN(Zip) AS Min_ZIP, MAX(Zip) AS Max_ZIP
FROM Zips
WHERE City = 'chicago'
OR Zip = '01234'
GROUP BY City, State
ORDER BY State;
It is trickier to use a single query to search on zip (unless it is null) or city (unless that is null). It typically ends up using question mark placeholders etc - and is not wholly portable across DBMS (whereas the queries above are all portable to any SQL DBMS).
SELECT City, State, MIN(Zip) AS Min_ZIP, MAX(Zip) AS Max_ZIP
FROM Zips
WHERE (? IS NOT NULL AND City = ?)
OR (? IS NOT NULL AND Zip = ?)
GROUP BY City, State
ORDER BY State;
Here, if it works, you'd provide the city parameter twice (once for each of the first two question marks) and the zip parameter twice (once for each of the last two question marks). This gets into the realm of discussing which programming language too. Some DBMS would allow you to write each parameter once, using a notation such as ':n' to identify the different parameters:
SELECT City, State, MIN(Zip) AS Min_ZIP, MAX(Zip) AS Max_ZIP
FROM Zips
WHERE (:1 IS NOT NULL AND City = :1)
OR (:2 IS NOT NULL AND Zip = :2)
GROUP BY City, State
ORDER BY State;
I don't know what MySQL provides in this area.