Which select is faster in Sqlite? - sql

a is a table index, b is a normal column.
select a,b from ( select a,b from table where a in (*listA*) ) where b in (*listB*)
or
select a,b from table where (a=listA[0] and b=listB[0]) or (a=listA[1] and b=listB[1])...
I am using pseudocode to represent a list declaration.

The first query is wrong because it never looks at the combinations of a and b.
To use a temp table, you have to join with it:
SELECT a, b
FROM MyTable
JOIN (SELECT 1 AS a, 2 AS b UNION ALL
SELECT 3, 4 UNION ALL
SELECT 5, 6 ...)
USING (a, b)
Which version is better optimized depends on too many factors; the only way to find out is to measure with representative data.

Related

SQL counting values and selecting Column Name as result

I am new to SQL and a little embarrassed to ask this.
I have A table that contains 2 columns A and B
A B
0 2
1 3
3 1
I want a query that will return
Category | Sum
A 4
B 6
What is the best way to write this query?
select 'A', sum(A) from table
union
select 'B', sum(B) from table
Or this...
SELECT SUM(A) A
, SUM(B) B
FROM #MyTable
If UNPIVOT is supported by the SQL product you are using:
SELECT Category, SUM(Value) AS Sum
FROM atable
UNPIVOT (Value FOR Category IN (A, B)) u
GROUP BY Category
;
In particular, the above syntax works in Oracle and SQL Server.

SELECT on two other queries in Oracle

So, lets say I want to do something like:
SELECT Query1.a,
Query2.b
FROM (
SELECT q as a
FROM somewhere
),
(
SELECT g as b
FROM elsewhere
)
where Query 1 is
(
SELECT q as a
FROM somewhere
)
and Query2 is
(
SELECT g as b
FROM elsewhere
)
So, i want to select from two other select statements.
Query 1 produces a table
a
value1
Query 2 produces a table
b
value 2
And Query 3 (the outer select statement) produces
a b
value 1 value 2
So, essentially, two result tables are combined as columns and not as rows.
Thank you, if you have any hints.
You basically have your solution. You are only missing the names of your queries, so do like this:
SELECT Query1.a,
Query2.b
FROM (
SELECT q as a
FROM somewhere
) Query1,
(
SELECT g as b
FROM elsewhere
) Query2
It's not clear how you need to connect different rows from tables but it can be something like this:
select query1.a,
query2.b
FROM
(select q as a, ROW_NUMBER() OVER (ORDER BY q) as RN from a) Query1
FULL JOIN
(select q as b, ROW_NUMBER() OVER (ORDER BY q) as RN from b) Query2
ON Query1.RN=Query2.RN
SQLFiddle example
Your syntax is a bit off the SQL charts, but in essence ritgh:
It is possible to do a subquery:
select A.field from (select field from a_table) A;
It is essential that you name your query, if you want to use it in the select or where clauses.
And even possible to combine them like regular tables:
select A.field, B.other_field from (select field from table1) A, (select other_field from table2) B;
It is also possible to do al kind of where, grouping and sorting stuff on it, but not needed in your case.
I assume this is what you're looking for:
SELECT query1.a, query2.b
FROM
(SELECT q as a FROM somewhere) query1,
(SELECT g as b FROM elsewhere) query2
Here is a SQLFiddle to test the query

Single line query for copying data from 3 tables to a empty table

Today in interview, I was asked, if I can write a single query to copy data from 3 tables to an empty table.
I started saying, I will use temp table or table variables but he said no, he wants to see in a single statement or query.... I was blank :(
Do any of you can share the right answer please :)
insert into <emplty_table>
select * from table1
union all
select * from table2
union all
select * from table3
provided all the table have the same structure
Depends upon the specifics, but in general I think the idea would be to use a union. The following is obviously pseudocode, but it conveys the idea:
insert into x (field1, field2, field3)
select a, b, c
from table1
union
select d, e, f
from table2
union
select g, h, i
from table3

Does table1 UNION ALL table2 guarantee output order table1, table2?

SELECT a FROM b
UNION ALL
SELECT a FROM c
UNION ALL
SELECT a FROM d
Does UNION ALL guarantee to print out records from tables b, c, d in that order? I.e., no records from c before any from b. This question is not for a specific DBMS.
No order by, no order guarantee whatsoever - that's for every database.
And for standard SQL, an ORDER BY is applied to the results from all the unioned queries.
To be sure in order use
Select 1 as TableNo,* from a
union all
select 2 as TableNo,* from b
union all
select 3 as TableNO,* from c
order by TableNo, [desired column]

In SQL Server, what's the best way to merge tables from multiple databases?

I'm sorry that I can't find a better title of my question. Not lemme describe it in detail.
I have 4 database which are a, b, c and d. Database a have all table's that appear in b, c and d, and they have the same structure with the same constraints(pk, fk, default, check). b, c,d just have some tables that appear in a. Now there already some data in a, b, c and d. In b, c,d there are more data than the counterparts in a. And probably a have duplicated data with b, c,d.
Now what I want to do is export all data in b, c,d and import them to a. I already have a solution but I want to know what is the best method to do such a complicated task.
Thanks.
The UNIONs (no ALL) in the subquery will remove duplicates. Then the IS NULL in the Where will only insert new rows into Table1.
Insert Into DatabaseA.dbo.Table1(ID, Value)
Select ID, Value
FROM (
Select ID, Value From DatabaseB.dbo.Table1
UNION
Select ID, Value From DatabaseC.dbo.Table1
UNION
Select ID, Value From DatabaseD.dbo.Table1
) T
LEFT JOIN DatabaseA.dbo.Table1 S ON T.ID = S.ID
WHERE S.ID IS NULL
You can perform a Insert Into statement with the use of a unions that obtains the results from other databases
Insert Into dboTableA(ID, Value)
Select ID, Value From dbo.DatabaseB.TableA
UNION AlL
Select ID, Value From dob.DatabaseC.TableA
UNION ALL
Select ID, Value From dbo.DatabaseD.TableA