您好,欢迎访问三七文档
--基础题InsertInto数据表名称(字段名称1,字段名称2,...)values(字段值1,字段值2,...)insertintouser(username,password,age)values('李老四','6666',45)Update数据表名称Set字段名称=字段值,字段名称=字段值,...[Where条件]DeleteFrom数据表下列查询返回在LONDON(伦敦)或SEATTLE(西雅图)的所有雇员:SELECT*FROMemployeesWHEREUPPER(city)IN('LONDON','SEATTLE')下面示例利用DATEDIFF函数,确定在pubs数据库中标题发布日期和当前日期间的天数。SELECTDATEDIFF(day,OrderDate,getdate())ASno_of_daysFROMtable1返回字符串wonderful在titles表的notes列中开始的位置。SELECTCHARINDEX('wonderful',notes)以下是返回的结果:(第47个字符位置)显示工作站的名称:selecthost_name()as[ClientComputerName]下例是检索titles表中百分之五十的书。如果titles表中包含了18行,则将检索前9行。SELECTTOP50PERCENTtitleFROMtitles字段名称[Not]Between起始值and终止值列出BOOK表中30至50元的书select*frombookwherepricebetween30and50字段名称[Not]In(列出值1,列出值2,...)从BOOK表中列出价格为30,40,50,60的所有书select*frombookwherepricein(30,40,50,60)字段名称[Not]Like通配符列出BOOK表中出版社含电的所有记录select*frombookwherepublishinglike'*电*'列出BOOK表中出版社第一个字是电的所有记录select*frombookwherepublishinglike'电*'selectSum/Count/Avg/Max/Min(字段名称)[As新名称]From数据表名称sum求和:求出总价格做为合计字段selectsum(price)as合计frombookcount统计数量:统计BOOK表中有多少条记录做为数量字段selectcount(id)as数量frombookAVG平均:算出BOOK表中所有书的平均价格selectavg(price)as平均价格frombookMAX最大:列出BOOK表中最贵的书selectmax(price)as最贵书frombookMIN最小:selectmin(price)as最便宜书frombook交叉联接:SELECT*FROMtable1CROSSJOINtable2selectx.[name],y.[name]fromxleftjoinyonx.[refid]=y.idselecty.[name],x.[name]fromxrightjoinyonx.[refid]=y.id表联接查询SELECTt1.ProductID,t2.VendorID,t2.NameFROMtable1t1,table2t2WHEREt1.VendorID=t2.VendorIDANDStandardPrice$10ANDNameLIKE'F%'SELECTProductID,talbe2.VendorID,NameFROMtable1JOINtable2ON(table1.VendorID=table2.VendorID)WHEREStandardPrice$10ANDNameLIKE'F%'COMPUTEby子句可选BY关键字。它基于每一列计算指定的行聚合。行聚合函数名称。包括SUM、AVG、MIN、MAX或COUNT。要对其执行行聚合函数的列。SELECTRateId,ConnectionFee,RateFROMUTC_RateORDERBYRateIdCOMPUTESUM(ConnectionFee),SUM(Rate)--Compute用法(总计)SELECTRateId,ConnectionFee,RateFROMUTC_RateORDERBYRateIdCOMPUTESUM(ConnectionFee),SUM(Rate)BYRateId--Computeby用法(小计)DISTINCT指定AVG只在每个值的唯一实例上执行,而不管该值出现了多少次。SELECTAVG(DISTINCTRate)—-计算不重复Rate的avgFROMUTC_RateSELECTdistinctRateFROMUTC_Rate表的复制SelectintoSELECT*intonewtableFROMoldtable联合查询UNION所有查询中的列数和列的顺序必须相同,数据类型必须兼容SELECTCountry,FirstName,LastNameFROMEmployeesUNIONSELECTCountry,NULL,NULLFROMRegions;子查询:--使用聚合函数SELECTtitleFROMtitlesWHEREadvance(SELECTmax(advance)FROMpublishersINNERJOINtitlesONtitles.pub_id=publishers.pub_idWHEREpub_name='NewMoonBooks')--使用allSELECTtitleFROMtitlesWHEREadvanceall(SELECTadvanceFROMpublishersINNERJOINtitlesONtitles.pub_id=publishers.pub_idWHEREpub_name='NewMoonBooks')--嵌套SELECTfieldFROMtable1WHEREidIN(SELECTidFROMtable2WHEREtitle_idIN(SELECTtitle_idFROMtable3WHEREtitle_idlike'PC%'))--EXISTS和NOTEXISTS查找交集与差集SELECTDISTINCTfieldFROMtable1WHEREEXISTS(SELECT*FROMtable2WHEREtable1.field=table2.field)--等效SELECTDISTINCTtable1.fieldFROMtable1INNERJOINtable2ONtable1.field=publishers.field--差集SELECTDISTINCTfieldFROMtable1WHERENOTEXISTS(SELECT*FROMtable2WHEREtable1.field=table2.field)--等效SELECTDISTINCTfieldFROMtable1WHEREfieldNOTIN(SELECTfieldFROMtable2)指定组或聚合的搜索条件。HAVING只能与SELECT语句一起使用。HAVING通常在GROUPBY子句中使用。如果不使用GROUPBY子句,则HAVING的行为与WHERE子句一样。SELECTid,SUM(price)FROMSalesGROUPBYidHAVINGSUM(price)10.00--检索超过$10.00的每个ID的总计ORDERBYid;更新表:UPDATEtitlesSETprice=price*2WHEREpub_idIN(SELECTpub_idFROMpublishersWHEREpub_name='NewMoonBooks')1:找出那些工资高于他们所在部门的manager的工资的员工。selectw.ename,w.sal,w.mgr,m.empno,m.ename,m.salfromempw,empmwherew.mgr=m.empnoandw.salm.sal2:哪些人是领导?selectdistinctm.enamefromempw,empmwherew.mgr=m.empnoselectenamefromempwhereempnoin(selectmgrfromemp)selectenamefromempowhereexists(select'a'fromempiwhereo.empno=i.mgr)3:哪些人不是领导?selectm.enamefromempm,empwwherem.empno=w.mgr(+)andw.mgrisnullselectenamefromempwhereempnonotin(selectmgrfromempwheremgrisnotnull)selectenamefromempowherenotexists(select'a'fromempiwhereo.empno=i.mgr)哪个部门没有员工?哪个学生没有选课?哪个部门没有smith这个人?selectename,emp.deptno,dept.deptno,dnamefromemp,deptwhereemp.deptno(+)=dept.deptnoandename(+)='SMITH'andenameisnull4:找出公司里收入最高的前三名员工:SQLselectrownum,last_name,salary2from(selectlast_name,salary3froms_emp4orderbysalarydesc)5whererownum=3;ROWNUMLAST_NAMESALARY---------------------------------------------1Velasquez47502Ropeburn29453Nguyen2897.5不用max找出工资最高的人?5:找出表中的某一行或某几行的数据:SQLl1selectlast_name,salary2from(selectrownuma,b.*3froms_empb)4*wherea=3SQL/LAST_NAMESALARY-----------------------------------Nagayama26606:找出第三行到第五行之间的数据:SQLl1selectlast_name,salary2from(selectrownuma,b.*3froms_empbwhererownum=5)4*whereabetween3and5SQL/LAST_NAMESALARY-----------------------------------Nagayama2660Quick-To-See2755Ropeburn29457:找出那些工资高于他们所在部门的平均工资的员工。(1):第一种方法:SQLselectlast_name,dept_id,salary2froms_empa3wheresalary(selectavg(salary)4froms_emp5wheredept_id=a.dept_id);LAST_NAMEDEPT_IDSALARY---------------------------------------------Velasquez504750Urguhart412280Menchu422375Biri432090Catchpole442470Havel452483.3Nguyen342897.5Maduro412660Nozaki422280Schwartz45209010rowsselected.(2):第二种方法:SQLl1selecta.last_name,a.salary,a.dept_id,b.avgsal2froms_empa,(selectdept_id,avg(salary)avgsal3froms_emp4groupbydept_id)b5wher
本文标题:SQL面试题
链接地址:https://www.777doc.com/doc-2860518 .html