Join two tables without relationship, add 1 to many - sql

I've been frequenting this site for awhile now but this time, I didn't find any answers around this question so here goes.
I have two tables that do not have any PK/FK or relationship otherwise. What I am trying to do is the following.
Table 1
Column A
A
B
C
Table 2
Column B
1
2
3
Desired Result:
Combined Table
Column A Column B
A 1
A 2
A 3
B 1
B 2
B 3
C 1
C 2
C 3
Pretty much, for every value in the first table, append the full list of the second table until there's no more.
I hope this makes sense on what I am trying to achieve.
Thank you!

You need to use cross join:
SELECT ColumnA, ColumnB
FROM table1
CROSS JOIN table2
ORDER BY ColumnA;

As alluded to in other answers, you'll want to use CROSS JOIN which will give you the Cartesian product of the two tables:
SELECT t1.columnA, t2.columnB
FROM t1 CROSS JOIN t2;
You might see this expressed in older code like this:
SELECT t1.columnA, t2.columnB
FROM t1, t2;
"Cartesian product" is a term from mathematics: the Cartesian product of two sets X and Y (tables in relational databases are actually sets) is the set of all ordered pairs (x,y) where x in X and y in Y. In your case you are getting the Cartesian product of the sets all values of column A in t1 and all values of column B in t2.
Hope this helps.

This should do it:
SELECT ColumnA, ColumnB
FROM TableA
CROSS JOIN TableB

SELECT *
FROM Table1
CROSS JOIN Table2

Related

Exclude one item with different corelated value in the next column SQL

I have two tables:
acc_num
ser_code
1
A
1
B
1
C
2
C
2
D
and the second one is:
ser_code
value
A
5
B
8
C
10
D
15
I want to exclude all the accounts with the service codes that they have value of 10 or 15.
Because my data set is huge, I want to use NOT EXIST but it just excludes combination of acc_num and ser_code.
I want to exclude the acc_num with all of it's ser_code, because on of it's ser_code meats my criteria.
I used:
select acc_num, ser_code
from table 1
where NOT EXIST (select 1
FROM table 2 where acc_num = acc_num and value in (10, 15)
out put with above code is:
acc_num
ser_code
1
A
1
B
Desire out put would be empty.
here you are
select t1.acc_num,t1.ser_code from table1 t1, table2 t2
where (t1.ser_code=t2.ser_code and t2.value not in (10,15))
and t1.acc_num not in
(
select t3.acc_num from table1 t3,table2 t4
where t1.acc_num=t3.acc_num and t3.ser_code=t4.ser_code
and t4.value in (10,15)
) ;
This could be achieved in many ways. However using NOT EXISTS is the best option. The problem with your query is for acc_num 1, there are ser_code that does not have value as 10, 15. So you will get A and B in result.
To overcome that you must pull acc_num inside the sub-query
Query 1 (using NOT EXISTS):
As you can see in the below query, I have included acc_num inside sub-query, so that the filter works properly,
SELECT DISTINCT a.acc_num, a.ser_code
FROM one as a
WHERE NOT EXISTS
(
SELECT DISTINCT one.acc_num
FROM two
INNER JOIN one
ON one.ser_code=two.ser_code
WHERE value IN (10,15) AND a.acc_num=one.acc_num
)
Query 2 (using LEFT JOIN):
NOT EXISTS often confusing due to it's nature (super fast though). Hence LEFT JOIN could also be used (expensive than NOT EXISTS),
SELECT DISTINCT a.acc_num, a.ser_code
FROM one as a
LEFT JOIN
(
SELECT DISTINCT one.acc_num
FROM two
INNER JOIN one
ON one.ser_code=two.ser_code
WHERE value IN (10,15)
) b
ON a.acc_num=b.acc_num
WHERE b.acc_num IS NULL
Query 3 (using NOT IN):
NOT IN would also achieve this with comprehensive query but expensive than both of the above methods,
SELECT DISTINCT a.acc_num, a.ser_code
FROM one as a
WHERE a.acc_num NOT IN
(
SELECT DISTINCT one.acc_num
FROM two
INNER JOIN one
ON one.ser_code=two.ser_code
WHERE value IN (10,15)
)
All 3 would yield same result. I would prefer to go with NOT EXISTS
See demo with time consumption in db<>fiddle

SQL subquery to get all combination in two different selected sets

I have question to regarding a query I am writing:
I have two different (columns) data sets which I want to combine the result of, for instance:
Set 1: Set 2:
1 A
2 B
3 C
I want to create a query with the result:
resultset:
1 A
1 B
1 C
2 A
2 B
2 C
3 A
3 B
3 C
Is this possible using a JOIN or UNION?
So I get all possible combinations between the different rows?
Thank you for helping out!
What you want to do is a cartesian join
SELECT
S1.col1,
S2.col2
FROM
S1,S2
Please see the example in the fiddle
You are looking for cross join:
select t1.col1, t2.col1
from table1 t1 cross join
table2 t2
order by t1.col1, t2.col1;

Natural join with more than one common attribute in two tables [duplicate]

This question already has answers here:
Difference between natural join and inner join
(12 answers)
Closed 1 year ago.
I can understand how natural join works when the two tables have only one common attribute. What if they have two ones?
Table 1 have 3 attributes: A, B, C
Table 2 has 3 attribute: A, B, D
First two rows in table 1:
1 2 3
4 5 6
First two rows in table 2:
1 3 4
8 5 8
What is the result of a natural join between the two tables?
In the case of your two records above, nothing will be matched. It will look for the case when A & B in the left table match A & B in the right table.
Natural Join is a variant of INNER JOIN where join condition is implicit on common column from both tables. In your case, the query in Natural Join can be written as below which will not return any result since it will try to match both A and B
select *
from table1
natural join table2
The same can be written in Inner Join like below
select t1.*
from table1 t1
inner join table2 t2
on t1.a = t2.a and t1.b = t2.b
See for yourself Fiddle Demo
Actually Natural Join is something (Cross product + some condition)
The short form of natural join is:
Select * from Table_A NATURAL JOIN Table_B
So, an alternative way of writing this would be:
Select * from Table_A , Table_B where (Table_A.id = Table_B .id)
This is equivalent to Natural Join
(Table_A , Table_B) symbolises cross product
(Table_A.id = Table_B .id) symbolises common condition
I will try to explain the difference between NATURAL JOIN and INNER JOIN on the basis of use-case.
We have two tables T1(A, B, C, D) & T2(B, C, X, Y), where the alphabets (A, B, C, D, X, Y) represent fields/attributes.
CASE 1: say I have to pull all rows which have a common entry in 'C' across both tables.
query: SELECT * FROM T1 INNER JOIN T2 ON T1.C=T2.C;
EXPLAINATION ON WHY NATURAL JOIN WILL NOT WORK IN THE ABOVE CASE --
say we use NATURAL JOIN.
SELECT * FROM T1 NATURAL JOIN T2;
We know that, T1 & T2 have two similar attributes/fields ('B' and 'C')
so, NATURAL JOIN will look for all such entries where
(T1.B=T2.B) AND (T1.C=T2.C)
Only the rows which satisfy the above condition, will be included in the result set.
Whereas, we only need the rows to have a common entry for field 'C' for our purpose.
I am studying for the certification and I got this doubt also.
NATURAL JOIN ALWAYS compare the similar columns in the tables.
If you have one pair of similar columns, it will compare and it'll show the matches. Chances are higher. Now, if you have 2 pairs, it will compare and it'll bring less results. the content has to be equal. Create a table yourself and do the test

SQL - Join two query in same query with two rows

i have one query that i need include the status in another row, i think that it's easy but i'm not remember it..
status
A
B
C
D
job
1
2
3
i want a output like:
job status
1 A
1 B
1 C
1 D
2 A
2 B
2 C
2 D
...
Can anyone help?
that is called CROSS JOIN
SELECT *
FROM status CROSS JOIN job;
I'm just guessing your table layout here because you did not give it in your question.
SELECT t1.job, t2.status
FROM t1 INNER JOIN t2 ON t1.something = t2.something
Note that if you had given the table structure something, t1 and t2 would actually be the real names.
If there is no particular key and you just want to match every value of t1.job to every value of t2.status then this will do the job:
SELECT t1.job, t2.status
FROM t1, t2
You might want to read a little bit about JOINs in SQL, here's a good resource:
http://www.w3schools.com/sql/sql_join.asp
As per your case, you can simply do:
SELECT * FROM job, status
Assuming that those are table names and these are the only columns in there.
EDIT:
Link with explanation on how CROSS JOIN works as well:
http://www.w3resource.com/sql/joins/cross-join.php

Select from two tables which have no relation

I don't know if it is possible or not. If possible, please help me to resolve this.
I have two tables, table1 and table2:
table1 table2
column1 column2 column3 column4
1 2 A B
3 4 C D
There is no relation between table1 and table2. I want to execute a query so that my output looks like this:
Output table:
column1 column2 column3 column4
1 2 A B
1 2 C D
3 4 A B
3 4 C D
Can anyone please tell me how can I achieve this? We are using SQL Server 2005.
Thanks,
Kartic
This is called a cross join, which produces a Cartesian product of all the records in each of the tables. The best way to do this is explicitly, with the cross join syntax:
select t1.*, t2.*
from table1 t1 cross join
table2 t2;
Note that if either table is empty, then you will not get any rows back.
You need to use cross join.
select t1.*,t2.* from table1 t1
Cross Join table2 t2;
Like this?
SELECT * FROM table1 CROSS JOIN table2
It's called a cross join, or cartesian product.
You can add additional filtering or join conditions using WHERE.