Find the sid who have reserved a red and a green boat.
Table reserves
sid bid
22 101
22 102
22 103
31 103
32 104
Table Boats
Bid Color
101 blue
102 red
103 green
104 red
Is it possible to use and operator with sub query something like this
select sid from reserves where bid in
(select bid from boats where color='red') and
(select bid from boats where color='green') ;
Here I need check whether "bid" is present in the results of both 1st and 2nd sub query then select the sid.It doesn't considers the result of second sub query result for me though.
Try like This
(Or)
I would write
You might also consider this approach (replacing the subquery with a join):
This query returns all
sid
in reserves that have a boats entry with the samebid
with one of the specified colors.You need to specify in the bin in against both subsqueries, and as you would both red and green boats (and boats can only be one colour) you are actually saying you would like the reservers that are either red or green, so OR is appropriate here.
But a more efficient way to do this is not with two subqueries but with a join:
If you only want the list to contain unduplicated SIDs you can use either of the following:
or
I have had mixed experience with the efficiency of GROUP BY vs DISTINCT on really big tables (40GB+)
UPDATE: As I miss understood your previous question this maybe a more suitable solution:
Here we are joining the tables, then grouping the rows by the SID. Using the having clause we are counting the sum of the boolean checks on b.color = 'red' (and 'green') the sum will be zero if you dont have any bids boats that are red (or green) and by anding those together you know that the sum of reds > 1 and sum(green) >1.
And a sqlfiddle for you to play with: http://sqlfiddle.com/#!2/b5ec1/8
It works for something like this