Identify if at least one row with given condition exists

前端 未结 4 1957
长发绾君心
长发绾君心 2021-02-05 02:59

Employee table has ID and NAME columns. Names can be repeated. I want to find out if there is at least one row with name like \'kaushik%\'.

So query should return true/f

4条回答
  •  你的背包
    2021-02-05 03:49

    Commonly, you'd express this as either

    SELECT COUNT(*)
      FROM employee
     WHERE name like 'kaushik%'
       AND rownum = 1
    

    where the rownum = 1 predicate allows Oracle to stop looking as soon as it finds the first matching row or

    SELECT 1
      FROM dual
     WHERE EXISTS( SELECT 1
                     FROM employee
                    WHERE name like 'kaushik%' )
    

    where the EXISTS clause allows Oracle to stop looking as soon as it finds the first matching row.

    The first approach is a bit more compact but, to my eye, the second approach is a bit more clear since you really are looking to determine whether a particular row exists rather than trying to count something. But the first approach is pretty easy to understand as well.

提交回复
热议问题