SQL Union of 4 tables - sql

I have 4 different tables:
Address: address_id, postcode.
Patient: address_id, name.
FocusArea: geom.
Postcode: geom, postcode.
I need to find the name of the patients that live within the focus area.
I have managed to get the postcodes where the patients live, and the postcodes within the focus area, but I don't know how to join both queries:
SELECT
air.address.postcode, air.patient.name
FROM
air.address
INNER JOIN
air.patient ON patient.address_id = address.address_id;
SELECT
postcode as postcode
FROM
air.postcode, air.focusarea
WHERE
air.focusarea.objectid = 1
AND ST_Intersects(air.postcode.geom, air.focusarea.geom);
air.focusarea.objectid = 1 as there are different boundaries of the focus area.
Any ideas?
Thank you
Tania

select
a.postcode, p.name
from air.address as a
inner join air.patient as p on
p.address_id = a.address_id
inner join air.postcode as pc on
pc.postcode = a.postcode
inner join air.focusarea as fa on
fa.objectid = 1 and
ST_Intersects(pc.geom, fa.geom);

Related

How to join few table and connect id with name column from another table

I have these tables with the following column names:
players:
id first_name last_name age position salary hire_date skills_data_id team_id
Skills:
id, dribbling, pace, passing, shooting, speed, strength
towns:
id, name, country_id
teams:
id name established fan_base stadium_id
On this base, I have found the players with the max speed in terms of towns where their team played.
At the same time, I have to skip players that played in team ‘Devify’.
At the moment I have tried with this code, but the final result is not correct.
select max(s.speed) as `max_speed`,tt.name as `town_name`
from skills_data as s
right join players as p on s.id = p.skills_data_id
inner join teams as t on p.team_id = t.id
inner join towns as tt on p.team_id = tt.id
where t.name not like 'Devify'
group by s.id
order by max(s.speed) desc, town_name;
The result should be something like that:
max_speed town_name
97 Smolensk
92 Bromma
92 Lühua
...
NULL Zavolzh’ye
My result is:
max_speed town_name
97 Montréal-Ouest
92 Dalubian
92 Samsan
Thank you in advance.
There is a problem in your group by clause: you should be grouping by town rather than by skill id.
I am also quite suspicious about the join condition that brings in the towns (see my comment under your question): but if you are getting results for your existing query, it must be right...
Other changes to your query:
changed your right join to an inner join
used more meaningful table aliases
used an inequality condition instead of not like to exclude the unwanted team
New query:
select
max(sk.speed) as max_speed,
to.name as `town_name`
from skills_data as sk
inner join players as pl on sk.id = pl.skills_data_id
inner join teams as te on pl.team_id = te.id and te.name <> 'Devify'
inner join towns as to on pl.team_id = to.id
group by to.id, to.name
order by max_speed desc, town_name;

How can I write SQL Query?

I need a request that displays the name and surname of the clients with the smallest credit limit - among married women who do not live in Japan, Brazil or Italy.
Diagram:
This will Give all the people not in 'Japan', 'Brazil' or 'Italy') ,
Select C.Cust_first_name,C.Cust_Last_name from Customers C
Inner Join Countries C1
on C.Country_Id=C1.Country_Id
Where C1.Country_Name Not in('Japan', 'Brazil' or 'Italy')
and
C.Cust_Credit_Limit=(Select Min(Cust_Credit_Limit) From from Customers C)
If we Convert The Question to Code that will be the above code,
The Script would return the name and last name of person not in ('Japan', 'Brazil' or 'Italy') And has the lowest salary In the entire customer Base.
select top 1 * from Customers C
Inner Join
(
select MIn(cust_credit_limit) from Customers C1
Inner Join Countries CT on C1.Country_id = C1.Country_id
where CT.Country_Name Not in ('Japan', 'Brazil','Italy')
)
C2 on C2.cust_credit_limit = C.cust_credit_limit

Structure of SQL query using NOT IN / NOT EXISTS(?) with three tables

I need help to figure out the right structure of query to
find the names of those clients, who stopped in exactly the same
hotels as John Doe.
I have tables :
Clients(clientID, clientName)
ClientTour(clientID, tourID)
Tours(tourID, country, hotel)
It all should be written down as one query.
I'm quite new to SQL and can't write down right subqueries for it, as it uses three tables... I somewhat understand how to write similar queries for two tables, but when I tried to write this one... well, I got lost. =/
As far as I understand, I have to find the names of those clients,
who stopped in
[All Hotels where John Doe stopped] - [Hotels where only John Doe stopped]
but who never stopped in those
[Hotels where John Doe never stopped].
Is it logically right? If it is, then I get the part that is
SELECT c1.clientName
FROM Clients c1
WHERE c1.clientID NOT IN (clientIDs of clients in [Hotels where John
Doe never stopped])
AND c1.clientID IN (clientIDs of clients in [[All Hotels where John
Doe stopped] - [Hotels where only John Doe stopped]])
but can't figure out the parts marked in Italics... how do I find clients who stopped in hotels where John Doe did/didn't stop, or the hotels where only John Doe stopped?
Also, is there any easier way to write this query down?
EDIT:
1. Added to the outer SELECT a GROUP BY clause, replacing the need for DISTINCT, and adding the option of countnig hotels for each client in the result set. Note the result set has only hotels that 'John Doe' has attended.
2. Added a SELECT statement to count number of hotels 'John Doe' has visited at.
3. Added a HAVING clause to eliminate from the result set clients with different numbers of hotels than 'John Doe'.
SELECT C.clientName
FROM (Clients AS C INNER JOIN ClientTour AS CT ON C.clientID = CT.clientID) INNER JOIN Tours AS T ON CT.tourID = T.tourID
WHERE C.clientName <> 'John Doe'
AND T.hotel IN
(
SELECT DISTINCT T1.hotel
FROM (Clients AS C1 INNER JOIN ClientTour AS CT1 ON C1.clientID = CT1.clientID) INNER JOIN Tours AS T1 ON CT1.tourID = T1.tourID
WHERE C1.clientName = 'John Doe'
)
GROUP BY C.clientName
HAVING COUNT(DISTINKT T.hotel) = SELECT COUNT(DISTINCT T2.hotel)
FROM (Clients AS C2 INNER JOIN ClientTour AS CT2 ON C2.clientID = CT2.clientID) INNER JOIN Tours AS T2 ON CT2.tourID = T2.tourID
WHERE C2.clientName = 'John Doe'
First atempt:
SELECT C.clientName
FROM (Clients AS C INNER JOIN ClientTour AS CT ON C.clientID = CT.clientID) INNER JOIN Tours AS T ON CT.tourID = T.tourID
WHERE Trim(C.clientName) <> 'John Doe'
AND T.hotel IN
(
SELECT DISTINCT T1.hotel
FROM (Clients AS C1 INNER JOIN ClientTour AS CT1 ON C1.clientID = CT1.clientID) INNER JOIN Tours AS T1 ON CT1.tourID = T1.tourID
WHERE Trim(C1.clientName) = 'John Doe'
)

select distinct out of distinct

I have two table one has employees goals and the other has list of employees. i have to match one to another. Seems easy to do. but in the employee table employees can be entered more than once with more than one way of spelling their names. How can I pick only one name for each ID, it really doesn't matter which one I pick.
this is the code i used:
select distinct (etar.EmplKey ), emp.EmplFullName
FROM EmployeeTarget etar
inner join DimEmployee emp on emp.emplkey = etar.emplkey
inner join dimbranch br on br.BranchId = etar.BranchId
where etar.BranchId = 8
this is the results i get:
EmplKey EmplFullName
100260 Ida Patton
101488 Don Sheppard
101488 Donald Sheppard
101489 Teresa Coverdale
103121 Harjinder Aujla
How can I have that Don Sheppard guy listed only once?
The easiest way is to do aggreagtion:
select etar.EmplKey, min(emp.EmplFullName)
FROM EmployeeTarget etar
inner join DimEmployee emp on emp.emplkey = etar.emplkey
inner join dimbranch br on br.BranchId = etar.BranchId
where etar.BranchId = 8
group by etar.EmplKey

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;