{"id":2210,"date":"2023-07-27T23:00:55","date_gmt":"2023-07-27T15:00:55","guid":{"rendered":"http:\/\/viplao.com\/?p=2210"},"modified":"2023-07-27T23:21:06","modified_gmt":"2023-07-27T15:21:06","slug":"%e5%b7%a5%e4%bd%9c%e4%b8%ad%e5%b8%b8%e7%94%a8sql%e8%af%ad%e5%8f%a5-%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"http:\/\/viplao.com\/index.php\/2023\/07\/27\/%e5%b7%a5%e4%bd%9c%e4%b8%ad%e5%b8%b8%e7%94%a8sql%e8%af%ad%e5%8f%a5-%e5%ae%9e%e8%b7%b5\/","title":{"rendered":"\u5de5\u4f5c\u4e2d\u5e38\u7528SQL\u8bed\u53e5 \u5b9e\u8df5"},"content":{"rendered":"\n<p>\u5de5\u4f5c\u4e2d\u6211\u4eec\u57fa\u672c\u4e0a\u6bcf\u5929\u90fd\u8981\u4e0e\u6570\u636e\u5e93\u6253\u4ea4\u9053\uff0c\u6570\u636e\u5e93\u7684\u77e5\u8bc6\u70b9\u5462\u4e5f\u7279\u522b\u591a\uff0c\u5168\u90e8\u8bb0\u4f4f\u5462\u4e5f\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u5b9e\u8df5\u4e00\u904d\u5904\u7406\u8d77\u6765\u6548\u7387\u5c31\u66f4\u9ad8<\/p>\n\n\n\n<p>Student(S#,Sname,Sage,Ssex) \u5b66\u751f\u8868<\/p>\n\n\n\n<p>Course(C#,Cname,T#) \u8bfe\u7a0b\u8868<\/p>\n\n\n\n<p>SC(S#,C#,score) \u6210\u7ee9\u8868<\/p>\n\n\n\n<p>Teacher(T#,Tname) \u6559\u5e08\u8868<\/p>\n\n\n\n<p>\u95ee\u9898\uff1a<\/p>\n\n\n\n<p><strong>\u200b1<\/strong>\u3001\u67e5\u8be2\u201c<strong>001<\/strong>\u201d\u8bfe\u7a0b\u6bd4\u201c<strong>002<\/strong>\u201d\u8bfe\u7a0b\u6210\u7ee9\u9ad8\u7684\u6240\u6709\u5b66\u751f\u7684\u5b66\u53f7\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select a.S# from (select s#,score from SC where C#='001') a,(select s#,score\n from SC where C#='002') b\n\u200b where a.score&gt;b.score and a.s#=b.s#;<\/code><\/pre>\n\n\n\n<p><strong>2<\/strong>\u3001\u67e5\u8be2\u5e73\u5747\u6210\u7ee9\u5927\u4e8e60\u5206\u7684\u540c\u5b66\u7684\u5b66\u53f7\u548c\u5e73\u5747\u6210\u7ee9\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select S#,avg(score)\n    from sc\n    group by S# having avg(score) &gt;60;<\/code><\/pre>\n\n\n\n<p><strong>3<\/strong>\u3001\u67e5\u8be2\u6240\u6709\u540c\u5b66\u7684\u5b66\u53f7\u3001\u59d3\u540d\u3001\u9009\u8bfe\u6570\u3001\u603b\u6210\u7ee9\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select Student.S#,Student.Sname,count(SC.C#),sum(score)\n from Student left Outer join SC on Student.S#=SC.S#\n group by Student.S#,Sname;<\/code><\/pre>\n\n\n\n<p><strong>4<\/strong>\u3001\u67e5\u8be2\u59d3\u201c\u674e\u201d\u7684\u8001\u5e08\u7684\u4e2a\u6570\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select count(distinct(Tname))\n from Teacher\n where Tname like '\u674e%';<\/code><\/pre>\n\n\n\n<p><strong>5<\/strong>\u3001\u67e5\u8be2\u6ca1\u5b66\u8fc7\u201c\u53f6\u5e73<\/p>\n\n\n\n<p>\u201d\u8001\u5e08\u8bfe\u7684\u540c\u5b66\u7684\u5b66\u53f7\u3001\u59d3\u540d\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select Student.S#,Student.Sname\n    from Student \n    where S# not in (select distinct( SC.S#) from SC,Course,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='\u53f6\u5e73');<\/code><\/pre>\n\n\n\n<p><strong>6<\/strong>\u3001\u67e5\u8be2\u5b66\u8fc7\u201c<strong>001<\/strong>\u201d\u5e76\u4e14\u4e5f\u5b66\u8fc7\u7f16\u53f7\u201c<strong>002<\/strong>\u201d\u8bfe\u7a0b\u7684\u540c\u5b66\u7684\u5b66\u53f7\u3001\u59d3\u540d\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#='001'and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#='002');<\/code><\/pre>\n\n\n\n<p><strong>7<\/strong>\u3001\u67e5\u8be2\u5b66\u8fc7\u201c\u53f6\u5e73\u201d\u8001\u5e08\u6240\u6559\u7684\u6240\u6709\u8bfe\u7684\u540c\u5b66\u7684\u5b66\u53f7\u3001\u59d3\u540d\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select S#,Sname from Student where S# in (select S# from SC ,Course ,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='\u53f6\u5e73' group by S# having count(SC.C#)=(select count(C#) from Course,Teacher where Teacher.T#=Course.T# and Tname='\u53f6\u5e73'));<\/code><\/pre>\n\n\n\n<p><strong>8<\/strong>\u3001\u67e5\u8be2\u8bfe\u7a0b\u7f16\u53f7\u201c<strong>002<\/strong>\u201d\u7684\u6210\u7ee9\u6bd4\u8bfe\u7a0b\u7f16\u53f7\u201c<strong>001<\/strong>\u201d\u8bfe\u7a0b\u4f4e\u7684\u6240\u6709\u540c\u5b66\u7684\u5b66\u53f7\u3001\u59d3\u540d\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Select S#,Sname from (select Student.S#,Student.Sname,score ,(select score from SC SC_2 where SC_2.S#=Student.S# and SC_2.C#='002') score2<\/code><\/pre>\n\n\n\n<p><strong>9<\/strong>\u3001\u67e5\u8be2\u6240\u6709\u8bfe\u7a0b\u6210\u7ee9\u5c0f\u4e8e60\u5206\u7684\u540c\u5b66\u7684\u5b66\u53f7\u3001\u59d3\u540d\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select S#,Sname\n from Student\n where S# not in (select Student.S# from Student,SC where S.S#=SC.S# and score&gt;60);<\/code><\/pre>\n\n\n\n<p><strong>10<\/strong>\u3001\u67e5\u8be2\u6ca1\u6709\u5b66\u5168\u6240\u6709\u8bfe\u7684\u540c\u5b66\u7684\u5b66\u53f7\u3001\u59d3\u540d\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select Student.S#,Student.Sname\n    from Student,SC\n    where Student.S#=SC.S# group by Student.S#,Student.Sname having count(C#) &lt;(select count(C#) from Course);<\/code><\/pre>\n\n\n\n<p><strong>11<\/strong>\u3001\u67e5\u8be2\u81f3\u5c11\u6709\u4e00\u95e8\u8bfe\u4e0e\u5b66\u53f7\u4e3a\u201c<strong>1001<\/strong>\u201d\u7684\u540c\u5b66\u6240\u5b66\u76f8\u540c\u7684\u540c\u5b66\u7684\u5b66\u53f7\u548c\u59d3\u540d\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select S#,Sname from Student,SC where Student.S#=SC.S# and C# in select C# from SC where S#='1001';<\/code><\/pre>\n\n\n\n<p><strong>12<\/strong>\u3001\u67e5\u8be2\u81f3\u5c11\u5b66\u8fc7\u5b66\u53f7\u4e3a\u201c<strong>001<\/strong>\u201d\u540c\u5b66\u6240\u6709\u4e00\u95e8\u8bfe\u7684\u5176\u4ed6\u540c\u5b66\u5b66\u53f7\u548c\u59d3\u540d\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select distinct SC.S#,Sname\n    from Student,SC\n    where Student.S#=SC.S# and C# in (select C# from SC where S#='001');<\/code><\/pre>\n\n\n\n<p><strong>13<\/strong>\u3001\u628a\u201cSC\u201d\u8868\u4e2d\u201c\u53f6\u5e73\u201d\u8001\u5e08\u6559\u7684\u8bfe\u7684\u6210\u7ee9\u90fd\u66f4\u6539\u4e3a\u6b64\u8bfe\u7a0b\u7684\u5e73\u5747\u6210\u7ee9\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>update SC set score=(select avg(SC_2.score)\n    from SC SC_2\n    where SC_2.C#=SC.C# ) from Course,Teacher where Course.C#=SC.C# and Course.T#=Teacher.T# and Teacher.Tname='\u53f6\u5e73');<\/code><\/pre>\n\n\n\n<p><strong>14<\/strong>\u3001\u67e5\u8be2\u548c\u201c<strong>1002<\/strong>\u201d\u53f7\u7684\u540c\u5b66\u5b66\u4e60\u7684\u8bfe\u7a0b\u5b8c\u5168\u76f8\u540c\u7684\u5176\u4ed6\u540c\u5b66\u5b66\u53f7\u548c\u59d3\u540d\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select S# from SC where C# in (select C# from SC where S#='1002')\n    group by S# having count(*)=(select count(*) from SC where S#='1002');<\/code><\/pre>\n\n\n\n<p><strong>15<\/strong>\u3001\u5220\u9664\u5b66\u4e60\u201c\u53f6\u5e73\u201d\u8001\u5e08\u8bfe\u7684SC\u8868\u8bb0\u5f55\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delete SC from course ,Teacher where Course.C#=SC.C# and Course.T#= Teacher.T# and Tname='\u53f6\u5e73';<\/code><\/pre>\n\n\n\n<p><strong>16<\/strong>\u3001\u5411SC\u8868\u4e2d\u63d2\u5165\u4e00\u4e9b\u8bb0\u5f55\uff0c\u8fd9\u4e9b\u8bb0\u5f55\u8981\u6c42\u7b26\u5408\u4ee5\u4e0b\u6761\u4ef6\uff1a\u6ca1\u6709\u4e0a\u8fc7\u7f16\u53f7\u201c<strong>003<\/strong>\u201d\u8bfe\u7a0b\u7684\u540c\u5b66\u5b66\u53f7\u3001<strong>2<\/strong>\u3001\u53f7\u8bfe\u7684\u5e73\u5747\u6210\u7ee9\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Insert SC select S#,'002',(Select avg(score)\n    from SC where C#='002') from Student where S# not in (Select S# from SC where C#='002');<\/code><\/pre>\n\n\n\n<p><strong>17<\/strong>\u3001\u6309\u5e73\u5747\u6210\u7ee9\u4ece\u9ad8\u5230\u4f4e\u663e\u793a\u6240\u6709\u5b66\u751f\u7684\u201c\u6570\u636e\u5e93\u201d\u3001\u201c\u4f01\u4e1a\u7ba1\u7406<\/p>\n\n\n\n<p>\u201d\u3001\u201c\u82f1\u8bed\u201d\u4e09\u95e8\u7684\u8bfe\u7a0b\u6210\u7ee9\uff0c\u6309\u5982\u4e0b\u5f62\u5f0f\u663e\u793a\uff1a\u5b66\u751fID,,\u6570\u636e\u5e93,\u4f01\u4e1a\u7ba1\u7406,\u82f1\u8bed,\u6709\u6548\u8bfe\u7a0b\u6570,\u6709\u6548\u5e73\u5747\u5206<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT S# as \u5b66\u751fID\n        ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='004') AS \u6570\u636e\u5e93\n        ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='001') AS \u4f01\u4e1a\u7ba1\u7406\n        ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='006') AS \u82f1\u8bed\n        ,COUNT(*) AS \u6709\u6548\u8bfe\u7a0b\u6570, AVG(t.score) AS \u5e73\u5747\u6210\u7ee9\n    FROM SC AS t\n    GROUP BY S#\n    ORDER BY avg(t.score)<\/code><\/pre>\n\n\n\n<p><strong>18<\/strong>\u3001\u67e5\u8be2\u5404\u79d1\u6210\u7ee9\u6700\u9ad8\u548c\u6700\u4f4e\u7684\u5206\uff1a\u4ee5\u5982\u4e0b\u5f62\u5f0f\u663e\u793a\uff1a\u8bfe\u7a0bID\uff0c\u6700\u9ad8\u5206\uff0c\u6700\u4f4e\u5206<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT L.C# As \u8bfe\u7a0bID,L.score AS \u6700\u9ad8\u5206,R.score AS \u6700\u4f4e\u5206\n    FROM SC L ,SC AS R\n    WHERE L.C# = R.C# and\n        L.score = (SELECT MAX(IL.score)\n                      FROM SC AS IL,Student AS IM\n                      WHERE L.C# = IL.C# and IM.S#=IL.S#\n                      GROUP BY IL.C#)\n        AND\n        R.Score = (SELECT MIN(IR.score)\n                      FROM SC AS IR\n                      WHERE R.C# = IR.C#\n                  GROUP BY IR.C#\n                    );<\/code><\/pre>\n\n\n\n<p><strong>19<\/strong>\u3001\u6309\u5404\u79d1\u5e73\u5747\u6210\u7ee9\u4ece\u4f4e\u5230\u9ad8\u548c\u53ca\u683c\u7387\u7684\u767e\u5206\u6570\u4ece\u9ad8\u5230\u4f4e\u987a\u5e8f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT t.C# AS \u8bfe\u7a0b\u53f7,max(course.Cname)AS \u8bfe\u7a0b\u540d,isnull(AVG(score),0) AS \u5e73\u5747\u6210\u7ee9\n        ,100 * SUM(CASE WHEN isnull(score,0)&gt;=60 THEN 1 ELSE 0 END)\/COUNT(*) AS \u53ca\u683c\u767e\u5206\u6570\n    FROM SC T,Course\n    where t.C#=course.C#\n    GROUP BY t.C#\n    ORDER BY 100 * SUM(CASE WHEN isnull(score,0)&gt;=60 THEN 1 ELSE 0 END)\/COUNT(*) DESC<\/code><\/pre>\n\n\n\n<p><strong>20<\/strong>\u3001\u67e5\u8be2\u5982\u4e0b\u8bfe\u7a0b\u5e73\u5747\u6210\u7ee9\u548c\u53ca\u683c\u7387\u7684\u767e\u5206\u6570(\u7528&#8221;1\u884c&#8221;\u663e\u793a): \u4f01\u4e1a\u7ba1\u7406\uff08<strong>001<\/strong>\uff09\uff0c\u9a6c\u514b\u601d\uff08<strong>002<\/strong>\uff09\uff0cOO&amp;UML \uff08<strong>003<\/strong>\uff09\uff0c\u6570\u636e\u5e93\uff08<strong>004<\/strong>\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT SUM(CASE WHEN C# ='001' THEN score ELSE 0 END)\/SUM(CASE C# WHEN '001' THEN 1 ELSE 0 END) AS \u4f01\u4e1a\u7ba1\u7406\u5e73\u5747\u5206\n        ,100 * SUM(CASE WHEN C# = '001' AND score &gt;= 60 THEN 1 ELSE 0 END)\/SUM(CASE WHEN C# = '001' THEN 1 ELSE 0 END) AS \u4f01\u4e1a\u7ba1\u7406\u53ca\u683c\u767e\u5206\u6570\n        ,SUM(CASE WHEN C# = '002' THEN score ELSE 0 END)\/SUM(CASE C# WHEN '002' THEN 1 ELSE 0 END) AS \u9a6c\u514b\u601d\u5e73\u5747\u5206\n        ,100 * SUM(CASE WHEN C# = '002' AND score &gt;= 60 THEN 1 ELSE 0 END)\/SUM(CASE WHEN C# = '002' THEN 1 ELSE 0 END) AS \u9a6c\u514b\u601d\u53ca\u683c\u767e\u5206\u6570\n        ,SUM(CASE WHEN C# = '003' THEN score ELSE 0 END)\/SUM(CASE C# WHEN '003' THEN 1 ELSE 0 END) AS UML\u5e73\u5747\u5206\n        ,100 * SUM(CASE WHEN C# = '003' AND score &gt;= 60 THEN 1 ELSE 0 END)\/SUM(CASE WHEN C# = '003' THEN 1 ELSE 0 END) AS UML\u53ca\u683c\u767e\u5206\u6570\n        ,SUM(CASE WHEN C# = '004' THEN score ELSE 0 END)\/SUM(CASE C# WHEN '004' THEN 1 ELSE 0 END) AS \u6570\u636e\u5e93\u5e73\u5747\u5206\n        ,100 * SUM(CASE WHEN C# = '004' AND score &gt;= 60 THEN 1 ELSE 0 END)\/SUM(CASE WHEN C# = '004' THEN 1 ELSE 0 END) AS \u6570\u636e\u5e93\u53ca\u683c\u767e\u5206\u6570\n FROM SC;<\/code><\/pre>\n\n\n\n<p><strong>21<\/strong>\u3001\u67e5\u8be2\u4e0d\u540c\u8001\u5e08\u6240\u6559\u4e0d\u540c\u8bfe\u7a0b\u5e73\u5747\u5206\u4ece\u9ad8\u5230\u4f4e\u663e\u793a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT max(Z.T#) AS \u6559\u5e08ID,MAX(Z.Tname) AS \u6559\u5e08\u59d3\u540d,C.C# AS \u8bfe\u7a0b\uff29\uff24,MAX(C.Cname) AS \u8bfe\u7a0b\u540d\u79f0,AVG(Score) AS \u5e73\u5747\u6210\u7ee9\n    FROM SC AS T,Course AS C ,Teacher AS Z\n    where T.C#=C.C# and C.T#=Z.T#\n GROUP BY C.C#\n ORDER BY AVG(Score) DESC;<\/code><\/pre>\n\n\n\n<p><strong>22<\/strong>\u3001\u67e5\u8be2\u5982\u4e0b\u8bfe\u7a0b\u6210\u7ee9\u7b2c&nbsp;<strong>3<\/strong>&nbsp;\u540d\u5230\u7b2c&nbsp;<strong>6<\/strong>&nbsp;\u540d\u7684\u5b66\u751f\u6210\u7ee9\u5355\uff1a\u4f01\u4e1a\u7ba1\u7406\uff08<strong>001<\/strong>\uff09\uff0c\u9a6c\u514b\u601d\uff08<strong>002<\/strong>\uff09\uff0cUML \uff08<strong>003<\/strong>\uff09\uff0c\u6570\u636e\u5e93\uff08<strong>004<\/strong>\uff09<\/p>\n\n\n\n<p>[\u5b66\u751fID],[\u5b66\u751f\u59d3\u540d],\u4f01\u4e1a\u7ba1\u7406,\u9a6c\u514b\u601d,UML,\u6570\u636e\u5e93,\u5e73\u5747\u6210\u7ee9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DISTINCT top 3\n      SC.S# As \u5b66\u751f\u5b66\u53f7,\n        Student.Sname AS \u5b66\u751f\u59d3\u540d ,\n      T1.score AS \u4f01\u4e1a\u7ba1\u7406,\n      T2.score AS \u9a6c\u514b\u601d,\n      T3.score AS UML,\n      T4.score AS \u6570\u636e\u5e93,\n      ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) as \u603b\u5206\n      FROM Student,SC LEFT JOIN SC AS T1\n                      ON SC.S# = T1.S# AND T1.C# = '001'\n            LEFT JOIN SC AS T2\n                      ON SC.S# = T2.S# AND T2.C# = '002'\n            LEFT JOIN SC AS T3\n                      ON SC.S# = T3.S# AND T3.C# = '003'\n            LEFT JOIN SC AS T4\n                      ON SC.S# = T4.S# AND T4.C# = '004'\n      WHERE student.S#=SC.S# and\n      ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)\n      NOT IN\n      (SELECT\n            DISTINCT\n            TOP 15 WITH TIES\n            ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)\n      FROM sc\n            LEFT JOIN sc AS T1\n                      ON sc.S# = T1.S# AND T1.C# = 'k1'\n           LEFT JOIN sc AS T2\n                      ON sc.S# = T2.S# AND T2.C# = 'k2'\n            LEFT JOIN sc AS T3\n                      ON sc.S# = T3.S# AND T3.C# = 'k3'\n            LEFT JOIN sc AS T4\n                      ON sc.S# = T4.S# AND T4.C# = 'k4'\n      ORDER BY ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) DESC);<\/code><\/pre>\n\n\n\n<p><strong>23<\/strong>\u3001\u7edf\u8ba1\u5217\u5370\u5404\u79d1\u6210\u7ee9,\u5404\u5206\u6570\u6bb5<\/p>\n\n\n\n<p>\u4eba\u6570:\u8bfe\u7a0bID,\u8bfe\u7a0b\u540d\u79f0,[100-85],[85-70],[70-60],[ &lt;60]<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT SC.C# as \u8bfe\u7a0bID, Cname as \u8bfe\u7a0b\u540d\u79f0\n        ,SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS &#91;100 - 85]\n        ,SUM(CASE WHEN score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS &#91;85 - 70]\n        ,SUM(CASE WHEN score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS &#91;70 - 60]\n        ,SUM(CASE WHEN score &lt; 60 THEN 1 ELSE 0 END) AS &#91;60 -]\n    FROM SC,Course\n    where SC.C#=Course.C#\n    GROUP BY SC.C#,Cname;<\/code><\/pre>\n\n\n\n<p><strong>24<\/strong>\u3001\u67e5\u8be2\u5b66\u751f\u5e73\u5747\u6210\u7ee9\u53ca\u5176\u540d\u6b21<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT 1+(SELECT COUNT( distinct \u5e73\u5747\u6210\u7ee9)\n              FROM (SELECT S#,AVG(score) AS \u5e73\u5747\u6210\u7ee9\n                      FROM SC\n                  GROUP BY S#\n                  ) AS T1\n            WHERE \u5e73\u5747\u6210\u7ee9 &gt; T2.\u5e73\u5747\u6210\u7ee9) as \u540d\u6b21,\n      S# as \u5b66\u751f\u5b66\u53f7,\u5e73\u5747\u6210\u7ee9\n    FROM (SELECT S#,AVG(score) \u5e73\u5747\u6210\u7ee9\n            FROM SC\n        GROUP BY S#\n        ) AS T2\n    ORDER BY \u5e73\u5747\u6210\u7ee9 desc;<\/code><\/pre>\n\n\n\n<p><strong>25<\/strong>\u3001\u67e5\u8be2\u5404\u79d1\u6210\u7ee9\u524d\u4e09\u540d\u7684\u8bb0\u5f55:(\u4e0d\u8003\u8651\u6210\u7ee9\u5e76\u5217\u60c5\u51b5)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT t1.S# as \u5b66\u751fID,t1.C# as \u8bfe\u7a0bID,Score as \u5206\u6570\n      FROM SC t1\n      WHERE score IN (SELECT TOP 3 score\n              FROM SC\n              WHERE t1.C#= C#\n            ORDER BY score DESC\n              )\n      ORDER BY t1.C#;<\/code><\/pre>\n\n\n\n<p><strong>26<\/strong>\u3001\u67e5\u8be2\u6bcf\u95e8\u8bfe\u7a0b\u88ab\u9009\u4fee\u7684\u5b66\u751f\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select c#,count(S#) from sc group by C#;<\/code><\/pre>\n\n\n\n<p><strong>27<\/strong>\u3001\u67e5\u8be2\u51fa\u53ea\u9009\u4fee\u4e86\u4e00\u95e8\u8bfe\u7a0b\u7684\u5168\u90e8\u5b66\u751f\u7684\u5b66\u53f7\u548c\u59d3\u540d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select SC.S#,Student.Sname,count(C#) AS \u9009\u8bfe\u6570\n from SC ,Student\n where SC.S#=Student.S# group by SC.S# ,Student.Sname having count(C#)=1;<\/code><\/pre>\n\n\n\n<p><strong>28<\/strong>\u3001\u67e5\u8be2\u7537\u751f\u3001\u5973\u751f\u4eba\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Select count(Ssex) as \u7537\u751f\u4eba\u6570 from Student group by Ssex having Ssex='\u7537';\n    Select count(Ssex) as \u5973\u751f\u4eba\u6570 from Student group by Ssex having Ssex='\u5973';<\/code><\/pre>\n\n\n\n<p><strong>29<\/strong>\u3001\u67e5\u8be2\u59d3\u201c\u5f20\u201d\u7684\u5b66\u751f\u540d\u5355<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT Sname FROM Student WHERE Sname like '\u5f20%';<\/code><\/pre>\n\n\n\n<p><strong>30<\/strong>\u3001\u67e5\u8be2\u540c\u540d\u540c\u6027\u5b66\u751f\u540d\u5355\uff0c\u5e76\u7edf\u8ba1\u540c\u540d\u4eba\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select Sname,count(*) from Student group by Sname having count(*)&gt;1;;<\/code><\/pre>\n\n\n\n<p><strong>31<\/strong>\u30011981\u5e74\u51fa\u751f\u7684\u5b66\u751f\u540d\u5355(\u6ce8\uff1aStudent\u8868\u4e2dSage\u5217\u7684\u7c7b\u578b\u662fdatetime)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>elect Sname, CONVERT(char (11),DATEPART(year,Sage)) as age\n    from student\n    where CONVERT(char(11),DATEPART(year,Sage))='1981';<\/code><\/pre>\n\n\n\n<p><strong>32<\/strong>\u3001\u67e5\u8be2\u6bcf\u95e8\u8bfe\u7a0b\u7684\u5e73\u5747\u6210\u7ee9\uff0c\u7ed3\u679c\u6309\u5e73\u5747\u6210\u7ee9\u5347\u5e8f\u6392\u5217\uff0c\u5e73\u5747\u6210\u7ee9\u76f8\u540c\u65f6\uff0c\u6309\u8bfe\u7a0b\u53f7\u964d\u5e8f\u6392\u5217<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Select C#,Avg(score) from SC group by C# order by Avg(score),C# DESC ;<\/code><\/pre>\n\n\n\n<p><strong>33<\/strong>\u3001\u67e5\u8be2\u5e73\u5747\u6210\u7ee9\u5927\u4e8e85\u7684\u6240\u6709\u5b66\u751f\u7684\u5b66\u53f7\u3001\u59d3\u540d\u548c\u5e73\u5747\u6210\u7ee9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select Sname,SC.S# ,avg(score)\n    from Student,SC\n    where Student.S#=SC.S# group by SC.S#,Sname having    avg(score)&gt;85;<\/code><\/pre>\n\n\n\n<p><strong>34<\/strong>\u3001\u67e5\u8be2\u8bfe\u7a0b\u540d\u79f0\u4e3a\u201c\u6570\u636e\u5e93\u201d\uff0c\u4e14\u5206\u6570\u4f4e\u4e8e60\u7684\u5b66\u751f\u59d3\u540d\u548c\u5206\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Select Sname,isnull(score,0)\n    from Student,SC,Course\n    where SC.S#=Student.S# and SC.C#=Course.C# and Course.Cname='\u6570\u636e\u5e93'and score &lt;60;<\/code><\/pre>\n\n\n\n<p><strong>35<\/strong>\u3001\u67e5\u8be2\u6240\u6709\u5b66\u751f\u7684\u9009\u8bfe\u60c5\u51b5\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT SC.S#,SC.C#,Sname,Cname\n    FROM SC,Student,Course\n    where SC.S#=Student.S# and SC.C#=Course.C# ;<\/code><\/pre>\n\n\n\n<p><strong>36<\/strong>\u3001\u67e5\u8be2\u4efb\u4f55\u4e00\u95e8\u8bfe\u7a0b\u6210\u7ee9\u572870\u5206\u4ee5\u4e0a\u7684\u59d3\u540d\u3001\u8bfe\u7a0b\u540d\u79f0\u548c\u5206\u6570\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT distinct student.S#,student.Sname,SC.C#,SC.score\n    FROM student,Sc\n    WHERE SC.score&gt;=70 AND SC.S#=student.S#;<\/code><\/pre>\n\n\n\n<p><strong>37<\/strong>\u3001\u67e5\u8be2\u4e0d\u53ca\u683c\u7684\u8bfe\u7a0b\uff0c\u5e76\u6309\u8bfe\u7a0b\u53f7\u4ece\u5927\u5230\u5c0f\u6392\u5217<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select c# from sc where scor e &lt;60 order by C# ;<\/code><\/pre>\n\n\n\n<p><strong>38<\/strong>\u3001\u67e5\u8be2\u8bfe\u7a0b\u7f16\u53f7\u4e3a003\u4e14\u8bfe\u7a0b\u6210\u7ee9\u572880\u5206\u4ee5\u4e0a\u7684\u5b66\u751f\u7684\u5b66\u53f7\u548c\u59d3\u540d\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select SC.S#,Student.Sname from SC,Student where SC.S#=Student.S# and Score&gt;80 and C#='003';<\/code><\/pre>\n\n\n\n<p><strong>39<\/strong>\u3001\u6c42\u9009\u4e86\u8bfe\u7a0b\u7684\u5b66\u751f\u4eba\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select count(*) from sc;<\/code><\/pre>\n\n\n\n<p><strong>40<\/strong>\u3001\u67e5\u8be2\u9009\u4fee\u201c\u53f6\u5e73\u201d\u8001\u5e08\u6240\u6388\u8bfe\u7a0b\u7684\u5b66\u751f\u4e2d\uff0c\u6210\u7ee9\u6700\u9ad8\u7684\u5b66\u751f\u59d3\u540d\u53ca\u5176\u6210\u7ee9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select Student.Sname,score\n    from Student,SC,Course C,Teacher\n    where Student.S#=SC.S# and SC.C#=C.C# and C.T#=Teacher.T# and Teacher.Tname='\u53f6\u5e73' and SC.score=(select max(score)from SC where C#=C.C# );<\/code><\/pre>\n\n\n\n<p><strong>41<\/strong>\u3001\u67e5\u8be2\u5404\u4e2a\u8bfe\u7a0b\u53ca\u76f8\u5e94\u7684\u9009\u4fee\u4eba\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select count(*) from sc group by C#;<\/code><\/pre>\n\n\n\n<p>\u200b\u200b42\u3001\u67e5\u8be2\u4e0d\u540c\u8bfe\u7a0b\u6210\u7ee9\u76f8\u540c\u7684\u5b66\u751f\u7684\u5b66\u53f7\u3001\u8bfe\u7a0b\u53f7\u3001\u5b66\u751f\u6210\u7ee9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select distinct A.S#,B.score from SC A ,SC B where A.Score=B.Score and A.C# &lt;&gt;B.C# ;<\/code><\/pre>\n\n\n\n<p><strong>43<\/strong>\u3001\u67e5\u8be2\u6bcf\u95e8\u529f\u6210\u7ee9\u6700\u597d\u7684\u524d\u4e24\u540d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT t1.S# as \u5b66\u751fID,t1.C# as \u8bfe\u7a0bID,Score as \u5206\u6570\n      FROM SC t1\n      WHERE score IN (SELECT TOP 2 score\n              FROM SC\n              WHERE t1.C#= C#\n            ORDER BY score DESC\n              )\n      ORDER BY t1.C#;<\/code><\/pre>\n\n\n\n<p><strong>44<\/strong>\u3001\u7edf\u8ba1\u6bcf\u95e8\u8bfe\u7a0b\u7684\u5b66\u751f\u9009\u4fee\u4eba\u6570\uff08\u8d85\u8fc710\u4eba\u7684\u8bfe\u7a0b\u624d\u7edf\u8ba1\uff09\u3002\u8981\u6c42\u8f93\u51fa\u8bfe\u7a0b\u53f7\u548c\u9009\u4fee\u4eba\u6570\uff0c\u67e5\u8be2\u7ed3\u679c\u6309\u4eba\u6570\u964d\u5e8f\u6392\u5217\uff0c\u67e5\u8be2\u7ed3\u679c\u6309\u4eba\u6570\u964d\u5e8f\u6392\u5217\uff0c\u82e5\u4eba\u6570\u76f8\u540c\uff0c\u6309\u8bfe\u7a0b\u53f7\u5347\u5e8f\u6392\u5217<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select C# as \u8bfe\u7a0b\u53f7,count(*) as \u4eba\u6570\n    from sc \n    group by C#\n    order by count(*) desc,c#<\/code><\/pre>\n\n\n\n<p><strong>45<\/strong>\u3001\u68c0\u7d22\u81f3\u5c11\u9009\u4fee\u4e24\u95e8\u8bfe\u7a0b\u7684\u5b66\u751f\u5b66\u53f7<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select S# \n    from sc \n    group by s#\n    having count(*) &gt; = 2;<\/code><\/pre>\n\n\n\n<p><strong>46<\/strong>\u3001\u67e5\u8be2\u5168\u90e8\u5b66\u751f\u90fd\u9009\u4fee\u7684\u8bfe\u7a0b\u7684\u8bfe\u7a0b\u53f7\u548c\u8bfe\u7a0b\u540d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select C#,Cname  from Course  where C# in (select c# from sc group by c#) ;<\/code><\/pre>\n\n\n\n<p><strong>47<\/strong>\u3001\u67e5\u8be2\u6ca1\u5b66\u8fc7\u201c\u53f6\u5e73\u201d\u8001\u5e08\u8bb2\u6388\u7684\u4efb\u4e00\u95e8\u8bfe\u7a0b\u7684\u5b66\u751f\u59d3\u540d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select Sname from Student where S# not in (select S# from Course,Teacher,SC where Course.T#=Teacher.T# and SC.C#=course.C# and Tname='\u53f6\u5e73');<\/code><\/pre>\n\n\n\n<p><strong>48<\/strong>\u3001\u67e5\u8be2\u4e24\u95e8\u4ee5\u4e0a\u4e0d\u53ca\u683c\u8bfe\u7a0b\u7684\u540c\u5b66\u7684\u5b66\u53f7\u53ca\u5176\u5e73\u5747\u6210\u7ee9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select S#,avg(isnull(score,0)) from SC where S# in (select S# from SC where score &lt;60 group by S# having count(*)&gt;2)group by S#;<\/code><\/pre>\n\n\n\n<p><strong>49<\/strong>\u3001\u68c0\u7d22\u201c<strong>004<\/strong>\u201d\u8bfe\u7a0b\u5206\u6570\u5c0f\u4e8e60\uff0c\u6309\u5206\u6570\u964d\u5e8f\u6392\u5217\u7684\u540c\u5b66\u5b66\u53f7<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select S# from SC where C#='004'and score &lt;60 order by score desc;<\/code><\/pre>\n\n\n\n<p><strong>50<\/strong>\u3001\u5220\u9664\u201c<strong>002<\/strong>\u201d\u540c\u5b66\u7684\u201c<strong>001<\/strong>\u201d\u8bfe\u7a0b\u7684\u6210\u7ee9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delete from Sc where S#='001'and C#='001';<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5de5\u4f5c\u4e2d\u6211\u4eec\u57fa\u672c\u4e0a\u6bcf\u5929\u90fd\u8981\u4e0e\u6570\u636e\u5e93\u6253\u4ea4\u9053\uff0c\u6570\u636e\u5e93\u7684\u77e5\u8bc6\u70b9\u5462\u4e5f\u7279\u522b\u591a\uff0c\u5168\u90e8\u8bb0\u4f4f\u5462\u4e5f\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u5b9e\u8df5\u4e00\u904d\u5904&hellip; <a href=\"http:\/\/viplao.com\/index.php\/2023\/07\/27\/%e5%b7%a5%e4%bd%9c%e4%b8%ad%e5%b8%b8%e7%94%a8sql%e8%af%ad%e5%8f%a5-%e5%ae%9e%e8%b7%b5\/\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">\u5de5\u4f5c\u4e2d\u5e38\u7528SQL\u8bed\u53e5 \u5b9e\u8df5<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[26],"views":960,"_links":{"self":[{"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/posts\/2210"}],"collection":[{"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/comments?post=2210"}],"version-history":[{"count":3,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/posts\/2210\/revisions"}],"predecessor-version":[{"id":2221,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/posts\/2210\/revisions\/2221"}],"wp:attachment":[{"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/media?parent=2210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/categories?post=2210"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/tags?post=2210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}