SQL : How can i join different tables from competition of football? - sql

The problem
I have to make a SQL (postgres) query, to show the names of players, whose teams participate in the "Primeira Liga" and the "Taça de Portugal". Also, include the names of players of "Académica" club and all of teams founded in the 1940s.
Image of Database design
I'm really stuck and can't figure out, how to connect all tables. Does anyone know how to solve the problem?
Thanks in advance.
Updated (10/01/2021):
** I had to change the names to match my DB, so everyone can understand.

(SELECT DISTINCT j.nome
FROM jogador as j
INNER JOIN equipa as E
ON E.id = j.equipa_id
INNER JOIN competicao_equipa as eq
ON E.id = eq.equipa_id
WHERE (eq.competicao_id = 1 OR eq.competicao_id = 3))
UNION
(SELECT j.nome
FROM jogador as j
where equipa_id = 1)
UNION
(SELECT j.nome
FROM jogador as j,equipa
where equipa.fundacao BETWEEN '01/01/1940' AND '31/12/1949')

Related

some errors when Im using ( join on) in sql

Im trying to use join on in my sql to link between three tables but still have errors
when I'm trying to retrieve:
(Aminah’s lecturer and subject)
i did this but still have errors
SELECT c.SUBJECT1,c.SUBJECT2,c.SUBJECT3,l.NAME
FROM
STUDENT s
JOIN COURSE c
ON c.COURSE = s.COURES
JOIN LECTURER l
ON l.LECT_ID =c.LECT1 AND c.LECT2 AND c.LECT3
WHERE s.NAME = 'Aminah' AND c.SUBJECT1 = c.LECT1 AND c.SUBJECT2 = c.LECT2 AND c.SUBJECT3 = c.LECT3
also same thing when i am trying to show:
(Subject that teach by Ahmad)
i am a beginner andit is still hard to me with using (join on)
i hope some one help me
I think you want multiple joins, like this:
select c.subject1, c.subject2, c.subject3,
l1.name as name_1, l2.name as name_2, l2.name as name_2
from student s join
course c
on s.course = c.course left join
lecturer l1
on c.lect_1 = l1.lect_id left join
lecturer l2
on c.lect_2 = l2.lect_id left join
lecturer l3
on c.lect_3 = l3.lect_id
where s.name = 'Aminah';
You should also know that this is a really bad data model. It is suspicious whenever you have "arrays" (such as courses) that are spread across columns. These should actually be in separate rows to be better suited to SQL.

Finding Non Entries within another data table (MS Access SQL)

I know there ust be a few hundred of this similar post, but I have tried all the other ways in MS Access and still cannot get it to work.
So my working code is as follows
SELECT FVR.*, V.[Week Commencing], F.Date, V.Date
FROM FVR
INNER JOIN (F
INNER JOIN V ON (F.[Week Commencing] = V.[Week Commencing]) AND (F.GUID = V.GUID))
ON (FVR.GUID = V.GUID) AND (FVR.GUID = F.GUID)
My desired effect would be to show the Dates of the "F" table that have no entries in the "V"Table.
Sorry for being crpytic on the tables but it is for work. I thoght i had a good idead on how to do most of this.
any help would be amazing as I have been pulling my hair over this for a while now.
Cheers and thanks in advance.
Editing this to add in the full code as it will make more sense.
I basically have am unable to produce the Data range from F(Forecast) that Does not match in V(Visits) am trying to bring up a list of forecasted dates that have not been visited using the Week Commencing and GUID from both tables, The FVR table is just a table that holds the regional data matching up to the GUID. #Hogan I tried your way and ended up with syntax errors, I almost got somewhere and then lost it again. I thought I had a bit more knowledge of SQL than this.
Full code is as follows
SELECT FVR.*, [Visits].[Week Commencing], [Forecast].[Forecast Date], [Visits].Date
FROM ForecastVisitRegion
INNER JOIN ([Forecast] INNER JOIN [Visits] ON ([Forecast].[Week Commencing] = [Visits].[Week Commencing])
AND ([Forecast].GUID = [Visits].GUID)) ON (FVR.GUID = [Visits].GUID)
AND (FVR.GUID = [External - Forecast].GUID)
Thanks again
Stephen Edwards
You need to use left joins:
SELECT FVR.*, V.[Week Commencing], NZ(V.Date,F.Date) as virtual_date
FROM FVR
LEFT JOIN F ON FVR.GUID = F.GUID
LEFT JOIN V ON FVR.GUID = V.GUID F.[Week Commencing] = V.[Week Commencing]
Not sure I understand why FVR is coming into the mix but you need a left Join.
Select F.*
from F
left join V on F.[Week Commencing] = V.[Week Commencing] AND F.GUID = V.GUID
where V.GUID is null
The left join ensures all the records (matched or not) from F are included in the result set. Then the where V.GUID is null removes the records where no match was found in V leaving you with the F records with no match.
Another approach would be to use the NOT EXISTS statement in the WHERE Clause
Select F.*
from F
where not exists (select * from V where F.[Week Commencing] = V.[Week Commencing] AND F.GUID = V.GUID)

SQL problems, JOINS

Im having some problem with my SQL code. My assignment is to present some information about teachers(Lärare.personnummer) who don't have teach the course "Java2"(Kurstilfälle.kurs). The code is right but my problem is that there is one teacher who don't have teach any course(kurs). So the information about her is not in the result. I want to get this last persons information in my result.
My code--> http://imgur.com/QT3u4TL
Database--> https://ilearn2.dsv.su.se/mod/resource/view.php?id=21941
SELECT DISTINCT Person.personnummer, Person.namn, tjänsterum, telefon
FROM Kurstillfälle,
Person,
Lärare
WHERE Person.personnummer = Lärare.personnummer
AND Kurstillfälle.lärare = Person.personnummer
AND Lärare.personnummer NOT IN (SELECT Kurstillfälle.lärare
FROM Kurstillfälle WHERE kurs = 'Java2')
Maybe someone can help me with this. Thanks.
With new style JOIN, return a teacher who NOT EXISTS as teacher for Java2:
SELECT p.personnummer, p.namn, tjänsterum, telefon
FROM Person p
INNER JOIN Lärare l ON p.personnummer = l.personnummer
WHERE NOT EXISTS (SELECT 1 FROM Kurstillfälle
WHERE kurs = 'Java2'
AND lärare = p.personnummer)
Edit: I don't know Access syntax, but try INNER JOIN instead of just JOIN!
Using Joins this should get what you want
SELECT DISTINCT p.personnummer, p.namn, tjänsterum, telefon
FROM Kurstillfälle k
INNER JOIN Person p ON k.larare = p.personnummer
INNER JOIN Lärare l ON p.personnummer = l.personnummer
WHERE k.kurs !='Java2'

sql one to many including itself?

Struggling with a one to many that also includes the master record without having to add a detail record that references itself?
select * from master m
inner join detail d on d.id2 = m.id
where d.id1 = 1
This will return
2,John,1,2
4,Nancy,1,4
How do I get it to also return
1,Jim,nul,nul
If I add 1,1 record to the below detail it works, but was hoping to get around that.
Master
1,Jim
2,John
3,Fred
4,Nancy
5,Jen
Detail
1,2
1,4
3,5
Thanks
Glenn
I think you want a more complicated on expression:
select *
from master m inner join
detail d
on d.id2 = m.id or d.id1 = m.id
where d.id1 = 1;
You could also write this as:
on m.id in (d.id1, d.id2)

SQL joining on names

I am querying two tables and joining them together on people's names. I am trying to produce all employees who have not filled out a form within the past month. The problem I am encountering is I am receiving an overlap of names when people list their short name (Joe rather than Joseph, or Mike rather than Michael). How can I still produce the list of people without overlap, even when they use their short names?
This is the query I have as of now:
SELECT DISTINCT ge.employeeNo,
(ge.firstName + ' ' + ge.lastName) AS empName,
ge.email
FROM dbo.hist_Employees ge
INNER JOIN dbo.ctrl_Sites cs ON ge.locationID = cs.ID
WHERE (ge.firstName + ' ' + ge.lastName) NOT IN
(SELECT sc.recordedBy
FROM GRSTOPS.dbo.hist_StopCard sc
INNER JOIN dbo.ctrl_Area a ON sc.area = a.ID
INNER JOIN dbo.ctrl_Site s ON a.site = s.ID
WHERE sc.recorded BETWEEN '10/01/2013' AND '10/30/2013'
AND s.code = 'gre')
AND cs.Abbreviation = 'gre'
AND ge.employmentStatus = 1
AND ge.primaryDept <> 3
It would be better not to join on peoples names as they are not unique.
You should join using a primary key like ID/employeeNo etc.