sql多表查询+小题目

查询

  1. 单表查询:聚合函数,条件查询,分页,排序,分组
  2. 多表查询:内连接(方言版,标准版),外连接(左外,右外),子查询(结果集多行多列)
单表查询:
  1. 条件查询:

    in函数表示一个集合

    SELECT * from t_emps;
    SELECT * FROM t_emps WHERE sex='女';
    SELECT * FROM t_emps WHERE sex<>'女';
    SELECT * FROM t_emps WHERE sex<>'女' and salary>5000 AND deptId=5;
    SELECT * FROM t_emps WHERE sex<>'女' and salary>5000 AND deptId in(4,5);
    
  2. 聚合查询:

    SELECT count(*) FROM t_emps;//总数
    SELECT sum(salary) from t_emps;//工资总数
    SELECT Max(salary) from t_emps;//最大工资
    SELECT MIN(salary) from t_emps;//最低工资
    SELECT avg(salary) from t_emps;//平均工资
    
  3. 分页查询:

    limit(0,5)0表示开始页数,5表示要展示的页数

    SELECT * from t_emps LIMIT 0,5;//第一页
    SELECT * from t_emps LIMIT 5,5;//第二页
    SELECT * from t_emps LIMIT 10,5;//第三页
    SELECT * from t_emps LIMIT 15,5;//第四页
    SELECT * from t_emps LIMIT 20,5;//第五页
    
  4. 排序

    SELECT * FROM t_emps ORDER BY birth ASC,salary Asc;
    SELECT * from t_emps WHERE sex<>'女' and deptId=1 ORDER BY salary desc ;
    SELECT * from t_emps WHERE sex='女' ORDER BY salary ASC;
    
    
  5. 分组

    结合聚合函数使用

    SELECT id,SUM(salary) from t_emps GROUP BY id;
    SELECT count(*),sex FROM t_emps GROUP BY deptId;
    
  6. 模糊查询

    %表示多个占位符

    _表示一个占位符

    SELECT * FROM t_emps WHERE name LIKE '%曹%';
    SELECT * FROM t_emps WHERE name LIKE '曹_';
    
多表查询:
  1. 笛卡尔连接

    SELECT *  FROM t_dept,t_emps;
    
  2. 内连接

    SELECT * FROM t_dept d,t_emps e WHERE d.did=e.deptId;
    //方言版内连接
    SELECT * from t_dept d INNER JOIN t_emps e on d.did=e.deptId;
    //标准版内连接
    
  3. 外连接

    SELECT * from t_dept d LEFT JOIN t_emps e on d.did=e.deptId;
    //左外连接
    SELECT * from t_dept d RIGHT JOIN t_emps e on d.did=e.deptId; 
    //右外连接
    
    SELECT * from t_dept d full JOIN t_emps e on d.did=e.deptId;//全连接,mysql不支持
    
  4. 工资最高的人所在的部门

思路:

  1. 首先找查询到最高的工资;
  2. 根据最高的工资找到对应的部门号(部门号是另一个表的主键)
  3. 将另一个表的id=部门号;
SELECT MAX(salary) from t_emps;
SELECT deptId from t_emps WHERE salary=(SELECT MAX(salary) from t_emps);

SELECT dname FROM t_dept where did=(SELECT deptId from t_emps WHERE salary=(SELECT MAX(salary) from t_emps));
  1. 工资在平均工资上下浮动1000以内的人和部门信息

    思路:

    1. 先在t_emps查询平均工资
    2. t_dept,t_emps进行内查询,找到人和部门信息
    3. 筛选条件当工资在平均工资上下浮动1000以内
    SELECT avg(salary) FROM t_emps;
    SELECT * from t_dept t INNER JOIN t_emps e WHERE did=deptId;
    SELECT * from t_dept t INNER JOIN t_emps e ON did=deptId WHERE salary 
    BETWEEN ((SELECT AVG(salary) from t_emps)-1000) AND ((SELECT AVG(salary) from t_emps)+1000);
    
  2. 人数最多的部门是哪个部门

    SELECT * from t_dept d INNER JOIN t_emps e WHERE d.did=e.deptId;//两表内连接查询
    SELECT count(*) c,dname from t_dept d INNER JOIN t_emps e WHERE d.did=e.deptId GROUP BY d.did;//根据did分组内连接显示数量和部门名
    SELECT COUNT(*) c,deptId FROM t_emps GROUP BY deptId;//根据部门号分组显示数量
    SELECT MAX(e.c)  FROM (SELECT COUNT(*) c,deptId FROM t_emps GROUP BY deptId) e;//筛选人数最多的部门
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包括:19道SQL语句查询题目及答案、建表SQL语句、题目相关的表截图。答案中除了包含intersect、except关键词的答案,其余都在MySQL上跑过,确保运行无误(MySQL不支持intersect、except关键词),因为脑细胞死得有些多,资源分不少请大家见谅。 题目如下: Q:Find all customers who have both an account and a loan at the Perryridge branch.(ps:MySQL不支持intersect运算符) Q:Find the number of depositors for each branch. Q:Find the names of all branches where the average account balance is more than $1,200. Q:Find the names of all branches that have greater assets than all branches located in Brooklyn. Q:Find all accounts with the maximum balance. Q:Find all branches that have greater assets than some branch located in Brooklyn. Q:Find all customers who have both an account and a loan at the bank. Q:Find all customers who have accounts at all branches located in Brooklyn. Q:Find the average account balance at the Perryridge branch. Q:Find the number of tuples in the customer relation. Q:Find the number of depositors in the bank. Q:Find the number of depositors for each branch. Q:Find all customers who have a loan at the bank but do not have an account at the bank. Q:Find all branches where the total account deposit is greater than the average of the total account deposits at all branches. Q:Find all customers who have both an account and a loan at the bank. Q:Find all customers who have at most one account at the Perryridge branch. Q:Provide as a gift for all loan customers of the Perryridge branch, a $200 savings account. Let the loan number serve as the account number for the new savings account. Q:Increase all accounts with balances over $10,000 by 6%, all other accounts receiver 5%.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值