Tuesday 4 September 2018

Having trouble creating a MYSQL query that selects data from two tables

I have some trouble doing an exercise in MYSQL.

There are two tables:
Table Employees:
ID    Name         InstitutionID

1     Tom          1
2     Bert         1
3     Steve        2
4     Marcus       3
5     Justin       1

Table Institutions:
InsID    InstitutionName      Location

1         Storage               London
2         Storage               Berlin
3         Research              London
4         Distribution          Stockholm

Now the task is to create a query that puts out a table with two columns:
 Employees.Name         Institutions.InstiutionName

With the statement that the location of the Institution is in London, that means the Institution ID from the table Employees is the same as the the InsID from the table Institutions.
The output should like this:
Name          InstituionName

Tom           Storage
Bert          Storage
Marcus        Research
Justin        Storage

To just get the Names without the InstitutionName is simple:
select Employees.Name from Employees
where InstitutionID in (select InsID from Institutions where Location = 'London')

But I don't get how to get the Employees' Names and the Institution Name in one table.
Pls help me :)

So you need a simple join query :
SELECT t.name,s.InstitutionName
FROM Employees t
INNER JOIN Institutions s
ON(t.InstitutionID = s.insID
   AND s.Location = 'London')

0 comments:

Post a Comment