一 查询常量
1. SELECT 学生编号, 学生姓名,性别 FROM tb_Student
2. SELECT 学生姓名 AS 姓名, 性别 AS 学生性别 FROM tb_Student
3. SELECT 学生姓名 姓名, 性别 学生性别 FROM tb_Student
4. SELECT 学生姓名 姓名, 高数+外语 AS 高数加外语分数 FROM tb_Student
5. SELECT 学生姓名,学生编号 FROM tb_Student WHERE LEN(学生姓名) = 2
6. SELECT 学生姓名,年龄 FROM tb_Student WHERE 年龄 = 24
7. SELECT 学生姓名,年龄,性别 FROM tb_Student WHERE 学生姓名 = ‘安心’
8. SELECT 学生姓名,年龄,出生年月 FROM tb_Student WHERE 出生年月 = ‘1981/12/8’
9. SELECT 学生姓名,年龄,统招否 FROM tb_Student WHERE 出生年月 = ‘TRUE’
10.SELECT 学生姓名,年龄,家庭住址 FROM tb_student WHERE 家庭住址 IS NULL OR 家庭地址 = ‘’
二 查询变量(string.Format())
1. SELECT 学生姓名,年龄,性别,家庭住址 FROM tb_Student WHERE 学生姓名 = ‘{0}’,Name
2. SELECT 学生姓名,年龄,性别,家庭住址 FROM tb_Student WHERE 年龄 = {0},Age
3. SELECT 学生姓名,年龄,性别,出生日期 FROM tb_Student WHERE 出生日期 = {0},dt.ToShortDatestring
4. SELECT 学生姓名,年龄,性别,出生日期 FROM tb_Student WHERE 出生日期 BETWEEN '2014/1/1' AND '2014/2/1'
三 模糊查询
1. SELECT 学生姓名,年龄,性别 FROM tb_Student WHERE 学生姓名 LIKE ‘张_’
2. SELECT 学生姓名,年龄,性别 FROM tb_Student WHERE 学生姓名 LIKE ‘张__’
3. SELECT 学生姓名,年龄,性别 FROM tb_Student WHERE 学生姓名 LIKE ‘张%’
4. SELECT 学生姓名,年龄,出生日期 FROM tb_Student WHERE 出生日期 LIKE ‘%1981%’
5. SELECT 学生姓名,年龄,出生日期 FROM tb_Student WHERE 年龄 LIKE ‘2[0-9]’
6. SELECT 学生姓名,年龄,出生日期 FROM tb_Student WHERE 年龄 LIKE ‘[1-2][0-9]’
7. SELECT 学生姓名,年龄,出生日期 FROM tb_Student WHERE 学生姓名 LIKE ‘[^李]%’
8. SELECT 学生姓名,年龄,出生日期 FROM tb_Student WHERE 年龄 LIKE ‘[1-2][^0-5]
9. SELECT 学生姓名,年龄,出生日期 FROM tb_Student WHERE 年龄 LIKE ‘[1-2][^0-5] AND 学生姓名 LIKE ‘李%’
四 TOP 和 PERCENT 限制查询结果
1. SELECT Top 10 学生编号,学生姓名,年龄,家庭住址 FROM tb_Student ORDER BY 学生编号 ASC
2. SELECT Top 10 学生编号,学生姓名,高数,外语 FROM tb_Student ORDER BY 高数+外语 ASC
3. SELECT Top 10 * FROM(SELECT TOP 20 * FROM tb_Grade ORDER BY 总分 DESC) AS st ORDER BY 总分 ASC
4. SELECT TOP 50 PERCENT 书号,书名,SUM(销售数量) AS 合计销售数量 FROM tb_Book GROUP BY 书号,书名,作者 ORDER BY 3 DESC
5. SELECT TOP 20 PERCENT * FROM tb_BookMessage ORDER BY 现存数量 ASC
五 数值查询
1. SELECT 学生姓名,ISNUMERIC(年龄)FROM tb_Student
2. SELECT dm AS 数值取整前, CEILING(dm) AS 数值取整后 FROM tb_Money
3. SELECT dm AS 数值取整前, FLOOR(dm) AS 数值取整后 FROM tb_Money
4. SELECT dm AS 四舍五入前, ROUND(dm,2) AS 四舍五入后 FROM tb_Money
5. SELECT angle AS 角度, SIN(ANGLE*PI()/180) AS 正弦值 FROM tb_Angle
6. SELECT angle AS 角度, COS(ANGLE*PI()/180) AS 余弦值 FROM tb_Angle
7. SELECT myvalue AS 数值, ABS(myvalue) AS 求绝对值后 FROM tb_Value
8. SELECT * FROM tb_Student WHERE 学生编号 = 22050110+FLOOR(RAND()*10)
9. SELECT myvalue AS 数值,SIGN(myvalue) AS 判断数值 FROM tb_value
六 字符串查询
1. SELECT friendname AS 字符串,LOWER(friendname) AS 全部转换为小写, UPPER(friendname)AS 全部转换为大写 FROM tb_string
2. SELECT friendname AS 字符串,(LEN(friendname)-LEN(REPLACE(friendname,'i','')))/LEN('i') FROM tb_string
3. SELECT 学生姓名,所在院校, SUBSTRING(所在院校,2,3) AS 所在学院子串 FROM tb_Student
4. SELECT 出生年月,SUBSTRING(CONVERT(VARCHAR(20),出生年月),6,5) AS 年 FROM tb_Student
5. SELECT 所在学院 AS 删除字符串前,STUFF(所在院校,2,3,'') AS 删除字符串后 FROM tb_Student
6. SELECT 所在学院,CHARINDEX('学院',所在学院) AS 索引位置 FROM tb_Student
7. SELECT 所在学院, REPLACE(所在学院,'学院','XueYuan')AS 替换后的信息 FROM tb_Student
8. SELECT 学生姓名, CASE WHEN 所在学院 = '计算机学院' THEN '计算机学院学生' ELSE '否' END AS 是否为计算机学院学生 FROM tb_Student
七 周期和日期的查询
1. SELECT 学生编号,学生姓名, CASE WHEN ISDATE(出生年月) =1 THEN '是日期信息' ELSE '非日期新奇' END AS 生日字段是否为日期信息 FROM tb_Student
2. SELECT 学生编号,学生姓名 FROM tb_Student WHERE 出生年月 = '1980/12/24'
3. SELECT 学生编号,学生姓名 FROM tb_Student WHERE 出生年月 LIKE '%1980%'
4. SELECT * FROM tb_Book WHERE 日期 BETWEEN '2005/8/1' AND '2005/10/1'
5. SELECT 书号,书名 FROM tb_Book WHERE YEAR(日期) = '2010'AND MONTH(日期) = '9' AND DAY(日期) = '26'
6. SELECT GETDATE() AS 当前日期时间, DATENAME(WEEKDAY,GETDATA()) AS 星期
7. SELECT 学生姓名,出生年月,DATEDIFF(TEAR,出生年月,GETDATE()) AS 学生年龄 FROM tb_Student
八 比较、逻辑、重复查询
1. SELECT * FROM tb_Grade WHERE 软件工程>90 AND 外语 NOT BETWEEN 70 AND 85
2. SELECT DISTINCT 书号,条形码,书名,作者 FROM tb_Book
3. SELECT COUNT(书号) AS 记录条数,书号,书名,作者 FROM tb_Book GROUP BY 书号,书名,作者 HAVING COUNT(书号)>1
九 在查询中使用OR 和 AND
1. SELECT * FROM tb_Grade WHERE (外语 > 90 OR 高数> 90) AND 语文 >80
2. SELECT * FROM tb_Grade WHERE 外语 > 90 AND 高数 > 90
十 排序、分组统计
1. SELECT 出版社 SUM(金额) AS 总金额 FROM tb_Book GROUP BY 出版社
2. SELECT 书名,出版社,SUM(金额) AS 总金额 FROM tb_Book WHERE 出版社 = '人邮' GROUP BY ALL,书名,出版社
3. SELECT 所属部门,性别, AVG(工资) AS 平均工资 FROM tb_Employee GROUP BY 所属部门,性别 WITH CUBE
4. SELECT 所属部门,性别, AVG(工资) AS 平均工资 FROM tb_Employee GROUP BY 所属部门,性别 WITH ROLLUP
5. SELECT * FROM tb_Grade ORDER BY 马经 ASC 高数 DESC
6. SELECT TOP 5 书号,书名,作者,出版社, SUM(销售数量) AS 合计销售数量 FROM tb_Book GROUP BY 书号,书名,作者,出版社 ORDER BY SUM(销售数量) DESC
7. SELECT 学生姓名,性别,年龄,所在学院 FROM tb_Student ORDER BY 学生姓名 COLLATE chinese_prc_stroke_cs_as_ks_ws
8.SELECT 学生姓名,性别,年龄,所在学院 FROM tb_Student ORDER BY 学生姓名 COLLATE chinese_prc_cs_as
9. SELECT 存放位置,书名,SUM(库存数量)AS 合计库存数量 FROM tb_Depot GROUP BY 存放位置,书名 ORDER BY SUM(库存数量) DESC
10. SELECT k.书号,k.书名,x.作者, SUM(k.现存数量) AS 现存数量, SUM(x.销售数量) AS 销售数量 FROM tb_Vendition AS x, tb_BookMessage AS k WHERE x.书号 = k.书号 GROUP k.书号,k.书名,x.作者,k.现存数量 ORDER BY 书号
11. SELECT * FROM tb_Employee ORDER BY 所属部门 COMPUTE SUM(工资)
12. SELECT * FROM tb_Employee ORDER BY 所属部门 COMPUTE SUM(工资) BY 所属部门
十一 聚合函数
1. SELECT SUM(销售数量) AS 总数量, SUM(金额) AS 总金额 FROM tb_Book
2. SELECT AVG(年龄) AS 平均年龄 FROM tb_Student
3. SELECT * FROM tb_Ware WHERE 销价 IN (SELECT MIN(销价) FROM tb_Ware)
4. SELECT * FROM tb_Ware WHERE 销价 IN (SELECT MAX(销价) FROM tb_Ware)
5. SELECT COUNT(DISTINCT 商品名称) AS 商品数 FROM tb_Ware WHERE 销价 > 1000
6. SELECT FIRST(BookNames) AS Bookname, FIRST(author) AS peo, FIRST(sellsum) AS 第一条数据记录 FROM tab_booksort
7. SELECT LAST(BookNames) AS Bookname, LAST(author) AS peo, LAST(sellsum) AS 第一条数据记录 FROM tab_booksort
8. SELECT 所在学院 COUNT(所在学院) AS 学生数量 FROM tb_Student GROUP BY 所在学院
9. SELECT 学生编号,学生姓名,高数 FROM tb_Grade WHERE 高数 > (SELECT AVG(高数) FROM tb_Grade)
10 SELECT COUNT(DISTINCT 家庭住址) FROM tb_Student WHERE NOT(家庭住址 IS NULL OR 家庭住址 = '')
11. SELECT COUNT(学生编号) AS 学生数量, SUM(高数) AS 高数总分数 FROM tb_Grade WHERE 学生编号 IN(SELECT FLOOR(RAND()*10)+22050110 AS 随机数 UNION SELECT FLOOR(RAND()*10) +22050110 UNION SELECT FLOOR(RAND()*10)+22050110)
十二 多表查询
1. SELECT s.学生编号,s.学生姓名,s.性别,m.高数 FROM tb_Student AS s, tb_Grade As m WHERE s.学生编号 = m.学生编号 AND m.高数 > 85
2. SELECT S.学生编号,S.学生姓名,M.高数,M.外语 FROM tb_Student AS S,tb_Grade AS M WHERE S.学生编号 = M.学生编号 AND S.所在学院 = '计算机学院'
3. SELECT 顾客编号 AS 编号,顾客姓名 AS 姓名,所在城市,邮编 FROM tb_Buyer UNION SELECT 雇员编号,雇员姓名,家庭住址,邮编 FROM tb_Saleman
4. SELECT st.学生姓名,st.性别,st.年龄, gr.高数,gr.外语,gr.软件工程 FROM tb_Student AS st, tb_Grade AS gr (笛卡尔成绩查询)
十三 嵌套查询
1. SELECT 学生编号,性别,姓名,所在专业 FROM tb_Student WHERE 学生编号 IN(SELECT 学生编号 FROM tb_Grade WHERE 总分 >= 580)
2. SELECT * FROM tb_EmployeeLaborage WHERE 工资月份 = 10 AND 人员姓名 IN(SELECT 负责人 FROM tb_EmployeeDepartment WHERE 负责人 IN(SELECT 人员姓名 FROM tb_EmployeePerson WHERE 学历 ='本科')) ORDER BY 人员编号
3. SELECT * FROM tb_Grade WHERE 高数 > ALL (SELECT 高数 FROM tb_Grade WHERE 学生姓名 IN('王立','李丽'))
4. SELECT * FROM tb_Grade WHERE 高数 > ANY (SELECT 高数 FROM tb_Grade WHERE 学生姓名 IN('王立','李丽'))
十四 子查询
1. SELECT * FROM (SELECT * FROM tb_Student WHERE 学生编号 IN(SELECT 学生编号 FROM tb_Grade WHERE 软件工程 > (SELECT 软件工程 > FROM tb_Grade WHERE tb_Grade WHERE 学生姓名 ='王晓亮'))) AS stu ORDER BY stu.学生编号
2. SELECT * FROM tb_Student WHERE 学生编号 IN(SELECT 学生编号 FROM tb_Grade WHERE 高数 > (SELECT AVG(高数) FROM tb_Grade))
3. SELECT st.*, gr.总分 FROM tb_student AS st INNER JOIN tb_Grade AS gr ON st.学生编号 = gr.学生编号 WHERE 所在学院 = '理学院' AND gr.总分 > ALL(SELECT gr.总分 FROM tb_Grade AS gr INNER JOIN tb_Student AS st ON st.学生编号 = gr.学生编号 WHERE st.所在学院 = '管理学院')
4. SELECT st.*, gr.总分 FROM tb_student AS st INNER JOIN tb_Grade AS gr ON st.学生编号 = gr.学生编号 WHERE 所在学院 = '理学院' AND gr.总分 > ANY(SELECT AVG(CONVERT(INT,总分) FROM tb_Grade)
5. SELECT * FROM tb_Student AS st WHERE EXISTS (SELECT * FROM tb_Grade AS gr WHERE st.学生编号 = gr.学生编号 AND gr.总分 > 580)
6. SELECT 所在学院,学生姓名,年龄,(SELECT AVG(年龄) FROM tb_Student) AS 平均年龄 FROM tb_Student GROUP BY 所在学院,学生姓名,年龄 HAVING 年龄 > (SELECT AVG(年龄) FROM tb_Student)
7. UPDATE tb_Laborage SET 基本工资 = (SELECT 基本工资 FROM tb_AppointedLaborage WHERE 工作时间 = '1年') WHERE 员工姓名 = '江南'
8. DELETE FROM tb_Student WHERE 学生编号 IN(SELECT 学生编号 FROM tb_Student WHERE 性别 ='女' AND 出生年月 = '1981/12/12' AND 所学专业 = '会计学')
十五 组合语句
1. SELECT 学生编号,学生姓名,性别,年龄 FROM tb_Student WHERE 所在学院 = '理学院' UNION SELECT 学生编号,学生姓名,性别,年龄 FROM tb_Student WHERE 学生编号 IN(SELECT 学生编号 FROM tb_Grade WHERE 总分 > 600)
2. SELECT 学生姓名 FROM tb_Student UNION SELECT CONVERT(VARCHAR(20),总分) FROM tb_Grade WHERE 总分 > 570 UNION SELECT 课程名称 FROM tb_Course
3. SELECT 年龄 AS 信息 FROM tb_Student UNION SELECT 总分 FROM tb_Grade UNION SELECT 课程编号 FROM tb_Course ORDER BY 信息 ASC
4. SELECT 学生编号,学生姓名 FROM tb_Student INTERSECT SELECT 学生编号,学生姓名 FROM tb_Grade
5. SELECT 学生编号,学生姓名 FROM tb_Student EXCEPT SELECT 学生编号,学生姓名 FROM tb_Grade
十六 内连接查询
1. SELECT tb_Student.学生姓名,tb_student.性别,tb_Grade.总分 FROM tb_Student INNER JOIN tb_Grade ON tb_Student.学生编号 = tb.Grade.学生编号
2. SELECT st.学生姓名,st.性别,st.年龄,gr.总分,tc.出勤率 FROM tb_student AS
st INNER JOIN tb_Grade AS gr ON st.学生编号 = gr.学生编号 INNER JOIN tb_StudentTimeCard AS tc ON st.学生编号 = tc.学生编号
3. SELECT st1.* FROM tb_Student AS st1 INNER JOIN tb_Student AS st2 ON st1.所在学院 = st2.所在学院 AND st2.学生姓名 = '李小龙'
4. SELECT st1.* FROM tb_Student AS st1 INNER JOIN tb_Student AS st2 ON st1.所在学院 <>st2.所在学院 AND st2.学生姓名 = '李小龙'
5. SELECT 员工信息表.人员编号,员工信息表.人员姓名 FROM tb_employeeperson AS 员工信息表 INNER JOIN tb_EmployeeLaborage AS 员工工资表 ON 员工信息表.AS 人员编号 = 员工工资表.人员编号
十七 外连接查询
1. SELECT tb_Student.学生姓名,tb_student.性别,tb_Grade.总分 FROM tb_Student LEFT OUTER JOIN tb_Grade ON tb_Student.学生编号 = tb.Grade.学生编号
2. SELECT tb_Student.学生姓名,tb_student.性别,tb_Grade.总分 FROM tb_Student RIGHT OUTER JOIN tb_Grade ON tb_Student.学生编号 = tb.Grade.学生编号
3.SELECT st.学生姓名,st.性别,st.年龄,gr.总分,tc.出勤率 FROM tb_student AS
st LEFT OUTER JOIN tb_Grade AS gr ON st.学生编号 = gr.学生编号 LEFT OUTER JOIN tb_StudentTimeCard AS tc ON st.学生编号 = tc.学生编号
十八 利用IN进行查询
1. SELECT * FROM tb_Ware WHERE 销价 IN (SELECT MAX(销价) FROM tb_Ware)
2. SELECT * FROM tb_Ware WHERE 销价 NOT IN (SELECT MAX(销价) FROM tb_Ware)
十九 交叉运算
1. Transform aggfunction selectstatement PIVOT pivotfield [IN(value1[value2[...]])]
2. Case input_expression
WHEN when_expression THEN result_expression
[...n]
ELSE else_result_expression
END
3. 存储过程?
二十 函数查询
1. SELECT 员工姓名,出生日期 AS 格式化前出生日期 FORMAT(出生日期,'yyyy年mm月dd日') AS 格式化后日期 FROM 员工生日表
2. SELECT 出生年月,DATENAME(YEAR,出生年月)+'年'+DATENAME(MONTH,出生年月)+'月'+DATENAME(DAY,出生年月)+'日' AS 处理后出生年月 FROM tb_Student
3. SELECT 员工姓名, FORNAT(出生日期,'yyyy年mm月dd日') AS 出生日期,MID(出生日期,1,7) AS 出生年月 FROM 员工生日表
4. SELECT 员工姓名, FORNAT(出生日期,'yyyy年mm月dd日') AS 出生日期,SUBSTRING(出生日期,1,7) AS 出生年月 FROM 员工生日表
5. SELECT 员工姓名,出生日期,DATEDIFF('yyyy',出生日期,DATE()) AS 年龄 FROM 员工生日表
二十一 索引查询
1. CREATE UNIQUE INDEX index_Student ON tb_Student(学生编号)
2. DROP INDEX tb_Student.index_Student
3. CREATE CLUSTERED INDEX index_Student ON tb_Student(学生编号)
4.
二十二 应用存储过程
1.
二十三 Having语句应用
1. SELECT 所在学院,COUNT(*) AS 人数,AVG(年龄) AS 平均年龄 FROM tb_Student GROUP BY 所在学院 HAVING AVG(年龄) > 22
2. SELECT 所在学院,COUNT(*) AS 人数,MAX(高数) AS 最高成绩,AVG(高数) AS 高数平均成绩 FROM tb_student AS st INNER JOIN tb_grade AS gr ON st.学生编号 = gr.学生编号 GROUP BY 所在学院 HAVING AVG(高数) > 60