ʵÑéÎå SQLÓïÑÔ ÏÂÔØ±¾ÎÄ

ʵÑéÎå SQLÓïÑÔ

Ò»¡¢Ä¿µÄÓëÒªÇó

1. ÕÆÎÕSQLÓïÑԵIJéѯ¹¦ÄÜ£» 2. ÕÆÎÕSQLÓïÑÔµÄÊý¾Ý²Ù×÷¹¦ÄÜ£»

3. ÕÆÎÕ¶ÔÏó×ÊÔ´¹ÜÀíÆ÷½¨Á¢²éѯ¡¢Ë÷ÒýºÍÊÓͼµÄ·½·¨£»

¶þ¡¢ÊµÑé×¼±¸

1. Á˽âSQLÓïÑԵIJé¸ÄÔöɾËÄ´ó²Ù×÷µÄÓï·¨£» 2. Á˽â²éѯ¡¢Ë÷ÒýºÍÊÓͼµÄ¸ÅÄ 3. Á˽â¸÷Àà³£Óú¯ÊýµÄº¬Òå¡£

Èý¡¢ÊµÑéÄÚÈÝ

(Ò»)SQL²éѯ¹¦ÄÜ

ʹÓÃÌṩµÄstudentdbÊý¾Ý¿âÎļþ£¬Ïȸ½¼Óµ½Ä¿Â¼Ê÷ÖУ¬ÔÙÍê³ÉÏÂÁÐÌâÄ¿£¬SQLÃüÁîÇë±£´æµ½½Å±¾ÎļþÖС£ 1£®»ù±¾²éѯ

(1) ²éѯËùÓÐÐÕÍõµÄѧÉúµÄÐÕÃû¡¢Ñ§ºÅºÍÐÔ±ð Select St_Name ,St_Sex, St_ID From st_info

Where St_Name like 'Íõ%'

(2) ²éѯȫÌåѧÉúµÄÇé¿ö£¬²éѯ½á¹¹°´°à¼¶½µÐòÅÅÁУ¬Í¬Ò»°à¼¶ÔÙ°´Ñ§ºÅÉýÐò£¬²¢½«½á¹û´æÈëбínewÖÐ select * into new from st_info

order by Cl_Name desc,St_ID asc

(3) ¶ÔS_C_info±íÖÐÑ¡ÐÞÁË¡°ÌåÓý¡±¿ÎµÄѧÉúµÄƽ¾ù³É¼¨Éú³É»ã×ÜÐкÍÃ÷ϸÐС£(Ìáʾ£ºÓÃcompute»ã×ܼÆËã) Select c_no,score From s_c_info

Where c_no=29000011 compute avg(score)

2£®Ç¶Ìײéѯ

(1) ²éѯÆäËû°à¼¶Öбȡ°²ÄÁÏ¿ÆÑ§0601°à¡±µÄѧÉúÄêÁä¶¼´óµÄѧÉúÐÕÃûºÍÄêÁä

Select St_Name ,Born_Date from st_info

where Cl_Name!='²ÄÁÏ¿ÆÑ§0601°à' and Born_Date<(select Min(Born_Date) from st_info where Cl_Name='²ÄÁÏ¿ÆÑ§0601°à')

(2) ÓÃexists²éѯѡÐÞÁË¡°9710041¡±¿Î³ÌµÄѧÉúÐÕÃû select St_Name from st_info

where exists (select * from s_c_info where c_no = 9710041 and st_id=st_info.St_ID )

(3) ÓÃin²éѯÕÒ³öûÓÐÑ¡ÐÞ¡°9710041¡±¿Î³ÌµÄѧÉúµÄÐÕÃûºÍËùÔڰ༶¡£

select St_Name,Cl_Name from st_info

where st_ID not in (select st_id from s_c_info where c_no ='9710041')

(4) ²éѯѡÐÞÁËѧºÅΪ¡°2001050105¡±µÄѧÉúËùѡȫ²¿¿Î³ÌµÄѧÉúÐÕÃû¡£ select St_Name

from st_info where St_ID in

(select distinct St_ID from s_c_info where not exists

(select * from s_c_info where st_id='2001050105' and not exists (select * from s_c_info where st_info.St_ID=s_c_info.st_id and c_no=any(select c_no from s_c_info where st_id='2001050105'))))

3£®Á¬½Ó×ۺϲéѯ¼°ÆäËû

(1) ²éѯÿ¸öѧÉúËùÑ¡¿Î³ÌµÄ×î¸ß³É¼¨£¬ÒªÇóÁгöѧºÅ£¬ÐÕÃû£¬¿Î³Ì±àºÅºÍ·ÖÊý¡£

select st_info.St_ID, St_Name,C_info.c_no,score

from st_info inner join s_c_info on st_info.St_ID=s_c_info.st_id inner join C_info on s_c_info.c_no=C_info.c_no

where score=(select max(s_c_info.score)from s_c_info where st_info.St_ID=s_c_info.st_id)

(2) ²éѯËùÓÐѧÉúµÄ×ܳɼ¨£¬ÒªÇóÁгöѧºÅ¡¢ÐÕÃû¡¢×ܳɼ¨£¬Ã»ÓÐÑ¡Ð޿γ̵ÄѧÉú×ܳɼ¨Îª¿Õ¡£

select st_info.St_ID,St_Name,×ܳɼ¨ from st_info

left outer join (select st_id,sum(score)as ×ܳɼ¨ from s_c_info group by st_id)s_c_info on st_info.St_ID=s_c_info.st_id