Query with JOIN on multiple tables - sql

I have this little query that is working fine:
SELECT * FROM Components AS T1
WHERE Cost=null or Cost=''
Order by Code
Now I need to grab also the filed "description" from other tables where code=code
SELECT * FROM Components AS T1
WHERE Cost=null or Cost=''
LEFT JOIN on Table_321 AS T2
where T1.Code=T2.Code
Order by Code
But it is giving me a sintax error around "LEFT" which I have not been able to solve and I am not sure if such JOIN is the correct way to get it. Some help indicating me the how to solve the problem will be really appreciated. Moreover, I have also another table "Table_621 from which I need to take the description. How can I add this second table in the query?

SELECT * FROM Components T1
LEFT JOIN Table_321 T2 ON T1.Code=T2.Code
LEFT JOIN Table3 T3 ON T3.Code = T1.Code
WHERE T1.Cost=null or T1.Cost=''
ORDER BY T1.Code

Order by is ambiguous In below case:
SELECT * FROM Components T1
LEFT JOIN Table_321 T2
ON T1.Code=T2.Code
WHERE T1.Cost=null or T1.Cost=''
ORDER BY Code
try like this
SELECT * FROM Components T1
LEFT JOIN Table_321 T2
ON T1.Code=T2.Code
WHERE T1.Cost=null or T1.Cost=''
ORDER BY T2.Code --or T1.Code

SELECT * FROM Components AS T1
LEFT JOIN Table_321 AS T2
ON T1.Code=T2.Code
WHERE Cost=null or Cost=''
Order by Code

Related

JOIN and SELECT values not included in table2

I appreciate this might be very simple for you guys but sometimes the logic behind JOIN can be difficult for beginners. I want to select "ID" from table1 but only those "ID"s which do NOT appear in table2."ID". I tested LEFT and RIGHT but cannot get it to work the way I need to. I am using dashDB.
You can use NOT IN and subquery
Select * from table1 where id NOT IN (select id from table2);
try this...
SELECT *
FROM table1
LEFT JOIN table2 ON table1.ID = table2.ID
WHERE table2.ID IS NULL
I always prefer NOT EXISTS to do this
Select * from table1 a
where NOT EXISTS (select 1 from table2 b where a.id = b.id);
Here is a excellent article by Aaron Bertrand that compares the performance of all the methods
Should I use NOT IN, OUTER APPLY, LEFT OUTER JOIN, EXCEPT, or NOT EXISTS?
Use the below script.
SELECT t1.ID
FROM table1 t1
LEFT JOIN table2 t2 ON t1.ID = t2.ID
WHERE t2.ID IS NULL

Incorrect Syntax near the keyword On

I'm trying to make a query at work in which I count all occurrences of an id in one table and then I link the description and the colors associated with those ids from another.
But I seem to have botched up my SQL Syntax (it's been a while, sorry!). It's probably just a silly mistake and I'd be so greatful to whoever could help me out!
SELECT
t1.activity_status_id,
count(*),
t2.description,
t2.color
FROM
dbo.Activity t1
INNER JOIN (
dbo.Activity_Status t2 ON t1.activity_status_id = t2.id)
GROUP BY
activity_status_id
Just remove unnecessary brackets ( and ) around inner join:
SELECT ....
FROM
dbo.Activity t1
INNER JOIN dbo.Activity_Status t2 ON t1.activity_status_id = t2.id
GROUP BY ....
SELECT t1.activity_status_id,
Count(*),
t2.description,
t2.color
FROM dbo.Activity t1
INNER JOIN dbo.Activity_Status t2
ON t1.activity_status_id = t2.id
GROUP BY t1.activity_status_id ,t2.description,t2.color

Left join subquery gives invalid object name error

I have a SQL query that looks like this:
SELECT TOP 1000 FROM [Mydb].[dbo].[Table1] AS t1
LEFT JOIN (
SELECT fk_id, Email FROM dbo.Table2
) AS t2 ON t1.id = t2.fk_id
But this gives me the error:
Invalid object name 'dbo.Table2'.
Any idea why SQL Server does not recognize Table2 in my subquery?
PS.
I tried to rename dbo.Table2 to [Mydb].[dbo].[Table2]. But that gives me the same error.
First of all, Your query formation isn't correct. no need of that subquery at all.
Your posted query
SELECT TOP 1000 FROM [Mydb].[dbo].[Table1] AS t1
LEFT JOIN (
SELECT fk_id, Email FROM dbo.Table2
) AS t2 ON t1.id = t2.fk_id
Can be simplified as below, give it a try
SELECT TOP 1000 * FROM [Table1] t1
LEFT JOIN Table2 t2 ON t1.id = t2.fk_id
Not very sure if this is the problem but you have missed the asterisk (*) after top 1000. So maybe it should be something like the following
SELECT TOP 1000 * FROM [Mydb].[dbo].[Table1] AS t1
LEFT JOIN (
SELECT fk_id, Email FROM dbo.Table2
) AS t2 ON t1.id = t2.fk_id
If this doesnt work, remove dbo from dbo.Table2 and then try.
Hope this helps.

SQL Joining three tables and using LEFT OUTER JOIN

I have three tables and two seperate SQL queries which are working correctly and I am having correct results.
If I try to join these three tables I am having null as result.
First query:
select T1.ID,T3.COMPANY
from T1,T3
where (T1.status!='CLOSED') and (T1.PRIORITY)>5 and T1.CLASSID=T3.CLASSID
Second query:
SELECT T1.ID, T2.DESCRIPTION
FROM T1
LEFT OUTER JOIN T2
ON T1.ID=T2.KEY
WHERE T1.status!='CLOSED'
AND (T2.CREATEDATE= (SELECT MAX(CREATEDATE)
FROM T2
WHERE T2.KEY=T1.ID))
I tried to join them but as result I am having null:
select T1.ID,T3.COMPANY,T2.DESCRIPTION
from T1
INNER JOIN T3 ON T1.CLASSID=T3.CLASSID
LEFT OUTER JOIN T2
ON T1.ID=T2.KEY
where (T1.status!='CLOSED') AND (T1.PRIORITY)>5
AND (T2.CREATEDATE= (SELECT MAX(CREATEDATE)
FROM T2
WHERE T2.KEY=T1.ID))
like it does not recognized last part for taking MAX value from T2 table.
What am I doing wrong? Thanks for help
Firstly, use an alias for the subquery on table T2.
T2.CREATEDATE =
(SELECT MAX(T2Alias.CREATEDATE)
FROM T2 AS T2Alias
WHERE T2Alias.KEY = T1.ID)
Secondly, consider moving this condition into the ON clause of the LEFT JOIN to table T2.
The first thing that jumps out at me is the new dependency on both T1.Priority > 5 and T2.CreateDate value being equal to the result of the inline query:
( AND (T1.PRIORITY) > 5
AND (T2.CREATEDATE =
(SELECT MAX(CREATEDATE) FROM T2 WHERE T2.KEY = T1.ID) )
Without the data it's difficult to check however this may be the issue

SQL select column when there's more than one of the same name

I have this query
select *
from alldistros t1
LEFT join origin t2 on t1.name=t2.name
LEFT join desktop t3 on t2.name=t3.name
LEFT join beginnerdistributions t4 on t3.name=t4.name
it add on all my tables. But now when I want to select the name field (which is in all of them) I can't show it. It's just blank when I call it. And I would think so since there's more than 1 columns of the same name.
What can I do to fix this?
Just a plain join won't work, since it removes some of the fields that does not have the properties in the other tables.
You can use the 'AS' keyword to name a column. For instance:
select t1.name AS DistroName, t2.name AS OriginName, t3.name AS DesktopName
from alldistros t1
LEFT join origin t2 on t1.name=t2.name
LEFT join desktop t3 on t2.name=t3.name
LEFT join beginnerdistributions t4 on t3.name=t4.name
select
t1.name as t1_name,
t2.name as t2_name,
t3.name as t3_name
from alldistros t1
LEFT join origin t2 on t1.name=t2.name
LEFT join desktop t3 on t2.name=t3.name
LEFT join beginnerdistributions t4 on t3.name=t4.name
Not sure if it's Oracle only, but USING can do this for you for ad-hoc queries:
SELECT *
FROM TABLEA
JOIN TABLEB USING (NAME)
This will only return one NAME column from the SELECT *.