I have written the query:
Select distinct a,b from t1 minus Select distinct a,b from t2.
Here t1 and t2 are two tables. I want distinct values of a and b that occur in t1 but not in t2. So I'm using minus operator. I want values of both a and b but I know that in some cases the value of b in t1 and t2 maybe different. This would result in values of a and b that are present in both t1 and t2 as minus would not happen if values of b do not match in both the tables. How can I do this successfully?
How can I get values of a and b that are present in table t1 but not in table t2 even though in some cases values of b might not match in both the tables?
table1: table2:
column1 column2 column1 column2
1 a 1 c
2 b 3 d
In this case I would want values (2,b) only. I would not want (1,a) as 1 is also present in table2.
Start with not exists:
select distinct. . .
from t1
where not exists (select 1 from t2 where t2.a = t1.a and t2.b = t1.b);
From you describe, you might want the comparison only on a:
select distinct a, b
from t1
where not exists (select 1 from t2 where t2.a = t1.a);
Another option is to use sub query in the WHERE condition as below-
SELECT A.*
FROM table1 A
WHERE A.column1 NOT IN
(SELECT DISTINCT column1 FROM table2)
You can also use LEFT JOIN as below which will provide you the same output as below-
SELECT A.*
FROM table1 A
LEFT JOIN table2 B ON A.column1 = B.column1
WHERE B.column1 IS NULL
For the data not include in t2, you can either go for the NOT EXISTS or LEFT OUTER JOIN.
Here is the solution.
Using NOT EXISTS
SELECT DISTINCT A,B FROM T1 WHERE NOT EXISTS (SELECT 1 FROM T2 WHERE T2.A = T1.A AND T2.B = T1.B);
Using Left Join
SELECT DISTINCT a,b,c FROM T1 LEFT JOIN T2 ON T1.a = T2.a and T1.b = T2.b WHERE T2.a IS NULL AND T2.b IS NULL
Hope it helps.
Related
I have a query.
with result as
(
select t1.name, t1.number, t2.number from table1 t1, table2 t2 where some conditions
union all
select t1.name, t1.number, t3.number from table1 t1, table3 t3 where some conditions
)select * from result
I need to insert in table5 t1.name and t2.number
table5 has the same columns as t1.
If I do something like
insert in table5(name, number)
select r.name, r.number from result r
what would be considered r.number? t1.number or t2.number? Because columns have the same name. Or is there a way to defferentiate? How can I make it so the query skips every row with t3.number? Can I even do it?
For example I have table1
A (+1)11111111
B (+1)22222222
C (+1)33333333
table2
(+2)44444444
(+2)55555555
first select will get me
A (+1)11111111 (+2)44444444
B (+1)22222222 (+2)55555555
table3
(+3)66666666
(+3)88888888
(+3)97898789
result of second select
B (+1)22222222 (+3)88888888
C (+1)33333333 (+3)97898789
this will be the result of union all
A (+1)11111111 (+2)44444444
B (+1)22222222 (+2)55555555
B (+1)22222222 (+3)88888888
C (+1)33333333 (+3)97898789
what I want in the end is
A (+2)44444444
B (+2)55555555
the end result should not have this rows
B (+1)22222222 (+3)88888888
C (+1)33333333 (+3)97898789
Both. In some rows, t2.number is number and in others t3.number is number.
The result of the union all in a single result set. The result set doesn't know the origin of the values in any particular column (although you could include another column with this information).
I need a select that return the values from T2.C from rows which combination of T2.A and T2.B doesn't exist in T1.
Something like:
select C from T2 where A,B not in (select A,B from T1)
Results from example must be:
C
--
y
z
Do you mean like
SELECT T2.*
FROM T2
LEFT OUTER JOIN T1
ON T2.ColumnA = T1.ColumnA
AND T2.ColumnB = T1.ColumnB
WHERE T1.ColumnA IS NULL;
If you do an outer join, you can easily notice a lack of a match (via nulls).
I have three tables table1, table2, table3. table1 has columns a, b. Table 2 has columns c, b, d. Table3 has columns d, e
Table 1 Table 2 Table 3
a b c, b, d d e
I need to match table1, table2 on b, and table2 and table3 on d. I need to display data in column 'a' in table 1 and if there are rows existing as a result of match in table2 and table3, then I need to display column 'e' in table 3 along with column 'a'. I can display null if no rows exist in the match between table2 and table 3.
Let me know for any clarifications or data. I'm using sqlite3, but exists clause works just like in sql it seems.
I attempted but could not write a proper query for this.
select a, e from table1 t1 join tablet2 t2 on t1.b = t2.b (and if exists?)
You need to use an outer join, like this:
select a, e
from table1 t1
left outer join table2 t2 on t1.b = t2.b
left outer join table3 t3 on t2.d = t3.d
where ...
If rows in any of table2 or table3 are missing, e will be null
SELECT A, E
FROM table1 t1
LEFT JOIN table2 t2 on T1.B=T2.B
LEFT JOIN table3 T3 on T3.D=T2.D
Is it possible to write a sql query where you know you have to use the left outer join..but cannot or are not allowed to use the "outer join" Key Word
I have two table sand want to get rows with null vaues from the left table ...this is pretty simple ...but am not supposed to use the key word....outer join....I need to right the logic for outer join myself
SELECT Field1
FROM table1
WHERE id NOT IN (SELECT id FROM table2)
SELECT Field1
FROM table1
WHERE NOT EXISTS (SELECT * FROM table2 where table2.id = table1.id)
This is something people do but it is deprecated and it does not currently work correctly (it sometimes will return a cross join instead of a left join) so it should NOT be used. I'm telling this only so you avoid using this solution.
SELECT Field1
FROM table1, table2 where table1.id *= table2.id
;WITH t1(c,d) AS
(
SELECT 1,'A' UNION ALL
SELECT 2,'B'
),t2(c,e) AS
(
SELECT 1,'C' UNION ALL
SELECT 1,'D' UNION ALL
SELECT 3,'E'
)
SELECT t1.c, t1.d, t2.c, t2.e
FROM t1, t2
WHERE t1.c = t2.c
UNION ALL
SELECT t1.c, t1.d, NULL, NULL
FROM t1
WHERE c NOT IN (SELECT c
FROM t2
WHERE c IS NOT NULL)
Returns
c d c e
----------- ---- ----------- ----
1 A 1 C
1 A 1 D
2 B NULL NULL
(Equivalent to)
SELECT t1.c, t1.d, t2.c, t2.e
FROM t1
LEFT JOIN t2
ON t1.c = t2.c
For SQL Server, you can just use LEFT JOIN - the OUTER is optional, just like INTO in an INSERT statement.
This is the same for all OUTER JOINs.
For an INNER JOIN you can just specify JOIN with no qualifiers and it is interpreted as an INNER JOIN.
This will give you all the rows in table A that don't have a matching row in table B:
SELECT *
FROM A
WHERE NOT EXISTS (
SELECT 1
FROM B
WHERE A.id = B.id
);
Returns all the matching rows from both tables:
SELECT a.*,b.* FROM table_a a, table_b b
WHERE a.key_field = b.key_field
Potential drawback is non-matches will be skipped.
I have the following query:
select A,
B
from table1
where A in (select c
from table 2
)
But, now I need to change this query and use exists instead of in, and it should give the same results.
My tables look like the following:
table1 table2
A B c
------ -----
1 x 1
2 y 3
3 z 4
4 w 7
5 a
1 b
How do I use the exists function?
You need to match the two columns that will be used in the exists together:
select
t1.a, t1.b
from
table1 t1
where
exists (select 1 from table2 t2 where t2.c = t1.a)
The reason why you have to do that, is because exists performs a semi-join on the table, and therefore, needs to have a join condition.
Changing the expression:
FROM Table1 WHERE a IN( SELECT c FROM Table2 )
To an EXISTS is a simple matter of:
Add a WHERE on the end of the internal SELECT
FROM Table1 WHERE a IN( SELECT c FROM Table2 WHERE )
Move the external match column (a) into the internal SELECT's WHERE clause
FROM Table1 WHERE IN( SELECT c FROM Table2 WHERE a )
Move the internal match column (c) to the WHERE clause, leaving a column placeholder (a constant or *):
FROM Table1 WHERE IN( SELECT * FROM Table2 WHERE a = c )
Change the IN to EXISTS:
FROM Table1 WHERE EXISTS( SELECT * FROM Table2 WHERE a = c )
To be safe add the table name onto the external column:
FROM Table1 WHERE EXISTS( SELECT * FROM Table2 WHERE Table1.a = c )
This will do it via direct inner join.
select
t1.a, t1.b
from
table1 as t1
inner join table2 as t2 on t1.a=t2.c