We have observed that there seems to be a maximum number of ids/variables which one can pass in the IN clause of SQL as comma separated values. To avoid this we are storing all the ids in a table and doing a SELECT within the IN clause. This however means extra database operations to store and retrieve ids. Is there any other way to use IN without SELECT?
Regards,
Sameer
In SQL Server 2008 you can declare a table variable and pass it to your query from the client or between the procedures.
For a modest number of values I would not have thought an IN (SELECT ..) would be that expensive on any rdbms.
You could INNER JOIN you IDs table or just break down the INs:
WHERE X IN (123,456 ...)
OR X IN (789,987 ...)
...
Agree with Alex
Instead of
Select * From TableA Where ID IN (Select ID from IDTable)
Use
Select * From TableA
INNER JOIN IDTable ON TableA.ID = IDTable.ID
The join will automatically filter the IDs for you.
If you put it in atable why are you still using the in clause, why not just join to the table?
Related
select *
from tableA ta
inner join tableB tb on tb.columnbid = ta.columnaid
When joining 2 tables, is the joining column preference important?
Should I use
on ta.columnaid = tb.columnbid
instead?
No, the order of the equation does not play any part in execution or optimisation.
The equation tb.columnbid=ta.columnaid simply means that show only those rows where both of them are equal. So the order doesn't matter. You can write ta.columnaid=tb.columnbid and will find the same result.
I've got an sql statement where I get a list of all Ids from a table (Machines).
Then need the latest instance of another row in (Events) where the the id's match so have been doing a subselect.
I need to latest instance of quite a few fields that match the id so have these subselects after one another within this single statement so end up with results similar to this...
This works and the results are spot on, it's just becoming very slow as the Events Table has millions of records. The Machine table would have on average 100 records.
Is there a better solution that subselects? Maybe doing inner joins or a stored procedure?
Help appreciated :)
You can use apply. You don't specify how "latest instance" is defined. Let me assume it is based on the time column:
Select a.id, b.*
from TableA a outer apply
(select top(1) b.Name, b.time, b.weight
from b
where b.id = a.id
order by b.time desc
) b;
Both APPLY and the correlated subquery need an ORDER BY to do what you intend.
APPLY is a lot like a correlated query in the FROM clause -- with two convenient enhances. A lateral join -- technically what APPLY does -- can return multiple rows and multiple columns.
I have to retrieve information for certain id numbers which are more than 300 numbers so I need to put all of them in the where clause as condition one by one which consume time. Is there a way or better solution to do that? using Oracle SQL
One way or another, you will need to enumerate those ids (the database cannot just guess them).
One convenient approach is to create a table to store the list of ids, where each id appears on a separate row. You can do that programmtically with the tool of your choice.
With this set up at hand, say table numbers, you can then use it in your query with a join or an exists subquery, like:
select t.*
from mytable t
inner join numbers n on n.id = t.id
Or:
select t.*
from mytable t
where exists (select 1 from numbers n where n.id = t.id)
From what it sounds like, the answer to your question is IN:
select t.*
from t
where t.id in ( . . . );
Oracle is smart enough to use an index in this case. Note that Oracle limits the number of values in an explicit in list to 1,000.
I'm trying to construct an SQL query that selects data from both tables but it doesn't seem to be working:
SELECT DISTINCT name,$price
FROM room
WHERE capacity>=$partySize
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date';
What am I doing wrong?
Move the WHERE clause down to the bottom:
SELECT DISTINCT name,$price
FROM room
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date' AND capacity>=$partySize
Definitely read some more SQL tutorials. You cannot use two WHERE conditions in one query:
SELECT DISTINCT name,'$price'
FROM room
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date' AND capacity>='$partySize';
Also, variables in the query should be quoted.
try
SELECT DISTINCT r.name,$price
FROM room r, room_booking b
WHERE r.capacity>=$partySize
and r.room_id=b.room_id
and r.date_booked<>'$us_date';
What's $price? is it a value? if variable try r.$price
How can I select rows that exist is two tables. The intersection I guess? Any help?
ProductosA and ProductosB, they're both tables with the exact same number and type of columns.
How can I select the things that are inside of both using a single select statement?
Try:
select * from ProductosA
intersect
select * from ProductosB
;
select a.column1, a.column2
from productosA a
join
productosB b
on
a.id = b.id
that will give you what you want
If there is a primary/composite key join the two tables where the keys match, if there is no primary key, join them using where "and"ing match for each column.
SELECT
ProductosATable.*
FROM
ProductosATable
INNER JOIN ProductosBTable
ON ProductosATable.NAME = ProductosBTable.NAME
Simply by specifying more than one table in your FROM clause you will get rows that exist in more than one table. Whether you get their entire rows, or just part of them, depends on how many of the columns you specify in the SELECT clause.