SQL LIKE Concerns - sql

I would like to ask if is there a way I can achieve this query:
Select *
from table1
where table1.column1 like '%' (Select table2.column1 from table2)
where table2 contains all the values being compared in the like statement.
Is there a way that I can achieve this?

You can do this with a correlated subquery:
Select t1.*
from table1 t1
where exists (select 1
from table2 t2
where t1.column1 like '%' + t2.column1
);

Related

How to do a left join on a run-time generated tables?

I have two tables in the database (myCustomTable1 and myCustomTable2) and from them I'm creating another two tables (Table1 and Table2) which are created during the run time. Now I need to take the rows which are inside of Table1, but are not in the Table2.
I found this question, which seems that contains the answer that I need, but I'm not able to implement this with my solution, because as said, tables which I need to "antijoin" are generated during run-time.
Both of my (run-time generated) tables have the format:
-----------------------
| Column1 | Column2 |
-----------------------
| | |
-----------------------
| | |
-----------------------
Here is the code that I have.
SELECT Table1.* FROM (
SELECT myCustomTable1.Column1,
myCustomTable1.Column2
) as Table1
LEFT JOIN
(
SELECT Table2.* FROM (
SELECT myCustomTable2.Column1,
myCustomTable2.Column2
) as Table2
)
ON Table1.Column1 = Table2.Column1
AND Table1.Column2 = Table2.Column2
Now I'm aware that this solution is not working, because when trying to join, I'm trying to use Table2, which is not available in the global scope, but I can not find any appropriate solution.
Maybe using NOT EXISTS, LEFT OUTER JOIN or NOT IN is also an option, but in every try, I faced with the same issue where the scope of the defined tables is a problem.
I find it a lot easier to separate your sets in CTEs:
;WITH Table1 AS
(
SELECT
myCustomTable1.Column1,
myCustomTable1.Column2
FROM
myCustomTable1
),
Table2 AS
(
SELECT
myCustomTable2.Column1,
myCustomTable2.Column2
FROM
myCustomTable2
)
SELECT *
FROM Table1 as t1
WHERE
NOT EXISTS (SELECT 1
FROM Table2 as t2
WHERE t1.Column1 = t2.Column1
AND t1.Column2 = t2.Column2);
You missed a from table in the inner suquery and a table alias in the outer left join try use
SELECT Table1.* FROM (
SELECT myCustomTable1.Column1,
myCustomTable1.Column2
) as Table1
LEFT JOIN
(
SELECT Table2.* FROM (
SELECT myCustomTable2.Column1,
myCustomTable2.Column2
FROM myCustomTable2
) as Table2
) as table 3
ON Table1.Column1 = Table3.Column1
AND Table1.Column2 = Table3.Column2
You can select the records from Table1 excluding the matches using not exists in the where:
select *
from Table1
where
not exists(select 1
from Table2
where Table1.Column1 = Table2.Column1 and Table1.Column2 = Table2.Column2);
If you meant the aliases Table1 and Table2 as runtime created wouldn't this work?:
SELECT Table1.*
FROM (
SELECT myCustomTable1.Column1,
myCustomTable1.Column2
) as Table1
LEFT JOIN
(
SELECT myCustomTable2.Column1,
myCustomTable2.Column2
) as Table2
ON Table1.Column1 = Table2.Column1
AND Table1.Column2 = Table2.Column2
where Table2.Column1 is null;
Or (better IMHO):
SELECT Column1,
Column2
from myCustomTable1 t1
where not exists
(
SELECT * from myCustomTable2 t2 where
t1.Column1 = t2.Column1 and t1.Column2 = t2.Column2
);

SQL using minus function to eliminate rows from specific columns but returning all columns from table

I'm trying to return all columns in a database where certain rows within certain columns have been eliminate. Is there any possible way to do this? I tried using code like this but I'm unsure what I am missing to make this work
Select * from table1
where (select column1 from table1
minus select column1 from table2);
You can do this with a WHERE NOT EXISTS:
Select T1.*
From Table1 T1
Where Not Exists
(
Select *
From Table2 T2
Where T2.Column1 = T1.Column1
)
Alternatively, you could use a LEFT OUTER JOIN:
Select T1.*
From Table1 T1
Left Join Table2 T2 On T2.Column1 = T1.Column1
Where T2.Column1 Is Null
Or even a WHERE NOT IN:
Select *
From Table1
Where Column1 Not In
(
Select Column1
From Table2
)
I would recommend the WHERE NOT EXISTS approach, but to fix the query you have in the question, you just need to add a WHERE IN:
Select *
From Table1
Where Column1 In
(
Select Column1
From Table1
Minus
Select Column1
From Table2
)
Try this:
select * from table1 where column1 in
(select column1 from table1
minus
select column1 from table2);

SQL command usage of in / or

I have an sql command similar to below one.
select * from table1
where table1.col1 in (select columnA from table2 where table2.keyColumn=3)
or table1.col2 in (select columnA from table2 where table2.keyColumn=3)
Its performance is really bad so how can I change this command? (pls note that the two sql commands in the paranthesis are exactly same.)
Try
select distinct t1.* from table1 t1
inner join table2 t2 ON t1.col1 =t2.columnA OR t1.col2 = t2.columnA
This is your query:
select *
from table1
where table1.col1 in (select columnA from table2 and t2.keyColumn = 3) or
table1.col2 in (select columnA from table2 and t2.keyColumn = 3);
Probably the best approach is to build an index on table2(keyColumn, columnA).
It is also possible that in has poor performance characteristics. So, you can try rewriting this as an exists query:
select *
from table1 t1
where exists (select 1 from table2 t2 where t2.columnA = t1.col1 and t2.keyColumn = 3) or
exists (select 1 from table2 t2 where t2.columnA = t2.col1 and t2.keyColumn = 3);
In this case, the appropriate index is table2(columnA, keyColumn).
Assuming you're doing this in VFP, use SYS(3054) to see how the query is being optimized and what part is not.
Are the main query and subqueries fully Rushmore-optimisable?
Since the subqueries do not appear to be correlated (i.e. they don't refer to table1 then as long as everything is fully supported by indexes you should be fine.

SQL: expading "in (...)" to "like"

I have a sql select like below:
select * from table1 where text in (select text from table2)
In real in in clausule is more complex select that this. text is string (varchar). How to expand that sql to select rows from table1 where text is like texts from table2 (not only exactly equals) ?
If you have your wild card expressions in the text column in Table2 you can do like this.
select *
from Table1 as T1
where exists (select *
from Table2 as T2
where T1.[text] like T2.[text])
Otherwise you need to add the % in your query.
select *
from Table1 as T1
where exists (select *
from Table2 as T2
where T1.[text] like '%'+T2.[text]+'%')
Something like this:
select *
from table1
where exists
(
select *
from table2
where table1.text like N'%'+table2.text+N'%'
)
Note
This may be a performance killer
I know I am late to the party here, but you can also just do this with a JOIN if you need the wildcard text as part of the query (as I did). Assuming you have all the entries to check against in table2.text,
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.text LIKE '%' + t2.text + '%'

Can you SELECT WHERE something's LIKE and IN at the same time?

I need to write what I'd like to call a Valley Girl-query. I need to SELECT something that's LIKE IN - something like this:
SELECT * FROM Table1 WHERE Name LIKE
IN ( SELECT Name FROM Table2 )
The reason for this is I've got a table full of company names, but they're not exactly the same - f.ex. in Table1 it might say "Chrysler Group", while in Table2 it might just say "Chrysler".
Is there any easy way to do this?
select
*
from
Table1 t1
inner join Table2 t2 on (t1.name like t2.name + '%')
or without '%' sign if you want :)
Here's one way you could do it:
SELECT t1.*
FROM Table1 t1
JOIN Table2 t2 ON t1.Name LIKE t2.Name + '%'
I think:
SELECT * FROM table1 WHERE EXISTS (SELECT Name FROM Table2 WHERE Table1.Name LIKE Name)
If you are trying to create a list of close matches then the SOUNDEX function can be helpful when there may be questionable spelling.
SELECT T1.*
FROM Table1 T1
JOIN Table2 T2 ON SOUNDEX(T1.Name) = SOUNDEX(T2.Name)