1、查询最晚入职的员工的所有信息
select * from employees order by hire_date desc limit 1
按照入职日期降序排列,然后只取一条
2、内连、外连、左连、右连
内连:仅将两个表中满足连接条件的行组合起来作为结果集。默认如果啥都不写就是内连
外连:包括左连和右连
左连:a left join b 先列出所有a的数据,然后a的如果没有就补null
右连:a right join b 先列出所有b的数据,然后a的如果没有就补null
如:
a表 id name b表 id job parent_id
1 张3 1 23 1
2 李四 2 34 2
3 王武 3 34 4
a.id同parent_id 存在关系
1) 内连接
select a.*,b.* from a inner join b on a.id=b.parent_id
结果是
1 张3 1 23 1
2 李四 2 34 2
2)左连接
select a.*,b.* from a left join b on a.id=b.parent_id
结果是
1 张3 1 23 1
2 李四 2 34 2
3 王武 null
3) 右连接
select a.*,b.* from a right join b on a.id=b.parent_id
结果是
1 张3 1 23 1
2 李四 2 34 2
null 3 34 4
4) 完全连接
select a.*,b.* from a full join b on a.id=b.parent_id
结果是
1 张3 1 23 1
2 李四 2 34 2
null 3 34 4
3 王武 null
内外连参考:https://blog.csdn.net/zhouboke/article/details/80431725
3、查找薪资表中,薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
select emp_no,count(emp_no) as t
from salaries
group by emp_no having t>15
备注:
where....group by..... SQL会在分组之前计算where语句。
group by...having... SQL会在分组之后计算having语句。