SQL : build a query that reads from a file - sql

i have a table with column name,add,no tel. ( millions of rows)
i need to pull a list of names ( like hundreds) from the table.
the straight forward way is like this...
select * from table where name = name1 or name = name2 .....or name = name300
i tried to make it more simpler,
i import the list of name into another table,say table2.
then i do a subquery.
select * from table where name like '%'+(select * from table2)+'%'
but it did not work.
please help how i can accomplish this efficiently.
thank u.

Just join the two tables:
Select a.* from tableA a join tableB b on a.name = b.name

Related

How to print all columns when columns are dynamic and one common column is present?

Lets say i have 2 table A,B .In both the tables (id) is the common column ,and rest columns dynamic.so write a query to print id of "A" and rest all columns .
A(id,name,city),B(id,phone,phone_num). Here I only know "id" column ,rest columns(name,city,phone) are coming dynamically,So i can not use A.name,A.city,B.phone etc .In
select * from A FULL OUTER JOIN B ON A.id = B.id;
is printing id column twice.
If you want to display the id column only once, use the using clause for the join :
SELECT *
FROM tableA a
JOIN tableB b using (id)
The using clause as the effect that the join column(s) are only included once with select *.
If I'm not wrong, you want to display all the column of both the tables and the common column i.e id should come only once.If that's the case use the below query and replace the column name with the column names you want to display.
SELECT a.id,a.column1,a.column2,b.column1,b.column2
FROM tableA a
INNER JOIN tableB b ON a.id=b.id
Demo
create table my_table_1 (id int,c1 int,c2 int,c3 int);
create table my_table_2 (id int,c4 int,c5 int);
select array_to_string(array_agg (table_name || '.' || column_name::text),',')
from information_schema.columns
where table_name in ('my_table_1','my_table_2')
and not (table_name,column_name) = ('my_table_2','id')
my_table_1.id,my_table_1.c1,my_table_1.c2,my_table_1.c3,my_table_2.c4,my_table_2.c5

SQL query to exclude original rows which there is a copy of them

In MyTable I have a column named CopyOf which holds the ID of other original row from the same table (MyTable). I want to select all rows except the original rows which there is a Copy of them. But I don't know how to manage conditions both in "ON" and "WHERE"
Select * from MyTable a left join MyTable b on ?????? where ???????
Sample data:
ID | CopyOf
1 |
2 |
3 | 1
I want just to select rows 2 and 3 because there is a copy of 1.
If you want to do this in a join and where then use a query like the one below;
SELECT
a.*
FROM MyTable a
LEFT JOIN MyTable b
ON a.ID = b.CopyOf
WHERE b.CopyOf IS NULL
This will join the table back to itself and then ignore any that are a copy of something in the same table.
Another option you appear to have is that you can just look for everything that isn't a copy of something else, like this;
SELECT
a.*
FROM MyTable a
WHERE a.CopyOf IS NULL
Another option is this;
SELECT
a.*
FROM MyTable a
WHERE a.ID NOT IN (SELECT CopyOf FROM MyTable)
Which will exclude any ID's completely that have a record in the CopyOf field. This final one will return only ID's that don't have any copies at all. Re-reading your question, I think this final query is the one you're after.
I assumed you want to show all data that is not available from table2. Here's my solution
Use NOT IN in your WHERE Clause.
Like this:
SELECT * FROM MyTable
WHERE ID NOT IN (SELECT DISTINCT ID FROM MyTable2)

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/

Include table name in column from select wildcard sql

Is it possible to include table name in the returned column if I use wildcard to select all columns from tables?
To explain it further. Suppose I want to join two tables and both tables have the column name “name” and many other columns. I want to use wildcard to select all columns and not explicitly specifying each column name in the select.
Select *
From
TableA a,
TableB b
Where
a.id = b.id
Instead of seeing two column with same name "name", could I write a sql to return one column name as "a.name" (or TableA.name) and one as "b.name"(or TableB.name) without explicitly putting the column name in select?
I would prefer a solution for mssql but other database could be a reference too.
Thanks!
You can use select a.*, ' ', b.* from T1 a, T2 b to make it more visible where columns from T1 end and columns from T2 begin.
You are basically joining two tables on the ID field, so you will only see one column labeled "ID", not two, because you are asking to see only those records where the ID is the same in table a and table b: they share the same id.
Try ...
SELECT 'TableA' AS 'Table', A.* FROM TableA A
WHERE A.id IN (SELECT id FROM TableB)
UNION
SELECT 'TableB' AS 'Table', B.* FROM TableB B
WHERE B.id IN (SELECT id FROM TableA)
ORDER BY id, [Table]

Why can I reference a field from an outer query in SQL

Given theses tables:
create temporary table a(pid integer, cid integer);
create temporary table b(id integer, code varchar);
This works but returns wrong results:
select pid
from a
where cid in (select cid from b where code like 'AE%')
I just had a query like that where I use the wrong field, and it surprised me that that query even works. Doesn't a query like this just return all the rows from table a ?
Do you have any example of a query written like this that would be useful ?
Maybe I'm missing something.
You regulary need fields from the outer query in the where clause of the inner query:
select * from a
where exists ( select 1 from b where id = cid );
or
select * from a
where 'HELLO' in (select code from b where id = cid );
We can also construct examples where the outer fields are (kind of) useful in the select clause:
select * from a
where 1 = any (select id-cid from b where code like 'HE%');
Therefore, access to the fields of the outer query is absolutely necessary.