I have a query below that I would like to change into a NOT EXITS to see if it would be more efficient. How would I convert the below query into a COUNT with NOT EXITS?
select count(distinct(serv_item.serv_item_id)) from serv_item where serv_item.status = '6' and serv_item.serv_item_id not in (select pbi_bill_activation.tbs_id from pbi_bill_activation where pbi_bill_activation.tbs_id_type in ('EUL','OFFER','S800I','SCRDI','SDIDI'));
Here is a sample NOT IN query:
select * from MyTable where MyCol not in ( select MyCol from MyOtherTable )The following query has the same results, though it uses a NOT EXISTS. Not the sub-query is correlated.
select * from MyTable where not exists ( select * from MyOtherTable where MyCol = MyTable.MyCol )Although you could easily rewrite your query using my example, I'll do it here to further illustrate the concept.
select count(distinct(serv_item.serv_item_id)) from serv_item where serv_item.status = '6' and not exists ( select pbi_bill_activation.tbs_id from pbi_bill_activation where pbi_bill_activation.tbs_id_type in ('EUL','OFFER','S800I','SCRDI','SDIDI') and pbi_bill_activation = serv_item.serv_item_id )
For More Information
- Dozens more answers to tough SQL questions from Jason Law are available here.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
Dig deeper on Oracle and SQL
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.