(数据库七)MySQL练习

/*查询emp表中工资大于1000的员工姓名以及工资*/
SELECT ename,sal FROM emp WHERE sal>1000;

/*查询emp表中值为'MANAGER'的员工姓名和工资以及职位*/
SELECT ename,sal,job FROM emp WHERE job='MANAGER';

/*查询emp表中名字长度为5的员工姓名和工资以及职位*/
SELECT ename 姓名,sal 工资,job 职位 FROM emp WHERE LENGTH(ename)=5;
SELECT ename AS 姓名,sal AS 工资,job AS 职位 FROM emp WHERE LENGTH(ename)=5;/*AS可省略,同上一语句*/
SELECT ename '姓 名',sal '工 资',job '职 位' FROM emp WHERE LENGTH(ename)=5;/*如果列别名要加空格,需将列别名加单引号*/

/*查询emp表中有奖金的员工信息,不可以用comm!=NULL,任何字段与NULL比较皆不相等,包括NULL*/
SELECT * FROM emp WHERE comm IS NOT NULL;

/*查询emp表中有奖金并且工资大于1000的员工姓名以及工资和奖金*/
SELECT ename,sal,comm FROM emp WHERE comm IS NOT NULL AND sal>1000;

/*查询工资在1000-2000之间的员工信息,BETWEEN为闭区间*/
SELECT * FROM emp WHERE sal BETWEEN 1000 AND 2000;

/*查询工资在大于1000并且小于2000之间的员工信息*/
SELECT * FROM emp WHERE sal>1000 AND sal<2000;

/*查询10部门和20部门的员工信息*/
SELECT * FROM emp WHERE deptno=10 OR deptno=20;
SELECT * FROM emp WHERE deptno IN (10,20);/*意思同上*/

/*查询emp表中职位为'CLERK','MANAGER','ANALYST'的员工信息(两种方式)*/
SELECT * FROM emp WHERE job='CLERK' OR job='MANAGER' OR job='ANALYST';
SELECT * FROM emp WHERE job IN ('CLERK','MANAGER','ANALYST');

/*查询emp表中既不是10部门也不是20部门的员工*/
SELECT * FROM emp WHERE deptno!=10 AND deptno!=20;
SELECT * FROM emp WHERE deptno NOT IN (10,20);/*同上*/

/*查询emp表中值不为'CLERK','MANAGER','ANALYST'的员工信息*/
SELECT * FROM emp WHERE job NOT IN ('CLERK','MANAGER','ANALYST');

/*查询emp表中工资大于SMITH,ALLEN,WARD中最大工资的员工,ALL(1500,2000,2500)方法在MySql中无法执行,但ORACLE中可以,集合函数中该四方法不适用于直接加数字,仅能够将该值转换为子查询,但语法无错误>all 大于最大的 >any大于最小的 SELECT * FROM emp WHERE sal>ALL (SELECT sal FROM emp WHERE ename IN ('SMITH','ALLEN','WARD'));

/*查询emp表中名字带有'S'的*/
SELECT * FROM emp WHERE ename LIKE '%S%';

/*查询名字中第五个字符为'S'的员工*/
SELECT * FROM emp WHERE ename LIKE '____S%';

/*查询emp表中职位的第二个字符为'a'的员工*/
SELECT * FROM emp WHERE job LIKE '_A%';
SELECT * FROM emp WHERE job LIKE '_a%';
SELECT * FROM emp WHERE LOWER(job) LIKE '_a%';

/*查询emp表中部门号为20号的工资,工资降序排列*/
SELECT ename,sal FROM emp WHERE deptno=20 ORDER BY sal DESC;

/*查询emp表中部门号为20号的工资,工资升序排列*/
SELECT ename,sal FROM emp WHERE deptno=20 ORDER BY sal ASC;

/*查询emp表中部门号为20号的工资,工资降序排列,工资相同时,按照姓名升序排列,若省略排序关键字默认按升序排列,若是多个排序规则,会在第一个排序结果相同时按下一排序规则进行排序,DESC也可查询表结构*/
SELECT ename,sal FROM emp WHERE deptno=20 ORDER BY sal DESC,ename ASC;

/*若字段名为关键字,可使用``符号进行转义*/
SELECT `desc` FROM product;

/*查询emp表中的员工都来自于哪个部门*/
SELECT DISTINCT deptno FROM emp;
/*查询每个部门中都有哪些职位(联合去重)*/
SELECT DISTINCT deptno,job FROM emp;

/*统计emp表中有多少条记录,COUNT统计指定字段的记录数*/
SELECT COUNT(*) FROM emp;

/*最大值,最小值,平均值*/
SELECT MAX(sal),MIN(sal),AVG(sal) FROM emp;

/*查询20部门中的最高工资,最低、平均工资*/
SELECT MAX(sal),MIN(sal),AVG(sal) FROM emp WHERE deptno=20;

/*查询每个部门中的最高工资,最低工资,平均工资*/
SELECT MAX(sal),MIN(sal),AVG(sal) FROM emp GROUP BY deptno;

/*查询emp表中的总工资和总奖金,聚合函数会忽略NULL值*/
SELECT SUM(sal),SUM(IFNULL(comm,0)) FROM emp;

/*查询emp表中每个人的工资年薪和奖金年薪*/
SELECT sal*12,IFNULL(comm,100)*12 FROM emp;

/*查询不同部门、不同职位的最高工资、最低工资、平均工资*/
SELECT MAX(sal),MIN(sal),AVG(sal) FROM emp GROUP BY deptno,job;

/*查询平均工资大于2000的部门的最高工资和最低工资以及平均工资*/
SELECT MAX(sal),MIN(sal),AVG(sal),deptno FROM emp GROUP BY deptno HAVING AVG(sal)>2000;

/*查询平均工资大于1500的职位的最高工资以及最低工资并且按照职位降序排序*/
SELECT MAX(sal),MIN(sal),job FROM emp GROUP BY job HAVING AVG(sal)>1500 ORDER BY job DESC;

/*查询平均工资大于1500,平均奖金小于200的职位*/
SELECT job FROM emp GROUP BY job HAVING AVG(comm)>200 AND AVG(sal)>1000;

/*事务*/
CREATE TABLE a(
aid INT(10),
aname VARCHAR(10)
);
INSERT INTO a VALUES(1,'郑');
SELECT * FROM a;
/*开启事务*/
START TRANSACTION;
/*设置保留点*/
SAVEPOINT a;
/*向表中插入数据*/
INSERT INTO a VALUES(2,'任');
/*查询表数据*/
SELECT * FROM a;
/*回滚*/
ROLLBACK;
/*查询表数据*/
SELECT * FROM a;
/*向表中插入数据*/
INSERT INTO a VALUES(2,'任');
/*提交*/
COMMIT;
/*查询表中数据*/
SELECT * FROM a;

/*查询emp表中和dept表中所欲的数据*/
SELECT * FROM emp,dept;
/*查询emp表中每个员工都在哪个城市*/
SELECT * FROM emp,dept WHERE emp.deptno=dept.deptno;
SELECT * FROM emp e,dept d WHERE e.deptno=d.deptno;
SELECT e.deptno FROM emp e,dept d WHERE e.deptno=d.deptno;
SELECT d.deptno FROM emp e,dept d WHERE e.deptno=d.deptno;

/*查询emp表中每个员工的公司都在哪个城市(内连接)*/
SELECT * FROM emp e JOIN dept d ON e.deptno=d.deptno;

/*查询SMITH所在的部门的部门名称以及位置以及smith的年薪以及年奖金*/
SELECT ename,dname,loc,sal*12,comm FROM emp e JOIN dept d ON e.deptno=d.deptno WHERE ename='SMITH';
SELECT ename,dname,loc,sal*12,comm FROM emp e,dept d WHERE e.deptno=d.deptno AND ename='SMITH';

/*查询20和30部门的部门名称,部门地址及其员工的姓名和工资,AND优先级别大于OR,若OR其中一条件成立,则另一条件不再继续判断*/
SELECT dname,loc,ename,sal FROM emp e,dept d WHERE e.deptno=d.deptno AND e.deptno IN (20,30);
SELECT dname,loc,ename,sal FROM emp e,dept d WHERE e.deptno=d.deptno AND (e.deptno=20 OR e.deptno=30);
SELECT dname,loc,ename,sal FROM emp e JOIN dept d ON e.deptno=d.deptno WHERE e.deptno IN (20,30);
SELECT dname,loc,ename,sal FROM emp e JOIN dept d ON e.deptno=d.deptno WHERE e.deptno=20 OR e.deptno=30;

/*查询emp表中所有员工的部门信息以及员工信息和薪资*/
SELECT dname,loc,e.deptno,ename,sal FROM emp e LEFT JOIN dept d ON e.deptno=d.deptno;
SELECT dname,loc,e.deptno,ename,sal FROM dept d RIGHT JOIN emp e ON e.deptno=d.deptno;

/*查询所有部门的员工信息以及部门名称和位置*/
SELECT dname,loc FROM dept d LEFT JOIN emp e ON e.deptno=d.deptno;
SELECT dname,loc FROM emp e RIGHT JOIN dept d ON e.deptno=d.deptno;

/*查询员表中的所有员工以及部门表中所有部门(用到了全连接,mysql不支持全外连接)*/
SELECT e.*,d.* FROM emp e FULL JOIN dept d ON e.deptno=d.deptno;

/*查询员工表中的员工姓名和薪水以及其领导的姓名以及薪水*/
SELECT e.ename AS 员工姓名,e.sal AS 员工薪水,m.ename AS 领导姓名,m.sal AS 领导薪水 FROM emp e JOIN emp m ON e.mgr=m.empno ORDER BY e.ename ASC;

/*查询员工表中的员工姓名以及薪水以及其下属的员工姓名和薪水*/
SELECT e.ename AS 员工姓名,e.sal AS 员工薪水,s.ename AS 下属姓名,s.sal AS 下属薪水 FROM emp e JOIN emp s ON e.empno=s.mgr ORDER BY e.ename ASC;

/*查询emp表中哪些人有领导*/
SELECT ename FROM emp WHERE mgr IS NOT NULL;

/*查询emp表中谁是最高领导*/
SELECT ename FROM emp WHERE mgr IS NULL;

/*查询工资大于员工JONES工资的员工信息*/
SELECT * FROM emp WHERE sal>(SELECT sal FROM emp WHERE ename='JONES');

/*查询员工表中工资大于10号部门的平均工资的员工的姓名、职位、工资及其部门号*/
SELECT ename,job,sal,deptno FROM emp WHERE sal>(SELECT AVG(sal) FROM emp WHERE deptno=10);

/*查询工资等于每个部门的平均工资的人员信息*/
SELECT * FROM emp WHERE sal IN (SELECT AVG(sal) FROM emp GROUP BY deptno);

/*查询工资大于所有部门的平均工资的人员信息*/
SELECT * FROM emp WHERE sal>ALL(SELECT AVG(sal) FROM emp GROUP BY deptno);

/*查询工资大于任意部门的平均工资的人员信息*/
SELECT * FROM emp WHERE sal>ANY(SELECT AVG(sal) FROM emp GROUP BY deptno);

/*查询工资和奖金与员工SCOTT相同的其他员工信息*/
SELECT * FROM emp WHERE sal=(SELECT sal FROM emp WHERE ename='SCOTT') AND IFNULL(comm,0)=IFNULL((SELECT comm FROM emp WHERE ename='SCOTT'),0) AND ename<>'SCOTT';

/*查询每个员工的工资,姓名和其部门的平均工资*/
SELECT ename,sal,t.deptno,t.avg FROM emp,(SELECT deptno,AVG(sal) `avg` FROM emp GROUP BY deptno) t WHERE emp.deptno=t.deptno;

/*查询每个员工的工资,姓名和其部门的平均工资,以及最大工资和最小工资*/
SELECT ename,sal,avgsal,maxsal,minsal FROM emp,(SELECT deptno,AVG(sal) avgsal,MAX(sal) maxsal,MIN(sal) minsal FROM emp GROUP BY deptno) a WHERE emp.deptno=a.deptno;

/*查询工资比本部门平均工资高的员工的姓名和工资*/
SELECT ename,sal FROM emp JOIN (SELECT deptno,AVG(sal) avgsal FROM emp GROUP BY deptno) a ON emp.deptno=a.deptno WHERE sal>avgsal;

/*查询部门平均工资大于30部门的平均工资的部门平均工资及工资之和*/
SELECT deptno,AVG(sal),SUM(sal) FROM emp GROUP BY deptno HAVING AVG(sal)>(SELECT AVG(sal) FROM emp WHERE deptno=30);

/*查询每个员工的姓名,工资及其部门的平均工资,工资之和*/
SELECT ename,sal,emp.deptno,avgsal,sumsal FROM emp LEFT JOIN (SELECT deptno,AVG(sal) avgsal,SUM(sal) sumsal FROM emp GROUP BY deptno) a ON emp.deptno=a.deptno;
SELECT ename,sal,(SELECT AVG(sal) FROM emp e2 WHERE e1.deptno=e2.deptno) avgsal,(SELECT SUM(sal) FROM emp e3 WHERE e1.deptno=e3.deptno) sumsal FROM emp e1;/*select中的子查询必仅返回一列,且记录数与主查询记录数相同*/

/*查询WARD,SMITH,CLARK的同部门同事*/
SELECT ename,deptno FROM emp WHERE deptno IN(SELECT deptno FROM emp WHERE ename IN ('WARD','SMITH','CLARK')) ORDER BY deptno ASC;

/*创建student,字段sno,sname,sage,要求sno字段为主键*/
CREATE TABLE student(
    sno INT PRIMARY KEY,
    sname VARCHAR(20),
    sage INT
);

/*创建student,字段sno,sname,sage,要求sno字段为主键,sname不允许为空*/
CREATE TABLE student(
    sno INT PRIMARY KEY,
    sname VARCHAR(20) NOT NULL,
    sage INT
)

/*创建student,字段sno,sname,sage,要求sno字段为主键,sname不可重复*/
CREATE TABLE student(
    sno INT PRIMARY KEY,
    sname VARCHAR(20) UNIQUE,
    sage INT
);

/*创建student,字段sno,sname,sage,ssex,要求sno字段为主键,sname不可为空,sage的值不能重复,ssex必须为'f','m'*/
CREATE TABLE student(
    sno INT PRIMARY KEY,
    sname VARCHAR(20),
    sage INT UNIQUE,
    ssex ENUM('f','m')
);

/*创建student,字段sno,sname,sage,ssex,要求sno字段为主键,sname不可为空,sage的值不能重复,ssex必须为'f','m',注:mysql中check约束是无效的*/
CREATE TABLE student(
    sno INT PRIMARY KEY,
    sname VARCHAR(20),
    sage INT UNIQUE,
    ssex VARCHAR(20),
    CHECK(ssex IN('f','m'))
);

/*创建student,字段sno,sname,sage,ssex,要求sno字段为主键,sname不可为空,sage的值在18·30之间,ssex必须为'f','m'*/
CREATE TABLE student(
    sno INT PRIMARY KEY,
    sname VARCHAR(20) UNIQUE,
    sage ENUM('18','19','20','21','22','23','24','25','26','27','28','29','30'),
    ssex ENUM('f','m'),
);

/*创建表t,tno主键,tnum依赖于tno*/
CREATE TABLE t(
    tno INT PRIMARY KEY,
    tnum INT,
    FOREIGN KEY (tnum) REFERENCES t(tno)
);

/*创建student,字段sno,sname,sage,ssex,要求sno字段为主键并且自增*/
CREATE TABLE student(
    sno INT PRIMARY KEY AUTO_INCREMENT,
    sname VARCHAR(20),
    sage INT,
    ssex ENUM('f','m'),
);

/*查询员工表,查询第一页的数据,每页10条*/
SELECT * FROM emp LIMIT 0,10;
/*查询员工表,查询第二页的数据,每页5条*/
SELECT * FROM emp LIMIT 5,5;
/*按照部门号升序排序,查询第二页的数据,每页五条*/
SELECT * FROM emp ORDER BY deptno ASC LIMIT 5,5 ;
/*按照员工编号降序排序,查询第二页的数据,每页五条*/
SELECT * FROM emp ORDER BY empno DESC LIMIT 5,5;
/*使用子查询,先排序,再查询每页的数据,按照员工工资降序排序,查询第二页的数据,每页6条*/
SELECT * FROM (SELECT * FROM emp ORDER BY sal DESC) e LIMIT 6,6;
/*将员工表中的10部门的数据封装到一个视图view_emp_deptno_10*/
CREATE VIEW view_emp_deptno_10 AS SELECT * FROM emp WHERE deptno=10 WITH CHECK OPTION;

课后作业:

/*Student学生表(SNO学号,SNAME姓名,SEX性别,BIRTHDAY出生年月,DEP院系)
  Course课程表(CNO编号,CNAME课程名称,CVAL学分)
  SC选课表(SNO学号,CNO课程编号,SCORE成绩)
  查询选修了‘数据结构’的总人数
  查询‘张宇’同学选修了的课程和成绩
  查询挂科学分最多的同学姓名和院系*/

答案

CREATE TABLE Student(
    SNO VARCHAR(20) PRIMARY KEY,
    SNAME VARCHAR(10),
    SEX ENUM('f','m'),
    BIRTHDAY DATE,
    DEP VARCHAR(20)
);

INSERT INTO Student VALUES('1611411101','王五','m','1998-03-05','信息科学技术学院');
INSERT INTO Student VALUES('1611411102','赵六','f','1997-06-12','计算机技术与工程学院');
INSERT INTO Student VALUES('1611411103','李四','m','1999-11-07','电气与信息学院');
INSERT INTO Student VALUES('1611411104','钱七','f','1998-05-05','外国语学院');
INSERT INTO Student VALUES('1611411105','王五','m','1998-09-05','国际教育学院');
INSERT INTO Student VALUES('1611411106','张宇','m','1998-09-05','国际教育学院');

CREATE TABLE Course(
    CNO VARCHAR(20) PRIMARY KEY,
    CNAME VARCHAR(20),
    CVAL INT
);

INSERT INTO Course VALUES('001','数据结构',2);
INSERT INTO Course VALUES('002','计算机组成原理',2);
INSERT INTO Course VALUES('003','数据库',3);
INSERT INTO Course VALUES('004','java',2);
INSERT INTO Course VALUES('005','c++',4);

CREATE TABLE SC(
    SNO VARCHAR(20),
    CNO VARCHAR(20),
    SCORE INT,
    FOREIGN KEY (SNO) REFERENCES Student(SNO),
    FOREIGN KEY (CNO) REFERENCES Course(CNO)
);

INSERT INTO SC VALUES('1611411101','001',1);
INSERT INTO SC VALUES('1611411101','003',2);
INSERT INTO SC VALUES('1611411103','001',1);
INSERT INTO SC VALUES('1611411103','002',1);
INSERT INTO SC VALUES('1611411102','005',2);
INSERT INTO SC VALUES('1611411104','003',2);
INSERT INTO SC VALUES('1611411105','002',1);
INSERT INTO SC VALUES('1611411105','001',2);

SELECT COUNT(SNO) FROM SC WHERE CNO='001';

SELECT CNO,SCORE FROM SC WHERE SNO=(SELECT SNO FROM Student WHERE SNAME='张宇');

SELECT SNAME 姓名,DEP 院系 FROM Student WHERE SNO=(SELECT SNO FROM SC WHERE SCORE=(SELECT MIN(SCORE) FROM SC WHERE SCORE<4));

你可能感兴趣的:((数据库七)MySQL练习)