Please help - SQL Join query - sql

I am working on a project, and need to join results from 2 tables into one set.
The tables are ordered as such:
gameData: [Id,TeamID, data..........]
players: [Id (same as above), name, data.....]
I need to do something like:
SELECT * FROM gameData and SELECT data FROM players WHERE gameData.Id = players.Id
And here is what I have thusfar.
SELECT * FROM gameData AS A LEFT OUTER JOIN players AS B on A.playerID = B.Id;
And have it return all of the values from A, and only the data from B.
I know that the syntax is not correct, I have little experience working with SQL Joins, any advice would be greatly appreciated!
Edit: Trying both answers now. Thanks!
Edit2: Can I do something like: "Select a.* from tableA as a"

I love you guys, working as intended now!
Thanks!
The query I ended up using was:
Select a.*, b.height, b.weight from gameData as a LEFT OUTER JOIN players b on a.playerID = b.Id;

You could enumerate the fields that you select and alias the tables, like:
select a.Id, a.TeamId, a.data, b.data
from tableA a
join tableB b on a.Id = b.Id

Select a.Id, a.TeamID, a.data, b.data
FROM gameData as a
LEFT OUTER JOIN
players b On a.ID = b.ID

Related

FROM Statement Giving an Error (3 Sources)

I am trying to SELECT a few things from tables A,B, and C, but when I try to LEFT JOIN, I keep getting an error. My code currently looks like this:
FROM (A LEFT JOIN B ON A.id = B.id), C
Am I not allowed to left join two tables and include the entirety of a third?
Thanks for your help.
EDIT
Here is a sample code:
SELECT A.ID, A.place, A.receipt, D.State, A.service, B.Description, C.ID, C.receipt, C.Source
FROM B, (A LEFT JOIN C ON A.receipt = C.receipt), D;
Access doesn't support combining cross-joins with other joins, so you will have to do the left join in a subquery, and then the cross-join:
FROM (SELECT * FROM A LEFT JOIN B ON A.id = B.id) As D, C

Join WHERE clause - table has row with defined value, or no entry

Can anyone help me figure out the correct WHERE clause for the following scenario:
select A.name
from tableA A, tableB B
where A.id = B.id
and
(
B.field = 5
OR
B.hasNoRowForJoinedID
)
I feel like I'm missing something really obvious here in how to accomplish this, but I can't for the life of me put my finger on it.
You are using an outdated SQL Syntax. To perform the LEFT OUTER JOIN based your your request above, you can do the following:
SELECT A.name
FROM A
LEFT OUTER JOIN B ON A.id = B.id
WHERE (B.field = 5 OR B.field IS NULL)
Use proper join syntax and not the outdated ones:
select A.name
from tableA A
left join tableB B on A.id = B.id and B.field = 5

How can you perform a join when also using a comma-separated list of tables in an SQL select statement?

This is evidently correct syntax in SQL Server:
SELECT a.id, b.name
FROM Table1 a, Table2 b
WHERE a.id = b.fk1
So is this:
SELECT a.id, c.status
FROM Table1 a
JOIN Table3 c ON a.id = c.fk2
But this apparently isn't:
SELECT a.id, b.name, c.status
FROM Table1 a, Table2 b
JOIN Table3 c ON a.id = c.fk2
WHERE a.id = b.fk1
I would NOT normally want to construct a query in the third case's style (and really not the first case's either), but it would probably be the path of least resistence in editing some code that's already been written at my company. Somebody used the first form with five different tables, and I really need to work in a sixth table through a JOIN statement, without taking chances of messing up what they already have. Even though I could re-write their stuff outright if I need to, I would really like to know how to do something like in the third case.
Running the code exactly as-is in the examples, the third case gives me this error message:
The multi-part identifier "a.id" could not be bound.
What is syntactically breaking the third case? What simple fix could be applied? Thanks!
I, likewise, would not recommend doing this. But, you can just change the , to a cross join:
SELECT a.id, b.name, c.status
FROM Table1 a cross join Table2 b
JOIN Table3 c ON a.id = c.fk2
WHERE a.id = b.fk1
This code:
SELECT a.id, b.name, c.status
FROM Table1 a, Table2 b
JOIN Table3 c ON a.id = c.fk2
WHERE a.id = b.fk1
is doing a cross join on a and the result of an inner join on b and c. c cannot access any of the fields in a because the join is being performed on b. what you should do is change your query to:
SELECT a.id, b.name, c.status
FROM Table1 a
inner join Table2 b on a.id = b.fk1
inner JOIN Table3 c ON a.id = c.fk2

SQL Join query help

I have 2 tables A and B with the following columns
Table A - id,bId,aName,aVal
Table B - id,bName
where A.bId is the same as B.id. I want a result set from a query to get
A.id, A.aName, B.bName where A.bId=B.id OR
A.id, A.aName, "" when A.bId=0.
In both cases, only those records should be considered where A.aVal LIKE "aVal"
Can someone please help me with the query? I can use left join but how do I get the blank string if bId=0 and B.bName otherwise?
Thanks
SELECT a.id, a.aname, COALESCE(b.bname, '')
FROM a
LEFT JOIN
b
ON b.id = NULLIF(a.bld, 0)
WHERE a.aval LIKE 'aval'

How do I find records that are not joined?

I have two tables that are joined together.
A has many B
Normally you would do:
select * from a,b where b.a_id = a.id
To get all of the records from a that has a record in b.
How do I get just the records in a that does not have anything in b?
select * from a where id not in (select a_id from b)
Or like some other people on this thread says:
select a.* from a
left outer join b on a.id = b.a_id
where b.a_id is null
select * from a
left outer join b on a.id = b.a_id
where b.a_id is null
The following image will help to understand SQL LET JOIN :
Another approach:
select * from a where not exists (select * from b where b.a_id = a.id)
The "exists" approach is useful if there is some other "where" clause you need to attach to the inner query.
SELECT id FROM a
EXCEPT
SELECT a_id FROM b;
You will probably get a lot better performance (than using 'not in') if you use an outer join:
select * from a left outer join b on a.id = b.a_id where b.a_id is null;
SELECT <columnns>
FROM a WHERE id NOT IN (SELECT a_id FROM b)
In case of one join it is pretty fast, but when we are removing records from database which has about 50 milions records and 4 and more joins due to foreign keys, it takes a few minutes to do it.
Much faster to use WHERE NOT IN condition like this:
select a.* from a
where a.id NOT IN(SELECT DISTINCT a_id FROM b where a_id IS NOT NULL)
//And for more joins
AND a.id NOT IN(SELECT DISTINCT a_id FROM c where a_id IS NOT NULL)
I can also recommended this approach for deleting in case we don't have configured cascade delete.
This query takes only a few seconds.
The first approach is
select a.* from a where a.id not in (select b.ida from b)
the second approach is
select a.*
from a left outer join b on a.id = b.ida
where b.ida is null
The first approach is very expensive. The second approach is better.
With PostgreSql 9.4, I did the "explain query" function and the first query as a cost of cost=0.00..1982043603.32.
Instead the join query as a cost of cost=45946.77..45946.78
For example, I search for all products that are not compatible with no vehicles. I've 100k products and more than 1m compatibilities.
select count(*) from product a left outer join compatible c on a.id=c.idprod where c.idprod is null
The join query spent about 5 seconds, instead the subquery version has never ended after 3 minutes.
Another way of writing it
select a.*
from a
left outer join b
on a.id = b.id
where b.id is null
Ouch, beaten by Nathan :)
This will protect you from nulls in the IN clause, which can cause unexpected behavior.
select * from a where id not in (select [a id] from b where [a id] is not null)