how to solve this specific Oracle query? - sql

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;

Related

SQL query using SET operator

Using a SET command, show all the countries in region 1 that do not have a location.
Display only the country_id.
This is my query and it's wrong. I can't figure it out.
SELECT c.country_id
FROM Countries c
WHERE r.region_id = 1
MINUS
SELECT c2.country_id
FROM Countries c2
WHERE l.location_id IS NULL;
You can use country and location table as follows:
SELECT c.country_id
FROM Countries c
WHERE c.region_id = 1
MINUS
SELECT c2.country_id
FROM location c2;

Oracle update and change few columns records in two tables with a single query

I have two tables with names of Locations and Countries in My Oracle database.
Locations table has columns with names location_id(fk), street_address, state_province, and country_id. Countries table has columns with names country_name, country_id, location_id(fk).
I wanna update some columns of Locations and Countries table in a single query.
I did it with the below query but it doesn't work.
update (select l.street_address, l.postal_code, l.city, l.state_province, c.country_name
from hr.countries c, hr.locations l where l.country_id = c.country_id )
set l.street_address = '147 Spadina Ave',
l.postal_code = '122000215',
l.city = 'Toronto',
l.state_province = 'Ontario',
c.country_name = 'US'
where l.location_id = 1200;
but I faced with the error from PL/SQL (ORA-00911 : invalid character).
please help me to fix this issue.
That error, ORA-00911 : invalid character has nothing to do with your update statement and may be coming from another part of your PL/SQL ?
What's actually wrong with your update statement is that you cannot update more than one table through an inline view. So, If you remove the alias l in the set clause and run the statement, you should get ORA-01776:cannot modify more than one base table through a join view
You may however be able to do it using an INSTEAD OF TRIGGER with a view.
In your case, since you are updating the tables based on a given primary key (location_id = 1200), running these two simple updates should be fine. There's no need of joins
UPDATE hr.locations
SET street_address = '147 Spadina Ave',
postal_code = '122000215',
city = 'Toronto',
state_province = 'Ontario'
WHERE location_id = 1200;
UPDATE hr.countries
SET country_name = 'US' -- Toronto in United states, how did that happen?
WHERE country_id IN ( SELECT country_id
FROM hr.locations
WHERE location_id = 1200
);

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

Querying with an outer join in SQL

I have a SQL database that contains geographic information. This database has three tables:
PostalCode
----------
Code (char(10))
StateID (uniqueidentifier)
State
-----
ID (uniqueidentifier)
Name (nvarchar(max))
CountryID (uniqueidentifier)
Country
-------
ID (uniqueidentifier)
Name
The relationship is: A country has states. States have postal codes. I'm trying to create a query where I can find all of the states in the country where a specific postal code belongs. Currently, I'm trying the following:
SELECT
s.*
FROM
[PostalCode] p,
[State] s,
[Country] c
WHERE
p.[Zip]='90028' AND
p.[StateID]=s.[ID] AND
s.[CountryID]=c.[ID]
Unfortunately, this result returns 1 record (The State record associated with California). However, in reality, I need it to return 50 records (one for each state in the united states). How do I modify this query to do this?
Thank you
You are using an INNER JOIN, you need to change your syntax to a LEFT JOIN:
SELECT s.*
FROM [State] s
LEFT JOIN [PostalCode] p
ON p.[StateID]=s.[ID]
AND p.[Zip]='90028'
LEFT JOIN [Country] c
ON s.[CountryID]=c.[ID]
You will notice that I changed to use ANSI JOIN syntax instead of the joining the tables with the commas and the WHERE clause.
A LEFT JOIN will return all rows from the state table even if there are not matching rows in the other tables.
If you want to return all states in a country where the postal code is equal to a specific code, then you can use:
select s.*
from state s
inner join
(
SELECT s.countryid
FROM [State] s
INNER JOIN [PostalCode] p
ON p.[StateID]=s.[ID]
INNER JOIN [Country] c
ON s.[CountryID]=c.[ID]
WHERE p.[Zip]='90028'
) c
on s.countryid = c.countryid;
Or you can use:
select s1.*
from state s1
where exists (select s2.countryid
from state s2
inner join country c
on s2.countryid = c.id
inner join postalcode p
on s2.id = p.stateid
where p.zip = 90028
and s1.countryid = s2.countryid)

SQL Selecting from two tables

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;