Sybase Delete command not working as expected - sybase

I wrote this simple commands to test the delete command but found some discrepancies with it:
delete table1 from table1,table2
where table1.col1 = table2.col1
and table1.col2= table2.col2
In table1 I have 272768 rows, table2 I have 1380 rows. Now I need to remove these 1380 rows available in table2 from table1. But to my surprise it removed 2234 rows from table1 after running the above script. The expected removal should be only 1380 rows. Is there anything I can do to optimize this ?

Try this way:
delete from table1
from table2
where table1.col1 = table2.col1
and table1.col2= table2.col2
or
delete from table1
where exists
(
select 1
from table2
where table1.col1 = table2.col1
and table1.col2= table2.col2
)

If you run the query:
SELECT COUNT(*)
FROM table1 JOIN table2
ON table1.col1 = table2.col1 AND table1.col2 = table2.col2
you will find that the 1380 rows in table2 match 2234 rows in table1, so the DELETE is doing exactly what it should do.
That query is the preferred form; you can use the antiquated notation without explicit joins:
SELECT COUNT(*)
FROM table1, table2
WHERE table1.col1 = table2.col1 AND table1.col2 = table2.col2
But you should be using the explicit JOIN notation in anything you write; you should only need to know about the comma-separated list in the FROM clause for understanding old queries written in another millennium.

Related

replace nested where condition with join

I have a SQL query that looks like this
Select a.*
From table1 a
where a.ColumnName in
(Select MAX(b.ColumnName)
from table2 b
where b.ColumnName2 in
(
Select MAX(c.columnName)
from table3 c
Group by c.ColumnName2
)
Group by b.ColumnName2
)
I am trying to write this in a join statement. I am positive inner join is what I need to get the right information. If someone could translate this to a join statement, I would be really glad.
Thank you.
EDIT 1:
I tried the typical Join statement that a rookie would.
Select a.*
from table1 a
inner join table2 b
on a.columnname = (Select max(b.columnName) from table2)
inner join table3 c
on b.columnName = (select max(c.columnName) from table3)
Obviously, that didn't work because I get 100,000+ results when I should be getting 800. I tried using an alias for table2 and table3 INSIDE the subselect statements and selecting the columnname using THAT alias like this:
Select max(bPart.columnName from table2 bPart)
Select max(cPart.columnName from table3 cPart)
Still the same result.
PERHAPS....
Though I'm not sure why a join is needed. Performance wise exists would likely be fastest, and since you're not returning values from table2 or 3 it seems like it would be the best approach.
SELECT a.*
FROM table1 a
INNER JOIN (SELECT MAX(ColumnName) MColumnName, columnname2
FROM table2
GROUP BY columnName2) B
ON A.columnName = B.mColumnName
INNER JOIN (SELECT MAX(columnName) mColumnName
FROM table3
GROUP BY ColumnName2) C
ON B.columname2 = C.MColumnName

Join three Tables with 2 different Columns

Here is the thing:
I have three tables:
Table1:
COL1 COL2
Table2:
COL2 COL3
Table3:
COL3 COL4
And I want to select the COL4 from Table3 when Table.COL3 = TABLE2.COL3, which the Table2.COL3 from when Table2.COL2 = Table1.COL2
It likes two join table, but when I use the following query it doesn't work.
SELECT * FROM
table3
INNER JOIN table2 ON table3.col3
=
(
SELECT table2.col3
FROM table2
INNER JOIN table1 ON
table1.col2 = table2.col2
)
You are selecting from table1 joining to the others. So table1 goes in the from.
SELECT TABLE3.col4
FROM TABLE1
JOIN TABLE2 ON TABLE2.COL2 = TABLE1.COL2
JOIN TABLE3 ON TABLE3.COL3= TABLE2.COL3
In the first table in the join statement. You have used a column name i.e. table3.col4 in stead of a table name i.e. table3. So, it's giving error.
Your correct query will be as follows
SELECT table3.col4 FROM table3
INNER JOIN table2
ON table3.col3 =
( SELECT table2.col3
FROM table2
INNER JOIN table1 ON
table1.col2 = table2.col2)

Using where condition in sql query

I have an sql query like this
Select col1, (select abc from table2 where def=1) as col2
From Table1 inner join table3 on Table1.id = table3.id
Where col2 = 4
The problem is that the where condition doesn't work. I get an error saying
Invalid column name 'col2'
Kindly help me fix this sql query.
Thanks in advance
You can define it in a CROSS APPLY and then reference in the SELECT and WHERE
SELECT col1,
col2
FROM Table1
INNER JOIN table3
ON Table1.id = table3.id
CROSS APPLY (SELECT abc
FROM table2
WHERE def = 1) C(col2)
WHERE col2 = 4
Using a CTE (Common Table Expression):
WITH SubQuery AS (Col2) {
SELECT
ABC
FROM
table2
WHERE
def = 1
}
SELECT
T.Col1,
S.Col2
FROM
SubQuery S,
Table1 T
INNER JOIN table3 t3
ON T.id = t3.id
WHERE
S.Col2 = 4
Although I must say I agree with the first comment - this makes no sense since your subquery is not correlated (joined) to the rest of your query...

Sql server DELETE and WITH clause

I need to build an SQL statement to delete from certain table the records that match another select statement.
In Teradata we use
delete from table1
where (col1, col2) in (
select col1,col2
from table2
)
While in SQL Server it's not allowed to have more than 1 column in the WHERE..IN clause. I thought I can use the WITH clause:
with tempTable(col1,col2) as (
select col1,col2
from table2
)
delete from table1
where table1.col1 = tempTable.col1
and table1.col2 = tempTable.col2
How to use WITH..DELETE clause? Is there another way?
This should do it:
DELETE Table1
from Table1 t1
inner join tempTable t2
on t2.Col1 = t1.Col1
and t2.Col2 = t1.Col2
First build a query that selects the rows you need:
SELECT t1.*
FROM [Table1] t1
INNER JOIN [Table2] t2 ON t1.[col1] = t2.[col1] AND t1.[Col2]=t2.[Col2]
Test it to make sure it returns exactly the rows you want to delete. Then turn it into a delete statement by changing the "SELECT" to "DELETE" and removing the column list:
DELETE t1
FROM [Table1] t1
INNER JOIN [Table2] t2 ON t1.[col1] = t2.[col1] AND t1.[Col
delete from table1 t1 where exists
(
select 1 from table2 t2 where t1.col1 = t2.col1 and t1.col2 > t2.col2
)
with tempTable(col1,col2) as (
select col1,col2
from table2
)
delete table1 from tempTable
where table1.col1 = tempTable.col1
and table1.col2 = tempTable.col2
This works for me
WITH CTE AS
(
SELECT TOP 50000 *
from v020101hist order by data
)
DELETE FROM CTE

Use of if exists( ) in select statement

I need to fetch a column from table2, if row exists, if not return it as null.
If i use ,case when it fetches only matched rows between table1 and table2.
If i use left outer join it fetches all the rows from table1 even though
condition table1.code='A'
So i need ,some thing like this.
select table1.id,
if(row exist in table2 for query(table2.relation_type_id=55 and table1.id=table2.related_id)
then
return table2.parent_id
else
null
as parent_id,
table1.description,
from table1,table2 where table1.code='A'
SELECT table1.id, table2.parent_id as parent_id
FROM table1
LEFT OUTER JOIN table2 ON (table1.id = table2.related_id)
WHERE table1.code = 'A';
EDIT based on comment :
SELECT table1.id, sub.parent_id as parent_id
FROM table1
LEFT OUTER JOIN (select parent_id,related_id from table2 where relation_type_id =55) sub ON (table1.id = sub.related_id)
WHERE table1.code = 'A';
Have you tried a sub-query?
select table1.id,
(select table2.parent_id from table2 where table2.relation_type_id=55 and table2.related_id=table1.id) parent_id,
table1.description,
from table1 where table1.code='A'

Resources