oracle sql row count affected by select clause - sql

While working on a oracle SQL , noticed that adding a column to the selected columns increases the total rows in the result. The query is using multiple subqueries declared using WITH. The join in the final query is a left join. Why is the row count getting impacted?

The only way I can think of to increase the number of result rows by adding a column to the SELECT clause is when using SELECT DISTINCT.
SELECT DISTINCT removes duplicates from the results, so
col1 col2
a b
a b
a c
a c
becomes
col1 col2
a b
a c
When adding a column
col1 col2 col3
a b d
a b e
a c f
a c f
becomes
col1 col2 col3
a b d
a b e
a c f
for instance, which is one row more than before.

Related

Removing duplicates in one column

I'm working in SQL and have the following table (named table1):
col1
col2
D
A
E
A
F
A
G
B
H
B
I
B
J
C
K
C
L
C
and I would like it to return:
col1
col2
D
A
G
B
J
C
I am looking for an SQL query that returns the first value in col1 for each distinct value in col2. Basically I am looking to only retrieve one value for each distinct value in col2 and remove any duplicates in col2. I am looking for a query that I would be able to apply to other tables where I might not know the positions of the distinct values. Thanks for in advance for any help!
Try the following:
SELECT MIN(col1) col1, col2 FROM table1
GROUP BY col2

How to combine two tables with different number of columns SQL

I have two tables A and B which look like this:
Table A
col1 col2 ID
--------------
a b 1
c d 2
Table B
col3 col4 ID
--------------
x t 1
y u 1
z o 2
m n 2
I want to create this new table:
Table C
col1 col2 col3 col4 ID
-------------------------
a b x t 1
a b y u 1
c d z o 2
c d m n 2
The ID's in A are not the unique ID's of those elements in that list. I just need to add a few more properties from their "parent" table(B) in this new table. I know there will be a lot of repetitions but I don't mind that for now.
I tried this following statement:
INSERT INTO C(SELECT A.*, B.col1, B.col2 from A LEFT OUTER JOIN B ON (B.ID = A.ID));
But it is resulting in something I did not expect. The row count of C must be the same as the row count of B. However when I used this SQL query, the resulting tables row count was even more than the total row count of A and B. I'm a beginner in using SQL I would appreciate any help.
A basic JOIN will do:
select
a.col1,
a.col2,
b.col3,
b.col4,
a.id
from tablea a
join tableb b on a.id = b.id
If I got you right your tables do not have any relation to each other.
So in this case you should be able to do something like this?
Select * from table1, table2
You can also combine this with an insert so you should get your new table

SQL query with both table columns-Oracle

There are two tables named, for an example A and B.
A and B has a unique column named key_ref. and key_ref is the primary key of both tables
I need to find records in A but not in B. Therefore I wrote a query like,
SELECT a.*,b* from A a,B b WHERE key_ref NOT IN (SELECT key_ref from B)
The issue with this is, I do not get the empty columns of table B for the result. My result should include all the columns of A and B.
If I write my query like following my results get wrong. Is there any way , where I can achieve this even with a join condition.
SELECT a.* from A a WHERE key_ref NOT IN (SELECT key_ref from B)
Please refer following example.
Table A Table B
key ref col1 key ref col2
A aaa A aaa
B bbb B bbb
C ccc C ccc
D ddd
My answer should be,
key ref col1 col2
D ddd
If I understand your request:
You can use a LEFT OUTER JOIN operation so you'll get all rows in A not present in B (with condition in WHERE b.key_ref IS NULL)
Try this:
SELECT *
FROM a
LEFT OUTER JOIN b
ON a.key_ref = b.key_ref
WHERE b.key_ref IS NULL
Is this what you need:
select * from A
except
select * from B
?
If the second table tableB doesn't have a record associated with first table, then how would you display record from second table tableB which are not exists in the first table tableA.
So, the one way is to include NULL as columns ref. to tableB like that :
select a.*, null col2
from tableA a
where not exists (select 1 from tableB b where b.key_ref = a.key_ref);

Count of Intersection Returns Unexpected Value

I have to tables, say A and B and a common column X which isn't nullable.
Query 1:
SELECT COUNT(*)
FROM A
WHERE A.X in
(SELECT distinct(B.X) FROM B)
Query 2:
SELECT COUNT(*)
FROM B
WHERE B.X in
(SELECT distinct(X) FROM A)
Query 3:
SELECT COUNT(*)
FROM A, B
WHERE A.X=B.X
Query 1 results 5990. Query 2 and 3 results 6222. Removing distinct or checking distinct count on top doesn't change the results. Can someone explain why the results aren't the same for all queries as they all return intersection count?
Assume A has values
A
B
C
Assume B has values
A
B
C
D
E
C
in this case A would have a count of 3 (QRY1)
while B would have a count of 4 since c is repeated (QRY2)
when you use a join C will match will all values of C has more duplicates than A. A may have duplicates but fewer of them. 4 again (QRY3)

Comparing two tables in SQLite

I have two tables and want to compare rows on sqlite like this
table1 table2
field1 field1
a a
b d
c f
d g
e
f
g
h
i
and I want to produce result like this
result_table
field1
b
c
e
h
i
How is the syntax in sqlite?
Thanks
SELECT DISTINCT Field1
FROM Table1
WHERE Field1 Not IN
(SELECT DISTINCT Field1 FROM Table2)
SELECT columns1 FROM table1 EXCEPT SELECT columns2 FROM table2;
The SQLite EXCEPT clause returns all rows from the left SELECT statement that are not in the result of the second SELECT statement. The number of columns selected must be the same in both SELECT statements.
This works fine for small to medium size tables. Avoid for tables with millions of lines.
See Compound Select Statements and the documentation of the SQLite SELECT statement.