Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify if at least one row with given condition exists

Tags:

sql

count

oracle

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/false or 1/0.

Is it possible to find it using single query. If we try something like

select count(1) from employee where name like 'kaushik%' 

in this case it does not return true/false. Also we are iterating over all the records in table. Is there way in simple SQL such that whenever first record which satisfies condition is fetched, it should stop checking further records. Or such thing can only be handled in Pl/SQL block ?

EDIT * First approach provided by Justin looks correct answer

SELECT COUNT(*) FROM employee WHERE name like 'kaushik%' AND rownum = 1 
like image 720
Kaushik Lele Avatar asked Jan 28 '13 16:01

Kaushik Lele


People also ask

Which SQL query check whether a query returns at least one row?

The SQL EXISTS condition is used in combination with a subquery and is considered to be met, if the subquery returns at least one row. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

How do you check if a row exists or not?

To test whether a row exists in a MySQL table or not, use exists condition. The exists condition can be used with subquery. It returns true when row exists in the table, otherwise false is returned. True is represented in the form of 1 and false is represented as 0.

Which of the given options Return rows when there is at least one match in both tables?

INNER JOIN − returns rows when there is a match in both tables.

How do you use exists and not exists in SQL?

Use EXISTS to identify the existence of a relationship without regard for the quantity. For example, EXISTS returns true if the subquery returns any rows, and [NOT] EXISTS returns true if the subquery returns no rows. The EXISTS condition is considered to be met if the subquery returns at least one row.


2 Answers

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.

like image 133
Justin Cave Avatar answered Sep 24 '22 15:09

Justin Cave


How about:

select max(case when name like 'kraushik%' then 1 else 0 end) from employee 

Or, what might be more efficient since like can use indexes:

select count(x) from (select 1 as x       from employee       where name like 'kraushik%'      ) t where rownum = 1 
like image 37
Gordon Linoff Avatar answered Sep 24 '22 15:09

Gordon Linoff