CREATE TABLE dept(
deptno INT PRIMARY KEY, – 部门编号
dname VARCHAR(14),-- 部门名称
loc VARCHAR(13)-- 部门地点
);
CREATE TABLE emp(
empno INT NOT NULL PRIMARY KEY,-- 员工号
ename VARCHAR(10),-- 员工姓名
job VARCHAR(10),-- 职位
mgr INT, – 上级领导
hiredate DATETIME,-- 受雇日期
sal DOUBLE,-- 薪金
comm DOUBLE,-- 佣金
deptno INT,-- 所属部门
CONSTRAINT FOREIGN KEY(deptno) REFERENCES dept(deptno)
);
INSERT INTO dept VALUES
(10, ‘Accounting’, ‘New York’),
(20, ‘Research’, ‘Dallas’),
(30, ‘Sales’, ‘Chicago’),
(40, ‘Operations’, ‘Boston’),
(50, ‘Admin’, ‘Washing’);
INSERT INTO emp VALUES
(7369, ‘Smith’, ‘Clerk’,7902, ‘1980-12-17’,800,0,20),
(7499, ‘Allen’, ‘Salesman’,7698,‘1981-2-20’,1600,300,30),
(7844, ‘Turner’, ‘Salesman’,7499, ‘1981-9-8’,1500,0,30),
(7698, ‘Tom’, ‘Manager’,0, ‘1981-9-8’,6100,600,40),
(7876, ‘Adams’, ‘Clerk’,7900, ‘1987-5-23’,1100,0,20),
(7900, ‘James’, ‘Clerk’,7698, ‘1981-12-3’,2400,0,30),
(7902, ‘Ford’, ‘Analyst’,7698, ‘1981-12-3’,3000,NULL,20),
(7901, ‘Kik’, ‘Clerk’,7900, ‘1981-12-3’,1900,0,30);
– 1.列出至少有一个员工的所有部门。
SELECT DISTINCT dept.dname AS 部门名称
FROM dept JOIN emp
ON dept.deptno=emp.deptno;
– 2.列出薪金比“SMITH”多的所有员工。()
SELECT *
FROM emp
WHERE sal>(SELECT sal FROM emp WHERE ename=‘SMITH’);
– 3.列出所有员工的姓名及其直接上级的姓名。
SELECT yg.ename AS 员工名称,ld.ename AS 领导名称
FROM emp yg JOIN emp ld ON yg.mgr=ld.empno;
– 4.列出受雇日期早于其直接上级的所有员工。(同上,日期可直接拿来比较) – 5.列出所有工作为“CLERK”(办事员)的姓名及其部门名称。(域,注意()) – 6.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。 – 7.列出薪金高于公司平均薪金的所有员工。(反复查自己) – 8.列出与“Kik”从事相同工作的所有员工。(排除自己) – 9.列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。(any的用法,且排挤) – 10.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。(max的用法) – 11.列出在每个部门工作的员工数量、平均工资和平均服务期限。 – 学生信息表 – 老师信息表 – 课程表 – 成绩表 – 请编写SQL语句,完成如下任务: – 2.查询平均成绩大于60分的同学的学号和平均成绩 – 3.查询所有同学的学号、姓名、选课数、总成绩 – 4.查询姓“王”的老师的个数 – 5.查询没学过"张三"老师课的同学的学号、姓名 – 6.查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; – 7.查询学过“Jacky”老师所教的所有课的同学的学号、姓名 – 8.查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名 – 9.查询所有课程的成绩小于60分的同学的学号、姓名 – 10.列出sql成绩大于等于 所有学生sql平均成绩的学生 – 11.列出没有上过“就业课”的同学 – 12.列出“王五”老师 所教授的课程有哪些?(列出课程名字,和老)师名字 – 13.列出被学生得到100分的课程有哪些? – 14.列出所有学生每门课程的考试成绩
SELECT yg.ename AS 员工名称,yg.hiredate AS 员工受雇日期,ld.ename 领导名称,ld.hiredate AS 领导受雇日期
FROM emp yg JOIN emp ld ON yg.mgr=ld.empno
WHERE yg.hiredate
SELECT e.ename AS 员工姓名,e.job AS 职位,d.dname AS 部门名称
FROM emp e JOIN dept d ON e.deptno=d.deptno
WHERE e.job=‘CLERK’;
SELECT e.ename AS 员工名称,d.dname AS 部门
FROM emp e JOIN dept d ON e.deptno=d.deptno
WHERE e.deptno=(SELECT deptno FROM dept WHERE dname=‘SALES’);
SELECT * FROM emp
WHERE sal>(SELECT AVG(sal) FROM emp);
SELECT * FROM emp
WHERE job=(SELECT job FROM emp WHERE ename=‘Kik’) AND ename!=‘Kik’;
SELECT ename AS 员工名称,sal AS 薪金
FROM emp
WHERE sal=ANY(SELECT sal FROM emp WHERE deptno=30) AND deptno !=30;
SELECT ename AS 员工名称,sal AS 薪金
FROM emp
WHERE sal>(SELECT MAX(sal) FROM emp WHERE deptno=30) AND deptno !=30;
SELECT d.dname AS 部门,COUNT(e.empno) AS 员工数量,floor(AVG(e.sal)) AS 平均工资,ceil(AVG(TIMESTAMPDIFF(DAY,e.hiredate,NOW()))) AS 平均服务年限(天)
FROM emp e JOIN dept d ON e.deptno=d.deptno
GROUP BY d.dname;
– 12.列出所有员工的姓名、部门名称和工资.
SELECT e.ename AS 员工名称,d.dname AS 部门名称,(IFNULL(e.sal,0)+IFNULL(e.comm,0)) AS 工资
FROM emp e JOIN dept d ON e.deptno=d.deptno;练习二
CREATE TABLE students(
studentid INT AUTO_INCREMENT PRIMARY KEY,-- 学号
studentName VARCHAR(20) NOT NULL,-- 姓名
gender CHAR(20),-- 性别
birthday DATE
)ENGINE=INNODB;
INSERT INTO students(studentname,gender,birthday) VALUES
(‘张三’,‘男’,‘1987-02-01’),
(‘李斯’,‘女’,‘1990-12-01’),
(‘王二’,‘男’,‘1988-09-01’),
(‘黎明’,‘男’,‘1991-08-01’);
CREATE TABLE teachers(
teacherid INT AUTO_INCREMENT NOT NULL PRIMARY KEY, – 老师编号
teacherName VARCHAR(20) NOT NULL-- 老师姓名
)ENGINE=INNODB;
INSERT INTO teachers(teacherName)
VALUES(‘张三’),(‘李四’),(‘王五’),(‘Jacky’),(‘Helen’),(‘Kevin’);
CREATE TABLE courses(
Courseid INT AUTO_INCREMENT NOT NULL, – 课程号
courseName VARCHAR(20) NOT NULL UNIQUE, – 课程名
TeacherID INT NOT NULL , – 教师编号
FOREIGN KEY (teacherID) REFERENCES teachers(teacherId),
PRIMARY KEY (courseid,teacherid)
)ENGINE=INNODB;
INSERT INTO courses(coursename, teacherID) VALUES
(‘pre’,4),(‘c’,4),(‘sql’,5),(‘java oo’,3),(‘oracle’,5),
(‘ooad’,3),(‘Web前端’,5),(‘Java web’,4),(‘Java EE’,3),(‘Android’,2),(‘毕业项目’,5),
(‘就业课’,1);
CREATE TABLE scores(
studentid INT NOT NULL, – 学号
courseid INT NOT NULL, – 课程号
mark INT, – 成绩
PRIMARY KEY (studentid,courseid),
FOREIGN KEY (studentid) REFERENCES students(studentid),
FOREIGN KEY (courseid) REFERENCES Courses(CourseID)
)ENGINE=INNODB;
INSERT INTO scores(studentid,courseid,mark) VALUES
(1,1,60),(1,2,70),(1,3,80),(1,4,55),(1,5,100),(1,6,90),
(2,1,50),(2,2,70),(2,3,80),(2,4,55),(2,5,100),(2,6,90),(2,7,50),(2,8,70),(2,9,50),(2,10,55),(2,11,100),(2,12,30),
(3,1,90),(3,2,70),(3,3,80),(3,4,55),(3,5,100),(3,6,90),(3,7,90),(3,8,80),(3,9,90),(3,10,55),(3,11,100),(3,12,100),
(4,1,100),(4,2,70),(4,3,80),(4,4,55),(4,5,100),(4,6,90),(4,9,100),(4,10,90);
– 1.查询课程"001"比课程"002"成绩高的所有学生的学号
SELECT s1.studentid as 学号 FROM (SELECT * FROM scores WHERE courseid=001)
s1 JOIN (SELECT * FROM scores WHERE courseid=002)
s2 ON s1.studentid=s2.studentid WHERE s1.mark>s2.mark
SELECT studentid AS 学号,AVG(mark) AS 平均成绩
FROM scores
GROUP BY studentid
HAVING AVG(mark)>60;
SELECT xs.studentid AS 学号,xs.studentName AS 姓名,COUNT(cj.courseid) AS 选课数,SUM(cj.mark) AS 总成绩
FROM students xs JOIN scores cj ON xs.studentid=cj.studentid
GROUP BY xs.studentid;
SELECT COUNT(teacherid) AS 姓王的老师的个数
FROM teachers
WHERE teacherName LIKE ‘王%’;
SELECT DISTINCT studentid AS 学号,studentName AS 姓名
FROM students
WHERE studentid NOT IN(
SELECT studentid FROM scores WHERE courseid IN(
SELECT kc.Courseid FROM courses kc JOIN teachers ls ON ls.teacherid=kc.TeacherID
WHERE ls.teacherName=‘张三’));
SELECT studentid AS 学号,studentName AS 姓名, FROM students
WHERE studentid IN(
SELECT s001.studentid FROM
(SELECT studentid FROM scores WHERE courseid=001) s001 JOIN
(SELECT studentid FROM scores WHERE courseid =002) s002
ON s001.studentid=s002.studentid
);
SELECT stu.studentid AS 学号,stu.studentName AS 名称,zsstu.选了几门
FROM students stu JOIN
查询那些人选了Jacky的课选了几门计数
(SELECT studentid AS 学号,COUNT(courseid) AS 选了几门 FROM scores
WHERE courseid IN (-- 查询Jacky教了那些课输出编号表
SELECT Courseid FROM courses
WHERE TeacherID=(SELECT teacherid FROM teachers
WHERE teacherName=‘Jacky’)
)
GROUP BY studentid) zsstu ON stu.studentid=zsstu.学号
WHERE zsstu.选了几门=(-- 查询Jacky教了那些课输出编号表计数
SELECT COUNT(Courseid) FROM courses
WHERE TeacherID=(SELECT teacherid FROM teachers
WHERE teacherName=‘Jacky’)
)
SELECT DISTINCT xs.studentid AS 学号,xs.studentName AS 姓名
FROM students xs JOIN scores cj ON xs.studentid=cj.studentid
WHERE xs.studentid IN(
SELECT s1.studentid FROM (SELECT * FROM scores WHERE courseid=002) s1 JOIN (SELECT * FROM scores WHERE courseid=001) s2 ON s1.studentid=s2.studentid
WHERE s1.mark>s2.mark)
SELECT xs.studentid AS 学号,xs.studentName AS 姓名
FROM students xs JOIN scores cj ON xs.studentid=cj.studentid
WHERE cj.studentid NOT IN(SELECT DISTINCT studentid FROM scores WHERE mark>=60)
SELECT xs.studentName AS 姓名, cj.mark AS 成绩
FROM students xs JOIN scores cj ON xs.studentid=cj.studentid
WHERE cj.mark>=(SELECT AVG(mark) FROM scores WHERE courseid=(SELECT Courseid FROM courses WHERE courseName=‘sql’))
AND cj.courseid=(SELECT Courseid FROM courses WHERE courseName=‘sql’);
SELECT xs.studentName AS 姓名
FROM students xs JOIN scores cj ON xs.studentid=cj.studentid
WHERE xs.studentid NOT IN(SELECT studentid FROM scores WHERE courseid=(SELECT Courseid FROM courses WHERE courseName=‘就业课’))
GROUP BY xs.studentid;
SELECT kc.courseName AS 课程,ls.teacherName AS 老师姓名
FROM courses kc JOIN teachers ls ON kc.TeacherID=ls.teacherid
WHERE ls.teacherName=‘王五’;
SELECT DISTINCT kc.courseName AS 课程名,cj.mark AS 分数
FROM scores cj JOIN courses kc ON cj.courseid=kc.Courseid
WHERE cj.mark=100;
SELECT xs.studentName AS 姓名,kc.courseName AS 课程,cj.mark AS 分数
FROM students xs,scores cj,courses kc
WHERE xs.studentid=cj.studentid AND cj.courseid=kc.Courseid
ORDER BY xs.studentid;