Identify if at least one row with given condition exists

前端 未结 4 1960
长发绾君心
长发绾君心 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:31

    since you require that the sql query should return 1 or 0, then you can try the following query :-

    select count(1) from dual 
    where exists(SELECT 1 
                 FROM employee
                 WHERE name like 'kaushik%')
    

    Since the above query uses Exists, then it will scan the employee table and as soon as it encounters the first record where name matches "kaushik", it will return 1 (without scanning the rest of the table). If none of the records match, then it will return 0.

提交回复
热议问题