{"id":852,"date":"2025-02-18T18:28:10","date_gmt":"2025-02-18T10:28:10","guid":{"rendered":"https:\/\/eve2333.top\/?p=852"},"modified":"2025-02-18T18:28:11","modified_gmt":"2025-02-18T10:28:11","slug":"mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e5%85%a5%e9%97%a8%e5%88%b0%e5%a4%a7%e8%9b%87%e5%b0%9a%e7%a1%85%e8%b0%b7%e5%ae%8b%e7%ba%a2%e5%ba%b7%e8%80%81%e5%b8%88%e7%ac%94%e8%ae%b0-%e5%9f%ba%e7%a1%80%e7%af%87-pa-6","status":"publish","type":"post","link":"https:\/\/eve2333.top\/?p=852","title":{"rendered":"MySQL\u6570\u636e\u5e93\u5165\u95e8\u5230\u5927\u86c7\u5c1a\u7845\u8c37\u5b8b\u7ea2\u5eb7\u8001\u5e08\u7b14\u8bb0 \u57fa\u7840\u7bc7 part 9"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">&nbsp;\u7b2c09\u7ae0_\u5b50\u67e5\u8be2<\/h1>\n\n\n\n<p>\u5b50\u67e5\u8be2\u6307\u4e00\u4e2a\u67e5\u8be2\u8bed\u53e5\u5d4c\u5957\u5728\u53e6\u4e00\u4e2a\u67e5\u8be2\u8bed\u53e5\u5185\u90e8\u7684\u67e5\u8be2\uff0c\u8fd9\u4e2a\u7279\u6027\u4eceMySQL 4.1\u5f00\u59cb\u5f15\u5165\u3002 SQL \u4e2d\u5b50\u67e5\u8be2\u7684\u4f7f\u7528\u5927\u5927\u589e\u5f3a\u4e86 SELECT \u67e5\u8be2\u7684\u80fd\u529b\uff0c\u56e0\u4e3a\u5f88\u591a\u65f6\u5019\u67e5\u8be2\u9700\u8981\u4ece\u7ed3\u679c\u96c6\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u6216\u8005\u9700\u8981\u4ece\u540c\u4e00\u4e2a\u8868\u4e2d\u5148\u8ba1\u7b97\u5f97\u51fa\u4e00\u4e2a\u6570\u636e\u7ed3\u679c\uff0c\u7136\u540e\u4e0e\u8fd9\u4e2a\u6570\u636e\u7ed3\u679c\uff08\u53ef\u80fd\u662f\u67d0\u4e2a\u6807\u91cf\uff0c\u4e5f\u53ef\u80fd\u662f\u67d0\u4e2a\u96c6\u5408\uff09\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u6709\u4e00\u4e2a\u9700\u6c42\uff1a\u8c01\u7684\u5de5\u8d44\u6bd4Abel\u7684\u9ad8\uff1f<br>\u65b9\u5f0f1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT salary\nFROM employees\nWHERE last_name ='Abel'\n\nSELECT last_name,salary\nFROM employees\nWHERE salary &gt; 11000;<\/code><\/pre>\n\n\n\n<p>&nbsp;#\u65b9\u5f0f2\uff1a\u81ea\u8fde\u63a5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT e2.last_name,e2.salary\nFROM employees el,employees e2\nWHERE e2.`salary` &gt; el.`salary` #\u591a\u8868\u7684\u8fde\u63a5\u6761\u4ef6\nAND e1.last_name 'Abel';<\/code><\/pre>\n\n\n\n<p>#\u65b9\u5f0f3\uff1a\u5b50\u67e5\u8be2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT last _name,salary\nFROM employees\nWHERE salary&gt;(\n                SELECT salary\n                FROM employees\n                WHERE last_name= 'Abel'\n             );<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b50\u67e5\u8be2\uff08\u5185\u67e5\u8be2\uff09\u5728\u4e3b\u67e5\u8be2\u4e4b\u524d\u4e00\u6b21\u6267\u884c\u5b8c\u6210\u3002<\/li>\n\n\n\n<li>\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u88ab\u4e3b\u67e5\u8be2\uff08\u5916\u67e5\u8be2\uff09\u4f7f\u7528 \u3002<\/li>\n\n\n\n<li>\u6ce8\u610f\u4e8b\u9879\u00a0 \u5b50\u67e5\u8be2\u8981\u5305\u542b\u5728\u62ec\u53f7\u5185<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5c06\u5b50\u67e5\u8be2\u653e\u5728\u6bd4\u8f83\u6761\u4ef6\u7684\u53f3\u4fa7<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5355\u884c\u64cd\u4f5c\u7b26\u5bf9\u5e94\u5355\u884c\u5b50\u67e5\u8be2\uff0c\u591a\u884c\u64cd\u4f5c\u7b26\u5bf9\u5e94\u591a\u884c\u5b50\u67e5\u8be2<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3 \u5b50\u67e5\u8be2\u7684\u5206\u7c7b<\/h3>\n\n\n\n<p>\u5206\u7c7b\u65b9\u5f0f1\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><img loading=\"lazy\" decoding=\"async\" height=\"487\" width=\"856\" src=\"https:\/\/i-blog.csdnimg.cn\/direct\/dcdc475a55d24b9a9ea59c490f3fa1aa.png\" alt=\"\"><\/h3>\n\n\n\n<p>&nbsp;\u5206\u7c7b\u65b9\u5f0f2\uff1a<\/p>\n\n\n\n<p>\u6211\u4eec\u6309\u5185\u67e5\u8be2\u662f\u5426\u88ab\u6267\u884c\u591a\u6b21\uff0c\u5c06\u5b50\u67e5\u8be2\u5212\u5206\u4e3a\u76f8\u5173(\u6216\u5173\u8054)\u5b50\u67e5\u8be2\u548c \u4e0d\u76f8\u5173(\u6216\u975e\u5173\u8054)\u5b50\u67e5\u8be2\u3002<\/p>\n\n\n\n<p>\u5b50\u67e5\u8be2\u4ece\u6570\u636e\u8868\u4e2d\u67e5\u8be2\u4e86\u6570\u636e\u7ed3\u679c\uff0c\u5982\u679c\u8fd9\u4e2a\u6570\u636e\u7ed3\u679c\u53ea\u6267\u884c\u4e00\u6b21\uff0c\u7136\u540e\u8fd9\u4e2a\u6570\u636e\u7ed3\u679c\u4f5c\u4e3a\u4e3b\u67e5\u8be2\u7684\u6761\u4ef6\u8fdb\u884c\u6267\u884c\uff0c\u90a3\u4e48\u8fd9\u6837\u7684\u5b50\u67e5\u8be2\u53eb\u505a\u4e0d\u76f8\u5173\u5b50\u67e5\u8be2\u3002<\/p>\n\n\n\n<p>\u540c\u6837\uff0c\u5982\u679c\u5b50\u67e5\u8be2\u9700\u8981\u6267\u884c\u591a\u6b21\uff0c\u5373\u91c7\u7528\u5faa\u73af\u7684\u65b9\u5f0f\uff0c\u5148\u4ece\u5916\u90e8\u67e5\u8be2\u5f00\u59cb\uff0c\u6bcf\u6b21\u90fd\u4f20\u5165\u5b50\u67e5\u8be2\u8fdb\u884c\u67e5 \u8be2\uff0c\u7136\u540e\u518d\u5c06\u7ed3\u679c\u53cd\u9988\u7ed9\u5916\u90e8\uff0c\u8fd9\u79cd\u5d4c\u5957\u7684\u6267\u884c\u65b9\u5f0f\u5c31\u79f0\u4e3a\u76f8\u5173\u5b50\u67e5\u8be2\u3002<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u6bd4\u5982\uff1a\u76f8\u5173\u5b50\u67e5\u8be2\u7684\u9700\u6c42\uff1a\u67e5\u8be2\u5de5\u8d44\u5927\u4e8e\u672c\u90e8\u95e8\u5e73\u5747\u5de5\u8d44\u7684\u5458\u5de5\u4fe1\u606f\u3002<br>\u4e0d\u76f8\u5173\u5b50\u67e5\u8be2\u7684\u9700\u6c42\uff1a\u67e5\u8be2\u5de5\u8d44\u5927\u4e8e\u672c\u516c\u53f8\u5e73\u5747\u5de5\u8d44\u7684\u5458\u5de5\u4fe1\u606f<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">&nbsp;2. \u5355\u884c\u5b50\u67e5\u8be2<\/h2>\n\n\n\n<p>2.1 \u5355\u884c\u6bd4\u8f83\u64cd\u4f5c\u7b26<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u64cd\u4f5c\u7b26<\/td><td>\u542b\u4e49<\/td><\/tr><tr><td>=<\/td><td>equal to<\/td><\/tr><tr><td>&gt;<\/td><td>greater than<\/td><\/tr><tr><td>&gt;=<\/td><td>greater than or equal to<\/td><\/tr><tr><td>&lt;<\/td><td>less than<\/td><\/tr><tr><td>&lt;=<\/td><td>less than or equal to<\/td><\/tr><tr><td>&lt;&gt;<\/td><td>not equal to<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u9898\u76ee\uff1a\u67e5\u8be2\u5de5\u8d44\u5927\u4e8e149\u53f7\u5458\u5de5\u5de5\u8d44\u7684\u5458\u5de5\u7684\u4fe1\u606f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT employee_id,last_name,salary\nFROM employees\nWHERE salary &gt; (\n                SELECT salary\n                FROM employees\n                WHERE employee_id = 149\n                );<\/code><\/pre>\n\n\n\n<p><strong>#\u9898\u76ee\uff1a\u8fd4\u56dejob_id\u4e0e141\u53f7\u5458\u5de5\u76f8\u540c\uff0csalary\u6bd4143\u53f7\u5458\u5de5\u591a\u7684\u5458\u5de5\u59d3\u540d\uff0cjob_id\u548c\u5de5\u8d44<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT last_name, job_id, salary\nFROM employees\nWHERE job_id = (\n    SELECT job_id\n    FROM employees\n    WHERE employee_id = 141\n)\nAND salary &gt; (\n    SELECT salary\n    FROM employees\n    WHERE employee_id = 143\n);<\/code><\/pre>\n\n\n\n<p>\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\u662f , \u5148\u6a21\u4eff\u90a3\u4e2a\u5206\u4e24\u6b65\u5b8c\u6210\u7684 , \u7136\u540e ,\u5957\u5165\u683c\u5f0f\u5373\u53ef<\/p>\n\n\n\n<p><strong>#\u9898\u76ee\uff1a\u67e5\u8be2\u4e0e141\u53f7\u5458\u5de5\u7684manager_id\u548cdepartment_id\u76f8\u540c\u7684\u5176\u4ed6\u5458\u5de5\u7684employee_id\uff0cmanager_id\uff0cdepartment_id\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT employee_id,manager_id,department_id\nFROM employees\nWHERE manager_id =(\n                    SELECT manager_id\n                    FROM employees\n                    WHERE employee_id = (141,174) #in\u662f\u591a\u884c\u7684\u65f6\u5019\u7528\u7684\uff0c\u8fd9\u91cc\u88ab\u6539\u6210\u4e86=  \n                    }\nAND department_id=(\n                    SELECT department_id\n                    FROM employees\n                    WHERE employee_id =141\n                    )\nAND employee_id &lt;&gt; 141;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u65b9\u5f0f2\uff1a\u4e86\u89e3\nSELECT employee_id, manager_id, department_id\nFROM employees\nWHERE (manager_id, department_id) = (\n    SELECT manager_id, department_id\n    FROM employees\n    WHERE employee_id = 141\n)\nAND employee_id &lt;&gt; 141;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">HAVING \u4e2d\u7684\u5b50\u67e5\u8be2<\/h3>\n\n\n\n<p>#<strong>\u9898\u76ee\uff1a\u67e5\u8be2\u6700\u4f4e\u5de5\u8d44\u5927\u4e8e50\u53f7\u90e8\u95e8\u6700\u4f4e\u5de5\u8d44\u7684\u90e8\u95e8id\u548c\u5176\u6700\u4f4e\u5de5\u8d44<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT department_id, MIN(salary)\nFROM employees\nWHERE department_id IS NOT NULL\nGROUP BY department_id\nHAVING MIN(salary) &gt; (\n    SELECT MIN(salary)\n    FROM employees\n    WHERE department_id = 50\n);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">CASE\u4e2d\u7684\u5b50\u67e5\u8be2<\/h3>\n\n\n\n<p><strong>#\u9898\u76ee\uff1a\u663e\u5f0f\u5458\u5de5\u7684employee_id,last_name\u548clocation\u3002<br>#\u5176\u4e2d\uff0c\u82e5\u5458\u5de5department_id\u4e0elocation_id\u4e3a1800\u7684department_id\u76f8\u540c\uff0c<br>#\u5219location\u4e3a\u2019Canada\u2019\uff0c\u5176\u4f59\u5219\u4e3a\u2019USA\u2019\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT employee_id,last_name,CASE department_id WHEN (SELECT department_id FROM departments WHERE location_id = 1800) THEN 'Canada'\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ELSE 'USA' END \"location\"\nFROM employees;\n\n#\u5b50\u67e5\u8be2\uff0c\u53ea\u80fd\u8fd4\u56de\u4e00\u884c\u6570\u636e\uff0c\u8fd4\u56de\u591a\u6761\u67e5\u8be2\u8bed\u53e5\u4f1a\u62a5\u9519\u7684\n#\u683c\u5f0f\u5316\u540e\u5982\u4e0b\nSELECT employee_id, last_name,\n       (CASE department_id\n        WHEN\n             (SELECT department_id FROM departments\n          WHERE location_id = 1800)           \n        THEN 'Canada' ELSE 'USA' END) location\nFROM   employees;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b50\u67e5\u8be2\u4e2d\u7684\u7a7a\u503c\u95ee\u9898&nbsp;<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT last_name, job_id\n FROM   employees\n WHERE  job_id =\n                (SELECT job_id\n                 FROM   employees\n                 WHERE  last_name = 'Haas');<\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" height=\"69\" width=\"895\" src=\"https:\/\/i-blog.csdnimg.cn\/direct\/78f66bd04da541cdac9d62baba4a5dab.png\" alt=\"\"><strong>\u5b50\u67e5\u8be2\u4e0d\u8fd4\u56de\u4efb\u4f55\u884c<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u975e\u6cd5\u4f7f\u7528\u5b50\u67e5\u8be2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code> SELECT employee_id, last_name\n FROM   employees\n WHERE  salary =            #\u5355\u884c\u64cd\u4f5c\u7b26\u4e0d\u80fd\u64cd\u4f5c\u591a\u884c\u7ed3\u679c\u96c6 \u7528IN\u624d\u5bf9\n                (SELECT   MIN(salary)\n                 FROM     employees\n                 GROUP BY department_id);\n<\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" height=\"209\" width=\"904\" src=\"https:\/\/i-blog.csdnimg.cn\/direct\/cfc2ec8426a34ba1b1be8871c8f192bd.png\" alt=\"\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u591a\u884c\u5b50\u67e5\u8be2<\/h2>\n\n\n\n<p>\u4e5f\u79f0\u4e3a\u96c6\u5408\u6bd4\u8f83\u5b50\u67e5\u8be2 \u5185\u67e5\u8be2\u8fd4\u56de\u591a\u884c \u4f7f\u7528\u591a\u884c\u6bd4\u8f83\u64cd\u4f5c\u7b26&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 \u591a\u884c\u6bd4\u8f83\u64cd\u4f5c\u7b26<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>IN<\/td><td>\u7b49\u4e8e\u5217\u8868\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a<\/td><\/tr><tr><td>ANY<\/td><td>\u9700\u8981\u548c\u5355\u884c\u6bd4\u8f83\u64cd\u4f5c\u7b26\u4e00\u8d77\u4f7f\u7528\uff0c\u548c\u5b50\u67e5\u8be2\u8fd4\u56de\u7684<strong>\u67d0\u4e00\u4e2a\u503c<\/strong>\u6bd4\u8f83<\/td><\/tr><tr><td>ALL<\/td><td>\u9700\u8981\u548c\u5355\u884c\u6bd4\u8f83\u64cd\u4f5c\u7b26\u4e00\u8d77\u4f7f\u7528\uff0c\u548c\u5b50\u67e5\u8be2\u8fd4\u56de<strong>\u7684\u6240\u6709\u503c<\/strong>\u6bd4\u8f83<\/td><\/tr><tr><td>SOME<\/td><td>\u5b9e\u9645\u4e0a\u662fANY\u7684\u522b\u540d\uff0c\u4f5c\u7528\u76f8\u540c\uff0c\u4e00\u822c\u5e38\u4f7f\u7528ANY<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>in\u548cany : in \u4ee3\u8868\u7b49\u4e8e\u67d0\u4e00\u4e2a \u800cany\u53ef\u4ee5\u7528\u5927\u4e8e\/\u5c0f\u4e8e\/\u7b49\u4e8e\u67d0\u4e00\u4e2a<\/p>\n\n\n\n<p>&nbsp;\u5f53\u6211\u4eec\u8fd9\u9898\u8001\u5e08\u7b54\u6848\u5339\u914d\u7684\u662f\u5de5\u8d44\u7b49\u4e8e\u5404\u90e8\u95e8\u6700\u4f4e\u5de5\u8d44\u4e4b\u4e00\u7684\u5458\u5de5&nbsp; ,\u8981\u60f3\u5339\u914d\u5404\u90e8\u95e8\u7684\u6700\u4f4e\u5de5\u8d44\u5458\u5de5\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a\u5c06IN \u548c=\u66ff\u6362&nbsp; , \u56e0\u4e3a\u6b64\u65f6\u662f\u4e00\u6837\u7684.<\/p>\n\n\n\n<p><strong>&nbsp;\u9898\u76ee\uff1a\u8fd4\u56de\u5176\u5b83job_id\u4e2d\u6bd4job_id\u4e3a\u2018IT_PROG\u2019\u90e8\u95e8\u4efb\u4e00\u5de5\u8d44\u4f4e\u7684\u5458\u5de5\u7684\u5458\u5de5\u53f7\u3001\u59d3\u540d\u3001job_id \u4ee5\u53casalary<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT employee_id,last_name,job_id,salary\nFROM employees\nWHERE job_id &lt;&gt; 'IT_PROG'\nAND salary &lt; ANY (\n\t\tSELECT salary\n\t\tFROM employees\n\t\tWHERE job_id = 'IT_PROG'\n\t\t);<\/code><\/pre>\n\n\n\n<p><strong>#\u9898\u76ee\uff1a\u8fd4\u56de\u5176\u5b83job_id\u4e2d\u6bd4job_id\u4e3a\u2018IT_PROG\u2019\u90e8\u95e8\u6240\u6709\u5de5\u8d44\u4f4e\u7684\u5458\u5de5\u7684\u5458\u5de5\u53f7\u3001<\/strong><\/p>\n\n\n\n<p><strong>#\u59d3\u540d\u3001job_id \u4ee5\u53casalary<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT employee_id,last_name,job_id,salary\n\nFROM employees\n\nWHERE job_id &lt;&gt; 'IT_PROG'\n\nAND salary &lt; ALL (\n\n&nbsp; &nbsp; &nbsp; &nbsp; SELECT salary\n\n&nbsp; &nbsp; &nbsp; &nbsp; FROM employees\n\n&nbsp; &nbsp; &nbsp; &nbsp; WHERE job_id = 'IT_PROG'\n\n&nbsp; &nbsp; &nbsp; &nbsp; );<\/code><\/pre>\n\n\n\n<p><strong>#\u9898\u76ee\uff1a\u67e5\u8be2\u5e73\u5747\u5de5\u8d44\u6700\u4f4e\u7684\u90e8\u95e8id<\/strong><\/p>\n\n\n\n<p><strong>#MySQL\u4e2d\u805a\u5408\u51fd\u6570\u662f\u4e0d\u80fd\u5d4c\u5957\u4f7f\u7528\u7684\u3002\u5982\u56feoracle\u5c31\u53ef\u4ee5\u76f4\u63a5\u5d4c\u5957\u7684<\/strong><img loading=\"lazy\" decoding=\"async\" height=\"185\" width=\"224\" src=\"https:\/\/i-blog.csdnimg.cn\/direct\/80ecd349655448c280e80ed1f742c6f9.png\" alt=\"\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u65b9\u5f0f1\uff1a\nSELECT department_id\nFROM employees\nGROUP BY department_id\nHAVING AVG(salary) = (\n\t\t\tSELECT MIN(avg_sal)\n\t\t\tFROM(\n\t\t\t\tSELECT AVG(salary) avg_sal\n\t\t\t\tFROM employees\n\t\t\t\tGROUP BY department_id\n\t\t\t\t) t_dept_avg_sal\n\t\t\t);\n\n#\u65b9\u5f0f2\uff1a\nSELECT department_id\nFROM employees\nGROUP BY department_id\nHAVING AVG(salary) &lt;= ALL(\t\n\t\t\tSELECT AVG(salary) avg_sal\n\t\t\tFROM employees\n\t\t\tGROUP BY department_id\n\t\t\t) <\/code><\/pre>\n\n\n\n<p>\u62c6\u89e3\u9898\u76ee\u5c31\u5b8c\u4e8b\u4e86\uff0c\u628a\u80fd\u505a\u7684\u70b9\u505a\u51fa\u6765\uff0c\u7136\u540e\u6362\u5143\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 \u7a7a\u503c\u95ee\u9898<\/h3>\n\n\n\n<p>\u5728\u5185\u67e5\u8be2\u4e2d\u6709\u4e00\u4e2anull\uff0c\u90a3\u4e48\u5728null\u7684\u6761\u4ef6\u4e0b\uff0c\u63a5\u7740where\u7b49\u7b49\u5224\u65ad\u5c31\u662f\u4e00\u4e2a\u7a7a\u7684\u60c5\u51b5\u4e86\uff08\u4e0enull\u8fd0\u7b97\u90fd\u4e3anull\uff09<\/p>\n\n\n\n<p>IN\u662f\u4e0e\u4efb\u4e00\u7684\u503c\u6bd4\u8f83\u7684\uff0c\u52a0\u4e86NOT\u5c31\u662f\u4e0e\u5168\u90e8\u6bd4\u4e86<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">&nbsp;4. \u76f8\u5173\u5b50\u67e5\u8be2<\/h2>\n\n\n\n<p>\u5982\u679c\u5b50\u67e5\u8be2\u7684\u6267\u884c\u4f9d\u8d56\u4e8e\u5916\u90e8\u67e5\u8be2\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u90fd\u662f\u56e0\u4e3a\u5b50\u67e5\u8be2\u4e2d\u7684\u8868\u7528\u5230\u4e86\u5916\u90e8\u7684\u8868\uff0c\u5e76\u8fdb\u884c\u4e86\u6761\u4ef6 \u5173\u8054\uff0c\u56e0\u6b64\u6bcf\u6267\u884c\u4e00\u6b21\u5916\u90e8\u67e5\u8be2\uff0c\u5b50\u67e5\u8be2\u90fd\u8981\u91cd\u65b0\u8ba1\u7b97\u4e00\u6b21\uff0c\u8fd9\u6837\u7684\u5b50\u67e5\u8be2\u5c31\u79f0\u4e4b\u4e3a \u5173\u8054\u5b50\u67e5\u8be2\u3002<\/p>\n\n\n\n<p>\u76f8\u5173\u5b50\u67e5\u8be2\u6309\u7167\u4e00\u884c\u63a5\u4e00\u884c\u7684\u987a\u5e8f\u6267\u884c\uff0c\u4e3b\u67e5\u8be2\u7684\u6bcf\u4e00\u884c\u90fd\u6267\u884c\u4e00\u6b21\u5b50\u67e5\u8be2\u3002<img loading=\"lazy\" decoding=\"async\" height=\"482\" width=\"682\" src=\"https:\/\/i-blog.csdnimg.cn\/direct\/6430145732a54fb18b57fcd3135dc4fe.png\" alt=\"\"><\/p>\n\n\n\n<p><strong>\u8bf4\u660e\uff1a\u5b50\u67e5\u8be2\u4e2d\u4f7f\u7528\u4e3b\u67e5\u8be2\u4e2d\u7684\u5217&nbsp;<\/strong><\/p>\n\n\n\n<p>&nbsp;\u53ef\u4ee5\u7406\u89e3\u4e3a\u5b50\u67e5\u8be2\u662f\u4e00\u4e2a\u5e38\u6570,\u800c\u76f8\u5173\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u5219\u4e3a\u4e00\u4e2a\u53d8\u91cfx<\/p>\n\n\n\n<p>\u9996\u5148\u53d6\u5916\u8868\u7684\u8bb0\u5f55\uff0c\u7136\u540e\u628a\u8bb0\u5f55\u7ed9\u4ed6\u9001\u8fdb\u53bb\uff0c\u7136\u540e\u9001\u8fdb\u53bb\u7684\u6570\u636e\uff0c\u7528\u7684\uff08\uff09\u91cc\u9762\u7684\u8bb0\u5f55\u7684\u54ea\u4e00\u4e2a\u5b57\u6bb5\uff0c\u5373\u5185\u90e8\u67e5\u8be2\u5f00\u59cb\u8ba1\u7b97\u4e86\uff0c\u6240\u4ee5\u53ebEXEUCUTE\uff0c\u5b50\u67e5\u8be2\u7684\u4f7f\u7528\u4e3b\u67e5\u8be2\u7684\u8fd9\u4e2a\u6570\u636e\uff0c\u8fd0\u7b97\u5b8c\u540e\u8fd4\u56de\u4e00\u4e2aselect\u7684\u8fd9\u6837\u7684\u4e00\u4e2a\u7ed3\u679c\u3002\u7b2c\u4e09\u6b65\u662f\u7b2c\u4e00\u6b65\u9001\u7684\u8bb0\u5f55\u548c\u7b2c\u4e8c\u6b65\u8fd4\u56de\u7684\u8fd9\u6761\u8bb0\u5f55\u5462\u53bb\u505a\u4e00\u4e2a\u6bd4\u8f83\uff0c\u770b\u770b\u662f\u5426\u6ee1\u8db3\u6bd4\u8f83\u7684\u6761\u4ef6\u3002<\/p>\n\n\n\n<p><strong>\u9898\u76ee\uff1a\u67e5\u8be2\u5458\u5de5\u4e2d\u5de5\u8d44\u5927\u4e8e\u672c\u90e8\u95e8\u5e73\u5747\u5de5\u8d44\u7684\u5458\u5de5\u7684last_name,salary\u548c\u5176department_id<\/strong><\/p>\n\n\n\n<p>\u65b9\u5f0f\u4e00\uff1a\u76f8\u5173\u5b50\u67e5\u8be2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>\u5176\u5b9e\u8fd9\u4e2a\u5b50\u67e5\u8be2\uff0c\u5c31\u770b\u6210\u4e24\u5c42for\u5faa\u73af\uff0c\u7b2c\u4e00\u5c42\u6bcf\u4e00\u4e2a\u53d8\u91cf\u6267\u884c\u5185\u5c42for\u7684\u5faa\u73af\u5c31\u884c\u4e86<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT last_name, salary, department_id\nFROM employees e1\nWHERE salary &gt; (\n&nbsp; &nbsp; SELECT AVG(salary)\n&nbsp; &nbsp; FROM employees e2\n    WHERE department_id =e1 .`department_id`\n);<\/code><\/pre>\n\n\n\n<p>\u65b9\u5f0f\u4e8c\uff1a\u5728 FROM \u4e2d\u4f7f\u7528\u5b50\u67e5\u8be2&nbsp; &nbsp; &nbsp; &nbsp; \u65b9\u5f0f\u4e8c\u662fsql92\u7684\u5185\u8fde\u63a5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT last_name,salary,e1.department_id\n FROM employees e1,(SELECT department_id,AVG(salary) dept_avg_sal FROM employees GROUP \nBY department_id) e2\n WHERE e1.`department_id` = e2.department_id\n AND e2.dept_avg_sal &lt; e1.`salary`;\n<\/code><\/pre>\n\n\n\n<p><strong>\u9898\u76ee\uff1a\u67e5\u8be2\u5458\u5de5\u7684id,salary,\u6309\u7167department_name \u6392\u5e8f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> SELECT employee_id,salary\n FROM employees e\n ORDER BY (\n SELECT department_name\n FROM departments d\n WHERE e.`department_id` = d.`department_id`\n );<\/code><\/pre>\n\n\n\n<p><strong>\u9898\u76ee\uff1a\u82e5employees\u8868\u4e2demployee_id\u4e0ejob_history\u8868\u4e2demployee_id\u76f8\u540c\u7684\u6570\u76ee\u4e0d\u5c0f\u4e8e2\uff0c\u8f93\u51fa\u8fd9\u4e9b\u76f8\u540c id\u7684\u5458\u5de5\u7684employee_id,last_name\u548c\u5176job_id<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT e.employee_id, last_name,e.job_id\nFROM   employees e \nWHERE  2 &lt;= (SELECT COUNT(*)\n            FROM job_history\n            WHERE employee_id = e.employee_id);\n\n\nSELECT *\nFROM job_history;\n\nSELECT employee_id, last_name, job_id\nFROM employees e\nWHERE 2 &lt;= (\n    SELECT COUNT(*)\n    FROM job_history j\n    WHERE e.employee_id = j.employee_id\n);<\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" height=\"231\" width=\"278\" src=\"https:\/\/i-blog.csdnimg.cn\/direct\/b329f661bfd142a7ba8f5461efed25ab.png\" alt=\"\">\u90a3\u4e48\u5728\u54ea\u4e9b\u4f4d\u7f6e\u53ef\u4ee5\u5199\u5b50\u67e5\u8be2\u5462\uff1f<strong>\u7ed3\u8bba\uff1a\u5728SELECT\u4e2d\uff0c\u9664\u4e86GROUP BY\u548cLIMIT\u4e4b\u5916\uff0c\u5176\u4ed6\u4f4d\u7f6e\u90fd\u53ef\u4ee5\u58f0\u660e\u5b50\u67e5\u8be2\uff01<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 EXISTS \u4e0e NOT EXISTS\u5173\u952e\u5b57<\/h3>\n\n\n\n<p>\u5173\u8054\u5b50\u67e5\u8be2\u901a\u5e38\u4e5f\u4f1a\u548c EXISTS\u64cd\u4f5c\u7b26\u4e00\u8d77\u6765\u4f7f\u7528\uff0c\u7528\u6765\u68c0\u67e5\u5728\u5b50\u67e5\u8be2\u4e2d\u662f\u5426\u5b58\u5728\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u5728\u5b50\u67e5\u8be2\u4e2d\u4e0d\u5b58\u5728\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\uff1a<br>\u6761\u4ef6\u8fd4\u56de FALSE \u7ee7\u7eed\u5728\u5b50\u67e5\u8be2\u4e2d\u67e5\u627e<\/p>\n\n\n\n<p>\u5982\u679c\u5728\u5b50\u67e5\u8be2\u4e2d\u5b58\u5728\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\uff1a<br>\u4e0d\u5728\u5b50\u67e5\u8be2\u4e2d\u7ee7\u7eed\u67e5\u627e \u6761\u4ef6\u8fd4\u56de TRUE<\/p>\n\n\n\n<p>NOT EXISTS\u5173\u952e\u5b57\u8868\u793a\u5982\u679c\u4e0d\u5b58\u5728\u67d0\u79cd\u6761\u4ef6\uff0c\u5219\u8fd4\u56deTRUE\uff0c\u5426\u5219\u8fd4\u56deFALSE\u3002<\/p>\n\n\n\n<p><strong>\u9898\u76ee\uff1a\u67e5\u8be2\u516c\u53f8\u7ba1\u7406\u8005\u7684employee_id\uff0clast_name\uff0cjob_id\uff0cdepartment_id\u4fe1\u606f&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u65b9\u5f0f1\uff1a\u81ea\u8fde\u63a5\nSELECT DISTINCT mgr.employee_id, mgr.last_name, mgr.job_id, mgr.department_id\nFROM employees emp JOIN employees mgr\nON emp.manager_id = mgr.employee_id;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u65b9\u5f0f2\uff1a\u5b50\u67e5\u8be2\nSELECT employee_id, last_name, job_id, department_id\nFROM employees\nWHERE employee_id IN (\n    SELECT DISTINCT manager_id\n    FROM employees\n);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u65b9\u5f0f3\uff1a\u4f7f\u7528EXISTS\nSELECT employee_id, last_name, job_id, department_id\nFROM employees e1\nWHERE EXISTS (\n    SELECT *\n    FROM employees e2\n    WHERE e1.manager_id = e2.employee_id\n);<\/code><\/pre>\n\n\n\n<p>WHERE EXISTS\u5b50\u53e5\u7684\u76ee\u7684\u662f\u68c0\u67e5\u662f\u5426\u81f3\u5c11\u5b58\u5728\u4e00\u884c\u6ee1\u8db3\u6761\u4ef6\uff0c\u800c\u4e0d\u662f\u8fd4\u56de\u6240\u6709\u5339\u914d\u5230\u7684\u884c\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>2. \u56e0\u6b64\u5f53\u7b26\u5408\u8fde\u63a5\u6761\u4ef6\u7684\u51fa\u73b0\u4e00\u5bf9\u591a\u65f6\u5c31\u4f1a\u4ea7\u751f\u591a\u6761\u7ed3\u679c<br>3.\u5728\u6b64\u4f8b\u4e2d\u5c31\u5305\u542b\u4e86 emp \u548c mgr \u8868\u4e2d\u90fd\u7b26\u5408 emp.employee_id=mgr.magager_id \u7684\u7ed3\u679c\u96c6<\/p>\n<\/blockquote>\n\n\n\n<p><strong>\u9898\u76ee\uff1a\u67e5\u8be2departments\u8868\u4e2d\uff0c\u4e0d\u5b58\u5728\u4e8eemployees\u8868\u4e2d\u7684\u90e8\u95e8\u7684department_id\u548cdepartment_name&nbsp;<\/strong><a href=\"https:\/\/www.bilibili.com\/video\/BV1iq4y1u7vj?t=2168.1&amp;p=46\" target=\"_blank\"  rel=\"nofollow\" >46-\u76f8\u5173\u5b50\u67e5\u8be2\u6848\u4f8b\u5206\u6790_\u54d4\u54e9\u54d4\u54e9_bilibili<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u65b9\u5f0f1\uff1a\nSELECT d.department_id, d.department_name\nFROM employees e RIGHT JOIN departments d\nON e.`department_id` = d.`department_id`\nWHERE e.`department_id` IS NULL;\n\n\n#\u65b9\u5f0f2\uff1a\nSELECT department_id, department_name\nFROM departments d\nWHERE NOT EXISTS (\n    SELECT *\n    FROM employees e\n    WHERE d.`department_id` = e.`department_id`\n);\n\nSELECT COUNT(*)\nFROM departments;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">&nbsp;4.4 \u76f8\u5173\u66f4\u65b0<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">4.4 \u76f8\u5173\u5220\u9664<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u629b\u4e00\u4e2a\u601d\u8003\u9898 \u95ee\u9898\uff1a\u8c01\u7684\u5de5\u8d44\u6bd4Abel\u7684\u9ad8\uff1f<\/h3>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bfe\u540e\u7ec3\u4e60<\/h2>\n\n\n\n<p>&nbsp;<a href=\"https:\/\/www.bilibili.com\/video\/BV1iq4y1u7vj?spm_id_from=333.788.player.switch&amp;vd_source=b31958e33027799323b8b7d77ae181d4&amp;p=47\" target=\"_blank\"  rel=\"nofollow\" >47-\u7b2c9\u7ae0\u5b50\u67e5\u8be2\u8bfe\u540e\u7ec3\u4e601_\u54d4\u54e9\u54d4\u54e9_bilibili<\/a><br><a href=\"https:\/\/www.bilibili.com\/video\/BV1iq4y1u7vj?spm_id_from=333.788.player.switch&amp;vd_source=b31958e33027799323b8b7d77ae181d4&amp;p=48\" target=\"_blank\"  rel=\"nofollow\" >48-\u7b2c9\u7ae0\u5b50\u67e5\u8be2\u8bfe\u540e\u7ec3\u4e602_\u54d4\u54e9\u54d4\u54e9_bilibili<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#1. \u67e5\u8be2\u548czlotkey\u76f8\u540c\u90e8\u95e8\u7684\u5458\u5de5\u59d3\u540d\u548c\u5de5\u8d44\nSELECT last_name, salary\nFROM employees\nWHERE department_id = (SELECT department_id FROM employees WHERE last_name = 'Zlotkey');\n\n#2. \u67e5\u8be2\u5de5\u8d44\u6bd4\u516c\u53f8\u5e73\u5747\u5de5\u8d44\u9ad8\u7684\u5458\u5de5\u7684\u5458\u5de5\u53f7\uff0c\u59d3\u540d\u548c\u5de5\u8d44\u3002\nSELECT employee_id, last_name, salary\nFROM employees\nWHERE salary > (SELECT AVG(salary) FROM employees);\n\n#3. \u9009\u62e9\u5de5\u8d44\u5927\u4e8e\u6240\u6709JOB_ID = 'SA_MAN'\u7684\u5458\u5de5\u7684\u5de5\u8d44\u7684\u5458\u5de5\u7684last_name\uff0cjob_id\uff0csalary\nSELECT last_name, job_id, salary\nFROM employees\nWHERE salary > ALL (SELECT salary FROM employees WHERE job_id = 'SA_MAN');\n\n#4. \u67e5\u8be2\u548c\u59d3\u540d\u4e2d\u5305\u542b\u5b57\u6bcdu\u7684\u5458\u5de5\u5728\u76f8\u540c\u90e8\u95e8\u7684\u5458\u5de5\u7684\u5458\u5de5\u53f7\u548c\u59d3\u540d\nSELECT e1.employee_id, e1.last_name\nFROM employees e1\nJOIN employees e2 ON e1.department_id = e2.department_id\nWHERE e2.last_name LIKE '%u%';\n\n#5. \u67e5\u8be2\u5728\u90e8\u95e8\u7684location_id\u4e3a1700\u7684\u90e8\u95e8\u5de5\u4f5c\u7684\u5458\u5de5\u7684\u5458\u5de5\u53f7\nSELECT employee_id\nFROM employees\nWHERE department_id = (SELECT department_id FROM departments WHERE location_id = 1700);\n\n#6. \u67e5\u8be2\u7ba1\u7406\u8005\u662fKing\u7684\u5458\u5de5\u59d3\u540d\u548c\u5de5\u8d44\nSELECT last_name, salary\nFROM employees\nWHERE manager_id = (SELECT employee_id FROM employees WHERE last_name = 'King');\n\n#7. \u67e5\u8be2\u5de5\u8d44\u6700\u4f4e\u7684\u5458\u5de5\u4fe1\u606f\uff1alast_name\uff0csalary\nSELECT last_name, salary\nFROM employees\nORDER BY salary ASC\nLIMIT 1;\n\n#8. \u67e5\u8be2\u5e73\u5747\u5de5\u8d44\u6700\u4f4e\u7684\u90e8\u95e8\u4fe1\u606f\nSELECT department_id, department_name\nFROM departments\nWHERE department_id = (\n  SELECT department_id FROM (\n    SELECT department_id, AVG(salary) AS avg_salary\n    FROM employees\n    GROUP BY department_id\n  ) AS subquery\n  ORDER BY avg_salary ASC\n  LIMIT 1\n);\n\n#9. \u67e5\u8be2\u5e73\u5747\u5de5\u8d44\u6700\u4f4e\u7684\u90e8\u95e8\u4fe1\u606f\u548c\u8be5\u90e8\u95e8\u7684\u5e73\u5747\u5de5\u8d44\uff08\u76f8\u5173\u5b50\u67e5\u8be2\uff09\nSELECT d.department_id, d.department_name, AVG(e.salary) AS avg_salary\nFROM departments d\nJOIN employees e ON d.department_id = e.department_id\nGROUP BY d.department_id\nORDER BY avg_salary ASC\nLIMIT 1;\n\n#10. \u67e5\u8be2\u5e73\u5747\u5de5\u8d44\u6700\u9ad8\u7684job\u4fe1\u606f\nSELECT job_id, job_title\nFROM jobs\nWHERE job_id = (\n  SELECT job_id FROM (\n    SELECT job_id, AVG(salary) AS avg_salary\n    FROM employees\n    GROUP BY job_id\n  ) AS subquery\n  ORDER BY avg_salary DESC\n  LIMIT 1\n);\n\n#11. \u67e5\u8be2\u5e73\u5747\u5de5\u8d44\u9ad8\u4e8e\u516c\u53f8\u5e73\u5747\u5de5\u8d44\u7684\u90e8\u95e8\u6709\u54ea\u4e9b\uff1f\nSELECT department_id, department_name\nFROM departments\nWHERE department_id IN (\n  SELECT department_id FROM (\n    SELECT department_id, AVG(salary) AS avg_salary\n    FROM employees\n    GROUP BY department_id\n  ) AS subquery\n  WHERE avg_salary > (SELECT AVG(salary) FROM employees)\n);\n\n#12. \u67e5\u8be2\u51fa\u516c\u53f8\u4e2d\u6240\u6709manager\u7684\u8be6\u7ec6\u4fe1\u606f\nSELECT * FROM employees WHERE job_id = 'MGR';\n\n#13. \u5404\u4e2a\u90e8\u95e8\u4e2d\u6700\u9ad8\u5de5\u8d44\u4e2d\u6700\u4f4e\u7684\u90a3\u4e2a\u90e8\u95e8\u7684\u6700\u4f4e\u5de5\u8d44\u662f\u591a\u5c11\uff1f\nSELECT MIN(max_salary) AS min_max_salary\nFROM (\n  SELECT MAX(salary) AS max_salary\n  FROM employees\n  GROUP BY department_id\n) AS subquery;\n\n#14. \u67e5\u8be2\u5e73\u5747\u5de5\u8d44\u6700\u9ad8\u7684\u90e8\u95e8\u7684manager\u7684\u8be6\u7ec6\u4fe1\u606f\uff1alast_name\uff0cdepartment_id\uff0cemail\uff0csalary\nSELECT e.last_name, e.department_id, e.email, e.salary\nFROM employees e\nJOIN (\n  SELECT department_id FROM (\n    SELECT department_id, AVG(salary) AS avg_salary\n    FROM employees\n    GROUP BY department_id\n  ) AS subquery\n  ORDER BY avg_salary DESC\n  LIMIT 1\n) AS d ON e.department_id = d.department_id AND e.job_id = 'MGR';\n\n#15. \u67e5\u8be2\u90e8\u95e8\u7684\u90e8\u95e8\u53f7\uff0c\u5176\u4e2d\u4e0d\u5305\u62ecjob_id\u662f\"ST_CLERK\"\u7684\u90e8\u95e8\u53f7\nSELECT department_id\nFROM departments\nWHERE department_id NOT IN (\n  SELECT department_id FROM employees WHERE job_id = 'ST_CLERK'\n);\n\n#16. \u9009\u62e9\u6240\u6709\u6ca1\u6709\u7ba1\u7406\u8005\u7684\u5458\u5de5\u7684last_name\nSELECT last_name FROM employees WHERE manager_id IS NULL;\n\n#17. \u67e5\u8be2\u5458\u5de5\u53f7\u3001\u59d3\u540d\u3001\u96c7\u7528\u65f6\u95f4\u3001\u5de5\u8d44\uff0c\u5176\u4e2d\u5458\u5de5\u7684\u7ba1\u7406\u8005\u4e3a'De Haan'\nSELECT employee_id, last_name, hire_date, salary\nFROM employees\nWHERE manager_id = (SELECT employee_id FROM employees WHERE last_name = 'De Haan');\n\n#18. \u67e5\u8be2\u5404\u90e8\u95e8\u4e2d\u5de5\u8d44\u6bd4\u672c\u90e8\u95e8\u5e73\u5747\u5de5\u8d44\u9ad8\u7684\u5458\u5de5\u7684\u5458\u5de5\u53f7\uff0c\u59d3\u540d\u548c\u5de5\u8d44\uff08\u76f8\u5173\u5b50\u67e5\u8be2\uff09\nSELECT e1.employee_id, e1.last_name, e1.salary\nFROM employees e1\nWHERE e1.salary > (\n  SELECT AVG(e2.salary) FROM employees e2 WHERE e2.department_id = e1.department_id\n);\n\n#19. \u67e5\u8be2\u6bcf\u4e2a\u90e8\u95e8\u4e0b\u7684\u90e8\u95e8\u4eba\u6570\u5927\u4e8e5\u7684\u90e8\u95e8\u540d\u79f0\uff08\u76f8\u5173\u5b50\u67e5\u8be2\uff09\nSELECT department_name\nFROM departments\nWHERE department_id IN (\n  SELECT department_id FROM (\n    SELECT department_id, COUNT(*) AS num_employees\n    FROM employees\n    GROUP BY department_id\n  ) AS subquery\n  WHERE num_employees > 5\n);\n\n#20. \u67e5\u8be2\u6bcf\u4e2a\u56fd\u5bb6\u4e0b\u7684\u90e8\u95e8\u4e2a\u6570\u5927\u4e8e2\u7684\u56fd\u5bb6\u7f16\u53f7\uff08\u76f8\u5173\u5b50\u67e5\u8be2\uff09\nSELECT country_id\nFROM countries\nWHERE country_id IN (\n  SELECT country_id FROM (\n    SELECT country_id, COUNT(*) AS num_departments\n    FROM departments\n    GROUP BY country_id\n  ) AS subquery\n  WHERE num_departments > 2\n);<img loading=\"lazy\" decoding=\"async\" height=\"15\" width=\"15\" src=\"blob:https:\/\/eve2333.top\/e9dce0af-f795-4a73-ab08-5df72c71078e\"><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;\u7b2c09\u7ae0_\u5b50\u67e5\u8be2 \u5b50\u67e5\u8be2\u6307\u4e00\u4e2a\u67e5\u8be2\u8bed\u53e5\u5d4c\u5957\u5728\u53e6\u4e00\u4e2a\u67e5\u8be2\u8bed\u53e5\u5185\u90e8\u7684\u67e5\u8be2\uff0c\u8fd9\u4e2a\u7279\u6027\u4eceMySQL 4.1\u5f00\u59cb\u5f15\u5165\u3002 SQL  &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[2],"tags":[16],"class_list":["post-852","post","type-post","status-publish","format-standard","hentry","category-2","tag-sql"],"_links":{"self":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=852"}],"version-history":[{"count":1,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/852\/revisions"}],"predecessor-version":[{"id":853,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/852\/revisions\/853"}],"wp:attachment":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}