How to find a record from more than one table in SQL - sql

I have two table containing some information as below
ENRNO, PROGRAM, NAME, ADDRESS, AGE
I want to find data referencing ENRNO which is containing from one of the given table but I don't know which table have the information.
Please suggest.
Malay Barik

If ENRNO is unique in tables u may try use UNION
select * from t1
where ENRNO ='ENRNO1'
UNION select * from t2
where ENRNO ='ENRNO1'
else use DISTINCT and subquery
select DISTINCT * from (
select * from t1
where ENRNO ='ENRNO1'
UNION select * from t2
where ENRNO ='ENRNO1')
But goodest way for solving this is redesign(normalize) you DB.

Related

How to add union data in table?

Thanks in advance !!
I want to get below data in separate table with column how can we achieved this.
From my reading of your question, you would like the results of that SELECT statement put into a new table?
Firstly, I'm assuming your original SQL works as a SELECT statement - e.g., all those tables have the same structure. Note that you can simplify the unions, but I haven't done so here, to keep the key part of the answer (saving the data) as the main focus.
To save the data into another table, you can either create a table first and make that into an insert, or just use 'SELECT INTO' within the main SELECT.
If you are happy with the columns being automatically created, the 'SELECT INTO' version will create columns (e.g., you do not need to specify the columns in a CREATE TABLE statement). However, when you run the SELECT INTO, it does create the table. Therefore if you want to insert further values, you need to specify the column list (or have matching column lists).
SELECT INTO version
select *
INTO #Temp -- Added This row
from
( select * from #OneyearExpiry
union all
select * from #OtherYearExpiry
) A
except
select * from
( select * from #ONEYRCON
union all
select * from #OTHERYRCON
) B
INSERT INTO version
CREATE TABLE #Temp (<your fields here to match the SELECT statement>)
INSERT INTO #Temp
select * from
( select * from #OneyearExpiry
union all
select * from #OtherYearExpiry
) A
except
select * from
( select * from #ONEYRCON
union all
select * from #OTHERYRCON
) B
Set operators are evaluated from top to bottom so there only needs to be 1 subquery. Something like this
select ab.* into #Temp
from (select * from #OneyearExpiry
union all
select * from #OtherYearExpiry
except
select * from #ONEYRCON
except
select * from #OTHERYRCON) ab;

how to merge/Combine two tables in oracle

Hello I want merge two tables in oracle like below -
I tried below answer from stack overflow but not getting answer like Output table
Combining Two Tables With Oracle SQL
Please try with below query:
select * from (
select id, name, rate,total from table1
union all
select id, name, rate,total from table2
) as t
order by t.id asc
use union all
select id, name, rate,total from table1
union all
select id, name, rate,total from table2

How to return unique records between two tables without using distinct and union?

I need to return the unique records between two tables. Ideally, an UNION would solve my problem but both tables contain an object field which gives me an error(cannot ORDER objects without MAP or ORDER method) when I do UNION/distinct.
So, I was wondering if I can do a UNION ALL(to avoid the error) to get all the records first then do something to return only the unique records from there. I tried analytic function combined with the UNION ALL query but no luck so far.
Select * from Table1
union all
Select * from table2
Any help? Note:I need to return all fields.
I actually solved the problem using analytic function+row_num. The query will choose the first record for each set of duplicates hence returning only the unique records.
select * from
(
select ua.*,row_number() over (partition by p_id order by p_id ) row_num from
(
select * from table1
union all
select * from table2
)ua
) inner
where inner.row_num=1
How about this :
SELECT DISTINCT A.* FROM
(
Select * from Table1
union all
Select * from table2
) A;
(or)
SELECT col1,col2,col3...coln FROM
(
Select col1,col2,col3...coln from Table1
union all
Select col1,col2,col3...coln from table2
) A
GROUP BY A.col1,col2,col3...coln;
UNION ALL will give duplicate values as well .. instead use UNION and see if you are facing the error

Count rows in more than one table with tSQL

I need to count rows in more than one table in SQL Server 2008. I do this:
select count(*) from (select * from tbl1 union all select * from tbl2)
But it gives me an error of incorrect syntax near ). Why?
PS. The actual number of tables can be more than 2.
In case you have different number of columns in your tables try this way
SELECT count(*)
FROM (
SELECT NULL as columnName
FROM tbl1
UNION ALL
SELECT NULL
FROM tbl2
) T
try this:
You have to give a name to your derived table
select count(*) from
(select * from tbl1 union all select * from tbl2)a
I think you have to alias the SELECT in the FROM clause:
select count(*)
from
(
select * from tbl1
union all
select * from tbl2
) AS SUB
You also need to ensure that the * in both tables tbl1 and tbl2 return exactly the same number of columns and they have to be matched in their type.
I don't like doing the union before doing the count. It gives the SQL optimizer an opportunithy to choose to do more work.
AlexK's (deleted) solution is fine. You could also do:
select (select count(*) from tbl1) + (select count(*) from tbl2) as cnt

SQL return all id's from 1 table and only ones that don't match from another

I have 2 tables: #tmptable1 and #tmptable2. These tables have an ID to uniquely identify each record.
I want to return all records from table 1, but I only want to return the records from table 2 that aren't in table 1
I want this done in 1 query.
Thanks!
SELECT * FROM '#tmptable1'
UNION ALL
SELECT * FROM '#tmptable2' WHERE
ID NOT IN (SELECT ID FROM #tmptable1 WHERE ID IS NOT NULL)
If all the other fields and data of the two tables is identical you could do this:
SELECT * FROM #tmptable1
UNION
SELECT * FROM #tmptable2
UNION without the ALL modifier removes duplicates.
I don't quite understand what is it that you want to get in the results, but UNION selects distinct values, so you won't have duplicate values (values from #tmptable2 that already exist in #tmptable1).
SELECT * FROM #tmptable1
UNION
SELECT * FROM #tmptable2
Anyways, these records are records from #tmptable2 that aren't in #tmptable1.
SELECT * FROM #tmptable2
EXCEPT
SELECT * FROM #tmptable1
SELECT ID FROM #tmptable1
UNION
SELECT ID FROM #tmptable2
The UNION operator will automatically remove duplicates.