Same Data In Multiple Column Or Table - sql

I have 3 column In Table 1 in SQL A,B,C and User_Column in Table 2.
I want a user who is present in all three column of Table 1??? .
Experts please guide me I'm a beginner.

It seems you want an intersection of A, B, and C users:
select a as usr from table1
intersect
select b as usr from table1
intersect
select c as usr from table1;

Related

Almost equal table with different running time

I’m using oracle. I have two table A and B
Table A has 8000 rows and 5 five columns
Table B has 5500 rows and same 5 columns
All of 5500 rows in table B are contained in Table A and they are the same
I have a query like
With t1 as (select distinct(id) from table A/B)
,T2 as (select a.id, c.value, d.value from Table A/B a
Join table c c on “conditions”
Join table d d on “conditions”
) select * from t2
So the query with Table A works excellent but with Table B it freezes for eternity.
Data types and other properties are equal in table A and table A.
Where should i look for the problem?
I tried to explain plan but differences only in row “PX PARTITION HASH JOIN-FILTER” in Table A and “PX BLOCK ITERATOR ADAPTIVE” in table B

Comparing two columns in two different tables

I have two tables A and B where table A has a column X, table B has column Y. These columns contain account number information. I want to check whether the account number information in column A.X is present in B.Y.
Note: Both column X and Y can have duplicates because these are like composite primary keys.
How can I do solve this?
You can use an INNER JOIN, like this:
SELECT *
FROM table1 a
INNER JOIN table2 b
ON a.X = b.Y
OR
you can go for IF EXISTS,like this:
SELECT *
FROM table1 a
WHERE EXISTS(
SELECT 1
FROM table2 b
WHERE a.x=b.Y )
This will give you account information in table A that is also present in table B. The check is done by a correlated subquery that checks for each row in A whether the account information is present in B.
SELECT DISTINCT
X
FROM
A
WHERE
EXISTS (
SELECT
*
FROM
B
WHERE
B.Y=A.X
);
select distinct(X)
from A,B
WHERE A.X=B.Y
This will give you a list of account numbers in A that are not in B:
SELECT X FROM (
SELECT DISTINCT X FROM A
) A
LEFT JOIN B ON Y = X
WHERE Y IS NULL

Oracle SQL to subtract 2 values from different table joins

I am trying to subtract sequences MN_SEQ from Table C generated based on join with other tables.
Here is the problem.
Query 1 -
Select M_Seq from Table C, Table A, Table B where C.date_sk=A.MTH_END_DT
and B.Loan_seq=A.Loan_seq
Query 2 -
Select M_Seq from Table C, Table B where C.date_sk=B.ORIG_DT
I have to get difference between 2 M_SEQ generated from the result set of query 1 and Query 2.
Below is what i tried, but I am getting error.
select mn_seq -mn_seq from
((select mn_seq from Table C, Table A, Table B where B.MTH_END_DT=C.DATE_SK and B.LOAN_SEQ=A.LOAN_SEQ)a,
(select mn_seq from Table C , Table B where B.ORIG_DT=C.DATE_SK
)b)
T
Kindly provide inputs . I am not sure if this is the right way to do it. I tried just using "-" between queries but didnt work. Thanks!
Try this..
SELECT (SELECT mn_seq
FROM TABLE c, TABLE a, TABLE b
WHERE b.mth_end_dt = c.date_sk
AND b.loan_seq = a.loan_seq) -
(SELECT mn_seq FROM TABLE c, TABLE b WHERE b.orig_dt = c.date_sk)
FROM dual
I assume both the mn_seq are NUMBER and also your WHERE clause returns only one record in each of the inner queries.

Singe SQL Query to retrieve fields from 2 different table.

Trying to explain my Question:
Say, Table A contains ID,Number,Location.
Table B contains Option 1,Option 2.
I want to write a single query that would select ID, Number & Option 1 from the Table A and Table B.
(At present I am doing something like:
SELECT ID FROM [A]
SELECT Option 1 FROM [B]
)
You do this:
SELECT A.ID, A.Number, B.Option1
FROM TableA as A, TableB as B
WHERE A.id = B.id;
This Part sets an alias for the table so that you don't have type in the full table name all the time:
TableA as A
TableB as B
This part is the relationship between table A and B.
WHERE A.id = B.id;
Consider reading SQL table relationships http://net.tutsplus.com/tutorials/databases/sql-for-beginners-part-3-database-relationships/

Create table with duplicate records?

I have identified duplicate records in my staging environment
SQL> SELECT COUNT(*)
2 FROM MASTER_CHILD_MERGE A
3 WHERE A.CAM_ID IN (SELECT B.CAM_ID FROM CAM_DIM B);
SQL> 703022
For the analyst in our team, I want to create a table that has A and B's columns. I tried doing that
SQL> CREATE TABLE DUPES AS
2 SELECT * FROM NDS_MASTER_CHILD_MERGE A
3 WHERE A.CAM_ID IN (SELECT B.CAM_ID FROM CAM_DIM B);
but I realized that it will only gives me A's columns. How do I add B's columns as well? I am pretty sure it is an obvious solution but I am not seeing it...
I am on Oracle 10g.
Just join those two tables
create table dupes as
select *
from nds_master_child_merge a
join cam_dim b
on a.can_id = b.cam_id
SELECT A.*, B.* INTO DUPES
FROM NDS_MASTER_CHILD_MERGE AS A INNER JOIN CAM_DIM AS B ON A.CAM_ID = B.CAM_ID