I have two sql statements that I wish to join via natural join but for some reason the following is giving me an error:
(select city_name
from city
left join country
on country.country_name=city.country_name
where country.continent='Europe'
and city.iscapitol='yes')
natural join
(select city_name
from city
left join country
on country.country_name=city.country_name
where country.continent='Europe'
and city.iscapitol='no';)
I am using the oracle platform and the error it is throwing is:
natural join
*
ERROR at line 7:
ORA-00933: SQL command not properly ended
What would be the reason this error would be appearing? Any help would be greatly appreciated.
select * from (
(select city_name
from city
left join country
on country.country_name=city.country_name
where country.continent='Europe'
and city.iscapitol='yes')
natural join
(select city_name
from city
left join country
on country.country_name=city.country_name
where country.continent='Europe'
and city.iscapitol='no'))
I've removed ;
and added outer query. I would also recommend to replace natural join
by explicit condition for join
with eurcities as (select city_name, iscapitol, country_name from city
left join country on country.country_name=city.country_name
where country.continent='Europe')
select c1.city_name, c2.city_name, c1.country_name
from eurcities c1 inner join eurcities c2 on (c1.country_name = c2.country_name)
where c1.iscapitol = 'yes' and c2.iscapitol = 'no';
Without with
it will look like:
select c1.city_name, c2.city_name, c1.country_name
from (select city_name, iscapitol, country_name from city
left join country on country.country_name=city.country_name
where country.continent='Europe') c1
inner join (select city_name, iscapitol, country_name from city
left join country on country.country_name=city.country_name
where country.continent='Europe') c2
on (c1.country_name = c2.country_name)
where c1.iscapitol = 'yes' and c2.iscapitol = 'no';
First, unlearn natural join
. It is an error waiting to happen. Not showing the join
keys in the code is dangerous. Ignoring declared foreign key relationships is unwise. Relying on naming conventions is awkward.
You can write this using using
. So, fixing the syntax, this looks like:
select *
from (select city_name
from city left join
country
on country.country_name = city.country_name
where country.continent='Europe' and city.iscapitol = 'yes'
) cc join
(select city_name
from city left join
country
on country.country_name = city.country_name
where country.continent = 'Europe' and city.iscapitol='no'
) cnc
using (city_name);
Note that the left join
s in the subquery are unnecessary.
That said, I think aggregation is a much simpler approach to the query:
select city_name
from city join
country
on country.country_name = city.country_name
where country.continent = 'Europe'
having sum(case when city.iscapitol = 'yes' then 1 else 0 end) > 0 and
sum(case when city.iscapitol = 'no' then 1 else 0 end) > 0;
Or, if iscapitol
[sic] only takes on two values, you can use this for the having
clause:
having min(city.iscapitol) <> max(city.iscapitol)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.