{"id":11739,"date":"2020-11-30T12:48:51","date_gmt":"2020-11-30T02:48:51","guid":{"rendered":"https:\/\/database.guide\/?p=11739"},"modified":"2024-09-17T07:00:40","modified_gmt":"2024-09-16T21:00:40","slug":"sql-exists-operator-for-beginners","status":"publish","type":"post","link":"https:\/\/database.guide\/sql-exists-operator-for-beginners\/","title":{"rendered":"SQL EXISTS Operator for Beginners"},"content":{"rendered":"\n<p>In <a href=\"https:\/\/database.guide\/what-is-sql\/\">SQL<\/a>, the <code>EXISTS<\/code> operator specifies a subquery to test for the existence of rows. It returns <code>TRUE<\/code> when the subquery returns one or more rows.<\/p>\n\n\n\n<p>A subquery is a query that is nested inside another query (or even another subquery)<\/p>\n\n\n\n<p>This article contains some basic examples of the <code>EXISTS<\/code> operator.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Source Tables<\/h2>\n\n\n\n<p>The following tables are used for the examples on this page.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM PetTypes;\nSELECT * FROM Pets;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-------------+-----------+\n| PetTypeId &nbsp; | PetType &nbsp; |\n|-------------+-----------|\n| 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Bird&nbsp; &nbsp; &nbsp; |\n| 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Cat &nbsp; &nbsp; &nbsp; |\n| 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Dog &nbsp; &nbsp; &nbsp; |\n| 4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Rabbit&nbsp; &nbsp; |\n+-------------+-----------+\n(4 rows affected)\n+---------+-------------+-----------+-----------+------------+\n| PetId &nbsp; | PetTypeId &nbsp; | OwnerId &nbsp; | PetName &nbsp; | DOB&nbsp; &nbsp; &nbsp; &nbsp; |\n|---------+-------------+-----------+-----------+------------|\n| 1 &nbsp; &nbsp; &nbsp; | 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 3 &nbsp; &nbsp; &nbsp; &nbsp; | Fluffy&nbsp; &nbsp; | 2020-11-20 |\n| 2 &nbsp; &nbsp; &nbsp; | 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 3 &nbsp; &nbsp; &nbsp; &nbsp; | Fetch &nbsp; &nbsp; | 2019-08-16 |\n| 3 &nbsp; &nbsp; &nbsp; | 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 2 &nbsp; &nbsp; &nbsp; &nbsp; | Scratch &nbsp; | 2018-10-01 |\n| 4 &nbsp; &nbsp; &nbsp; | 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 3 &nbsp; &nbsp; &nbsp; &nbsp; | Wag &nbsp; &nbsp; &nbsp; | 2020-03-15 |\n| 5 &nbsp; &nbsp; &nbsp; | 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 1 &nbsp; &nbsp; &nbsp; &nbsp; | Tweet &nbsp; &nbsp; | 2020-11-28 |\n| 6 &nbsp; &nbsp; &nbsp; | 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 4 &nbsp; &nbsp; &nbsp; &nbsp; | Fluffy&nbsp; &nbsp; | 2020-09-17 |\n| 7 &nbsp; &nbsp; &nbsp; | 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 2 &nbsp; &nbsp; &nbsp; &nbsp; | Bark&nbsp; &nbsp; &nbsp; | NULL &nbsp; &nbsp; &nbsp; |\n| 8 &nbsp; &nbsp; &nbsp; | 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | 4 &nbsp; &nbsp; &nbsp; &nbsp; | Meow&nbsp; &nbsp; &nbsp; | NULL &nbsp; &nbsp; &nbsp; |\n+---------+-------------+-----------+-----------+------------+\n(8 rows affected)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s an example to demonstrate the <code>EXISTS<\/code> operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    pt.PetTypeId,\n    pt.PetType\nFROM PetTypes pt\nWHERE EXISTS ( \n    SELECT p.PetTypeId \n    FROM Pets p \n    WHERE p.PetTypeId = pt.PetTypeId \n    );<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-------------+-----------+\n| PetTypeId &nbsp; | PetType &nbsp; |\n|-------------+-----------|\n| 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Bird&nbsp; &nbsp; &nbsp; |\n| 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Cat &nbsp; &nbsp; &nbsp; |\n| 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Dog &nbsp; &nbsp; &nbsp; |\n+-------------+-----------+<\/pre>\n\n\n\n<p>This example shows us how many pet types are in our pet hotel. There are actually four pet types, but only three of those match with an actual pet in the <code>Pets<\/code> table.<\/p>\n\n\n\n<p>This provides the same result that we would have gotten with the following query that uses the <a href=\"https:\/\/database.guide\/sql-in-operator-for-beginners\/\" title=\"SQL IN Operator for Beginners\"><code>IN<\/code> operator<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    PetTypeId,\n    PetType\nFROM PetTypes\nWHERE PetTypeId IN ( SELECT PetTypeId FROM Pets );<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using <code>NOT EXISTS<\/code><\/h2>\n\n\n\n<p>We could add the <code>NOT<\/code> operator to negate the results and see how many pet types are <em>not<\/em> in our pet hotel.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    pt.PetTypeId,\n    pt.PetType\nFROM PetTypes pt\nWHERE NOT EXISTS ( \n    SELECT p.PetTypeId \n    FROM Pets p \n    WHERE p.PetTypeId = pt.PetTypeId \n    );<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-------------+-----------+\n| PetTypeId &nbsp; | PetType &nbsp; |\n|-------------+-----------|\n| 4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Rabbit&nbsp; &nbsp; |\n+-------------+-----------+<\/pre>\n\n\n\n<p>In this case, our database contains a pet type of <code>Rabbit<\/code>, but we don&#8217;t currently have any rabbits as pets.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In SQL, the EXISTS operator specifies a subquery to test for the existence of rows. It returns TRUE when the subquery returns one or more rows. A subquery is a query that is nested inside another query (or even another subquery) This article contains some basic examples of the EXISTS operator.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[15,85,48,173,20],"class_list":["post-11739","post","type-post","status-publish","format-standard","hentry","category-sql","tag-create-query","tag-operators","tag-sql","tag-subqueries","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/11739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=11739"}],"version-history":[{"count":1,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/11739\/revisions"}],"predecessor-version":[{"id":11743,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/11739\/revisions\/11743"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=11739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=11739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=11739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}