'Joining three tables on two IDs
I have this exercise for an application and I can't really check if I'm doing it right because I'm a beginner and I don't how to make my own database.
These are the given data:
Table 1: Company
| Company_ID | Name | Desc | Location | Ticker |
|---|---|---|---|---|
| 1 | ABC | An.. | US | AB |
| 2 | EFG | An.. | US | EF |
| 3 | HIJ | An.. | EU | HI |
Table 2: Others
| Company_ID | Ratings | Issue | BUD_ID |
|---|---|---|---|
| 1 | ABC | An... | US |
| 2 | EFG | An... | US |
| 3 | HIJ | An... | EU |
Table 3: Sectors
| Company_ID | Sector | Code |
|---|---|---|
| 1 | Electronic | |
| 2 | Industrial | 000111 |
| 3 | Manufacturing | 222333 |
Here's what I've written: I need to extract the name, description, location, issue, and BUD_ID based on company id's 2 and 3
SELECT c.Company_ID, c.Name, c.Description, c.Location, cxi.Issue, cxi.BUD_ID
FROM Companies as c
JOIN Others as cxi
ON c.Company_ID = cxi.Company_ID
JOIN Sectors as csx
ON c.Company_ID = csx.Company_ID
WHERE c.Company_ID = '2'
AND c.Company_ID = '3';
Thank you so much.
Solution 1:[1]
Change:
SELECT c.Company_ID, c.Name, c.Description, c.Location, cxi.Issue, cxi.BUD_ID
FROM Companies as c
JOIN Others as cxi
ON c.Company_ID = cxi.Company_ID
JOIN Sectors as csx
ON c.Company_ID = csx.Company_ID
WHERE c.Company_ID = '2'
AND c.Company_ID = '3';
to:
SELECT c.Company_ID, c.Name, c.Description, c.Location, cxi.Issue, cxi.BUD_ID
FROM Companies as c
JOIN Others as cxi
ON c.Company_ID = cxi.Company_ID
JOIN Sectors as csx
ON c.Company_ID = csx.Company_ID
WHERE c.Company_ID = '2'
OR c.Company_ID = '3';
You cannot have ID 2 and 3 on the same row. If you want to get data for both use the OR rather than AND.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sergiu |
