Mysql 入门小练习

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql 入门小练习相关的知识,希望对你有一定的参考价值。

自学了一段时间java,这几天看了些面试题目,刚好把之前的mysql数据库拿出来翻了翻。

    查询语句书写顺序:SELECT – FROM- WHERE- GROUP BY- HAVING- ORDER BY-LIMIT
    查询语句执行顺序:FROM - WHERE -GROUP BY - HAVING - SELECT - ORDER BY-LIMIT    
1.1 查询stu所有列
    SELECT * FROM stu;
1.2 查询stu表指定列
    SELECT sid,sname FROM stu;
2.2 查询stu表性别为女,并且年龄50的记录
    SELECT * FROM stu WHERE gender=‘female‘ AND age<50;
2.3 查询stu表表学号为S_1001,或者姓名为liSi的记录
    SELECT * FROM stu WHERE sid=‘S_1001‘ OR sname=‘liSi‘;
2.4 查询stu表学号为S_1001,S_1002,S_1003的记录
    SELECT * FROM stu WHERE sid IN (‘S_1001‘,‘S_1002‘,‘S_1003‘);
2.5 查询stu表学号不是S_1001,S_1002,S_1003的记录
    SELECT * FROM stu WHERE sid NOT IN(‘S_1001‘,‘S_1002‘,‘S_1003‘);
2.6 查询stu表年龄为NULL的记录
    SELECT * FROM stu WHERE age IS NULL;
2.7 查询年龄在20到40之间的学生记
    SELECT *FROM stu WHERE age BETWEEN 20 AND 40;
2.8 查询性别非男的学生记录
    SELECT * FROM stu WHERE gender NOT IN (‘male‘);
2.9 查询姓名不为NULL的学生记录
    SELECT * FROM stu WHERE sid IS NOT NULL;


3.1 查询姓名由5个字母构成的学生记录
    SELECT * FROM stu WHERE sname LIKE ‘_____‘;
3.2 查询姓名由5个字母构成,并且第5个字母为“i”的学生记录
    SELECT * FROM stu WHERE sname LIKE ‘____i‘;
3.3 查询姓名以“z”开头的学生记录
    SELECT * FROM stu WHERE sname LIKE ‘z%‘;
3.4 查询姓名中第2个字母为“i”的学生记录
    SELECT * FROM stu WHERE sname LIKE ‘_i%‘;
3.5 查询姓名中包含“a”字母的学生记录
    SELECT * FROM stu WHERE sname LIKE ‘%a%‘;


4.1 去除emp表重复记录
    SELECT DISTINCT sal FROM emp ;
4.2 查看emp表雇员的月薪与佣金之和
    SELECT ename, SUM(sal+IFNULL(comm,0)) AS ‘收入‘ FROM emp;
4.3 给emp表列名添加别名
    SELECT ename AS ‘名称‘ FROM emp;


5.1 查询所有学生记录,按年龄升序排序
    SELECT * FROM stu ORDER BY age ASC;
5.2 查询所有学生记录,按年龄降序排序
    SELECT * FROM stu ORDER BY age DESC;
5.3 "查询所有雇员,按月薪降序排序,如果月薪相同时,按编号升序排序
    select * from emp order by sal Desc,empno asc;"


6.1  查询emp表中记录数:
    SELECT COUNT(*) AS ‘总记录数‘ FROM emp;
6.2  查询emp表中有佣金的人数:
    SELECT COUNT(*) FROM emp WHERE comm IS NOT NULL;
6.3  查询emp表中月薪大于2500的人数:
     SELECT COUNT(*) FROM emp WHERE sal>2500;
6.4  统计月薪与佣金之和大于2500元的人数:
    SELECT COUNT(*) FROM emp WHERE sal+IFNULL(comm,0)>2500;  
6.5  ‘查询有佣金的人数,以及有领导的人数:
    select count(sal),count(comm) from emp;‘


7.1  查询所有雇员月薪和:
    SELECT SUM(sal) FROM emp;
7.2  查询所有雇员月薪和,以及所有雇员佣金和:
    SELECT SUM(sal) AS ‘月薪和‘,SUM(comm) FROM emp;
7.3  查询所有雇员月薪+佣金和:
    SELECT SUM(sal+IFNULL(comm,0)) AS ‘月需发工资‘ FROM emp;
7.4  统计所有员工平均工资:
    SELECT AVG(sal) FROM emp;
7.5  查询最高工资和最低工资:
    SELECT MAX(sal),MIN(sal) FROM emp;
7.6  查询每个部门的部门编号和每个部门的工资和:
    SELECT deptno , SUM(sal) FROM emp GROUP BY deptno;
7.7  查询每个部门的部门编号以及每个部门的人数:
    SELECT deptno,COUNT(empno) FROM emp GROUP BY deptno;
7.8  查询每个部门的部门编号以及每个部门工资大于1500的人数:
    SELECT deptno,COUNT(*) FROM emp WHERE sal>1500 GROUP BY deptno ;
7.9  查询工资总和大于9000的部门编号以及工资和:
    SELECT deptno , SUM(sal) FROM emp GROUP BY deptno HAVING SUM(sal)>9000;
8.1 查询5行记录,起始行从0开始
    SELECT * FROM emp LIMIT 0,5;
8.2 查询10行记录,起始行从3开始
    SELECT * FROM emp LIMIT 3,10;

9.1 工资高于JONES的员工。
    SELECT e1.empno,e1.ename,e1.sal FROM emp e1 ,(SELECT sal FROM emp WHERE ename=‘JONES‘) e2 WHERE e1.sal >e2.sal;  
9.2 查询与SCOTT同一个部门的员工。
    SELECT ename FROM emp WHERE deptno = (SELECT deptno FROM emp WHERE ename=‘SCOTT‘);
9.3 工资高于30号部门所有人的员工信息
    SELECT * FROM emp WHERE sal>(SELECT MAX(sal) FROM emp WHERE deptno=30);    
9.4 查询工作和工资与MARTIN(马丁)完全相同的员工信息
    SELECT *
    FROM emp e1,(SELECT ename,job,sal FROM emp WHERE ename=‘MARTIN‘) e2
    WHERE e1.job=e2.job AND e1.sal=e2.sal AND e1.ename != (‘MARTIN‘);
9.5 有2个以上直接下属的员工信息
    SELECT * FROM EMP WHERE empno IN(SELECT mgr FROM emp GROUP BY mgr HAVING COUNT(*)>2);
9.6 查询员工编号为7788的员工名称、员工工资、部门名称、部门地址
    SELECT e.ename AS ‘员工姓名‘,e.sal AS ‘员工工资‘ , d.dname AS ‘部门名称‘,d.loc AS ‘部门地址‘
    FROM emp e,dept d
    WHERE e.empno =7788 AND e.deptno = d.deptno;
9.7 求7369员工编号、姓名、经理编号和经理姓名
    SELECT e.empno,e.ename,e.mgr,a.ename
    FROM emp e, emp a
    WHERE e.empno=7369 AND e.mgr = a.empno;

9.8 求各个部门薪水最高的员工所有信息
    SELECT *
    FROM emp e,(SELECT e2.deptno ,MAX(e2.sal) AS ‘ss‘ FROM emp e2 GROUP BY e2.deptno) s
    WHERE   e.sal = s.ss  AND e.deptno = s.deptno;

======================================================================
学生表

CREATE TABLE stu (
    sid    CHAR(6),
    sname        VARCHAR(50),
    age        INT,
    gender    VARCHAR(50)
);
INSERT INTO stu VALUES(‘S_1001‘, ‘liuYi‘, 35, ‘male‘);
INSERT INTO stu VALUES(‘S_1002‘, ‘chenEr‘, 15, ‘female‘);
INSERT INTO stu VALUES(‘S_1003‘, ‘zhangSan‘, 95, ‘male‘);
INSERT INTO stu VALUES(‘S_1004‘, ‘liSi‘, 65, ‘female‘);
INSERT INTO stu VALUES(‘S_1005‘, ‘wangWu‘, 55, ‘male‘);
INSERT INTO stu VALUES(‘S_1006‘, ‘zhaoLiu‘, 75, ‘female‘);
INSERT INTO stu VALUES(‘S_1007‘, ‘sunQi‘, 25, ‘male‘);
INSERT INTO stu VALUES(‘S_1008‘, ‘zhouBa‘, 45, ‘female‘);
INSERT INTO stu VALUES(‘S_1009‘, ‘wuJiu‘, 85, ‘male‘);
INSERT INTO stu VALUES(‘S_1010‘, ‘zhengShi‘, 5, ‘female‘);
INSERT INTO stu VALUES(‘S_1011‘, ‘xxx‘, NULL, NULL);


============================================================================
部门表

CREATE TABLE dept(
    deptno        INT,
    dname        VARCHAR(14),
    loc        VARCHAR(13)
);
INSERT INTO dept VALUES(10, ‘ACCOUNTING‘, ‘NEW YORK‘);
INSERT INTO dept VALUES(20, ‘RESEARCH‘, ‘DALLAS‘);
INSERT INTO dept VALUES(30, ‘SALES‘, ‘CHICAGO‘);
INSERT INTO dept VALUES(40, ‘OPERATIONS‘, ‘BOSTON‘);


=============================================================================
雇员表

CREATE TABLE emp(
    empno        INT,
    ename        VARCHAR(50),
    job        VARCHAR(50),
    mgr        INT,
    hiredate    DATE,
    sal        DECIMAL(7,2),
    comm        DECIMAL(7,2),
    deptno        INT
) ;
INSERT INTO emp VALUES(7369,‘SMITH‘,‘CLERK‘,7902,‘1980-12-17‘,800,NULL,20);
INSERT INTO emp VALUES(7499,‘ALLEN‘,‘SALESMAN‘,7698,‘1981-02-20‘,1600,300,30);
INSERT INTO emp VALUES(7521,‘WARD‘,‘SALESMAN‘,7698,‘1981-02-22‘,1250,500,30);
INSERT INTO emp VALUES(7566,‘JONES‘,‘MANAGER‘,7839,‘1981-04-02‘,2975,NULL,20);
INSERT INTO emp VALUES(7654,‘MARTIN‘,‘SALESMAN‘,7698,‘1981-09-28‘,1250,1400,30);
INSERT INTO emp VALUES(7698,‘BLAKE‘,‘MANAGER‘,7839,‘1981-05-01‘,2850,NULL,30);
INSERT INTO emp VALUES(7782,‘CLARK‘,‘MANAGER‘,7839,‘1981-06-09‘,2450,NULL,10);
INSERT INTO emp VALUES(7788,‘SCOTT‘,‘ANALYST‘,7566,‘1987-04-19‘,3000,NULL,20);
INSERT INTO emp VALUES(7839,‘KING‘,‘PRESIDENT‘,NULL,‘1981-11-17‘,5000,NULL,10);
INSERT INTO emp VALUES(7844,‘TURNER‘,‘SALESMAN‘,7698,‘1981-09-08‘,1500,0,30);
INSERT INTO emp VALUES(7876,‘ADAMS‘,‘CLERK‘,7788,‘1987-05-23‘,1100,NULL,20);
INSERT INTO emp VALUES(7900,‘JAMES‘,‘CLERK‘,7698,‘1981-12-03‘,950,NULL,30);
INSERT INTO emp VALUES(7902,‘FORD‘,‘ANALYST‘,7566,‘1981-12-03‘,3000,NULL,20);
INSERT INTO emp VALUES(7934,‘MILLER‘,‘CLERK‘,7782,‘1982-01-23‘,1300,NULL,10);





































































































































































以上是关于Mysql 入门小练习的主要内容,如果未能解决你的问题,请参考以下文章

Python(入门小练习2)

Python入门小练习

机器学习入门 机器学习简介 | 附加小练习

Java从入门到放弃 · ArrayList集合小练习

机器学习入门 KNN分类算法 | 附加小练习

MySQL 表记录查询小练习