SQL高级查询

发布 2021-05-02 03:02:28 阅读 8923

sql server t-sql 1

基本常用查询 3

-select 3

-all 查询所有 3

-distinct 过滤重复 3

-count 统计 3

-top 取前n条记录 3

-alias column name 列重命名 3

-alias table name 表重命名 3

-column 列运算 3

-where 条件 3

-and 并且 4

-or 或者 4

-between ..and ..相当于并且 4

-like 模糊查询 4

-in 子查询 4

-not in 不在其中 4

-is null 是空 4

-is not null 不为空 4

-order by 排序 4

-group by 分组 4

-group by all 所有分组 5

-h**ing 分组过滤条件 5

嵌套子查询 5

# from (select … table)示例 5

# in, not in子句查询示例 6

# exists和not exists子句查询示例 6

# some、any、all子句查询示例 6

聚合查询 7

1、 distinct去掉重复数据 7

2、 compute和compute by汇总查询 7

3、 cube汇总 7

排序函数 8

# row_number函数 8

# rank函数函数 8

# dense_rank函数 8

# partition by分组子句 8

# ntile平均排序函数 9

集合运算 9

1、 union和union all进行并集运算 9

2、 intersect进行交集运算 9

3、 except进行减集运算 9

公式表表达式 10

连接查询 10

1、 简化连接查询 10

2、 left join左连接 10

3、 right join右连接 10

4、 inner join内连接 11

5、 cross join交叉连接 11

6、 自连接(同一张表进行连接查询) 11

函数 111、 聚合函数 11

2、 日期时间函数 11

3、 数学函数 12

4、 元数据 12

5、 字符串函数 12

6、 安全函数 13

7、 系统函数 14

8、 配置函数 14

9、 系统统计函数 15

10、 用户自定义函数 15

高级查询在数据库中用得是最频繁的,也是应用最广泛的。

基本常用查询

-select

select * from student;

-all 查询所有。

select all sex from student;

-distinct 过滤重复。

select distinct sex from student;

-count 统计。

select count(*)from student;

select count(sex) from student;

select count(distinct sex) from student;

-top 取前n条记录。

select top 3 * from student;

-alias column name 列重命名。

select id as 编号, name '名称', sex 性别 from student;

-alias table name 表重命名。

select id, name, from student s;

-column 列运算。

select (age + id) col from student;

select + from classes c, student s where =

-where 条件。

select * from student where id = 2;

select * from student where id > 7;

select * from student where id < 3;

select * from student where id <>3;

select * from student where id >=3;

select * from student where id <=5;

select * from student where id !>3;

select * from student where id !<5;

-and 并且。

select * from student where id > 2 and sex = 1;

-or 或者。

select * from student where id = 2 or sex = 1;

-between ..and ..相当于并且。

select * from student where id between 2 and 5;

select * from student where id not between 2 and 5;

-like 模糊查询。

select * from student where name like '%a%';

select * from student where name like '%a][o]%'

select * from student where name not like '%a%';

select * from student where name like 'ja%';

select * from student where name not like '%j,n]%'

select * from student where name like '%j,n,a]%'

select * from student where name like '%ja,as,on]%'

select * from student where name like '%ja_on]%'

SQL高级查询

高级查询概念。在掌握查询的基础知识后,您就可以探知查询解决方案中使用的下列高级查询概念 在选择列表中使用聚合函数。使用 group by 对多行分组。使用 union 组合结果。子查询基础知识。使用 case 处理条件数据。并行查询。汇总数据 在选择列表中使用聚合函数。聚合函数 例如 sum g c...

SQL高级查询语句

交互式sql语句。1.1 创建数据库 unis db 日期 及其基本表 学生 课程 选课 1 建立一个 学生 表student,它由学号sno 姓名sname 性别ssex 年龄sage 所在系sdept五个属性组成,其中学号为主属性,ssex默认为 男 年龄大于0。2 建立 课程 表course,...

SQL 06高级查询

练习6复杂的结构化查询语句。6 1上机目的。1 掌握select语句的统计函数的作用和使用方法。2 通过练习select语句的group by和order by字句的用法,理解其作用,掌握语句的写法。3 通过练习涉及多张表的连接查询,掌握它的作用和写法。6 2上机练习预备知识点。6 2 1汇总函数。...