{"id":3965,"date":"2025-06-07T08:26:13","date_gmt":"2025-06-07T08:26:13","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3965"},"modified":"2025-06-07T10:00:14","modified_gmt":"2025-06-07T10:00:14","slug":"sql-exists-any-all","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-exists-any-all\/","title":{"rendered":"SQL EXISTS \/ ANY \/ ALL"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">These keywords are used with subqueries to perform advanced comparisons.<br>Let\u2019s break them down simply:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <code>EXISTS<\/code> \u2013 Check if Subquery Returns Rows<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Returns <code>TRUE<\/code> if the subquery returns <strong>at least one row<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT department_name\nFROM departments d\nWHERE EXISTS (\n  SELECT 1\n  FROM employees e\n  WHERE e.department_id = d.department_id\n);<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Lists departments that <strong>have employees<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <code>ANY<\/code> \u2013 Compare with <strong>Any Matching Value<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Returns <code>TRUE<\/code> if <strong>at least one<\/strong> value from the subquery meets the condition.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT employee_name, salary\nFROM employees\nWHERE salary &gt; ANY (\n  SELECT salary\n  FROM employees\n  WHERE department_id = 50\n);<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Fetches employees earning <strong>more than at least one<\/strong> employee in dept 50.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also use: <code>= ANY<\/code> (equivalent to <code>IN<\/code>), <code>&lt;&gt; ANY<\/code>, <code>&lt; ANY<\/code>, etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <code>ALL<\/code> \u2013 Compare with <strong>All Values<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Returns <code>TRUE<\/code> if the condition holds true for <strong>every<\/strong> value from the subquery.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT employee_name, salary\nFROM employees\nWHERE salary &gt; ALL (\n  SELECT salary\n  FROM employees\n  WHERE department_id = 50\n);<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Fetches employees earning <strong>more than everyone<\/strong> in dept 50.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Quick Recap<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Keyword<\/th><th>Purpose<\/th><th>Returns TRUE if&#8230;<\/th><\/tr><\/thead><tbody><tr><td><code>EXISTS<\/code><\/td><td>Subquery returns any row<\/td><td>At least one row is found<\/td><\/tr><tr><td><code>ANY<\/code><\/td><td>Comparison true for <strong>at least one<\/strong><\/td><td>Condition matches one or more values<\/td><\/tr><tr><td><code>ALL<\/code><\/td><td>Comparison true for <strong>every<\/strong> value<\/td><td>Condition matches all values in subquery<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udca1 Use these for <strong>powerful filtering<\/strong> when comparing against multiple rows.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>These keywords are used with subqueries to perform advanced comparisons.Let\u2019s break them down simply: \ud83d\udd39 EXISTS \u2013 Check if Subquery Returns Rows Returns TRUE if the subquery returns at least one row. \u2705 Lists departments that have employees. \ud83d\udd39 ANY \u2013 Compare with Any Matching Value Returns TRUE if at least one value from the [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3965","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}