tags:

views:

113

answers:

7

If I have a query like this

SELECT * FROM table1 WHERE col1 IN ({SUBS})

Is there anything I can replace {SUBS} with that will return all rows in the table?

Further details:

I am building the SQL dynamically in my app, so I cannot (should not) edit other parts of the query except what's in braces. So,

SELECT * FROM table1

will not do.

Also,

SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table1)

would be hackish and highly inefficient. Consider the table have more than 50k rows.

+5  A: 

This would do it:

select col1 from table1

Edit: There seems to be a bit of confusion - the OP asked what value could be used to replace {SUBS} that would return all rows from table1. My answer above is what you could use in place of {SUBS} that would return all the rows.

Andrew Hare
you mean **select * from table1**
pstanton
Nope, I meant `select col1 from table1` :)
Andrew Hare
I think viligant is trying to find out if someone could use SQL injection to find all the data in the table, which Andrew's reply would allow, although I might useSELECT distinct col1 FROM table1But his answer is proper
Sparky
@Sparky - right on, that is what I was thinking as well.
Andrew Hare
From the posted question, it appears vigilant is trying to get full rows, not just col1.
Donnie DeBoer
distinct? ? ? ?
ysth
@pstanton, @Donnie DeBoer: the answer is just the part to put in place of the .. the IN (...)
ysth
@Donnie DeBoer - Andrew meant replace {SUBS} with "select col1 from table1"
Chris Simmons
@Donnie DeBoer and @pstanton, what Andrew Hare means is that your should replace `{SUBS}` in the OP's question, with the SQL he posted in his answer which would result in getting all columns, not just `col1`. See my answer for an elaboration.
Asaph
Oh oh oh oh... right on. Very good. Carry on.
Donnie DeBoer
@Sparky: I don't think the use of `DISTINCT` would be any more efficient. The query plan would in any case remove duplicates if deemed beneficial. Using `DISTINCT` essentially forces the query plan to always remove duplicates - even when not beneficial.
Craig Young
+2  A: 

If you replaced {SUBS} with SELECT col1 FROM table1, you would end up with

SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table1);

which would return all rows from table1. This is, of course, simply a more roundabout way of saying:

SELECT * FROM table1;
Asaph
A: 

If you're simply trying to retrieve every row in the table, then:

select * from table1

If you're trying to prove a point or win a bet or something, then:

select * from table1 where col1 in (select col1 from table1)
Dewayne Christensen
+2  A: 

This works for me in SQL Server:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN (COLUMN_NAME)

Have you tried just using COL1 for {SUBS}?

e.g.

SELECT * FROM table1 WHERE col1 IN (col1)
beach
+1  A: 

You're right,

SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table1)

does work, but is highly inefficient; requiring a merge join to return all rows.

Use the following which is just as efficient as regular SELECT * FROM table1

SELECT * FROM table1 WHERE col1 IN (col1)

However, that said; I suggest you have a chat to the person who is trying to impose the SELECT * FROM table1 WHERE col1 IN ({SUBS}) structure. There is no good reason to do so.

  • It unnecessarily complicates queries.
  • Creates risk of highly inefficient queries.
  • Potentially even limits developers to use certain techniques.

I suspect the person imposing this is trying to implement some sort of silver-bullet framework. Remember, the golden rule in software development is that there are no silver-bullets.

Craig Young
A: 

If the query requires some WHERE condition, then I would try to replace it with an EXISTS statement:

select
  *
from
  table1 t1
where
  exists ( {subs} )

Then {subs} can be replaced with any expression that does not yield NULL.

Quick Joe Smith
A: 

This works in Oracle:

select * from table1 where col1 in (col1)

Regards
K

Khb