Getting DISTINCT values from 3 columns - sql

I have 1 single table with 3 columns say col1,col2,col3
VALUES:
col1 with values (in individual 3 rows) as a, b, c
col2 with values (in individual 3 rows) as b, c, d
col3 with values (in individual 3 rows) as c, d, e
AIM:
To get distinct list to populate in html drop down box (i.e i want a,b,c,d,e) in alphabetical order ?

Pseudocode:
Select col1 from table
UNION
Select col2 from table
UNION
Select col3 from table

Related

How to select rows without duplicates when one column is different?

This is my table with 4 columns:
a b e d
a f c d
I want to get all 1st and 4th columns, so that the first two rows will be merged into one row in the example, since they are the same:
a d
a d
When I use the command:
select column1, column4 from my_table;
Would this automatically remove duplicates? If not, how to get distinct rows with only the 1 and 4 columns?
little confusing question.
Do you want to delete duplicate data or you want to just select non-duplicate data?
If you want to delete duplicate data then it will be like this -
insert overwrite my_table
select * from my_table
join (
Select col1||col2||col3||col4 key, row_number() over (partition by col1,col4 order by col1 ) as rn
from my_table) rs on rs.key = col1||col2||col3||col4 and rs.rn=1
If you want to select the unique col1 and col4 and dont want to change underlying data, you can simply fire
select distinct column1, column4 from my_table;

Insert multiple rows into a table from two different tables in oracle?

I'm trying to insert multiple rows into my table using select but I'm getting not enough values error.
My query:
Insert into c(x, y) select * from a union all select * from d;
table a and b contains 2 records each and table c has one record.
try like below by specifying both column names
Insert into c(x, y)
select col1,col2 from a
union all
select col1,col2 from d
for union all both tables have the same number of colums and their data type also need to be same
List the columns explicitly:
Insert into c (x, y)
select col1, col2
from a
union all
select col1, col2
from d;
If one of tables has only one column, then use a placeholder for the value:
Insert into c (x, y)
select col1, col2
from a
union all
select col1, NULL
from d;

Combining two columns in SQL

I want to combine two columns in such a way that the second column gets added below the first column.
For eg:
Col 1: A B C
Col 2: D E F
Result :
Col : A B C D E F
Do a UNION ALL to get the two columns as one single column:
select col1 from tablename
UNION ALL
select col2 from tablename
If you absolutely want col1 values before col2 values, wrap it up in a derived table and add an ORDER BY:
select col from
(
select col1 as col, 1 as ob from tablename
UNION ALL
select col2, 2 as ob from tablename
) dt
order by ob
SELECT CONCAT(col1, col2) AS col FROM table

sqlite: select all columns where one filed has max value over all columns

I have a table like this:
id int, col1 int, ...
Different rows can have col1 of same value.
Now I want to gather all rows where col1 has a the maximum value.
e.g. this table values
1 4
2 3
3 4
The query shall give my row 1 and 3
You can use subquery:
SELECT id, col1
FROM tab
WHERE col1 = (SELECT MAX(col1) FROM tab);
SqlFiddleDemo

insert into table A from table B, if Table B cols count equal to tabl A?

I have Table A with 10 columns and Table B has 3 columns only.I want insert Table B data into Table A with and remaining 7 fields with empty.
How can I do that?
If your table columns has default values then you have to use:-
insert into tableA select col1,col2,col3,'','','','','','','' from tableB;
for inserting empty values in remaining 7 columns.
use
insert into table A(coulmn1,column2,coulmn3) select * from B;
Insert into tableA(col1, col2, col3) select col1, col2, col3 from tableB
where col1=condition;
Tested in oracle