Joining 2 Tables in a Database - sql

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';

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 to show only one column after sorting in SQL?

I have a table called 'customers', and I have to sort it first by country and by city, which I have successfully done.
Using this code:
SELECT *
FROM customers
ORDER BY Country, City
But from the output that I have, how do I print only the list of cities?
My table has several attributes or columns such as companyName, contactName, etc...
Thank you very much.
SELECT City FROM customers ORDER BY Country, City
Replace * with the columns you want to show - Cityin your case.
The SELECT criteria determines the columns displayed, while the WHERE criteria determines what rows are displayed :)
In your case it would be: SELECT City FROM customers ORDER BY Country, City
The * represents a 'wildcard' which in this case means display all.
If you wished to display both Country and city it would be:SELECT City, Country FROM customers ORDER BY Country, City
The order of the columns is determined by which order you write them in the SELECT statement.
Enter the specific column names instead of * , which indicates all the column names.
e.g.
SELECT City FROM customers
ORDER BY Country, City

Counting occurrences of distinct multiple columns in SQL

I'm trying to count the occurrences of a distinct set of cities and countries in a user table.
The table is set out similar to:
userid city country
------ --------- --------------
1 Cambridge United Kingdom
2 London United Kingdom
3 Cambridge United Kingdom
4 New York United States
What I need is a list of every city, country pair with the number of occurrences:
Cambridge, United Kingdom, 2
London, United Kingdom, 1
New York, United States, 1
Currently I run an SQL query to get the distinct pairs:
$array = SELECT DISTINCT city, country FROM usertable
then read it into an array in PHP, and loop through the array, running a query to count each occurrences for each row in the array:
SELECT count(*) FROM usertable
WHERE city = $array['city']
AND country = $array['country']
I'm assuming my scant grasp of SQL is missing something - what would be the correct way to do this, preferably without the intervention of PHP?
select city, country, count(*)
from usertable
group by city, country
What you need is a group by:
Select city, country, count(*) as counter
from usertable
group by city, country
SELECT cityandcountry, count(*) as occurrences FROM (
SELECT DISTINCT concat(city, country) FROM tablename
) as baseview;
if you want city and country preformated, or
SELECT cityandcountry, count(*) as occurrences FROM (
SELECT DISTINCT city, country FROM tablename
) as baseview;
if not.

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?

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.