Oracle Query comparing two table - sql

I have two tables T1 and T2.
T1 having records like A,B,C,D
T2 having records like A,B,D,E
Now out of the query should be C when we compare both tables as C is not available in T2
Please help here..

In Oracle, you can use the minus set operator:
select t1.*
from t1
minus
select t2.*
from t2;

You should be able to just use an inner join, this will return everything that both tables have in common, so:
SELECT T1.* FROM T1 INNER JOIN T2 ON T1.id = T2.id

Another way of achieving this would be:
SELECT
C1
FROM
T1
WHERE
C1 NOT IN (
SELECT
C1
FROM
T2
);

Related

create a sql join with more columns

i want create a sql statemnt (in PL SQL Developer) with a join with comma seperated?
SELECT * FROM TABLE1 t1 JOIN TABLE2 t2 ON t1.tab_id, second_id = t2.tab_id, second_id;
I always get a ORA-00920 Exception. If i change it to two Rows:
t1.tab_id = t2.tab_id AND t1.second_id = t2.second_id;
Then i get rows.
Can some say me if i can use the first step with coma seperated columns?
Greetz
You need a valid condition:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
ON t1.tab_id = t2.tab_id AND t1.second_id = t2.second_id;
I think Oracle will also let you do:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
ON (t1.tab_id, t1.second_id) in ( (t2.tab_id, t2.second_id) );
Or even:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
USING (tab_id, second_id);
This works because the JOIN keys have the same names in the two tables.

Oracle SQL: JOIN between tables but just show columns from tablename1

Is there a quick way of doing a JOING between two tables (tablename1 and tablename2) and just show the columns of the tablename1.
This is an example:
select * from tablename1 t1
left join tablename t2
on t2.id=t1.id
Thank you
Just use the alias with the *
select t1.*
from tablename1 t1
left join tablename2 t2 on t2.id=t1.id;
But that doesn't make sense because it will return the same result as
select t1.*
from tablename1 t1
Edit jarlh is right: the second query will only return the same result if id is unique in tablename2
You could specify t1.* in you select list:
SELECT t1.* -- here!
FROM tablename1 t1
LEFT JOIN tablename t2 ON t2.id = t1.id

PROC SQL select all of 2 tables and specifics in another

I have code like this
PROC SQL;
CREATE TABLE my_table as
SELECT DISTINCT(
t1.*,
t2.*,
t3.value3 )
FROM table1 as t1
INNER JOIN table2 as t2
ON t1.value = t2.value
INNER JOIN t3.value as t3
ON t1.value2 = t3.value2
;
quit;
But SAS sees t1.* as a format. what can I do about this? Is there a way to express this without building 2 separate tables first?
Distinct
needs not the parenthes and it applies to all items listed under
Select
I suggest you to consider the use of
Coalesce
or
Coalescec ( for character)
For the duplicated variables existed at both t1 and t2. Cartisan product would return some duplicated attributes that is not preferable.
An alternative would be to separate the sql statements and it would be much clear
Proc sql;
Select distinct * from table1 as L1
Inner join
Select distinct * from table 2 as L2
On L1.value =L2.value
Inner join
...
You may have another problem with the code.
By using t1.* and t2.* you end up with two variables named value which SAS will not condone.
I'm afraid the best syntax you can use in SAS would be:
PROC SQL;
CREATE TABLE my_table as
SELECT DISTINCT(
t1.*,
t2.value2,
t3.value3 )
FROM table1 as t1
INNER JOIN table2 as t2
ON t1.value = t2.value
INNER JOIN t3.value as t3
ON t1.value2 = t3.value2
;
QUIT;

How to make a proper cross join

I want to cross join the two table
Query
Select * from table1 cross join select * from table2
Above query is showing error.
What wrong in my query
Reference
The result set of the Cross Join is the number of rows in the first table multiplied by the number of rows in the second table
SELECT T1.Columns1, T1.Column2, T2.ColumnName FROM Table1 T1
CROSS JOIN Table2 t2
You must use select like this : SELECT T1.FIELD1, T2.FIELD2 FROM TABLE1 AS T1 CROSS JOIN TABLE2 AS T2 - FIELD[X] FOR EXAMPLE ONLY
Try this:
SELECT * FROM table1, table2
or this:
select * from table1 CROSS JOIN table2;

query in sql server 2005

there are two tables ...know i need
1st condition:
all the records in table
2 nd condition:
In table2 i need only records which have data
...i want one query for the aove two conditions...
SELECT
*
FROM Table1 t1
INNER JOIN Table2 t2 on t1.PK = t2.FK
This will return all rows in table1 that have at least one corresponding row in table2
But if you want all rows from t1 no matter what then this may be what you want
SELECT
*
FROM Table1 t1
LEFT JOIN Table2 t2 on t1.PK = t2.FK
Finally, As I dont know the structure in place perhaps table1 and table2 have similar structures. If this is true perhaps you may want a union of the two
SELECT
*
FROM Table1 t1
UNION ALL
SELECT
*
FROM Table2 t2