Join two tables to get common data based on a column - sql

I have two tables:
Table1 with columns colA, colB, colC
Table2 with columns colX, colY, colZ
I'm trying to get all rows from Table1 which have colC values that match Table2 on colZ.
I tried the following:
select Table1.colA,Table1.colB,Table1.colC
from Table1 inner join Table2 on Table1.colC = Table2.colZ
This does not seem to work as the result of the query had 20 times the number of rows present in Table1.
Any help is sincerely appreciated.

You can use EXISTS like this.
select Table1.colA,Table1.colB,Table1.colC from Table1
WHERE EXISTS (SELECT 1 FROM Table2 WHERE Table1.colC = Table2.colZ)

Three options:
Use INNER JOIN with DISTINCT
SELECT DISTINCT Table1.colA,
Table1.colB,
Table1.colC
FROM Table1
INNER JOIN Table2 ON Table1.colC = Table2.colZ
Use EXISTS
SELECT Table1.colA,
Table1.colB,
Table1.colC
FROM Table1 WHERE EXISTS (SELECT 1 FROM Table2 WHERE ColZ = ColC)
Use IN
SELECT Table1.colA,
Table1.colB,
Table1.colC
FROM Table1
WHERE ColC IN (SELECT ColZ FROM Table2)

Use DISTINCT in your query:
SELECT DISTINCT
Table1.colA,Table1.colB,Table1.colC
FROM Table1
INNER JOIN Table2
ON Table1.colC = Table2.colZ

Related

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.

SQL Query, how to get data from two tables

Table 1:
ID (unqiue), Name, Address
Table 2:
RecordId, ID (key of table 1), Child name
In one query, I want to retrieve all rows of Table 1 with one additional column which will be the count of all record in table 2 from ID (that is number of children for each ID in table 1). Can't figure out how to format a query to retrieve this data.
Simply Join and apply count
select T1.*, COUNT(T2.RECORDID)AS T2COUNT from Table1 T1
INNER JOIN TABLE2 T2 ON T1.ID= T2.ID
--LEFT JOIN TABLE2 T2 ON T1.ID= T2.ID --if you need 0 child records (from commets by #Cha)
GROUP BY T1.ID , T1.Name, T1.Address
The correct way of doing this will be with a OUTER JOIN:
SELECT a.ID, a.Name, a.Address, b.cnt
FROM Table1 a
LEFT OUTER JOIN
(SELECT ID, count(*) cnt from Table2 GROUP BY ID) b
ON a.ID = b.ID
The incorrect way will be with a help of a correlated sub-query:
SELECT a.ID, a.Name, a.Address,
(SELECT count(*) FROM Table2 b WHERE b.ID = a.ID) as cnt
FROM Table1 a
Here is a discussion about correlated subqueries vs OUTER JOINs, if you are interested
Group by table1 fields and count total records in table2:
here T1 alias of table1 and T2 alias of table2.
select T1.ID, T1.Name, T1.Address, count(T2.ID) as total_records
from table1 as T1
left outer join table2 as T2 on T2.ID=T1.ID
group by T1.ID, T1.Name, T1.Address

Join then Distinct Count

. I need to find only Employees that are type A. I know this can be done with a JOIN. Then I need to get a count of all distinct EMP_ID for each Region.Also note this will be done in Oracle
These are basic things, you need join and condition on the table containing type = 'A':
select count(distinct emp_id)
from table1 t1
join table2 t2 on t1.job_code = t2.job_code
where t2.type = 'A'
group by t1.region
Could use a CTE.
With someCte
as
(
Select * from table1 t1
Inner join table2 t2 on t1.Job_code = t2.Job_code
where t1.type like 'A' and t2.type like 'A'
)
select distinct emp_id from someCte

sql query join multiple tables on a case when column

i have sql like this:
select case when.....end as colA, table2.col3
from table1
join table2 on table2.colB = colA
in the case when statement, there are some other tables columns, is this doable? I have got error saying that colA is not valid.
Column aliases are not understood in the where or on clauses.
You can do this with a subquery, assuming the case only involves columns from table1:
select case when.....end as colA, t2.col3
from (select table1.*, (case when.....end) as colA
from table1
) t1 join
table2 t2
on t2.colB = t1.colA;
Otherwise, you could put the case statement in the on clause:
select case when.....end as colA, t2.col3
from (select table1.*, (case when.....end) as colA
from table1
) t1 join
table2 t2
on t2.colB = ( case when.....end );

SQL to select rows where at least one row in a group, has a corresponding entry in another table

I have two tables
Table1: FieldA, FieldB
Table2: FieldA
Table1 is grouped by FieldB, and FieldA is the link between the two tables.
For each grouping in Table1, if all rows in that group do not have an entry in Table2, then return no rows corresponding to this group. If at least one row in the group has an entry in Table2, then return all rows in the group.
Is this kind of query possible?
Thanks
Simon
it sounds like you need to do a simple inner join:
SELECT t1.* FROM Table1 t1 INNER JOIN Table2 t2 ON t1.FieldA=t2.FieldA;
I'm unclear from your question if the values in Table2 are unique. If they are not unique then this subquery might work better:
SELECT * FROM Table1 WHERE FieldA in (SELECT distinct(FieldA) FROM Table2);
If I understand your problem correctly, this JOIN would to do it;
SELECT DISTINCT t1a.*
FROM Table1 t1a
JOIN Table1 t1b
ON t1a.FieldB = t1b.FieldB
JOIN Table2 t2
ON t2.FieldA=t1b.FieldA;
Demo here.
SELECT *
FROM Table1
WHERE FieldB IN (
SELECT t1.FieldB
FROM Table1 t1
JOIN Table2 t2
ON t1.FieldA = t2.FieldA
GROUP BY t1.FieldB
)