sqlite combine select all from multiple columns - sql

How to combine many select queries into 1 statement in SQLite?
SELECT * FROM Table1 WHERE condition1
SELECT * FROM Table2 WHERE condition2
SELECT * FROM Table3 WHERE condition3
...

Use UNION to combine the results of the three queries.
UNION will remove duplicate leaving on unique rows on the final result. If you want to keep the duplicate rows, use UNION ALL.
SELECT * FROM Table1 WHERE condition1
UNION
SELECT * FROM Table2 WHERE condition2
UNION
SELECT * FROM Table3 WHERE condition3
caveat: the number of columns (as well as the data type) must match on each select statement.
UPDATE 1
based on your comment below, You are looking for JOIN,
SELECT a.*, b.*, c.*
FROM table1 a
INNER JOIN table2 b
ON a.ColName = b.ColName
INNER JOIN table3 c
ON a.ColName = c.ColName
-- WHERE .. add conditions here ..
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins

Your can also query also like
SELECT * FROM users where bday > '1970-05-20'
UNION ALL SELECT * FROM users where bday < '1970-01-20'

Related

How to combine multiple SELECT statements into a single query & get a single result output

I have multiple SELECT queries which is ran against different tables.
The output of all the queries have the same number of rows (every query when ran individually will have the same number of rows). Is there a way I can combine the output of all these queries into a single result? (Keep out from first query and add the output of next query as a column to the output of the next query). I dont want to save these tables into database as I am just doing some validation testing.
Example:
SELECT AAA,BBB,CCC FROM Table1
SELECT Table2.DDD, Table1.AAA
FROM Table2
INNER JOIN Table1
ON Table1.AAA = Table2.AAA
I tried writing combining the query as
SELECT Table1.AAA,Table1.BBB,Table1.CCC,T1.DDD
FROM Table1,
(SELECT Table2.DDD, Table1.AAA
FROM Table2
INNER JOIN Table1
ON Table1.AAA = Table2.AAA)T1
I tried doing the above combined query, but instead of getting 11 rows as output (both queries above had result of 11 rows), I am getting 35 rows as output.
Hope the question made sense!
You'll need to specify a criteria to match each row the first query with which row of the second query.
If, for example, the column AAA is unique in both queries and you want to match rows with the same values you could do:
select a.*, b.*
from (
SELECT AAA,BBB,CCC FROM Table1
) a
full join join (
SELECT Table2.DDD, Table1.AAA
FROM Table2
INNER JOIN Table1
ON Table1.AAA = Table2.AAA
) b on b.aaa = a.aaa
If there aren't any clear matching rules, you can produce an artificial row number on each result set and use it to match rows. For example:
select
a.aaa, a.bbb, a.ccc,
b.ddd, b.aaa
from (
SELECT AAA, BBB, CCC,
row_number() over(order by aaa) as rn
FROM Table1
) a
full join join (
SELECT Table2.DDD, Table1.AAA,
row_number() over(order by table1.aaa, table2.ddd) as rn
FROM Table2
INNER JOIN Table1
ON Table1.AAA = Table2.AAA
) b on b.rn = a.rn
If you have several results and want to have all of them as additional columns you can simply use ",":
create table temp1 as select '1' as c1 from DUAL;
create table temp2 as select '2' as c2 from DUAL;
create table temp3 as select '3' as c3 from DUAL;
select a.c1, b.c2, c.c3 from temp1 a, (select c2 from temp2) b, (select c3 from temp3) c;
An alternative could also be that you want to have all the results as additional rows then you would use UNION ALL between the individual results.

How to Query from 2 tables

I have a database by the name CARS with 2 tables. I need help in querying data from both these tables and get a output based on ID(100)..
Any help is highly appreciated.
he following should answer your question. Ask for the fields you want(with a table identifier) in the SELECT clause and then JOIN the tables on the common ID:
SELECT table1.ID, table1.GroupID, table1.Text, table2.GroupID, table2.Text
FROM table1
JOIN table2
ON table1.ID = table2.ID
or:
Looking at is again I noticed table 1 and table 2 are identical in design, just hte entries are different. You can use UNION ALL to concatenate two SELECT statements
SELECT * FROM table1 UNION ALL SELECT * FROM table2;
:)
To only return the entries with ID = 100
SELECT * FROM table1
WHERE ID = 100
UNION ALL
SELECT * FROM table2
WHERE ID = 100;

how can we select data from two or more tables if similar data is not present in both tables(without join)

I need solution for this type query.Means this query is not real but I need to fetch data like this:(except join and union)
select a b,c
from (table1 or table2)
where name="rohit";
it's simple to do with union
select *
from t1
where t1.name='a'
union (all)
select *
from t2
where t2.name='a'
If union and join is not allowed you can try select both table and create kind of union via GROUP BY trick. Like this
select *
from t1, t2
where t1.name='a' or t2.name='a'
group by (case t1.name
when 'a' then CONCAT('t1',t1.id)
else CONCAT('t2',t2.id) end case)
Haven't tested though. sqlfiddle.com somehow doesn't work showing errors when I try to execute a query like this

Fastest SQL & HQL Query for two tables

Table1: Columns A, B, C
Table2: Columns A, B, C
Table 2 is a copy of Table 1 with different data. Assume all columns to be varchar
Looking for a single efficient query which can fetch:
Columns A, B, C from Table1
Additional Rows from Table2 where values of Table2.A are not present in Table1.A
Any differences between the Oracle SQL & HQL for the same query will be appreciated.
I'm fiddling with Joins, Unions & Minus but not able to get the correct combination.
SQL:
SELECT *
FROM Table1
UNION ALL
SELECT *
FROM Table2 T2
WHERE NOT EXISTS(
SELECT 'X' FROM Table1 T1
WHERE T1.A = T2.A
)
HQL:
You must execute two different query an discard the element by Table2 result in a Java loop because in HQL doesn't exist UNION command.
Alternatatively you can write the first query for Table1 and the second query must have a not in clause to discard Table1 A field.
Solution 1:
Query 1:
SELECT * FROM Table1
Query 2:
SELECT * FROM Table2
and then you apply a discard loop in Java code
Solution 2:
Query 1:
SELECT * FROM Table1
Query 2:
SELECT * FROM Table2 WHERE Table2.A not in (SELECT Table1.A from Table1)
This query returns all rows in table1, plus all rows in table2 which does not exist in table1, given that column a is the common key.
select a,b,c
from table1
union
all
select a,b,c
from table2
where a not in(select a from table1);
There may be different options available depending on the relative sizes of table1 and table2 and the expected overlap.

How do I merge data from two tables in a single database call into the same columns?

If I run the two statements in batch will they return one table to two to my sqlcommand object with the data merged. What I am trying to do is optimize a search by searching twice, the first time on one set of data and then a second on another. They have the same fields and I’d like to have all the records from both tables show and be added to each other. I need this so that I can sort the data between both sets of data but short of writing a stored procedure I can’t think of a way of doing this.
Eg. Table 1 has columns A and B, Table 2 has these same columns but different data source. I then wan to merge them so that if a only exists in one column it is added to the result set and if both exist it eh tables the column B will be summed between the two.
Please note that this is not the same as a full outer join operation as that does not merge the data.
[EDIT]
Here's what the code looks like:
Select * From
(Select ID,COUNT(*) AS Count From [Table1]) as T1
full outer join
(Select ID,COUNT(*) AS Count From [Table2]) as T2
on t1.ID = T2.ID
Perhaps you're looking for UNION?
IE:
SELECT A, B FROM Table1
UNION
SELECT A, B FROM Table2
Possibly:
select table1.a, table1.b
from table1
where table1.a not in (select a from table2)
union all
select table1.a, table1.b+table2.b as b
from table1
inner join table2 on table1.a = table2.a
edit: perhaps you would benefit from unioning the tables before counting. e.g.
select id, count() as count from
(select id from table1
union all
select id from table2)
I'm not sure if I understand completely but you seem to be asking about a UNION
SELECT A,B
FROM tableX
UNION ALL
SELECT A,B
FROM tableY
To do it, you would go:
SELECT * INTO TABLE3 FROM TABLE1
UNION
SELECT * FROM TABLE2
Provided both tables have the same columns
I think what you are looking for is this, but I am not sure I am understanding your language correctly.
select id, sum(count) as count
from (
select id, count() as count
from table1
union all
select id, count() as count
from table2
) a
group by id