SQL Server query delete results of a join from a table - sql

I'm not a SQL Server expert and I'm struggling with this query. Can anyone help?
DELETE
FROM PPTMAILLISTC.dbo.emailTables
WHERE email IN (SELECT *
FROM PPTMAILLISTC.dbo.emailTables tab1
INNER JOIN PPTMAILLISTAB.dbo.emailTables tab2
ON tab1.email = tab2.email)
SQL Server Management Studio returns.
Msg 116, Level 16, State 1, Line 1
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Basically, there are 2 separate tables both called the same (dbo.emailTables) in 2 separate databases (PPTMAILLISTC and PPTMAILLISTAB).
Where the both databases have the same results (which I can find out using the join i.e.)
SELECT *
FROM PPTMAILLISTC.dbo.emailTables tab1
INNER JOIN PPTMAILLISTAB.dbo.emailTables tab2
ON tab1.email = tab2.email
I want to delete the results of this join from PPTMAILLISTC.dbo.emailTables.

You can get rid of the use of IN and just use your inner SELECT statement and convert it to a DELETE and just reference the ALIAS (tab1) of the table you actually want to affect like this:
DELETE tab1
FROM PPTMAILLISTC.dbo.emailTables tab1
INNER JOIN PPTMAILLISTAB.dbo.emailTables tab2 ON tab1.email = tab2.email

You need something like this:
DELETE FROM PPTMAILLISTC.dbo.emailTables
WHERE email IN (SELECT tab1.email
FROM PPTMAILLISTC.dbo.emailTables tab1 INNER JOIN
PPTMAILLISTAB.dbo.emailTables tab2
ON tab1.email = tab2.email
);
Even if both tables have only one column, the * resolves to two columns tab1.email and tab2.email. As the error message says, the select list for an in subquery can have only one column.
EDIT:
This is actually simpler to write as:
DELETE FROM PPTMAILLISTC.dbo.emailTables
WHERE email IN (SELECT tab2.email
FROM PPTMAILLISTAB.dbo.emailTables tab2
);
You don't need to do the join in the in subquery.

Related

H2 does not allow to execute select with join inside set

I want to fill all columns in one table basing on columns from select with left join from two others:
update TAB1 as P
set P.COL1 = (
select CODE from (
select * from TAB2 as A left outer join TAB3 as T on A.TAGID = T.ID
) as O
where P.ACTID = O.ACTID
);
It works properly on Oracle, but when i want to execute it on h2 I got this error:
Duplicate column name "ID"; SQL statement
I dont know where is a problem. I could'nt find any solution for that.
Thx for the answers
This statement is your problem:
(select * from TAB2 as A left outer join TAB3 as T on A.TAGID = T.ID)
Presumably, you have an ID in both tables, so the SELECT * returns two columns named ID. I'm surprised this works in Oracle -- but perhaps Oracle optimizes the code because the IDs are not needed.
Just return the value you want:
(select ?.CODE from TAB2 as A left outer join TAB3 as T on A.TAGID = T.ID)
The question mark is either A or T, depending on which table the value comes from.

Convert SQL Server Delete statement to use inner join

My goal is to convert the below query to use inner join.
DELETE ##TABLE1
FROM ##TABLE1 A, ##TABLE2 B
WHERE A.VND_ACCT_NUM in (select distinct VND_ACCT_NUM from ##TABLE2)
I tried something like this but it doesn't seem right:
DELETE ##TABLE1
FROM ##TABLE1 A
INNER JOIN ##TABLE2 B
ON A.VND_ACCT_NUM in (select distinct VND_ACCT_NUM from ##TABLE2)
My task is to convert the first query to use the inner join syntax.
Just match the tables up directly:
DELETE FROM A
FROM ##TABLE1 A
INNER JOIN ##TABLE2 B
ON A.VND_ACCT_NUM i= B.VND_ACCT_NUM
SQL Server doesn't mind if you attempt to delete the same row multiple times by such a join. Also, the ... IN(SELECT DISTINCT ... was pointless anyway since IN returns true as soon as it has located one qualifying row.
Try this:
DELETE A
FROM ##MX_DEALS A
INNER JOIN ##MX_DAR_ISSUE_DEALS B
ON A.VND_ACCT_NUM in (select distinct VND_ACCT_NUM from ##MX_DAR_ISSUE_DEALS)

SQL-Duplicate column names

I am using SQL developer and I am trying an outer join on two tables. The error it is showing is "Duplicate column names". I have used the table nameswhile comparison but still it is giving error. Code is as following:
CREATE VIEW OECD_VIEW AS
SELECT * FROM DM_OECD_GDP FULL OUTER JOIN DM_OECD_DOCTORS
ON DM_OECD_GDP.DATA_YEAR = DM_OECD_DOCTORS.DATA_YEAR;
I have heard that this error can be resolved by aliasing but I don't know how to alias while comparing. Can this be done?
Thanks
You have to explicitly name returned fields from both tables and alias fields having the same name, like:
CREATE VIEW OECD_VIEW AS
SELECT DM_OECD_GDP.DATA_YEAR AS GDP_DATA_YEAR,
DM_OECD_DOCTORS.DATA_YEAR AS DOC_DATA_YEAR,
... rest of the fields here
FROM DM_OECD_GDP
FULL OUTER JOIN DM_OECD_DOCTORS
ON DM_OECD_GDP.DATA_YEAR = DM_OECD_DOCTORS.DATA_YEAR;
This is the generalised query to create views with alias and full outer join :
create view v6 as select t1.c1, t2.c2 from t1 FULL OUTER JOIN t2 on t1.id = t2.id;
If both tables have same column names, may be the foreign keys, dont use * , Manually select the column names
table_one - ID,Name
table_two - ID,SecondName
select table_one.ID,table_one.Name,table_two.ID as ID2,
table_two.SecondName full outer join on table_one.ID=table_two.ID

MS Access Inner Join On 3 Tables with the same field_name

I'm doing an assignment for class, and I'm at my whits end. Basically, I need to write a query that uses INNER JOIN's to combine all the data in 4 tables, while avoiding having titles columns with identical names (Ie Table1.Master_Number and Table2.Master_Number). The literal instructions are as follows:
Step 1: Create a view of all the columns (only list the columns once if the columns are duplicated in the tables) in all the tables of the Lecture 5 database. Save this query as Lecture_5_View and us this query to build the following 3 queries. In the “FROM” clause, you MUST use an “INNER JOIN” in this query. Remember ACCESS does not support the “CREATE VIEW” command so you must create a select query. `
Here is a screenshot of the database, that shows all the headings and column headings.
Based on other posts like this, I thought that it would be pretty simple. I have this so far, but it does not want to run
(ie Syntax error(missing operator) in query expression 'Table1.Master_Number = Table2.Master_Number INNER JOIN Table4 ON Table1.Master_Number = Table4.Master_Number)
SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.Master_Number = Table2.Master_Number
INNER JOIN Table4 ON Table1.Master_Number = Table4.Master_Number
Where am I going wrong so far and how to I accomplish this query?
Much thanks,
Josh
With Access you need to use parentheses when doing more than one join:
SELECT *
FROM (Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
This is essentiall spliting down you query so you have at most one join per section, i.e. Your First query is:
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number;
Then you have:
SELECT *
FROM YourFirstQuery
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
This differs slightly from subqueries as you are still able to reference all fields from Table1 and Table2.
EDIT
To avoid duplicating columns you will need to explicitly list the columns you want (although you should be doing this anyway):
SELECT Table1.Master_Number,
Table1.Asset_Tag,
Table1.Serial_Number,
Table2.Last_Name,
Table2.First_Name,
Table4.Office_Number,
Table4.Location,
Table4.Department
FROM (Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
Try this:
SELECT * FROM ((Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number)

sql access inner join of 4 tables

I am new to sql and I am trying to join 4 tables together but just cant get the hang of it.
I am trying to do this with an inner join but I always get an syntax error with access.
SELECT *
from kdst,aufpos
inner join( artst inner join vert on kdst.vertreter = vert.vertnr)
on aufpos.artnr = artst.artnr;
This is my code but it does not work. I dont know what to do anymore, I hope someone can help me.
Build using the query design window
Then switch to sql view
Select *
From table1 t1
Inner join table2 t2 on t1.id = t2.fkid
Inner join table3 t3 on t1.id = t3.fkid
...
This is if you want to join multiple tables to the same parent table (table1). Fkid is the column of the foreign key field that refers to the primary key of the parent table.