⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 实例6(简单查询).sql

📁 经典的sql资料
💻 SQL
📖 第 1 页 / 共 2 页
字号:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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
















-- 复习:


						 -- *  					--全部列
						|
						|-- 字段名1,字段名2...字段名n		--指定列 
						|
						|-- DISTINCT 字段名			--去掉重复值(NULL如何处理?)
						|
						|-- TOP n *				--前n 行全部列
						|
						|-- TOP n 字段名1,...字段名n		--前n 行指定列
						|
						|-- TOP n PERCENT *			--前n% 行全部列
SELECT select_list		选择列表	|
						|-- TOP n PERCENT 字段名1,...字段名n	--前n% 行指定列
						|
						|-- 表达式
						|
						|-- 函数
						|
						|-- AS 别名
						|
						|-- 聚合函数(AVG COUNG MAX MIN SUM) 	-分组计算



[INTO new_table]		新的表

						 -- [server_name].[database_name].[owner].[object]
						|
						|-- 可以有多个表
FROM table_source 		表		|
						 -- 可以是其他数据库对象(例如:视图)


						 -- 逻辑操作符 AND OR NOT
						|
						|-- 比较操作符 > < = 等等						
						|
						|-- LIKE 或 NOT LIKE(通配符 %  _  []  [^])
[WHERE search_condition]	条件语句	|
						|-- BETWEEN AND
						|
						|-- IN 或 NOT IN						
						|
						|-- NULL 或 NOT NULL					
						|
						 -- 嵌套查询


						 -- 字段名1,字段名2...字段名N
						|
[GROUP BY group_by_expr]	分组表达式	|-- CUBE						
						|
						 -- ROLLUP


[HAVING search_condition]	查询条件

						 -- 字段名1,字段名2...字段名N
						|
[ORDER BY order_exp[ASC|DESC]]	排序表达式	|-- ASC  升序						
						|
						 -- DESC 降序

























--练习参考答案:

-- 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 like '%1984%'

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

-- 5. 查询 test 数据库中 exam表中所有学生的记录
select t_number from exam group by t_number 

-- 6. 查询 test 数据库中 用COMPUTE计算exam表中所有课程考试成绩的平均分
select c_number,t_grade  from exam  compute avg(t_grade)

-- 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









⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -