SQL subquery to get all combination in two different selected sets - sql

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;

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

Join two tables without relationship, add 1 to many

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

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.

SQL Getting Data with extra row

I have two tables with columns of my interests as Table1.Column1 and Table2.Column2
Table1 is Kind of a Group Table and Table2 is items table. The join query of these two tables gets the data in the followinf format
Column1 Column2
A 1
A 2
B 1
B 2
B 3
What I want is to get data in the following format:
Column1 Column2
A 0
A 1
A 2
B 0
B 1
B 2
B 3
i.e. getting extra 0 for each group at the start each time. The 0 does not exits in the database.
Does anyone know how to achive this in SQL?
Many Thanks,
This is one way to do it.
SELECT DISTINCT Column1, [Column2] = 0
FROM (
YourOriginalQuery
) q
UNION ALL
YourOriginalQuery
Most likely, there are better solutions by incorporating this requirement into your original query. If you post your query, we can come up with better alternatives.
Or something like:
select C.CategoryId, drv.CategoryGroupId from Category as C
cross join (
select 0 as CategoryGroupId
UNION
select CG.CategoryGroupId from CategoryGroup as CG
)drv order by CategoryId, CategoryGroupId