SQL DELETE WITH JOIN not working - sql

I have this query, which returns all records from Keywords which does not have a Quote
The query is working, but I want to DELETE all these records, the problem is that if I put DELETE instead of SELECT I get errors.
SELECT Keywords.[Id]
,[QuoteId]
FROM [QuotesTemple].[dbo].[Keywords]
LEFT JOIN [QuotesTemple].[dbo].Quotes ON Keywords.QuoteId=Quotes.Id
WHERE Quotes.Id IS NULL
This does not work.
DELETE
FROM [QuotesTemple].[dbo].[Keywords]
LEFT JOIN [QuotesTemple].[dbo].Quotes ON Keywords.QuoteId=Quotes.Id
WHERE Quotes.Id IS NULL
I get this error:
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'LEFT'.

Write it like this:
DELETE [QuotesTemple].[dbo].[Keywords] FROM [QuotesTemple].[dbo].[Keywords]
LEFT JOIN [QuotesTemple].[dbo].Quotes ON Keywords.QuoteId=Quotes.Id
WHERE Quotes.Id IS NULL

Valid syntax is:
DELETE [QuotesTemple].[dbo].[Keywords]
FROM [QuotesTemple].[dbo].[Keywords] AS k
LEFT JOIN [QuotesTemple].[dbo].[Quotes] AS q ON k.QuoteId = q.Id
WHERE q.Id IS NULL

you can also write as below
delete t1 FROM projects AS t1 LEFT OUTER JOIN [QuotesTemple].[dbo].Quotes AS t2 on t1.QuoteId= t2.QuoteId where t2.Id is Null

Related

Both left and right aliases encountered in JOIN '0' In Hive : Operator With Left Join in Hive

I am joining 2 tables and using <= in Join Condition also applying one filter condition so that it can only fetch remaining data from left table where filter condition is true.
I am using below query.
SELECT * FROM test.TABLE1 T1
left join test.TABLE2 T2
on (
T1.low<=T2.low
)
where t1.ID='1';
Error:
Error while compiling statement: FAILED: SemanticException
[Error 10017]: Line 4:0 Both left and right aliases encountered in JOIN '0' (state=42000,code=10017)
When I am only giving '=' condition instead of <= then its running without any issue.
You could do it like this:
with data as ( Select * FROM test.TABLE1 t1 WHERE t1.ID='1')
Select *
FROM data T1
LEFT JOIN test.TABLE2 T2 on T1.low<=T2.low

Joining two select statements together with outer join

My query on SQL Server:
select
s.learners_id, s.cv_student_id,
s.first_name + ' ' + s.last_name student_name,
p.program_name, dc.fulldate program_start_date,
sd.discount_value, dt.type_name
from
fact_student_programs_t sp
left join
object_sTATUSES_T os on os.object_statusid = sp.status_id
inner join
dim_programs_t p on p.programs_sk_id = sp.programs_sk_id
left join
dim_calendar dc on dc.datekey = sp.start_date_key
inner join
dim_students_t s on s.students_sk_id = sp.students_sk_id
outer join
(select
s.learners_id, sd.discount_value, dt.type_name
from
fact_student_discountS_T sd
inner join
discount_types_t dt on dt.id = sd.discount_type_id
inner join
dim_students_t s on s.students_sk_id = sd.students_sk_id
where
sd.curr_in = 1) discounts on discounts.learners_id = s.learners_id
where
sp.curr_in = 1
and dc.fulldate is not null
and os.status_name in ('Active')
and s.learners_id in ('201328', '237744', '237817', '239826', '308486', '308961',
'308973', '309352', '311521', '312269', '312951',
'313254', '313289', '384170', '384224', '384228',
'408911', '408912', '408936', '411293', '411308',
'411322', '411324', '411325', '411352', '411413',
'411417', '412865', '412923')
I am trying to join these two queries and am having trouble. Is the outer join statement in the wrong place? Can someone please help me fix this code?
EDIT:
outer join is not valid syntax here, so I am using left join. The query still returns an error:
Msg 4104, Level 16, State 1. The multi-part identifier "dt.type_name" could not be bound. Msg 4104, Level 16, State 1. The multi-part identifier "sd.discount_value" could not be bound. (Line 2)
Summary
outer join is not valid in SQL Server/t-sql I believe. It should either be left outer join, right outer join or full outer join.
In your situation, I suspect you want it to be a left outer join.
Explanation/longer version
In left and right outer joins, the 'left' and 'right' refer to the tables/etc literally to the left and right on the join (e.g., before and after the join, respectively).
In a left outer join, it takes all the values from the table on the left (first table) and any matching rows in the table on the right
In a right outer join, it takes all the values from the table on the right (second table) and any matching rows in the table on the left
A full outer join gets all rows from both tables, and matches them when they can.
Here is an example
/* Data setup */
CREATE TABLE #T1 (T1_ID int);
CREATE TABLE #T2 (T2_ID int);
INSERT INTO #T1 (T1_ID) VALUES (1), (2);
INSERT INTO #T2 (T2_ID) VALUES (1), (3);
/* Example joins */
SELECT #T1.T1_ID, #T2.T2_ID
FROM #T1
LEFT OUTER JOIN #T2 ON #T1.T1_ID = #T2.T2_ID;
SELECT #T1.T1_ID, #T2.T2_ID
FROM #T1
RIGHT OUTER JOIN #T2 ON #T1.T1_ID = #T2.T2_ID;
SELECT #T1.T1_ID, #T2.T2_ID
FROM #T1
FULL OUTER JOIN #T2 ON #T1.T1_ID = #T2.T2_ID;
/* Results
-- LEFT OUTER JOIN
T1_ID T2_ID
1 1
2 NULL
-- RIGHT OUTER JOIN
T1_ID T2_ID
1 1
NULL 3
-- FULL OUTER JOIN
T1_ID T2_ID
1 1
2 NULL
NULL 3
*/
Given you have WHERE s.learners_id in (...) in your WHERE clause, it implies you do not want rows where s.learners would be NULL.
If you had a right outer join, that WHERE requirement will effectively turn the right outer join into an inner join (as it would exclude all rows where s.learners_id is NULL).
If you had a full outer join, that WHERE requirement would effectively turn the full outer join into a left outer join - along similar logic lines.

Right syntax to use near ORDER BY when I try to delete some number of rows

When I try to delete some rows from tables (rows got from joined tables) (Ex : if I get 10 records, then need to delete first 2 records) using ORDER BY ASC and LIMIT = 2.
But getting error
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY sales_flat_quote.entity_id ASC LIMIT 2' at line 9
Below is my code:
DELETE table1,table2,table3
FROM table1
LEFT JOIN table2 on table1.entity_id=table2.quote_id
LEFT JOIN table3 on table1.entity_id=table3.quote_id
WHERE table1.entity_id <= 101
ORDER BY table1.entity_id ASC LIMIT 2;
You are doing a multi-table delete and the documentation states the following:
You cannot use ORDER BY or LIMIT in a multiple-table DELETE.
Some workarounds are eventually possible with nested selects. Check this answer to a similar question.
If temp table is acceptable to you, you can try something like this. Not superb but straightforward solution.
CREATE TEMPORARY TABLE TempToBeDeleted (
id_table1 INT NOT NULL
, id_table2 INT NOT NULL
, id_table3 INT NOT NULL
);
INSERT INTO (id_table1, id_table2, id_table3)
SELECT DISTINCT table1.primary_key, table2.primary_key, table3.primary_key
FROM
table1
LEFT JOIN table2 ON table1.entity_id = table2.quote_id
LEFT JOIN table3 ON table1.entity_id = table3.quote_id
WHERE table1.entity_id <= 101
ORDER BY table1.entity_id ASC LIMIT 2;
DELETE FROM table1, table2, table3
FROM
TempToBeDeleted
INNER JOIN table1 ON TempToBeDeleted.id_table1 = table1.primary_key
INNER JOIN table2 ON TempToBeDeleted.id_table2 = table2.primary_key
INNER JOIN table3 ON TempToBeDeleted.id_table3 = table3.primary_key
;
DROP TABLE TempToBeDeleted;
Solution :
DELETE table1,table2,table3
FROM table1
INNER JOIN (
SELECT entity_id
FROM table1
WHERE entity_id<= 101
ORDER BY entity_id ASC
LIMIT 2
) sub1
ON table1.entity_id=sub1.entity_id
LEFT JOIN table2 ON table2.quote_id = table1.entity_id
LEFT JOIN table3 ON table1.entity_id = table3.quote_id

UPDATE on LEFT JOIN error

I have an SQL query which keeps giving me an error.
I have tried multiple ways of writing the query but I have had no luck in fixing it.
I have two tables(table1 and table2) with duplicate columns orgcodeold and orgcode. table1.orgcode is empty but table2.orgcode is populated.
I am trying to populate table1.orgcode with table2.orgcode where table1.orgcodeold=table2.orgcodeold.
THE ERROR
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'AS'.
THE QUERY
UPDATE table1 AS t1
LEFT JOIN table2 AS t2
ON t2.orgcodeold = t1.orgcodeold
SET t1.orgcode = t2.orgcode
WHERE t1.orgcodeold = t2.orgcodeold
Please help.
Well, you have almost the whole syntax wrong. It should be:
UPDATE t1
SET t1.orgcode = t2.orgcode
FROM table1 AS t1
INNER JOIN table2 AS t2
ON t2.orgcodeold = t1.orgcodeold;
This should work:
UPDATE t1
SET t1.orgcode = t2.orgcode
from table1 AS t1
LEFT JOIN table2 AS t2
ON t2.orgcodeold = t1.orgcodeold

syntax error in join operation access

SELECT *
FROM
(table1 FULL OUTER JOIN [FY14 PLEDGE_TOTAL]
ON table1.[Id] = [FY14 PLEDGE_TOTAL].[SID]);
I don't know why I'm getting this error on Access. When I remove the parentheses after 'FROM', I get syntax error on From clause. Please advise. Thanks!
Access does not support OUTER JOIN. You need the variant which would be LEFT JOIN or RIGHT JOIN with an Is Null criteria on the field where the data do not exist.
Here is Microsoft's take on the issue : http://office.microsoft.com/en-gb/access-help/creating-an-outer-join-query-in-access-HA001034555.aspx
Or something much helpful : http://www.databasejournal.com/features/msaccess/article.php/3516561/Implementing-the-Equivalent-of-a-FULL-OUTER-JOIN-in-Microsoft-Access.htm
MS Access don't have support for FULL OUTER JOIN but the same can be emulated using UNION of both a LEFT JOIN and RIGHT JOIN like below
SELECT *
FROM table1 t1
LEFT JOIN [FY14 PLEDGE_TOTAL] fpt
ON t1.[Id] = fpt.[SID]
UNION
SELECT *
FROM table1 t2
RIGHT JOIN [FY14 PLEDGE_TOTAL] fpt1
ON t2.[Id] = fpt1.[SID];