SQL总结

作者: clshinem | 来源:发表于2017-12-05 19:47 被阅读0次
1:limit的用法:1,2,3,4,5,6,7,8,9,10,11
select * from employees order by id limit 10,1 > 11
select * from employees oeder by id limit 1 > 1
select * from employees order by id limit 1 offset 3 > 4
2:eg:查找不是manager的员工
select * from employees where id not in (select id from manager );
3:多表联查的问题:

inner join ; left join ; right join (我感觉我用这个比较少,都是两个表互换位置,然后使用left join)
inner join 两个表中都有的数据
left join 左表的数据全部都有,如果b表也有,那就取对应的值,如果b表没有,那就取null

4:嵌套查询
select * from (select * from **)where ***·····
5:order by 是排序,group by 是分组

order by DESC 倒序
如果不使用group 会有很多重复的项出来

id    salary
1       100
1       100
2       200

使用后

id    salary 
 1      100
 2      200

having在group by 后面

6:DISTINCT 去重
7:不等于 <>
select * from employees where id.1 <> id.2
8:SQL语句中可以有计算 where emp_no % 2 == 1
9:聚集函数中可以有distent eg: count (diatent emp_no)
10:三张表链接可以使用两次 left join 不用加括号
11:如果要多个列排名,可以用两个表来相比较,然后统计另一个表比自身多的记录数,(count)但是后面要加group by 不加的话则只会输出一条数据
12:【对于employees表,在对first_name进行排名后,选出奇数排名对应的first_name】。
select e4.first_name from (select e3.first_name ,e3.id from (select e1.first_name,count (e2.first_name) as id from employees as e1,employees as e2 where e1.first_name >= e2.first_name group by e1.first_name )as e3 where e3.id % 2 =1)as e4;
13:这个问题我也觉得很有意思,在数据库中增加一列的做法
select e1.emp_no ,e1.salary,count(distinct e2.salary) as rank from salaries as e1,salaries as e2 where e1.to_date ='9999-01-01' and e2.to_date='9999-01-01' and e1.salary >= e2.salary group by e1.salary order by e1.emp_no; 

要注意 取得S2中比S1大的数量。然后形成一列rank,然后排序。

14:获取员工其当前的薪水比其manager当前薪水还高的相关信息

这个题的有意思程度是**,重点就在于创建两张表,一张是当前所有员工的薪水(员工号,部门号,薪水)另一张是当前所有manager的薪水(员工号,部门号,薪水)然后再过滤条件,要部门号相等,然后员工的salary大于manager的salary

相关文章

  • sql注入总结(转载)

    sql注入总结(一)--2018自我整理 SQL注入总结 前言: 本文和之后的总结都是进行总结,详细实现过程细节可...

  • SQL 常用优化手段总结 - 索引的应用

    系列文章回顾SQL 常用优化手段总结 - 分析 SQL 语句的一般步骤SQL 常用优化手段总结 - 索引的应用SQ...

  • SQL 常用优化手段总结 - 分析 SQL 语句的一般步骤

    系列文章回顾SQL 常用优化手段总结 - 分析 SQL 语句的一般步骤SQL 常用优化手段总结 - 索引的应用SQ...

  • SQL Server知识总结

    SQL Server知识总结

  • SQL题练习

    SQL语法总结:http://www.w3school.com.cn/sql/sql_func_min.aspSQ...

  • 黑猴子的家:mysql 字符型

    1、字符型总结 sql

  • 新学习计划

    本周的学习安排是sql,由于时间关系仅看了sql必知必会前两章,下周总结,安装sql,总结。对学习计划进行修改,在...

  • SQL总结

    常用的数据类型 SQL种类 创建table insert数据 select update delete 改进版建表...

  • SQL总结

    目录 简介 在Android中存储数据有时会用到数据库,Android给我们提供了 一系列的API来操作数据库,非...

  • SQL总结

    1:limit的用法:1,2,3,4,5,6,7,8,9,10,11 2:eg:查找不是manager的员工 3:...

网友评论

      本文标题:SQL总结

      本文链接:https://www.haomeiwen.com/subject/fnthixtx.html