您的位置:首页 > 数据库

Day 9:(5)简单查询练习参考答案

2015-11-22 23:53 471 查看
--简单查询练习参考答案:

-- 1. 查询本数据库服务器上有哪些数据库,查看test数据库信息 

sp_helpdb

sp_helpdb test

-- 2. 查询test数据库中,stu_info表的信息

sp_help stu_info

-- 3. 查询 test 数据库中 stu_info表中所有性别为'男',而且1984年出生的学生记录 

--select * from stu_info where t_gender='男' and DATEPART(yy,t_birthday)='1984' 

--或

select * from stu_info where t_gender='男' and t_birthday between  '19840101' and '19841231'

-- 4. 查询 test 数据库中 stu_info表中前5条记录写入到stu_info04表中

select top 5 * into stu_info04 from stu_info 

select * from stu_info04

-- 5. 查询 test 数据库中 exam表中有那些学生参加了考试的记录(显示t_number)

select distinct t_number from exam

select t_number from exam group by t_number 

-- 7. 查询 Northwind 数据库中 Products 数据表中 ProductID 为 14 的产品信息

select * from products where productid='14'

-- 8. 查询 Northwind 数据库中 Orders 数据表中没有按时送达的订单

select * from orders where requireddate<shippeddate

-- 9. 查询 Northwind 数据库中 Territories 数据表中的地域信息,按照地域名升序排序,去掉重复项

select * from territories order by territorydescription  --有53条记录

select distinct territorydescription from territories order by territorydescription asc

--查询结果有52条记录

-- 10. 查询 Pubs 数据库中 Titles 数据表中 书名含有 'computer' 的书,并按照价格降序排序

select * from titles where title like '%computer%' order by price desc

-- 11. 查询 Pubs 数据库中 Titles 数据表中每种类型的书的数量

select type,count(*) as '计数' from titles group by type

select * from titles

-- 12. 查询 Pubs 数据库中 Titles 数据表中出版日期在1992年——2000年之间的记录

select * from titles where pubdate>='19920101' and pubdate<='20010101'

--或

select * from titles where pubdate between '19920101' and '20010101'

-- 13. 查询pubs数据库中的titles表中书的原价(price)和书价打8折后的价格

select title_id,title,price,price*0.8 as '8折价格' from titles

-- 14. 查询 Pubs 数据库中 publishers数据表中美国 TX州的出版商信息

select * from publishers where country='USA' and state='TX'

-- 15. 查询 Pubs 数据库中 publishers数据表中某国家某州的出版商

select state,country from publishers group by country,state

-- 16. 查询 Pubs 数据库中 authors 数据表中 姓是以'Gr'开头的作者

--select * from authors where substring(au_lname,1,2)='Gr' order by au_lname

--或

select * from authors where au_lname like 'Gr%'

-- 17. 查询pubs数据库中的authors表,要求查找居住在CA、MI州的作家的信息。

select * from authors where state in('CA','MI')

-- 18. 查询pubs数据库中的authors表,要求查找不居住在CA、KS州的作家的信息。

select * from authors where state  not in('CA','KS')

-- 19. 查询pubs数据库中的authors表中au_lname中含有字母t的作家的信息。

select * from authors where au_lname like '%t%'

-- 20. 查询pubs数据库中的authors表中au_lname以字母s、g开头的作家的信息。

select au_lname from authors where au_lname like '[s,g]%'

--或

select au_lname from authors where au_lname like 's%' or au_lname like 'g%'

-- 21. 查询pubs数据库中的authors表中au_lname不以字母s、g、v开头的作家的信息。

select au_lname from authors where au_lname like '[^s^g]%'

-- 22. 查询pubs数据库中的authors表中au_lname为5个字母的作家的信息。

select au_lname from authors where len(au_lname)=5

-- 23. 查询pubs数据库中employee表的前20%的数据,要求在结果中显示emp_id,fname,job_id和job_lvl字段。

select top 20 percent emp_id,fname,job_id,job_lvl from employee 

-- 24. 查询pubs数据库中employee表中job_id字段的值,要求去掉重复值。

select distinct job_id from employee

-- 25. 查询pubs数据库中的employee表,要求查找job_lvl不在80到100之间的雇员的信息。

select * from employee where job_lvl not between 80 and 100

--或

select * from employee where job_lvl<80 or job_lvl>100 

-- 26. 查询pubs数据库中的employee表,要求查找job_lvl在80到100之间的数据记录。

select * from employee where job_lvl>80 and job_lvl<100 

-- 27. 查询pubs数据库中的employee表,要求查找job_lvl在80到100之间,并且job_id在5到10之间的数据记录。

select * from employee where job_lvl>80 and job_lvl<100  and job_id between 5 and 10

-- 28. 查询pubs数据库中的employee表,要求查找job_lvl在80到100之间,或者job_id小于10的数据记录。

select * from employee where job_lvl  between 80 and 100 or job_id<10
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql server 数据库