SQL server 数据库 SQL语句高级用法

1、表的高级查询
use student
select * from stuinfo1
-- 使用 in 的子查询
select * from stuinfo where stu_age in ( select stu_age from stuinfo where
cla_id = '12345' )
select * from stuinfo where stu_age in ( 19 , 20 , 21 , 25 , 23 , 16 , 31 , 11 , 19 )
-- 使用 = 号的子查询
select * from stuinfo where stu_age = ( select stu_age from stuinfo where
stu_name = ' 张三 ' )
select * from stuinfo where stu_age = 20
-- 设置字段名称:给字段命名的时候,一般用的是英文,很理解,查询时,可以给字段进行重命名,让其便于
理解
/*
给字段设置别名格式:
1 、原字段名 as 新名称
2 、新名称 = 原字段名
3 、原字段名 新名称
*/
select stu_name from stuinfo
select stu_name as 姓名 from stuinfo
select 姓名 = stu_name from stuinfo
select stu_name 姓名 from stuinfo
/* 字段拼接:指的将多个字段合并一个新的字段,使用的是 "+" 号作为连接符。
注意:所连接的字段数据类型应该是相同类型,而且不能是数值类型;
*/
select stu_name,stu_add from stuinfo
select stu_name + stu_add from stuinfo
-- 去重 distinct ,去掉重复值
select stu_add from stuinfo
select distinct stu_add from stuinfo
-- top: 显示指定前多少条数据或前百分比的数据,后面要跟上一个数据或百分比
select * from stuinfo
select top 3 * from stuinfo -- 查询前 3 条记录
select top 10 percent * from stuinfo -- 查询前百分之 10 的记录
-- 范围查询: between and 是在一定的范围内,包含界值
-- 案例:查询年龄在 18 23 之间的学生信息,包含 18 23
select * from stuinfo where stu_age between 18 and 23
-- not between and 不在一个范围之内,不包含界值
-- 案例:查询年龄不在 18 23 之间的学生信息,结果不包含 18 23
select * from stuinfo where stu_age not between 18 and 23
-- not 否定后面的任何内容
-- 查询姓名叫张三学生
select * from stuinfo where stu_name = ' 张三 '
-- 查询姓名不叫张三的学生信息
select * from stuinfo where not stu_name = ' 张三 '
select * from stuinfo where stu_name <> ' 张三 '
select * from stuinfo where stu_name != ' 张三 '
/* 模糊查询:
关键字: like
通配符: %:0 个或多个任意字符
_ :单个字符
[] :表示在这个范围之内
[^] :表示不在这个范围之内
*/
-- 查询学生信息表中姓李学生的信息
select * from stuinfo where stu_name like ' %'
-- 查询学生信息表中姓李且名字只有一个单字的学生的信息
select * from stuinfo where stu_name like ' _'
-- 查询学生信息表中姓李或姓张学生的信息
select * from stuinfo where stu_name like ' %' or stu_name like ' %'
select * from stuinfo where stu_name like '[ 李张 ]%'
-- 查询学生信息表中即不是姓李的学生,也不是姓张的学生信息。
select * from stuinfo where not stu_name like ' %' and not stu_name like ' %'
select * from stuinfo where stu_name like '[^ 李张 ]%'
-- 排序: order by
-- 语法格式: select * from < 表名 > order by 字段名 ASC /DESC
-- 注意:如果按升序排序,可以省略 asc ,默认就是以升序进行排序;如果是降序,不可以省略 desc
-- 注意:如果有多个字段参与排序,优先级按从左至右进行排序
-- 案例:查询学生的信息,并按照年龄的升序进行排序
select * from stuinfo order by stu_age asc
select * from stuinfo order by stu_age
-- 案例:查询学生的信息,并按照年龄的降序进行排序
select * from stuinfo order by stu_age desc
-- 案例:查询学生的信息,并按照年龄降序进行排序,按照学号的升序进行排序
select * from stuinfo order by stu_age desc ,stu_id asc
-- 案例:查询学生的信息,并按照学号降序进行排序,按照年龄的升序进行排序
select * from stuinfo order by stu_id desc ,stu_age asc
-- 案例:排序与 top 结合使用
select top 2 * from stuinfo order by stu_age desc ,stu_id asc
-- 案例:按学生年龄的降序排序,并输出姓李的学生信息
select * from stuinfo where stu_name like ' %' order by stu_age desc
2、 聚合函数及 group by 分组用法
-- 求和: sum 函数,格式: sum ( 字段 ) ,注意:字段的类型只能为数值型;
-- 案例:计算学生信息表中所有学后的年龄之和
select sum ( stu_age ) 年龄之和 from stuinfo
-- 求平均值: avg 函数,格式: avg ( 字段 ) 注意:字段的类型只能为数值型
-- 案例:计算学生的平均年龄
select avg ( stu_age ) 平均年龄 from stuinfo
-- 求最大值: max 函数 格式: max ( 字段 )
-- 列出学生信息表中年龄最大的学生信息
select max ( stu_age ) 最大年龄 from stuinfo
-- 列出学生信息表中最大学号的学生信息
select max ( stu_id ) from stuinfo
-- 求最小值 : min 函数 格式: min ( 字段 )
-- 列出学生信息表中年龄最小的学生信息
select min ( stu_age ) 最小年龄 from stuinfo
-- 列出学生信息表中最小学号的学生信息
select min ( stu_id ) 最小学号 from stuinfo
-- 计数 : count 函数 格式: count ( 字段 )
-- 计算出学生信息表中学生的数量
select * from stuinfo
select count ( stu_name ) from stuinfo
select count ( stu_age ) from stuinfo
-- 计算出班级编号为 12345 的学生的人数
select count ( stu_id ) from stuinfo where cla_id = '12345'
select count ( * ) from stuinfo where cla_id = '12345'
-- 创建一个科目表
create table km (
id int identity ( 1 , 1 ) , -- 序号
km_id int primary key not null , -- 课程号
km_name varchar ( 10 ) -- 课程名称
)
insert into km values ( 1 , ' 语文 ' ) , ( 2 , ' 数学 ' ) , ( 3 , ' 英语 ' ) , ( 4 , ' 计算机 ' )
-- 创建一个成绩表
create table cj (
id int identity ( 1 , 1 ) ,
stu_id varchar ( 20 ) references stuinfo ( stu_id ) ,
km_id int references km ( km_id ) ,
cj int check ( cj >= 0 and cj <= 100 ) not null
)
insert into cj values ( '10001' , 1 , 80 ) , ( '10001' , 2 , 75 ) , ( '10001' , 3 , 90 ) ,
( '10001' , 4 , 95 ) , ( '10002' , 1 , 88 ) , ( '10002' , 2 , 98 ) , ( '10002' , 3 , 70 ) ,
( '10002' , 4 , 95 ) , ( '10003' , 1 , 30 ) , ( '10003' , 2 , 55 ) , ( '10003' , 3 , 91 ) , ( '10003' , 4 , 92 ) ,
( '10004' , 1 , 83 ) , ( '10004' , 2 , 85 ) , ( '10004' , 3 , 70 ) , ( '10004' , 4 , 65 )
select * from km
select * from cj
-- 查询学生为 10001 学生的平均成绩
select avg ( cj ) 平均成绩 from cj where stu_id = '10001'
-- 查询学生为 10001 学生的总成绩
select sum ( cj ) 总绩 from cj where stu_id = '10001'
-- 查询每个学生的总成绩
select stu_id 学号 , sum ( cj ) 总成绩 from cj group by stu_id
-- 查询每门课程平均成绩
select km_id 课程号 , avg ( cj ) 平均成绩 from cj group by km_id
-- 查询每门课程最高成绩
select km_id 课程号 , max ( cj ) 平均成绩 from cj group by km_id
-- 列出学生的总成绩大于 350 分的学生的信息
-- 使用 group by 进行分组时,如果需要用到条件语句,后面只能使用 having 做条件表达式关键字,不能使用
where
select stu_id 学号 , sum ( cj ) 总成绩 from cj group by stu_id having sum ( cj ) > 350
-- 列出每门课程的平均成绩
select km_id 课程号 , avg ( cj ) 平均成绩 from cj group by km_id
select * from class
select * from stuinfo
-- 将学生信息表中 id 编号为 7 9 的学生的班级修改为 1234
update stuinfo set cla_id = '1234' where id between 7 and 9
3、连接查询
use student
/*
连接查询:
连接查询主要应用在两张及以上有数据关联的数据查询过程中。
连接查询的分类:内连接、外连接、交叉连接
内连接:指的两表所查询结果是一样的;
左外连接:指的是查询的结果以左表为准,如果右表没有内容,则显示 null
右外连接:指的是查询的结果以右表为准,如果左表没有内容,则显示 null
全外连接:指的是查询的结果为全部结果,即两个表的所有行,如果一个没有,则显示空值。
*/
-- 案例:查询学生的总成绩,要求显示学生的姓名,总成绩
-- 查看学生信息表
select * from stuinfo
-- 查看科目信息表
select * from km
-- 查看成绩信息表
select * from cj
-- 案例:查询学生的总成绩,要求显示学生的学号,总成绩
select stu_id 学号 , sum ( cj ) 总成绩 from cj group by stu_id
select stu_name,cj,km_name from stuinfo st,cj,km where st .stu_id = cj .stu_id and
km .km_id = cj .km_id
/*
内连接:
select 字段 from 1 inner join 2 on 条件表达式(表 1 和表 2 相同的字段)
*/
-- 列出有成绩学生的所有信息及成绩信息
select st. * ,cj from stuinfo st inner join cj on st .stu_id = cj .stu_id
select st. * ,cj from stuinfo st join cj on st .stu_id = cj .stu_id
/*
左外连接:
select 字段 from 1 left join 2 on 条件表达式(表 1 和表 2 相同的字段)
右外连接:
select 字段 from 1 right join 2 on 条件表达式(表 1 和表 2 相同的字段)
全外连接
select 字段 from 1 full join 2 on 条件表达式(表 1 和表 2 相同的字段)
*/
-- 左连接
select st. * ,cj from stuinfo st left join cj on st .stu_id = cj .stu_id
-- 右连接
select st. * ,cj from stuinfo st right join cj on st .stu_id = cj .stu_id
select * from class
-- 插入一条数据
insert into stuinfo ( stu_id,stu_name,stu_sex,stu_age,stu_tel,stu_add )
values ( '10010' , ' 张飞 ' , ' ' , 20 , '133235' , ' 二七区 ' )
-- 左连接:以左表为主,左表 stuinfo, 右表为 class
select * from stuinfo -- 左表
select * from class -- 右表
-- 左连接
select * from stuinfo st left join class cl on st .cla_id = cl .cla_id
-- 右连接:
select * from stuinfo st right join class cl on st .cla_id = cl .cla_id
-- 全连接
select * from stuinfo st full join class cl on st .cla_id = cl .cla_id
-- 内连接
select * from stuinfo st inner join class cl on st .cla_id = cl .cla_id
select * from stuinfo st join class cl on st .cla_id = cl .cla_id where
cl .cla_id = '12345'
select stu_name,st .cla_id from stuinfo st,class cl where st .cla_id = cl .cla_id
-- 修改学生张三的班级编号信息
update stuinfo set cla_id = NULL where stu_id = '10001'
-- 查询学生的成绩,要求显示学生的姓名,所选课程的名称,成绩
select stu_name 学生姓名 , km_name 课程名称 ,cj 成绩 from stuinfo st
join cj on st .stu_id = cj .stu_id
join km on cj .km_id = km .km_id
-- 查询学生的成绩,要求显示学生的姓名,所选课程的名称,成绩,以内连接实现
select cla_name 班级名称 ,stu_name 学生姓名 , km_name 课程名称 ,cj 成绩 from stuinfo st
join cj on st .stu_id = cj .stu_id
join km on cj .km_id = km .km_id
join class cl on st .cla_id = cl .cla_id
-- 查询学生的成绩,要求显示学生的姓名,所选课程的名称,成绩,以左外连接实现
select cla_name 班级名称 ,stu_name 学生姓名 , km_name 课程名称 ,cj 成绩 from stuinfo st
left join cj on st .stu_id = cj .stu_id
left join km on cj .km_id = km .km_id
left join class cl on st .cla_id = cl .cla_id
-- 查询学生的成绩,要求显示学生的姓名,所选课程的名称,成绩,以右外连接实现
select cla_name 班级名称 ,stu_name 学生姓名 , km_name 课程名称 ,cj 成绩 from stuinfo st
right join cj on st .stu_id = cj .stu_id
right join km on cj .km_id = km .km_id
right join class cl on st .cla_id = cl .cla_id
-- 查询学生的成绩,要求显示学生的姓名,所选课程的名称,成绩,以全外连接实现
select cla_name 班级名称 ,stu_name 学生姓名 , km_name 课程名称 ,cj 成绩 from stuinfo st
full join cj on st .stu_id = cj .stu_id
full join km on cj .km_id = km .km_id
full join class cl on st .cla_id = cl .cla_id
/* 交叉连接也叫笛卡尔积:
格式:
select 字段名 from < A> cross join < B> [where 条件表达式 ]
不带 where 条件,显示是两个表的行数的乘积,例如 A 表为 10 行, B 表为 5 行,则结果返回为 50
如果带 where 条件,则显示满足 where 条件的记录行
*/
-- 案例一 : 不带 where 条件
select * from stuinfo cross join class
-- 案例二:带上 where 条件
select * from stuinfo st cross join class cl where cl .cla_id = '12345' and
st .cla_id is not null

相关推荐

  1. SQL server 数据库 SQL语句高级

    2023-12-22 17:34:02       30 阅读
  2. Sqlserver数据库触发器sql案例

    2023-12-22 17:34:02       39 阅读
  3. 2024.1.30 Spark SQL高级

    2023-12-22 17:34:02       29 阅读
  4. SQL server 数据库 sql语句

    2023-12-22 17:34:02       44 阅读
  5. SQLServer数据库的查询语句

    2023-12-22 17:34:02       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-22 17:34:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-22 17:34:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-22 17:34:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-22 17:34:02       18 阅读

热门阅读

  1. GO设计模式——25、模板模式(行为型)

    2023-12-22 17:34:02       36 阅读
  2. ThreadLocal和Synchronized的用法和区别

    2023-12-22 17:34:02       45 阅读
  3. 6.1 指针的认识

    2023-12-22 17:34:02       38 阅读
  4. SQL server 数据库 sql常用语句

    2023-12-22 17:34:02       44 阅读
  5. 基于SpringBoot的体育馆使用预约管理系统

    2023-12-22 17:34:02       49 阅读
  6. Zookeeper 集群搭建过程中常见错误

    2023-12-22 17:34:02       26 阅读