前两天看到一个sql面试题,发现不太会做,在Javaeye论坛上提问,N多达人进行了回答,个人进行总结归纳一下,以备忘,也可以让大家借鉴一下,很不错。
在数据库中已存在如下表(’pk’表示主键,fk引用pk)
flight(航班信息表) city(城市表)
Flowid (流水号) pk
|
orgCityCode(起飞城市编号) fk
dstCityCode(到达城市编号) fk
flightnum(航班号)
deptDateTime(起飞时间)
|
cityCode(城市编号) PK
|
cityName(城市名称)
|
1) 请用SQL语句选出从“北京”出发飞往各地的航班号,并按照到达城市编号排序
2) 请用SQL语句选出2009-6-1以后起飞的航班,并按起飞时间从早到晚排序。输出内容:起飞城市名称、到达城市名称、航班号、起飞时间
3) 请用SQL语句统计2009-6-1至2009-6-10每天从“北京”出发飞往各地的航班的数量。
4) 请用SQL语句找出今天只有去程,没有回程的航线。
----------------------------------------------------------------------------------------------------------------------------------
1)select flightnum from flight f inner join city c
on f.orgCityCode=c.cityCode
where c.cityName='北京'
order by f.dstCityCode
2)推荐使用第二种方法。第一种较复杂,不太容易理解。
select start_flight.startName as startCityName,c2.cityname as endCityName,start_flight.flightnum,start_flight.deptDateTime
from (select f.*,c1.cityname as startName from flight f inner join city c1 on c1.citycode=f.orgCityCode) start_flight
inner join city c2 on start_flight.dstCityCode=c2.cityCode
where start_flight.deptDateTime>cast('2009-06-01' as datetime)
order by start_city.deptDateTime
select c1.cityname,c2.cityname,f.flightnum,f.deptDateTime
from flight f
inner join city c1 on c1.cityCode=f.orgCityCode
inner join city c2 on c2.cityCode=f.dstCityCode
where f.deptDateTime>'2009-06-01'
order by f.deptDateTime
3)
select count(f.flowid)
from flight f inner join city c on f.orgCityCode=c.cityCode
where c.cityName='北京' and f.deptDateTime>=cast('2009-06-01' as datetime) and f.deptDateTime<=cast('2010-06-10' as datetime)
group by convert(varchar(10),f.deptDateTime,23)
4)推荐使用第二种方法较好。第一种方法是一种union all连接,它会把flight所有的字段合在一起两次(有两个flight表),sql的执行效率会很低;第二种使用了一个not exist,很聪明,它只需要进行简单的两次查询就可以得到结果。
select distinct f1.*
from flight f1,flight f2
where f1.orgCityCode!=f2.dstCityCode and f1.dstCityCode!=f2.orgCityCode and convert(char(10),f.deptDateTime,23)=convert(char(10),getdate(),23)
select f.*
from flight f
where convert(char(10),f.deptDateTime,23)=convert(char(10),getdate(),23)
and not exists(select * from train f1 where f.startCode=f1.endCode and f.endCode=f1.startCode)
分享到:
相关推荐
SQLServer面试题SQLServer面试题SQLServer面试题SQLServer面试题SQLServer面试题
一道SQL面试题 希望对你有用!!!!!!!!
SQLServer高频面试题及答案
sqlserver+sqlserver面试题及答案 sqlserver+sqlserver面试题及答案 sqlserver+sqlserver面试题及答案
自己整理的SQLserver笔试题。 当做知识点记了 内附答案
收录的sql server 面试题大全!!!
支持navicate 连接 sqlserver 2019 的 驱动sqlserver native client 11.0 ,亲测可用,ssms自带的不能连接,会报远程关闭错误。
无法升级数据库因为此版本的 SQL Server 不支持该数据库的非发布版本(539) 解决方案无法升级数据库因为此版本的 SQL Server 不支持该数据库的非发布版本(539) 解决方案无法升级数据库因为此版本的 SQL Server 不支持...
SQL Server数据库试题 SQL经典面试题及答案 《SQL数据库管理与开发》试题 SQL Server 上机考试题 综合练习
SQL存储过程习题,SQL触发器习题,SQL习题,基础语法,与W3School类似,初学者学习使用。
战胜SQL Server必做练习50题 战胜SQL Server必做练习50题 战胜SQL Server必做练习50题
完美支持SQL Server2019,亲测可用 具体参照下面页面。 https://www.freesion.com/article/37021337912/
压缩包内附带链接服务器创建脚本方式,此SQL Server Native Client 10.0无病毒,有64位和32位可供选择。...安装完SQL Server Native Client 10.0后再创建个链接服务器,可以实现高版本SQLserver远程链接SQLserver2000。
SQlServer面试题,为SQL Server 配置一个邮件配置文件
SQLServer+目前支持SQLServer2000个人版、企业版、开发版、标准版、MSDE等将根据您不同的选择来完成您的需要。 SQLServer+现在支持Windows98/Me/2000/XP/XPSP2/2003等大多数操作系统,让您的程序无处不在。 SQL...
完美支持SQL Server2019,亲测可用 还有详细PJ教程,自带Styles覆盖即可使用
此链接库针对使用机器码 API (ODBC、OLE DB 和 ADO) 的应用程序提供运行时间支持,以连接到 Microsoft SQL Server 2000、2005 或 2008。SQL Server Native Client 应该用来建立新的应用程序,或者加强需要利用新 SQL...
SQL Server笔试题解答,word形式
SQLServer考试题及答案 选择题: 1、在MS SQL Server中,用来显示数据库信息的系统存储过程是( ) A sp_ dbhelp B sp_ db C sp_ help D sp_ helpdb 2、SQL语言中,删除一个表的命令是( ) A DELETE B DROP C ...
Microsoft SQL Server Management Studio Express(SSMSE)是一种免费、易于使用的图形管理工具,用于管理SQL Server 2005 Express Edition和SQL Server 2005 ExpressEdition with Advanced Services。注意:SSMSE ...