SQL query using SET operator - sql

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;

Related

oracle SQL - unpivot

Can u tell me, why it wont work?:)
select *
from( select r.region_id, c.country_id
from countries c join regions r on r.region_id = c.region_id)
unpivot(
valuee for columnValue in (r.region_id))
ORA-01748: only simple column names allowed here
01748. 00000 - "only simple column names allowed here"
With this part:
select *
You are selecting columns: region_id and country_id from your inner select. So you do not need r.region_id in your UNPIVOT section, only region_id.
This code is correct(without error):
select *
from(select r.region_id
, c.country_id
from countries c
join regions r on r.region_id = c.region_id)
unpivot(valuee for columnValue in (region_id));
Your query is strange and it isn't really clear what you're trying to achieve; but following up on #VBoka's correction of your code, you don't actually need the join at all (unless you have countries with non-existent regions) - you can do:
select *
from (
select region_id, country_id
from countries
)
unpivot(valuee for columnValue in (region_id));
But you can get the same result without unpivoting; with the join if you have a real reason to include it:
select c.country_id, 'REGION_ID' as columnvalue, r.region_id as valuee
from countries c
join regions r on r.region_id = c.region_id;
or without the join:
select country_id, 'REGION_ID' as columnvalue, region_id as valuee
from countries;
Either way you get a result set with one row for every country.

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;

Inner Join from group by

I have a table countries containing country codes and names:
name code
FRANCE FR
JAPAN JP
And a table of page view stats views:
page_id code date etc...
34 FR 2015-01-01
34 FR 2015-01-02
34 JP 2015-01-02
I'd like to output a list of all the countries that the page was seen in, but with the country name, so the results should say: FRANCE, JAPAN
I can easily get the list of country codes that it runs in:
SELECT code FROM views WHERE page_id = 34 GROUP BY code;
But mapping them across to the names in the countries table is the hard bit. Any ideas?
I've tried this, but it doesn't work:
SELECT
countries.name as name_from_countries,
FROM
countries
INNER JOIN name_from_countries ON countries.code = views.code
WHERE
views.page_id = 34
GROUP BY
views.code
You don't need GROUP BY here. Just use DISTINCT.
SELECT DISTINCT countries.name as name_from_countries
FROM countries
INNER JOIN views ON countries.code = views.code
WHERE
views.page_id = 34
You need to add countries.name to your GROUP BY - I'm assuming you have some aggregates here:
SELECT v.code, c.name AS name_from_countries
FROM views v INNER JOIN countries c
ON v.code = c.code
WHERE v.page_id = 34
GROUP BY v.code, c.name;
If you don't have any aggregates (e.g., COUNT(*)), then you can get a list of countries with at least one view as follows:
SELECT c.code, c.name AS name_from_countries
FROM countries c
WHERE EXISTS ( SELECT 1 FROM views v
WHERE v.code = c.code
AND v.page_id = 34 );

Trying to find all the cities that there is not a direct flight to from a city (PostgreSQL)

I'm trying to write a query that determines which cities I can't fly to directly from a city, say London. Given the schema:
cities:
| c_id | city_name |
flights:
| f_id | departure_city_id | destination_city_id |
currently my query returns the opposite, i.e. it returns the cities for which there is a direct flight from London
SELECT c2.city_name as "City"
FROM flights AS f
JOIN cities AS c2 ON f.destination_city_id != c2.c_id
JOIN cities AS c ON c.c_id = c.c_id
WHERE c.city_name = 'London'
AND c.c_id != c2.c_id
AND f.departure_city_id = c.c_id;
I would have thought it would be easy to change it to get what I want.
I thought changing the third line to
JOIN cities AS c2 ON f.destination_city_id = c2.c_id
Would have done the trick but it didn't. Any help?
cities I can't fly to directly from a city, say London.
Meaning one can fly there, just not directly from London. So JOIN (not LEFT JOIN) city to flight via destination_city_id:
SELECT DISTINCT c.city_name
FROM cities c
JOIN flights f ON f.destination_city_id = c.c_id
JOIN cities c2 ON c2.c_id = f.departure_city_id
WHERE c2.city_name <> 'London';
Then I only have to exclude flights originating from London, apply DISTINCT to get unique city names and we are done.
A more sophisticated interpretation of this question would be:
"Cities you can fly to from London, just not directly"
But since this looks like basic homework I don't assume they'd expect a recursive query from you.
Try something like:
SELECT *
FROM cities c
WHERE c.c_id NOT IN
(SELECT f.destination_city_id
FROM flights f
JOIN cities c2 ON f.departure_city_id = c.c_id
WHERE c2.city_name = 'London')

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;