About 518,000 results
Open links in new tab
  1. sql - NOT IN vs NOT EXISTS - Stack Overflow

    May 18, 2007 · For this, we can use NOT EXISTS, which negates the logic of the EXISTS operator. Therefore, the NOT EXISTS operator returns true if the underlying subquery returns no record. However, if a single record is matched by the inner subquery, the NOT EXISTS operator will return false, and the subquery execution can be stopped.

  2. Difference between "IF EXISTS" and "IF NOT EXISTS" in SQL?

    Here are 4 examples illustrating when you would use IF EXISTS and when you would use IF NOT EXISTS: A) Delete related records from more than 1 table: IF EXISTS (SELECT TOP(1) 1 FROM Table1 WHERE ORDER_ID = 11032) BEGIN DELETE FROM Table1 WHERE ORDER_ID = 11032 DELETE FROM Table2 WHERE ORDER_ID = 11032 -- possibly more statements following here ...

  3. SQL Server IF NOT EXISTS Usage? - Stack Overflow

    Jul 24, 2009 · IF EXISTS ( SELECT 1 FROM Timesheet_Hours WHERE Posted_Flag = 1 AND Staff_Id = @PersonID ) BEGIN RAISERROR('Timesheets have already been posted!', 16, 1) ROLLBACK TRAN END ELSE IF NOT EXISTS ( SELECT 1 FROM Timesheet_Hours WHERE Staff_Id = @PersonID ) BEGIN RAISERROR('Default list has not been loaded!', 16, 1) ROLLBACK TRAN END

  4. SQL using NOT EXISTS - Stack Overflow

    Jul 19, 2013 · Note that the NOT EXISTS subquery references c.clientId, which is the value from the clientId column of the clients table in the outer query. We call this a "correlated subquery", because for each row returned by the outer query, we are (effectively) running the subquery, and using the clientId from that row in the predicate (WHERE clause) of ...

  5. How do SQL EXISTS statements work? - Stack Overflow

    Nov 18, 2013 · Just to add that EXISTS is SEMI JOIN in disguise. Most SQL dialects do not have SEMI JOIN in their syntax but provide EXISTS instead which amounts to the same thing. It's just like INNER JOIN except that. the right table is suppressed in the output; the rows in the left table are never duplicated; By the same token, NOT EXISTS corresponds to ...

  6. how to use NOT EXISTS in sql server - Stack Overflow

    Feb 24, 2014 · The use of NOT EXISTS in SQL. 2. How to use if not exists in sql. 0. NOT EXIST in SQL. 0.

  7. How to use not exists in a sql query with w3schools?

    Aug 12, 2013 · I have an issue with not exists sql query at w3schools. I want to select all customers that work with shipperid = 1 BUT not shipperid = 3. I tried the following: select o1.customerid, o1.shipperid from orders o1 where o1.shipperid=1 and not exists (select o2.customerid from orders o2 where o1.orderid=o2.orderid and o2.shipperid=3) order by ...

  8. mysql - When to use NOT EXISTS in SQL? - Stack Overflow

    Oct 2, 2013 · As per what I know, EXISTS returns true when a sub query contains atleast a row whereas NOT EXIST returns true if the subquery returns nothing. So for a given subquery either of the two should return

  9. sql server - How to use EXISTS and NOT EXISTS in one query?

    Aug 5, 2018 · for example Table3 has Table1_ID column value only 1 now new NOT exist query should give me result 2,3,4,5. I tried like - SELECT Table1.Id FROM Table1 as Table1 WHERE EXISTS ( SELECT * FROM Table2 as Table2 WHERE Table1.DemoID = Table2.DemoID AND Table2.IsTrue= 1 ) AND NOT EXISTS (SELECT * FROM Table3)

  10. SQL: How to properly check if a record exists - Stack Overflow

    Nov 23, 2010 · You can use: SELECT COUNT(1) FROM MyTable WHERE ... or. WHERE [NOT] EXISTS ( SELECT 1 FROM MyTable WHERE ... ) This will be more efficient than SELECT * since you're simply selecting the value 1 for each row, rather than all the fields. There's also a subtle difference between COUNT(*) and COUNT(column name): COUNT(*) will count all rows ...

Refresh