How to join full name with shortcut - sql

I have two tables, first with full names of countries (only one column):
Poland
Germany
Czech Republic
and second table with shortcuts of those names
PL
DE
CZ
How to join those two tables to one with two columns: country and country shortcut?
Poland PL
Germany DE ect

If the first table is named country, the second table is called abbr and the third table called table3 to find the abbrivation from the name it would look like this.
SELECT *
FROM country AS c
JOIN table3 as t3 on c.country = T3.country
JOIN abbr AS a on a.abbrivation = T3.abbrivation

Related

How can I create a BigQuery record with a join, but without specifying all fields?

Question: How to join tables using join clause without listing all the fields?
Data
Given two tables, Person and Address:
Person
name
address_id
Alice
10
Bob
11
Charlie
10
Address
id
street
city
10
William Street
NYC
11
Old Street
London
Desired result:
I'd like to join them with a record, like so:
name
address.street
address.city
Alice
William Street
NYC
Bob
Old Street
London
Charlie
William Street
NYC
However, I have many columns in both tables and I don't want to specify them all.
So something a bit like using EXCEPT but with the joined columns becoming nested in an address record:
SELECT * EXCEPT (address_id)
FROM person p
JOIN address a
ON p.address_id = a.id
Is this possible in BigQuery?
Consider below query:
SELECT p.* EXCEPT(address_id), (SELECT AS STRUCT a.* EXCEPT(id)) AS address
FROM Person p JOIN Address a ON p.address_id = a.id;
output:
You can join more tables with similar approach.
SELECT p.* EXCEPT(address_id),
(SELECT AS STRUCT a.* EXCEPT(id)) AS address,
(SELECT AS STRUCT j.* EXCEPT(name)) AS Job
FROM Person p
JOIN Address a ON p.address_id = a.id
JOIN Job j ON p.name = j.name;
output:

How to select all rows but join information under condition

Simplified I have two tables NAMES (with columns ID and NAME) and ADRESSES (with columns COUNTRY and CITY and NAME_ID).
Example for NAMES:
1 - Hans
2 - Mark
3 - Joseph
Example for ADRESSES:
Denmark - Kopenhagen - 1
Germany - Berlin - 3
I need to select all Names which either have no adress at all OR with their CITY but only when their country is... Denmark.
SELECT
NA.NAME AS NAME,
AD.CITY AS CITY
FROM NAMES AS NA
LEFT JOIN ADRESSES AS AD ON AD.NAME_ID = NA.ID
now when I add something like
WHERE AD.COUNTRY="Denmark"
or
WHERE (AD.COUNTRY="Denmark" OR AD.COUNTRY=NULL)
I still only get a list of Names with Cities in Denmark but not all other Names which have no Adress/City at all.
When I remove the condition of course I get all Names and existing Cities but even in all other countries.
The desired result would be:
Hans - Kopenhagen
Mark - NULL
I guess you mean WHERE AD.COUNTRY='Denmark' not WHERE AD.CITY='Denmark'. Also, Using WHERE clause with LEFT JOIN makes it like INNER JOIN. So shift your condition to LEFT JOIN clause -
SELECT NA.NAME AS NAME,
AD.CITY AS CITY
FROM NAMES AS NA
LEFT JOIN ADRESSES AS AD ON AD.NAME_ID = NA.ID
WHERE AD.COUNTRY='Denmark' OR AD.COUNTRY IS NULL
I need to select all Names which either have no adress at all OR with their CITY but only when their country is... Denmark.
I believe you want:
SELECT NA.NAME AS NAME, AD.CITY AS CITY
FROM NAMES NA LEFT JOIN
ADRESSES AD
ON AD.NAME_ID = NA.ID
WHERE AD.COUNTRY = 'DENMARK' OR AD.COUNTRY IS NULL;
This should not return names in other countries, such as Germany.

Matching company names in two tables

I have two tables with company names and their ids, the table Corporation_Name has ID and NAME, the other table DATA_Excel has CORPORATION as ID and C_Name as name; I have to match company names in the data table with Corporation Name to make sure all the companies exist if not i only have to insert the company which is not present in Corporation name.
Currently i am using this query:
Select Distinct (B.corporation), B.C_name
from data_excel B, corporation_name A
where B.C_name <> A.name
also some times this is the case:
87 Société Générale de Belgique
87 Societe Generale de Belgique
Your query is not finding what you are looking for. You are all companies from A and matching them with all companies in B, meaning it will return a record for Societe Generale <> Fortis
Start with this:
SELECT B.Corporation, B.C_Name
FROM data_excel B
LEFT OUTER JOIN corporation_name A
ON B.C_Name = A.Name
WHERE A.Name IS NULL
This will still not solve everything, you will still have to replace
ON B.C_Name = A.Name
With something like what John Doyle suggested, because Société will still not match Societe!

An SQL query with a WHERE greater than a count, when another field is already in GROUP BY

(I gladly welcome better title suggestions)
I'm trying to write an SQL query in Oracle that only outputs entries WHERE one field has a count above a certain value.
Specifically, I have tables AIRPORTS, MARKETS, and STATES.
The AIRPORTS table is information about airports with fields ID (primary key), NAME, ABBR (abbreviation), MARKET (foreign key from MARKETS table), STATE (foreign key from STATE table), and CITY. Like so:
14122 PITTSBURGH INTERNATIONAL PIT 30198 42 PITTSBURGH, PA
14150 PELLSTON REGIONAL AIRPORT PLN 34150 26 PELLSTON, MI
14193 PENSACOLA GULF COAST REGIONAL PNS 33728 12 PENSACOLA, FL
MARKETS is information about different markets airports can be in. It contains an ID (primary key) and NAME field. Like so:
30576 Baglung, Nepal
30577 Binghamton, NY
30578 Bruggen, Germany
30579 Bergen, Norway
STATES contains information about states in the USA, using the government's FIPS codes. It contains fields FIPS (primary key), NAME, and ABBR (abbreviation). Like so:
1 ALABAMA AL
2 ALASKA AK
4 ARIZONA AZ
5 ARKANSAS AR
I'm trying to write an SQL query that outputs the AIRPORTS.NAME, MARKETS.NAME, and STATES.ABBR fields for all airports in a market that has airports in more than one state, and I'd like to do it without creating a view. I've gotten as far as a query that shows me all the MARKETS.ID with more than 2 airports:
SELECT *
FROM(
SELECT markets.id as "market", count(markets.name) as "airports"
FROM markets
INNER JOIN airports
ON airports.market = markets.id
GROUP BY markets.id)
WHERE "airports" > 2
But I'm not exactly sure where to go from here. And I'm sure there's a better way to do this.
Thank you!
SELECT m.id AS market, COUNT(*) AS airports
FROM markets AS m
INNER JOIN airports AS a
ON a.market = m.id
GROUP BY m.id
HAVING COUNT(*) > 2
SELECT airport, market, state
FROM (
SELECT airports.name AS airport
,markets.name AS market
,states.abbr AS state
,count(DISTINCT airports.state) OVER (PARTITION BY airports.market)
AS states_per_market
FROM airports
JOIN markets
ON airports.market = markets.id
JOIN states
ON airports.state = states.fips
) WHERE states_per_market > 1;

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')