{"id":4917,"date":"2013-01-21T23:38:40","date_gmt":"2013-01-22T06:38:40","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=4917"},"modified":"2024-01-03T03:26:14","modified_gmt":"2024-01-03T10:26:14","slug":"mysql-exists","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/","title":{"rendered":"MySQL EXISTS"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the MySQL <code>EXISTS<\/code> operator and when to use it to improve the performance of the queries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL EXISTS operator<\/h2>\n\n\n\n<p>The <code>EXISTS<\/code> operator is a <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-boolean\/\">boolean<\/a> operator that returns either true or false. The <code>EXISTS<\/code> operator is often used to test for the existence of rows returned by the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-subquery\/\">subquery<\/a>.<\/p>\n\n\n\n<p>The following illustrates the basic syntax of the <code>EXISTS<\/code> operator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    select_list\n<span class=\"hljs-keyword\">FROM<\/span>\n    a_table\n<span class=\"hljs-keyword\">WHERE<\/span>\n    &#91;<span class=\"hljs-keyword\">NOT<\/span>] <span class=\"hljs-keyword\">EXISTS<\/span>(subquery);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If the subquery returns at least one row, the <code>EXISTS<\/code> operator returns true, otherwise, it returns false.<\/p>\n\n\n\n<p>In addition, the <code>EXISTS<\/code> operator terminates further processing immediately once it finds a matching row, which can help improve the performance of the query.<\/p>\n\n\n\n<p>The <code>NOT<\/code> operator negates the <code>EXISTS<\/code> operator. In other words, the <code>NOT EXISTS<\/code> returns true if the subquery returns no row, otherwise it returns false.<\/p>\n\n\n\n<p>Note that you can use <code>SELECT *<\/code>, <code>SELECT column<\/code>, <code>SELECT a_constant<\/code>, or anything in the subquery. The results are the same because MySQL ignores the select list that appeared in the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">SELECT<\/a><\/code>&nbsp;clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL EXISTS operator examples<\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>EXISTS<\/code> operator to understand how it works.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL SELECT EXISTS examples<\/h3>\n\n\n\n<p>Consider the following <code>customers<\/code> and <code>orders<\/code> tables in the <a href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/mysql-sample-database\/\">sample database<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"434\" height=\"304\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png\" alt=\"\" class=\"wp-image-8036\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png 434w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders-200x140.png 200w\" sizes=\"auto, (max-width: 434px) 100vw, 434px\" \/><\/figure>\n\n\n\n<p>The following statement uses the <code>EXISTS<\/code> operator to find the customer who has at least one order:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    customerNumber, \n    customerName\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    <span class=\"hljs-keyword\">EXISTS<\/span>(\n\t<span class=\"hljs-keyword\">SELECT<\/span> \n            <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">FROM<\/span>\n            orders\n        <span class=\"hljs-keyword\">WHERE<\/span>\n            orders.customernumber \n\t\t= customers.customernumber);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/queryhttps:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/#1\">Try It Out<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"262\" height=\"221\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-Exists-Customers-have-Sales-Orders.jpg\" alt=\"MySQL Exists - Customers have Sales Orders\" class=\"wp-image-5003\"\/><\/figure>\n\n\n\n<p>In this example, for each row in the <code>customers<\/code> table, the query checks the <code>customerNumber<\/code>&nbsp;in&nbsp;the <code>orders<\/code> table. If the <code>customerNumber<\/code>, which appears in the <code>customers<\/code> table, exists in the <code>orders<\/code> table, the subquery returns the first matching row. <\/p>\n\n\n\n<p>As a result, the <code>EXISTS<\/code> operator returns true and stops examining the <code>orders<\/code> table. Otherwise, the subquery returns no row, and the <code>EXISTS<\/code> operator returns false.<\/p>\n\n\n\n<p>The following example uses the <code>NOT EXISTS<\/code> operator to find customers who do not have any orders:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    customerNumber, \n    customerName\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">EXISTS<\/span>( \n\t<span class=\"hljs-keyword\">SELECT<\/span> \n            <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">FROM<\/span>\n            orders\n        <span class=\"hljs-keyword\">WHERE<\/span>\n            orders.customernumber = customers.customernumber\n\t);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/queryhttps:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/#2\">Try It Out<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"278\" height=\"224\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-NOT-EXISTS-example.jpg\" alt=\"MySQL NOT EXISTS example\" class=\"wp-image-5005\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL UPDATE EXISTS examples<\/h3>\n\n\n\n<p>Suppose that you have to update the phone extensions of the employees who work at the office in San Francisco.<\/p>\n\n\n\n<p>The following statement finds employees who work at the office in <code>San Franciso<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    employeenumber, \n    firstname, \n    lastname, \n    extension\n<span class=\"hljs-keyword\">FROM<\/span>\n    employees\n<span class=\"hljs-keyword\">WHERE<\/span>\n    <span class=\"hljs-keyword\">EXISTS<\/span>( \n        <span class=\"hljs-keyword\">SELECT<\/span> \n            <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">FROM<\/span>\n            offices\n        <span class=\"hljs-keyword\">WHERE<\/span>\n            city = <span class=\"hljs-string\">'San Francisco'<\/span> <span class=\"hljs-keyword\">AND<\/span> \n           offices.officeCode = employees.officeCode);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/queryhttps:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/#3\">Try It Out<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"316\" height=\"155\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-UPDATE-example.jpg\" alt=\"MySQL EXISTS UPDATE example\" class=\"wp-image-5006\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-UPDATE-example.jpg 316w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-UPDATE-example-300x147.jpg 300w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><\/figure>\n\n\n\n<p>This example adds the number 1 to the phone extension of employees who work at the office in San Francisco:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">UPDATE<\/span> employees \n<span class=\"hljs-keyword\">SET<\/span> \n    extension = <span class=\"hljs-keyword\">CONCAT<\/span>(extension, <span class=\"hljs-string\">'1'<\/span>)\n<span class=\"hljs-keyword\">WHERE<\/span>\n    <span class=\"hljs-keyword\">EXISTS<\/span>( \n        <span class=\"hljs-keyword\">SELECT<\/span> \n            <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">FROM<\/span>\n            offices\n        <span class=\"hljs-keyword\">WHERE<\/span>\n            city = <span class=\"hljs-string\">'San Francisco'<\/span>\n                <span class=\"hljs-keyword\">AND<\/span> offices.officeCode = employees.officeCode);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basicshttps:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/\">EXISTS<\/a><\/code> operator in the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-where\/\">WHERE<\/a><\/code> clause gets only employees who work at the office in San Fransisco.<\/li>\n\n\n\n<li>Second, the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-string-functions\/mysql-concat\/\">CONCAT()<\/a><\/code> function concatenate the phone extension with the number 1.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL INSERT EXISTS example<\/h3>\n\n\n\n<p>Suppose that you want to archive customers who don&#8217;t have any sales orders in a separate table. To do this, you use these steps:<\/p>\n\n\n\n<p>First, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create a new table<\/a> for archiving the customers by <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-copy-table\/\">copying<\/a> the structure from the <code>customers<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> customers_archive \n<span class=\"hljs-keyword\">LIKE<\/span> customers;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, insert customers who do not have any sales orders into the <code>customers_archive<\/code> table using the following <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">INSERT<\/a><\/code> statement.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> customers_archive\n<span class=\"hljs-keyword\">SELECT<\/span> * \n<span class=\"hljs-keyword\">FROM<\/span> customers\n<span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">EXISTS<\/span>( \n   <span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-number\">1<\/span>\n   <span class=\"hljs-keyword\">FROM<\/span>\n       orders\n   <span class=\"hljs-keyword\">WHERE<\/span>\n       orders.customernumber = customers.customernumber\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/queryhttps:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/#4\">Try It Out<\/a><\/p>\n\n\n\n<p>Third,&nbsp;<a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">query data<\/a> from the <code>customers_archive<\/code> table to verify the insert operation.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> customers_archive;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/queryhttps:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/#6\">Try It Out<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"701\" height=\"174\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-INSERT-example.jpg\" alt=\"MySQL EXISTS INSERT example\" class=\"wp-image-5007\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-INSERT-example.jpg 701w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-INSERT-example-300x74.jpg 300w\" sizes=\"auto, (max-width: 701px) 100vw, 701px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL DELETE EXISTS example<\/h3>\n\n\n\n<p>One final task in archiving the customer data is to delete the customers that exist in the <code>customers_archive<\/code> table from the <code>customers<\/code> table.<\/p>\n\n\n\n<p>To do this, you use the <code>EXISTS<\/code> operator in <code>WHERE<\/code> clause of the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-delete\/\">DELETE<\/a><\/code>&nbsp;statement as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">FROM<\/span> customers\n<span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-keyword\">EXISTS<\/span>( \n    <span class=\"hljs-keyword\">SELECT<\/span> \n        <span class=\"hljs-number\">1<\/span>\n    <span class=\"hljs-keyword\">FROM<\/span>\n        customers_archive a\n    \n    <span class=\"hljs-keyword\">WHERE<\/span>\n        a.customernumber = customers.customerNumber);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">MySQL EXISTS operator vs. IN operator<\/h2>\n\n\n\n<p>To find the customer who has placed at least one order, you can use the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-in\/\">IN<\/a><\/code>&nbsp;operator as shown in the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    customerNumber, \n    customerName\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    customerNumber <span class=\"hljs-keyword\">IN<\/span> (\n        <span class=\"hljs-keyword\">SELECT<\/span> \n            customerNumber\n        <span class=\"hljs-keyword\">FROM<\/span>\n            orders);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/queryhttps:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/#5\">Try It Out<\/a><\/p>\n\n\n\n<p>Let&#8217;s compare the query that uses the <code>IN<\/code> operator with the one that uses the <code>EXISTS<\/code> operator by using the <code>EXPLAIN<\/code> statement.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">EXPLAIN<\/span> <span class=\"hljs-keyword\">SELECT<\/span> \n    customerNumber, \n    customerName\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    <span class=\"hljs-keyword\">EXISTS<\/span>( \n        <span class=\"hljs-keyword\">SELECT<\/span> \n            <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">FROM<\/span>\n            orders\n        <span class=\"hljs-keyword\">WHERE<\/span>\n            orders.customernumber = customers.customernumber);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"366\" height=\"293\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-vs-IN-EXISTS-performance.jpg\" alt=\"MySQL EXISTS vs IN - EXISTS performance\" class=\"wp-image-4919\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-vs-IN-EXISTS-performance.jpg 366w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-vs-IN-EXISTS-performance-300x240.jpg 300w\" sizes=\"auto, (max-width: 366px) 100vw, 366px\" \/><\/figure>\n\n\n\n<p>Now, check the performance of the query that uses the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-in\/\">IN<\/a><\/code> operator.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    customerNumber, customerName\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    customerNumber <span class=\"hljs-keyword\">IN<\/span> (<span class=\"hljs-keyword\">SELECT<\/span> \n            customerNumber\n        <span class=\"hljs-keyword\">FROM<\/span>\n            orders);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/queryhttps:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/#6\">Try It Out<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"316\" height=\"348\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-vs-IN-IN-performance.jpg\" alt=\"MySQL EXISTS vs IN - IN performance\" class=\"wp-image-4920\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-vs-IN-IN-performance.jpg 316w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-vs-IN-IN-performance-272x300.jpg 272w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><\/figure>\n\n\n\n<p>The query that uses the <code>EXISTS<\/code> operator is much faster than the one that uses the <code>IN<\/code> operator.<\/p>\n\n\n\n<p>The reason is that the <code>EXISTS<\/code> operator works based on the &#8220;at least found&#8221; principle. The <code>EXISTS<\/code> stops scanning the table when a matching row is found.<\/p>\n\n\n\n<p>On the other hand, when the <code>IN<\/code> operator is combined with a subquery, MySQL must process the subquery first and then use the result of the subquery to process the whole query.<\/p>\n\n\n\n<p>The general rule of thumb is that if the subquery contains a large volume of data, the <code>EXISTS<\/code> operator provides better performance.<\/p>\n\n\n\n<p>However, the query that uses the <code>IN<\/code> operator will perform faster if the result set returned from the subquery is very small.<\/p>\n\n\n\n<p>For example, the following statement uses the <code>IN<\/code> operator to select all employees who work at the office in San Francisco.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    employeenumber, \n    firstname, \n    lastname\n<span class=\"hljs-keyword\">FROM<\/span>\n    employees\n<span class=\"hljs-keyword\">WHERE<\/span>\n    officeCode <span class=\"hljs-keyword\">IN<\/span> (<span class=\"hljs-keyword\">SELECT<\/span> \n            officeCode\n        <span class=\"hljs-keyword\">FROM<\/span>\n            offices\n        <span class=\"hljs-keyword\">WHERE<\/span>\n            offices.city = <span class=\"hljs-string\">'San Francisco'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/queryhttps:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/#7\">Try It Out<\/a><\/p>\n\n\n\n<p>Let&#8217;s check the performance of the query.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"326\" height=\"269\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-IN-vs-EXISTS.jpg\" alt=\"MySQL IN vs EXISTS\" class=\"wp-image-4924\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-IN-vs-EXISTS.jpg 326w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-IN-vs-EXISTS-300x248.jpg 300w\" sizes=\"auto, (max-width: 326px) 100vw, 326px\" \/><\/figure>\n\n\n\n<p>It is a little bit faster than the query that uses the <code>EXISTS<\/code> operator that we mentioned in the first example. See the performance of the query that uses the <code>EXIST<\/code> operator below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"315\" height=\"281\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-vs-IN-IN-is-faster.jpg\" alt=\"MySQL EXISTS vs IN - IN is faster\" class=\"wp-image-4925\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-vs-IN-IN-is-faster.jpg 315w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2016\/01\/MySQL-EXISTS-vs-IN-IN-is-faster-300x268.jpg 300w\" sizes=\"auto, (max-width: 315px) 100vw, 315px\" \/><\/figure>\n\n\n\n<p>In this tutorial, you have learned how to use the MySQL <code>EXISTS<\/code> operator to test for the existence of rows returned by a subquery.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"4917\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/\"\n\t\t\t\tdata-post-title=\"MySQL EXISTS\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"4917\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/\"\n\t\t\t\tdata-post-title=\"MySQL EXISTS\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use the MySQL EXISTS operator and when to use it to improve the performance of the queries.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":26,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4917","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL EXISTS<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use the MySQL EXISTS operator and when to use it to increase the performance of the queries.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL EXISTS\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use the MySQL EXISTS operator and when to use it to increase the performance of the queries.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-03T10:26:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-exists\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-exists\\\/\",\"name\":\"MySQL EXISTS\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-exists\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-exists\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/customers-orders.png\",\"datePublished\":\"2013-01-22T06:38:40+00:00\",\"dateModified\":\"2024-01-03T10:26:14+00:00\",\"description\":\"This tutorial shows you how to use the MySQL EXISTS operator and when to use it to increase the performance of the queries.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-exists\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-exists\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-exists\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/customers-orders.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/customers-orders.png\",\"width\":434,\"height\":304},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-exists\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Basics\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL EXISTS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL EXISTS","description":"This tutorial shows you how to use the MySQL EXISTS operator and when to use it to increase the performance of the queries.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/","og_locale":"en_US","og_type":"article","og_title":"MySQL EXISTS","og_description":"This tutorial shows you how to use the MySQL EXISTS operator and when to use it to increase the performance of the queries.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/","og_site_name":"MySQL Tutorial","article_modified_time":"2024-01-03T10:26:14+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/","url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/","name":"MySQL EXISTS","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png","datePublished":"2013-01-22T06:38:40+00:00","dateModified":"2024-01-03T10:26:14+00:00","description":"This tutorial shows you how to use the MySQL EXISTS operator and when to use it to increase the performance of the queries.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png","width":434,"height":304},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-exists\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Basics","item":"https:\/\/www.mysqltutorial.org\/mysql-basics\/"},{"@type":"ListItem","position":3,"name":"MySQL EXISTS"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/4917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=4917"}],"version-history":[{"count":3,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/4917\/revisions"}],"predecessor-version":[{"id":14071,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/4917\/revisions\/14071"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/174"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=4917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}