How to make a proper cross join - sql

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;

Related

How to use Substring in left outer join

I tried to join these 2 tables with ALTERNATE_ID's using substring in left outer join but it doesn't work. Can someone suggest me how to do it.
Here Table1 is master table,
SELECT
tab1.USER_NAME,
tab2.ALTERNATE_ID as 'Contract_no'
FROM TABLE1 tab1
LEFT OUTER JOIN TABLE2 tab2 ON Table2.ALTERNATE_ID = SUBSTRING(tab1.ALTERNATE_ID,0,CHARINDEX('/',tab1.ALTERNATE_ID)
TABLE1:
ALTERNATE_ID 100-0000053-001/0001
TABLE2
ALTERNATE_ID 100-0000053-001
You have to use substring from first char to position of \ - 1
But if you have to join based on part of column, then you should created index on the substring part.
SELECT
tab1.USER_NAME,
tab2.ALTERNATE_ID as 'Contract_no'
FROM TABLE1 tab1
LEFT OUTER JOIN TABLE2 tab2
ON Table2.ALTERNATE_ID
=
SUBSTRING(tab1.ALTERNATE_ID,1,CHARINDEX('/',tab1.ALTERNATE_ID-1)
You can also use SUBSTRING_INDEX to split the string like this:
SELECT tab1.USER_NAME, tab2.ALTERNATE_ID as 'Contract_no'
FROM TABLE1 tab1
LEFT OUTER JOIN TABLE2 tab2
ON Table2.ALTERNATE_ID = SUBSTRING_INDEX(tab1.ALTERNATE_ID, "/", 1);
You can use OUTER APPLY in the following way:
SELECT
tab1.USER_NAME,
tab2.ALTERNATE_ID as 'Contract_no'
FROM TABLE1 tab1
OUTER APPLY (Select ALTERNATE_ID
From TABLE2
Where ALTERNATE_ID=SUBSTRING(tab1.ALTERNATE_ID,1,CHARINDEX('/',tab1.ALTERNATE_ID)-1)) As tab2
If you use PATINDEX, then it's easy to get the position of the digit before the slash.
SELECT *
FROM TABLE1 t1
LEFT JOIN TABLE2 t2
ON t2.ALTERNATE_ID = LEFT(t1.ALTERNATE_ID, PATINDEX('%[0-9]/%', t1.ALTERNATE_ID))
Or looking at it differently, the one in Table1 is like the one in Table2
SELECT *
FROM TABLE1 t1
LEFT JOIN TABLE2 t2
ON t1.ALTERNATE_ID LIKE t2.ALTERNATE_ID+'/%'
Demo on db<>fiddle here

How can i find different of this sql 2 rows

i want to count sum of this after minus please help me
SELECT t1.*
FROM table1 t1
MINUS
SELECT t2.*
FROM table2 t1
JOIN customers c ON t1.number = t2.number;
Another way is using CTE
With cte as (SELECT t1.*
FROM table1 t1
MINUS
SELECT t2.*
FROM table2 t1
JOIN customers c ON t1.number = t2.number)
Select count(*) from cte;
one way is :
select count(*) from (
SELECT *
FROM table1 t1
MINUS
SELECT *
FROM table2 t1
JOIN customers c ON t1.number = t2.number
) t
I suspect that you really want:
select count(*)
from (select number
from table1
minus
select number
from customers
) t;
It seems really odd that you would have two tables (table1 and table2 in your question) with exactly the same columns.
In addition, this does something useful, which is to count the number of numbers in table1 that are not customers.

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 Query comparing two table

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
);

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