ZEE
ZEE

Reputation: 5869

how to solve this specific Oracle query?

I have the following tables :

COUNTRIES :

enter image description here

LOCATIONS :

enter image description here

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

Upvotes: 0

Views: 104

Answers (2)

void
void

Reputation: 7890

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)

Upvotes: 1

Wio
Wio

Reputation: 1251

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;

Upvotes: 1

Related Questions