SQL Selecting from two tables - sql

I have the following two tables...
I am trying to select all the cities belonging to a country using the country name, or using the country id but displaying only the city name and country name.
I am using the following statement but is not working, this is my first time doing SQL
SELECT CI.CITY_NAME, CO.COUNTRY_NAME
FROM CITY CI INNER JOIN COUNTRY CO
ON CI.CITY_ID = CO.COUNTRY_ID
WHERE CO.COUNTRY_ID = 1;

You're comparing a country id with a city id, seems like you'd really want to do;
SELECT CI.CITY_NAME, CO.COUNTRY_NAME
FROM CITY CI INNER JOIN COUNTRY CO
ON CI.COUNTRY_ID = CO.COUNTRY_ID
WHERE CO.COUNTRY_ID = 1;

Related

How to write the SQL QUERY for this question

Given the CITY and COUNTRY tables, query the names of all the continents (COUNTRY.Continent) and their respective average city populations (CITY.Population) rounded down to the nearest integer.
Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
Try the following
select
c.continent,
floor(avg(ci.population))
from country c
join city ci
on c.Code = ci.countrycode
group by
c.continent;

Join two tables and select a row using foreign key SQL query

I have two tables Cities and Country. I am using the query
SELECT *
FROM citiesTable
WHERE cityName LIKE 'F%'
LIMIT 30
Each cityName has their respective country joined with foreign key in country table.
I want to select each city with and their respective country like Fayzabad , Afghanistan. Which query should I use? Please mention must since I am new to SQL
Better use left join to get all city table data
select city.*, ctry.countryName
from citytable city
left join country ctry on city.countryid = ctry.countryid
Use join query
SELECT *
FROM citiesTable, country
WHERE country.country_id = citiesTable.country_id
AND cityName LIKE 'F%' LIMIT 30
Try this query:
SELECT *
FROM CitiesTable a, Country b
WHERE a.country_id = b.id
AND a.cityName LIKE 'F%'
LIMIT 30

SQL Union of 4 tables

I have 4 different tables:
Address: address_id, postcode.
Patient: address_id, name.
FocusArea: geom.
Postcode: geom, postcode.
I need to find the name of the patients that live within the focus area.
I have managed to get the postcodes where the patients live, and the postcodes within the focus area, but I don't know how to join both queries:
SELECT
air.address.postcode, air.patient.name
FROM
air.address
INNER JOIN
air.patient ON patient.address_id = address.address_id;
SELECT
postcode as postcode
FROM
air.postcode, air.focusarea
WHERE
air.focusarea.objectid = 1
AND ST_Intersects(air.postcode.geom, air.focusarea.geom);
air.focusarea.objectid = 1 as there are different boundaries of the focus area.
Any ideas?
Thank you
Tania
select
a.postcode, p.name
from air.address as a
inner join air.patient as p on
p.address_id = a.address_id
inner join air.postcode as pc on
pc.postcode = a.postcode
inner join air.focusarea as fa on
fa.objectid = 1 and
ST_Intersects(pc.geom, fa.geom);

Select single row or multiple rows based on condition

I'm trying to identify a student's home district by joining a student's zip code to a district zip code. A given district may overlap several zip codes, so several possible home districts may appear for the student. For example, Zip code 99999 may include the Houston and Sugarland school districts. I can narrow down the home district to a single record when the student's city has the same name as the district name as for example if the student's city is Houston and the district name is Houston. In that case, I only want to retrieve the Houston district, not both Houston and Sugarland. However, if the student happened to live in Bayou with the zip code of 99999, then I'd want to retrieve both Houston and Sugarland districts since I don't have a fix on the district. I've tried several approaches but cannot come up with a solution. Here's
a primitive attempt:
Select S.Name, S.City, S.Zip, D.DistrictName
From tblStudent S
Left Join tblDistrict D on D.zip=S.zip
Where
(Case
When D.DistrictName=S.City then D.DistrictName
Else D.DistrictName
End)=D.DistrictName
Any suggestions are greatly appreciated!
You can try selecting each case separately and then making a union of the two queries.
Case 1: District Name equals City Name
Case 2: There is no District Name that is equal to the City Name
Something like this:
Select S.Name, S.City, S.Zip, D.DistrictName
From tblStudent S
Inner Join tblDistrict D on D.zip=S.zip and D.DistrictName = S.City
Union
Select S.Name, S.City, S.Zip, D.DistrictName
From tblStudent S
Inner Join tblDistrict D on D.zip=S.zip
Where not exists (
Select D2.* from tblDistrict D2
Where D2.DistrictName = S.City
And D2.zip = S.zip
)
SQL Fiddle: http://sqlfiddle.com/#!9/06d6d7/2/0

how to solve this specific Oracle query?

I have the following tables :
COUNTRIES :
LOCATIONS :
What I'm trying to do is to select COUNTRY_ID and COUNTRY_NAME from COUNTRIES only if STATE_PROVINCE in the table LOCATIONS is equals to null .
what I've done so far :
select country_id, country_name
from countries
union
select country_id
from locations
where state_province = null;
but this is doesn't seem to work !
EDIT : I forget to specify that I've to write this query without using JOIN
I want to select only countries that don't have a state province ! so
they're state province needs to be = null
I've to write this query without using
JOIN
select ctr.country_id, ctr.country_name
from countries ctr
where not exists (select l.country_id
from locations l
where l.state_province is not null and l.country_id=ctr.country_id)
How about you use a join rather than a union?
select distinct countries.country_id, countries.country_name
from countries
left join locations
on countries.country_id = locations.country_id
where locations.state_province is null;