nested - Can you use EXISTS with OR in SQL? -
ok question states have list restaurants have been reviewed “daniel johnston” or have both type “italian” , @ least 1 rating score of @ least 4.
i've been trying query:
select name restaurant exists (select rest_id restaurant, rates restaurant.rest_id=rates.rest_id , restaurant.type = 'italian' , rates.score>=4) or rest_id in (select rest_id reviews, user user.user_id=reviews.user_id , user.fname = 'daniel' , user.sname = 'johnston');
but keeps returning errors. i've run different parts separately , ok. i'm wondering if can't use exists , or, , if can't, better way?
my logic want name of restaurant if it's italian , has @ least 1 review score of on 4, exists suitable that, , using 2 exists doesn't seem make difference why revert normal nested query second part. i've messed around lot , cannot seem figure out wrong, appreciated!
subqueries seldom give best performance, faster straight forward join; makes naming easier fewer conflicting names (rest_id conflicts in non obvious way 1 of inner queries in example)
select name restaurant left join rates on restaurant.rest_id = rates.rest_id left join reviews on restaurant.rest_id = reviews.rest_id left join [user] on reviews.user_id = [user].user_id restaurant.type = 'italian' , rates.score>=4 or [user].fname = 'daniel' , [user].sname = 'johnston';
simple demo here.
as more point response question, yes, can use exists or, have keep names straight between main query , subqueries , query have should work fine too.
Comments
Post a Comment