SQL常见面试题 .pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《SQL常见面试题 .pdf》由会员分享,可在线阅读,更多相关《SQL常见面试题 .pdf(35页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、SQL 常见面试题1.用一条 SQL 语句查询出每门课都大于80 分的学生姓名name kecheng fenshu 张三语文81 张三数学75 李四语文76 李四数学90 王五语文81 王五数学100 王五英语90 A:select distinct name from table where name not in(select distinct name from table where fenshu 10)begin -print Dadadada!print Dadadada!end -else begin print XiaoXiao!print XiaoXiao!end-Whil
2、e 循环控制declare i int;set i=12;print i return;while(i 18)begin print i;set i=i+1;if i 15 break;end;-CASE 分支判断select au_lname,state,犹他州 from authors where state=UT 名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 35 页 -select au_lname,state,密西西比州 from authors where state=MI select au_lname,state,肯塔基州 from authors where
3、state=KS select au_lname,state,case state when UT then 犹他州 when MI then 密西西比州 when KS then 肯塔基州 when CA then 加利福利亚 else state end from authors(4.1)系统函数-获取指定字符串中左起第一个字符的ASC 码print ascii(ABCDEF)-根据给定的ASC 码获取相应的字符print char(65)-获取给定字符串的长度print len(abcdef)-大小写转换print lower(ABCDEF)print upper(abcdef)-去空格
4、print ltrim(abcd dfd df )print rtrim(abcd dfd df )-求绝对值print abs(-12)-幂-3 的 2 次方print power(3,2)print power(3,3)-随机数-0-1000 之间的随机数print rand()*1000-获取圆周率print pi()名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 35 页 -获取系统时间print getdate()-获取 3 天前的时间print dateadd(day,-3,getdate()-获取 3 天后的时间print dateadd(day,3,getdate
5、()-获取 3 年前的时间print dateadd(year,-3,getdate()-获取 3 年后的时间print dateadd(year,3,getdate()-获取 3 月后的时间print dateadd(month,3,getdate()-获取 9 小时后的时间print dateadd(hour,9,getdate()-获取 9 分钟后的时间print dateadd(minute,9,getdate()-获取指定时间之间相隔多少年print datediff(year,2005-01-01,2008-01-01)-获取指定时间之间相隔多少月print datediff(mo
6、nth,2005-01-01,2008-01-01)-获取指定时间之间相隔多少天print datediff(day,2005-01-01,2008-01-01)-字符串合并print abc+def print abcder print abc+456 print abc+456-类型转换print abc+convert(varchar(10),456)select title_id,type,price from titles-字符串连接必须保证类型一致(以下语句执行将会出错)-类型转换select title_id+type+price from titles-正确select tit
7、le_id+type+convert(varchar(10),price)from titles 名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 35 页 -print 123+convert(varchar(3),123)print 123+123 print convert(varchar(12),2005-09-01,110)-获取指定时间的特定部分print year(getdate()print month(getdate()print day(getdate()-获取指定时间的特定部分print datepart(year,getdate()print datepar
8、t(month,getdate()print datepart(day,getdate()print datepart(hh,getdate()print datepart(mi,getdate()print datepart(ss,getdate()print datepart(ms,getdate()-获取指定时间的间隔部分-返回跨两个指定日期的日期和时间边界数print datediff(year,2001-01-01,2008-08-08)print datediff(month,2001-01-01,2008-08-08)print datediff(day,2001-01-01,2
9、008-08-08)print datediff(hour,2001-01-01,2008-08-08)print datediff(mi,2001-01-01,2008-08-08)print datediff(ss,2001-01-01,2008-08-08)-在向指定日期加上一段时间的基础上,返回新的datetime 值print dateadd(year,5,getdate()print dateadd(month,5,getdate()print dateadd(day,5,getdate()print dateadd(hour,5,getdate()print dateadd(mi
10、,5,getdate()print dateadd(ss,5,getdate()-其他print host_id()print host_name()print db_id(pubs)print db_name(5)名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 35 页 -利用系统函数作为默认值约束drop table ttt create table ttt(stu_name varchar(12),stu_birthday datetime default(getdate()alter table ttt add constraint df_ttt_stu_birthday
11、 default (getdate()for stu_birthday insert into ttt values(ANiu,2005-04-01)insert into ttt values(ANiu,getdate()insert into ttt values(AZhu,default)sp_help ttt select*from ttt(4.2)自定义函数select title_id from titles where type=business select stuff(title_id,1,3,ABB),type from titles where type=business
12、 select count(title_id)from titles where type=business select title_id from titles where type=business select *,count(dbo.titleauthor.title_id)FROM dbo.authors INNER JOIN dbo.titleauthor ON dbo.authors.au_id=dbo.titleauthor.au_id select au_id,count(title_id)from titleauthor group by au_id SELECT dbo
13、.authors.au_id,COUNT(dbo.titleauthor.title_id)AS 作品数量 FROM dbo.authors left outer JOIN dbo.titleauthor ON dbo.authors.au_id=dbo.titleauthor.au_id GROUP BY dbo.authors.au_id order by 作品数量 名师资料总结-精品资料欢迎下载-名师精心整理-第 8 页,共 35 页 -自定义函数的引子(通过这个子查询来引入函数的作用)-子查询-统计每个作者的作品数-将父查询中的作者编号传入子查询-作为查询条件利用聚合函数count 统
14、计其作品数量select au_lname,(select count(title_id)from titleauthor as ta where ta.au_id=a.au_id )as TitleCount from authors as a order by TitleCount-是否可以定义一个函数-将作者编号作为参数统计其作品数量并将其返回select au_id,au_lname,dbo.GetTitleCountByAuID(au_id)as TitleCount from authors order by TitleCount-根据给定的作者编号获取其相应的作品数量create
15、 function GetTitleCountByAuID(au_id varchar(12)returns int begin return(select count(title_id)from titleauthor where au_id=au_id)end-利用函数来显示每个作者的作品数量create proc pro_CalTitleCount as select au_id,au_lname,dbo.GetTitleCountByAuID(au_id)as TitleCount from authors order by TitleCount go-执行存储过程execute pr
16、o_CalTitleCount 名师资料总结-精品资料欢迎下载-名师精心整理-第 9 页,共 35 页 -vb 中函数定义格式function GetTitleCountByAuID(au_id as string)as integer .GetTitleCountByAuID=?end function-SALES 作品销售信息select*from sales-根据书籍编号查询其销售记录(其中,qty 表示销量)select*from sales where title_id=BU1032-根据书籍编号统计其总销售量(其中,qty 表示销量)select sum(qty)from sale
17、s where title_id=BU1032-利用分组语句(group by),根据书籍编号统计每本书总销售量(其中,qty 表示销量)select title_id,sum(qty)from sales group by title_id-是否可以考虑定义一个函数根据书籍编号来计算其总销售量-然后,将其应用到任何一条包含了书籍编号的查询语句中select title_id,title,dbo.GetTotalSaleByTitleID(title_id)as TotalSales from titles order by TotalSales-定义一个函数根据书籍编号来计算其总销售量cre
18、ate function GetTotalSaleByTitleID(tid varchar(24)returns int begin return(select sum(qty)from sales where title_id=tid)end-统计书籍销量的前10 位-其中,可以利用函数计算结果的别名作为排序子句的参照列select top 10 title_id,title,dbo.GetTotalSaleByTitleID(title_id)as TotalSales from titles order by TotalSales desc-根据书籍编号计算其销量排名create fu
19、nction GetTheRankOfTitle(id varchar(20)returns int begin return(select count(TotalSales)from titles 名师资料总结-精品资料欢迎下载-名师精心整理-第 10 页,共 35 页 -where ToalSales(select TotalSales from titles where title_id=id)end-根据书籍编号计算其销量排名select dbo.GetTheRankOfTitle(pc1035)from titles select count(title_id)+1 from tit
20、les where dbo.GetTotalSaleByTitleID(title_id)dbo.GetTotalSaleByTitleID(pc1035)-删除函数drop function GetRankByTitleId-根据书籍编号计算其销量排名create function GetRankByTitleId(tid varchar(24)returns int begin return(select count(title_id)+1 from titles where dbo.GetTotalSaleByTitleID(title_id)dbo.GetTotalSaleByTitl
21、eID(tid)end-在查询语句中利用函数统计每本书的总销量和总排名select title_id,title,dbo.GetTotalSaleByTitleID(title_id)as TotalSales,dbo.GetRankByTitleId(title_id)as TotalRank from titles order by TotalSales desc-查看表结构sp_help titles-查看存储过程的定义内容sp_helptext GetRankByTitleId sp_helptext sp_helptext sp_helptext xp_cmdshell-ORDER
22、DETAILS 订单详细信息select*from order details select*from order details where productid=23 名师资料总结-精品资料欢迎下载-名师精心整理-第 11 页,共 35 页 -根据产品编号在订单详细信息表中统计总销售量select sum(quantity)from order details where productid=23-构造一个函数根据产品编号在订单详细信息表中统计总销售量create function GetTotalSaleByPID(Pid varchar(12)returns int begin retu
23、rn(select sum(quantity)from order details where productid=Pid)end select*from products-在产品表中查询,统计每一样产品的总销量select productid,productname,dbo.GetTotalSaleByPID(productid)from products-CREATE FUNCTION LargeOrderShippers(FreightParm money)RETURNS OrderShipperTab TABLE(ShipperID int,ShipperName nvarchar(8
24、0),OrderID int,ShippedDate datetime,Freight money)AS BEGIN INSERT OrderShipperTab SELECT S.ShipperID,S.CompanyName,O.OrderID,O.ShippedDate,O.Freight FROM Shippers AS S INNER JOIN Orders AS O ON S.ShipperID=O.ShipVia WHERE O.Freight FreightParm RETURN END SELECT*FROM LargeOrderShippers($500)-根据作者编号计算
25、其所得版权费create function fun_RoyalTyper(au_id id)名师资料总结-精品资料欢迎下载-名师精心整理-第 12 页,共 35 页 -returns int as begin declare rt int select rt=sum(royaltyper)from titleauthor where au_id=au_id return(rt)end go select top 1 au_lname,au_fname,dbo.fun_RoyalTyper(au_id)as 版权费 from authors order by dbo.fun_RoyalTyper
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SQL常见面试题 2022 SQL 常见 试题
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内