mysql-练习题

练习一:student,teacher,course,score

#http://adminv2.happymmall.com/
#--学生表 
use mysqldata;
drop table Student;
drop table course;
drop table teacher;
drop table score;
CREATE TABLE `Student`( 
	`s_id` VARCHAR(20), 
	`s_name` VARCHAR(20) NOT NULL DEFAULT '', 
	`s_birth` VARCHAR(20) NOT NULL DEFAULT '', 
	`s_sex` VARCHAR(10) NOT NULL DEFAULT '', 
	PRIMARY KEY(`s_id`) 
); 

#--课程表 
CREATE TABLE `Course`( 
	`c_id` VARCHAR(20), 
	`c_name` VARCHAR(20) NOT NULL DEFAULT '', 
	`t_id` VARCHAR(20) NOT NULL, 
	PRIMARY KEY(`c_id`) 
); 

#--教师表 
CREATE TABLE `Teacher`( 
	`t_id` VARCHAR(20), 
	`t_name` VARCHAR(20) NOT NULL DEFAULT '', 
	PRIMARY KEY(`t_id`) 
); 

#--成绩表 
CREATE TABLE `Score`( 
	`s_id` VARCHAR(20), 
	`c_id` VARCHAR(20), 
	`s_score` INT(3), 
	PRIMARY KEY(`s_id`,`c_id`) 
); 

#--插入学生表测试数据 
insert into Student values('01' , '赵雷' , '1990-01-01' , '男'); 
insert into Student values('02' , '钱电' , '1990-12-21' , '男'); 
insert into Student values('03' , '孙风' , '1990-05-20' , '男'); 
insert into Student values('04' , '李云' , '1990-08-06' , '男'); 
insert into Student values('05' , '周梅' , '1991-12-01' , '女'); 
insert into Student values('06' , '吴兰' , '1992-03-01' , '女'); 
insert into Student values('07' , '郑竹' , '1989-07-01' , '女'); 
insert into Student values('08' , '王菊' , '1990-01-20' , '女'); 

#--课程表测试数据 
insert into Course values('01' , '语文' , '02'); 
insert into Course values('02' , '数学' , '01'); 
insert into Course values('03' , '英语' , '03'); 

#--教师表测试数据 
insert into Teacher values('01' , '张三'); 
insert into Teacher values('02' , '李四'); 
insert into Teacher values('03' , '王五'); 

#--成绩表测试数据 
insert into Score values('01' , '01' , 80); 
insert into Score values('01' , '02' , 90); 
insert into Score values('01' , '03' , 99); 
insert into Score values('02' , '01' , 70); 
insert into Score values('02' , '02' , 60); 
insert into Score values('02' , '03' , 80); 
insert into Score values('03' , '01' , 80); 
insert into Score values('03' , '02' , 80); 
insert into Score values('03' , '03' , 80); 
insert into Score values('04' , '01' , 50); 
insert into Score values('04' , '02' , 30); 
insert into Score values('04' , '03' , 20); 
insert into Score values('05' , '01' , 76); 
insert into Score values('05' , '02' , 87); 
insert into Score values('06' , '01' , 31); 
insert into Score values('06' , '03' , 34); 
insert into Score values('07' , '02' , 89); 
insert into Score values('07' , '03' , 98);

select * from Student;

select * from Course;

select * from teacher;

select * from Score;

-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数 
#1-1 查询所有课程的学生成绩
select st.*,sc.s_score,sc.c_id
from student st
left join score sc on sc.s_id=st.s_id
#1-2查询01"课程学生的信息及课程分数 
select st.*,sc.s_score,sc.c_id
from student st
left join score sc on sc.s_id=st.s_id and sc.c_id='01' 

#1-3查询"01"课程比"02"课程成绩高的学生的信息及课程分数 
select st.*,sc.s_score as '语文' ,sc2.s_score '数学' 
from student st
left join score sc on sc.s_id=st.s_id and sc.c_id='01' 
left join score sc2 on sc2.s_id=st.s_id and sc2.c_id='02'  
where sc.s_score>sc2.s_score

select st.*,sc.s_score as '语文' ,sc2.s_score '数学' 
from student st,score sc,score sc2
where sc.s_score>sc2.s_score and sc.s_id=st.s_id and sc.c_id='01' and sc2.s_id=st.s_id and sc2.c_id='02'

-- 2、查询"李"姓老师的数量 
-- 2-1查看“李”姓老师
select * from teacher;
select t.t_name from teacher t
where t.t_name like "李%"; 
-- 2-2查询"李"姓老师的数量 
select t.t_name,count(t.t_id) from teacher t
group by t.t_id having t.t_name like "李%"; 

#3-1张三老师教的课
select c.* from course c left join teacher t on t.t_id=c.t_id where  t.t_name="张三"

#3-2有张三老师课成绩的st.s_id
select sc.s_id from score sc where sc.c_id in (select c.c_id from course c left join teacher t on t.t_id=c.t_id where  t.t_name="张三")

##3-3有张三老师课成绩的st.s_id 的学生姓名
SELECT * from student WHERE s_id in (select sc.s_id from score sc where sc.c_id in (select c.c_id from course c left join teacher t on t.t_id=c.t_id where  t.t_name="张三"))

练习二:DEPT,EMP,

DROP DATABASE IF EXISTS test1;
CREATE DATABASE test1;
USE test1;
##部门表
#DROP IF EXISTS TABLE DEPT;
CREATE TABLE DEPT(
DEPTNO int PRIMARY KEY,##部门编号
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');
##员工表
#DROP IF EXISTS TABLE EMP;
CREATE TABLE EMP(
EMPNO int PRIMARY KEY, #员工编号
ENAME VARCHAR(10), #员工姓名
JOB VARCHAR(9), #员工工作
MGR int, #员工直属领导编号
HIREDATE DATE, #入职时间
SAL double, #工资
COMM double, #奖金
DEPTNO int #对应dept表的外键
);
## 添加 部门 和 员工 之间的主外键关系
ALTER TABLE EMP ADD CONSTRAINT FOREIGN KEY EMP(DEPTNO) REFERENCES DEPT (DEPTNO);
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-07-03',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-07-13',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,'1981-01-23',1300,NULL,10);
#工资等级表
#DROP IF EXISTS TABLE SALGRADE;
CREATE TABLE SALGRADE(
GRADE int, #等级
LOSAL double, #最低工资
HISAL double ); #最高工资
INSERT INTO SALGRADE VALUES (1,700,1200);
INSERT INTO SALGRADE VALUES (2,1201,1400);
INSERT INTO SALGRADE VALUES (3,1401,2000);
INSERT INTO SALGRADE VALUES (4,2001,3000);
INSERT INTO SALGRADE VALUES (5,3001,9999);

#1、查找部门30中员工的详细信息。
select * from emp where deptno = 30;
#2、找出从事clerk工作的员工的编号、姓名、部门号。
select empno,ename,deptno from emp where job = 'clerk';
#3、检索出奖金多于基本工资的员工信息。
select * from emp where comm > sal;
#4、检索出奖金多于基本工资60%的员工信息。
select * from emp where comm > sal * 0.6;
#5、找出10部门的经理、20部门的职员 的员工信息。
select * from emp where deptno = 10 and job='MANAGER' or deptno = 20 and job = 'CLERK';
#6、找出10部门的经理、20部门的职员 或者既不是经理也不是职员但是工资高于2000元的员工信息。
select * from emp
where deptno = 10 and job='MANAGER'
or deptno = 20 and job = 'CLERK'
or job!='MANAGER' and job != 'CLERK' and sal > 2000 ;
## job not in ('MANAGER','CLERK')
#7、找出获得奖金的员工的工作。
select * from emp where comm > 0;
#8、找出奖金少于100或者没有获得奖金的员工的信息。
select * from emp where comm < 100 or comm is null;
#9、找出姓名以A、B、S开始的员工信息。
select * from emp where ename like 'A%' or ename like 'B%' or ename like 'S%';
#10、找到名字长度为6个字符的员工信息。
select * from emp where length(ename) = 6;
#select * from emp where ename like '______';
#11、名字中不包含R字符的员工信息。
select * from emp where ename not like '%R%';
#12、返回员工的详细信息并按姓名排序。
select * from emp order by ename asc;
#13、返回员工的信息并按工作降序工资升序排列。
select * from emp order by job desc , sal asc;

#14、找出姓名中包含A的员工信息。
select * from emp where ename like '%A%';

#1、返回拥有员工的部门名、部门号。
select distinct d.dname, d.deptno from dept d,emp e where d.deptno = e.deptno;
#2、工资水平多于smith的员工信息。
select *from emp where sal > (select sal from emp where ename = 'smith');

#3、返回员工姓名及其所在的部门名称。
select e.ename,d.dname from emp e , dept d where e.deptno = d.deptno;
#4、返回从事clerk工作的员工姓名和所在部门名称。
select e.ename,d.dname
from emp e , dept d
where e.deptno = d.deptno and e.job = 'CLERK';
#5、返回部门号及其本部门的最低工资。
select deptno ,min(sal) sal
from emp
group by deptno
#6、返回销售部(sales)所有员工的姓名。
select e.ename from emp e,dept d
where e.deptno = d.deptno and d.dname = 'sales';
select ename from emp where deptno=(select deptno from dept where dname='sales');


你可能感兴趣的:(mysql-练习题)