数据库是Oracle,SQL也都是Oracle数据库的语法格式
-- 学生信息
create table HAND_STUDENT
(
STUDENT_NO VARCHAR2(10) not null,
STUDENT_NAME VARCHAR2(20),
STUDENT_BIRTHDAY DATE,
STUDENT_GENDER VARCHAR2(5)
);
comment on table HAND_STUDENT is '学生信息表';
comment on column HAND_STUDENT.STUDENT_NO is '学号';
comment on column HAND_STUDENT.STUDENT_NAME is '姓名';
comment on column HAND_STUDENT.STUDENT_BIRTHDAY is '年龄';
comment on column HAND_STUDENT.STUDENT_GENDER is '性别';
alter table HAND_STUDENT
add constraint PK_STUDENT primary key (STUDENT_NO);
-- 教师信息表
create table HAND_TEACHER
(
TEACHER_NO VARCHAR2(10) not null,
TEACHER_NAME VARCHAR2(20),
MANAGER_NO VARCHAR2(10)
);
comment on table HAND_TEACHER is '教师信息表';
comment on column HAND_TEACHER.TEACHER_NO is '教师编号';
comment on column HAND_TEACHER.TEACHER_NAME is '教师名称';
comment on column HAND_TEACHER.MANAGER_NO is '上级编号';
alter table HAND_TEACHER
add constraint PK_TEACHER primary key (TEACHER_NO);
-- 课程信息表
create table HAND_COURSE
(
COURSE_NO VARCHAR2(10) not null,
COURSE_NAME VARCHAR2(20),
TEACHER_NO VARCHAR2(20) not null
);
comment on table HAND_COURSE is '课程信息表';
comment on column HAND_COURSE.COURSE_NO is '课程号';
comment on column HAND_COURSE.COURSE_NAME is '课程名称';
comment on column HAND_COURSE.TEACHER_NO is '教师编号';
alter table HAND_COURSE
add constraint PK_COURSE primary key (COURSE_NO, TEACHER_NO);
-- 成绩信息表
create table HAND_STUDENT_CORE
(
STUDENT_NO VARCHAR2(10) not null,
COURSE_NO VARCHAR2(10) not null,
CORE NUMBER(4, 2)
);
comment on table HAND_STUDENT_CORE is '学生成绩表';
comment on column HAND_STUDENT_CORE.STUDENT_NO is '学号';
comment on column HAND_STUDENT_CORE.COURSE_NO is '课程号';
comment on column HAND_STUDENT_CORE.CORE is '分数';
alter table HAND_STUDENT_CORE
add constraint PK_SC primary key (STUDENT_NO, COURSE_NO);
/*******初始化学生表的数据******/
insert into HAND_STUDENT
values ('01', '赵雷', TO_DATE('1990-01-01', 'yyyy-mm-dd'), '男');
insert into HAND_STUDENT
values ('02', '钱电', TO_DATE('1990-12-21', 'yyyy-mm-dd'), '男');
insert into HAND_STUDENT
values ('03', '孙风', TO_DATE('1990-05-20', 'yyyy-mm-dd'), '男');
insert into HAND_STUDENT
values ('04', '李云', TO_DATE('1990-08-06', 'yyyy-mm-dd'), '男');
insert into HAND_STUDENT
values ('05', '周梅', TO_DATE('1991-12-01', 'yyyy-mm-dd'), '女');
insert into HAND_STUDENT
values ('06', '吴兰', TO_DATE('1992-03-01', 'yyyy-mm-dd'), '女');
insert into HAND_STUDENT
values ('07', '郑竹', TO_DATE('1989-07-01', 'yyyy-mm-dd'), '女');
insert into HAND_STUDENT
values ('08', '王菊', TO_DATE('1990-01-20', 'yyyy-mm-dd'), '女');
commit;
/******************初始化教师表***********************/
insert into HAND_TEACHER
values ('01', '张三', '');
insert into HAND_TEACHER
values ('02', '李四', '01');
insert into HAND_TEACHER
values ('03', '王五', '02');
commit;
/***************初始化课程表****************************/
insert into HAND_COURSE
values ('01', '语文', '02');
insert into HAND_COURSE
values ('02', '数学', '01');
insert into HAND_COURSE
values ('03', '英语', '03');
commit;
/***************初始化成绩表***********************/
insert into HAND_STUDENT_CORE
values ('01', '01', 80);
insert into HAND_STUDENT_CORE
values ('01', '02', 90);
insert into HAND_STUDENT_CORE
values ('01', '03', 99);
insert into HAND_STUDENT_CORE
values ('02', '01', 70);
insert into HAND_STUDENT_CORE
values ('02', '02', 60);
insert into HAND_STUDENT_CORE
values ('02', '03', 80);
insert into HAND_STUDENT_CORE
values ('03', '01', 80);
insert into HAND_STUDENT_CORE
values ('03', '02', 80);
insert into HAND_STUDENT_CORE
values ('03', '03', 80);
insert into HAND_STUDENT_CORE
values ('04', '01', 50);
insert into HAND_STUDENT_CORE
values ('04', '02', 30);
insert into HAND_STUDENT_CORE
values ('04', '03', 20);
insert into HAND_STUDENT_CORE
values ('05', '01', 76);
insert into HAND_STUDENT_CORE
values ('05', '02', 87);
insert into HAND_STUDENT_CORE
values ('06', '01', 31);
insert into HAND_STUDENT_CORE
values ('06', '03', 34);
insert into HAND_STUDENT_CORE
values ('07', '02', 89);
insert into HAND_STUDENT_CORE
values ('07', '03', 98);
commit;
张三没有上级领导,说明他就是最大的领导
08编号的学生没有成绩
FROM
ON
JOIN
WHERE
GROUP BY
HAVING
SELECT
DISTINCT
ORDER BY
LIMIT
了解以上执行顺序也会为得到两个有用的启发:
WHERE age>AVG(age)
--1.外连接
SELECT hs.STUDENT_NAME,
hsc1.CORE,
hsc2.CORE
FROM HAND_STUDENT hs,
HAND_STUDENT_CORE hsc1,
HAND_STUDENT_CORE hsc2
WHERE hsc1.COURSE_NO = '01'
AND hsc2.COURSE_NO = '02'
AND hsc1.CORE > hsc2.CORE
AND hsc1.STUDENT_NO = hsc2.STUDENT_NO
AND hsc1.STUDENT_NO = hs.STUDENT_NO
--2.行列转换
SELECT hs.*, t.hsc1, t.hsc2
FROM (SELECT hsc.STUDENT_NO,
MAX(CASE WHEN hsc.COURSE_NO = '01' THEN hsc.CORE END) hsc1,
MAX(CASE WHEN hsc.COURSE_NO = '02' THEN hsc.CORE END) hsc2
FROM HAND_STUDENT_CORE hsc
GROUP BY hsc.STUDENT_NO) t,
HAND_STUDENT hs
WHERE t.hsc1 > t.hsc2
AND t.STUDENT_NO = hs.STUDENT_NO
--1.外连接
SELECT hs.STUDENT_NO, hs.STUDENT_NAME, ROUND(AVG(hsc.CORE), 2) avg_score
FROM HAND_STUDENT hs LEFT JOIN
HAND_STUDENT_CORE hsc ON hsc.STUDENT_NO = hs.STUDENT_NO
GROUP BY hs.STUDENT_NO, hs.STUDENT_NAME
HAVING AVG(hsc.CORE) >= 60
ORDER BY avg_score ASC
--2.子查询
SELECT hsc.STUDENT_NO,
(SELECT hs.STUDENT_NAME FROM HAND_STUDENT hs WHERE hs.STUDENT_NO = hsc.STUDENT_NO) name,
ROUND(AVG(hsc.CORE), 2) avg_score
FROM HAND_STUDENT_CORE hsc
GROUP BY hsc.STUDENT_NO
HAVING AVG(hsc.CORE) >= 60
ORDER BY avg_score ASC
这里外层必须是
hsc.STUDENT_NO
而不是hs.STUDENT_NO
替代。
SELECT hs.STUDENT_NO, hs.STUDENT_NAME, ROUND(AVG(NVL(hsc.CORE, 0)), 2) avg_score
FROM HAND_STUDENT hs
LEFT JOIN
HAND_STUDENT_CORE hsc ON hs.STUDENT_NO = hsc.STUDENT_NO
GROUP BY hs.STUDENT_NO, hs.STUDENT_NAME
HAVING AVG(NVL(hsc.CORE, 0)) < 60
ORDER BY avg_score ASC
SELECT hs.STUDENT_NO, hs.STUDENT_NAME, COUNT(hsc.COURSE_NO) total_courses, SUM(NVL(hsc.CORE, 0)) total_scores
FROM HAND_STUDENT hs
LEFT JOIN HAND_STUDENT_CORE hsc ON hs.STUDENT_NO = hsc.STUDENT_NO
GROUP BY hs.STUDENT_NO, hs.STUDENT_NAME
ORDER BY total_scores ASC
SELECT COUNT(ht.TEACHER_NO)
FROM HAND_TEACHER ht
WHERE ht.TEACHER_NAME LIKE '李%';
SELECT hs.*
FROM HAND_STUDENT hs,
HAND_COURSE hc,
HAND_TEACHER ht,
HAND_STUDENT_CORE hsc
WHERE ht.TEACHER_NAME = '张三'
AND ht.TEACHER_NO = hc.TEACHER_NO
AND hc.COURSE_NO = hsc.COURSE_NO
AND hsc.STUDENT_NO = hs.STUDENT_NO
ORDER BY hs.STUDENT_NO ASC
--1.NOT EXISTS
SELECT hs.*
FROM HAND_STUDENT hs
WHERE NOT EXISTS(SELECT 1
FROM (SELECT hsc.STUDENT_NO
FROM HAND_TEACHER ht,
HAND_COURSE hc,
HAND_STUDENT_CORE hsc
WHERE ht.TEACHER_NAME = '张三'
AND ht.TEACHER_NO = hc.TEACHER_NO
AND hsc.COURSE_NO = hc.COURSE_NO) t
WHERE t.STUDENT_NO = hs.STUDENT_NO)
--2.NOT IN
SELECT ht.*
FROM HAND_STUDENT ht
WHERE ht.STUDENT_NO NOT IN (SELECT hsc.STUDENT_NO
FROM HAND_COURSE hc,
HAND_TEACHER ht,
HAND_STUDENT_CORE hsc
WHERE ht.TEACHER_NAME = '张三'
AND ht.TEACHER_NO = hc.TEACHER_NO
AND hc.COURSE_NO = hsc.COURSE_NO)
ORDER BY ht.STUDENT_NO ASC;
这里并不能简单的
ht.TEACHER_NAME='张三'
--1.自连接
SELECT hs.*
FROM HAND_STUDENT hs,
HAND_STUDENT_CORE hsc1,
HAND_STUDENT_CORE hsc2
WHERE (hsc1.COURSE_NO = '01' AND hsc2.COURSE_NO = '02')
AND hsc1.STUDENT_NO = hs.STUDENT_NO
AND hsc2.STUDENT_NO = hs.STUDENT_NO
--1.行列转换
SELECT hs.*, t.hsc1, t.hsc2
FROM (SELECT hsc.STUDENT_NO,
MAX(CASE WHEN hsc.COURSE_NO = '01' THEN hsc.CORE END) hsc1,
MAX(CASE WHEN hsc.COURSE_NO = '02' THEN hsc.CORE END) hsc2
FROM HAND_STUDENT_CORE hsc
GROUP BY hsc.STUDENT_NO) t
LEFT JOIN
HAND_STUDENT hs ON t.STUDENT_NO = hs.STUDENT_NO
WHERE t.hsc1 IS NOT NULL
AND t.hsc2 IS NULL
这里不能简单地
hsc2.COURSE_NO != '02'
SELECT hs.STUDENT_NO, hs.STUDENT_NAME, hs.STUDENT_BIRTHDAY, hs.STUDENT_GENDER, COUNT(hsc.COURSE_NO) total_courses
FROM HAND_STUDENT hs
LEFT JOIN HAND_STUDENT_CORE hsc ON hs.STUDENT_NO = hsc.STUDENT_NO
GROUP BY hs.STUDENT_NO, hs.STUDENT_NO, hs.STUDENT_NAME, hs.STUDENT_BIRTHDAY, hs.STUDENT_GENDER
HAVING COUNT(hsc.COURSE_NO) < (SELECT COUNT(hc.COURSE_NO) FROM HAND_COURSE hc)
ORDER BY hs.STUDENT_NO
SELECT hs.STUDENT_NO, hs.STUDENT_NAME, hs.STUDENT_BIRTHDAY, hs.STUDENT_GENDER
FROM HAND_STUDENT hs
LEFT JOIN HAND_STUDENT_CORE hsc ON hs.STUDENT_NO = hsc.STUDENT_NO
WHERE hsc.COURSE_NO IN (SELECT hsc.COURSE_NO
FROM HAND_STUDENT_CORE hsc
WHERE hsc.STUDENT_NO = '01')
GROUP BY hs.STUDENT_NO, hs.STUDENT_NAME, hs.STUDENT_BIRTHDAY, hs.STUDENT_GENDER
ORDER BY hs.STUDENT_NO
SELECT hs.STUDENT_NO, hs.STUDENT_NAME, ROUND(AVG(hsc.CORE), 2) avg_core
FROM HAND_STUDENT hs
LEFT JOIN
HAND_STUDENT_CORE hsc ON hs.STUDENT_NO = hsc.STUDENT_NO
GROUP BY hs.STUDENT_NO, hs.STUDENT_NAME
HAVING SUM(CASE WHEN hsc.CORE < 60 THEN 1 ELSE 0 END) >= 2
ORDER BY avg_core
SELECT hs.*, hsc.core
FROM HAND_STUDENT hs
LEFT JOIN HAND_STUDENT_CORE hsc ON hs.STUDENT_NO = hsc.STUDENT_NO
WHERE hsc.COURSE_NO = '01'
AND hsc.CORE < 60
ORDER BY hsc.CORE DESC
--1.子查询
SELECT t1.*, t2.avg_core
FROM (SELECT * FROM HAND_STUDENT_CORE) t1,
(SELECT hsc.STUDENT_NO, ROUND(AVG(hsc.CORE), 2) avg_core FROM HAND_STUDENT_CORE hsc GROUP BY hsc.STUDENT_NO) t2
WHERE t1.STUDENT_NO = t2.STUDENT_NO
ORDER BY t2.avg_core DESC
--开窗函数
SELECT hsc.*, ROUND(AVG(hsc.CORE) OVER (PARTITION BY hsc.STUDENT_NO), 2) avg_core
FROM HAND_STUDENT_CORE hsc
ORDER BY avg_core DESC
及格为[60, +∞]
中等为[70, 80)
优良为[80, 90)
优秀为[90, +∞]
SELECT hc.COURSE_NO,
hc.COURSE_NAME,
MAX(hsc.CORE),
MIN(hsc.CORE),
ROUND(AVG(hsc.CORE), 2),
(ROUND(sum(CASE WHEN hsc.CORE >= 60 THEN 1 ELSE 0 END)/COUNT(1), 2)*100) || '%' "及格率",
(ROUND(sum(CASE WHEN hsc.CORE >= 70 AND hsc.CORE < 80 THEN 1 ELSE 0 END)/COUNT(1), 3)*100) || '%' "中等率",
(ROUND(sum(CASE WHEN hsc.CORE >= 80 AND hsc.CORE < 90 THEN 1 ELSE 0 END)/COUNT(1), 3)*100) || '%' "优良率",
(ROUND(sum(CASE WHEN hsc.CORE >= 90 THEN 1 ELSE 0 END)/COUNT(1), 3)*100) || '%' "优秀率"
FROM HAND_COURSE hc
LEFT JOIN
HAND_STUDENT_CORE hsc ON hc.COURSE_NO = hsc.COURSE_NO,
(SELECT COUNT(hc.COURSE_NO) FROM HAND_COURSE hc) total_course
GROUP BY hc.COURSE_NO, hc.COURSE_NAME
--1.开窗函数
SELECT hsc.COURSE_NO, hsc.CORE, RANK() OVER (PARTITION BY hsc.COURSE_NO ORDER BY hsc.CORE DESC) rank
FROM HAND_STUDENT_CORE hsc
--2.子查询
SELECT hsc.COURSE_NO,
hsc.CORE,
(SELECT COUNT(hsc2.CORE)
FROM HAND_STUDENT_CORE hsc2
WHERE hsc.COURSE_NO = hsc2.COURSE_NO AND hsc.CORE < hsc2.CORE) + 1
FROM HAND_STUDENT_CORE hsc
ORDER BY hsc.COURSE_NO, hsc.CORE DESC
mysql 8.0以后的版本才加入
rank()
函数
DENSE_RANK():排序会出现重复,序号连续
ROW_NUMBER():排序不会出现重复,序号连续
SELECT t.*, RANK() OVER (ORDER BY t.sum_core DESC) rank
FROM (SELECT hsc.STUDENT_NO, SUM(hsc.CORE) sum_core
FROM HAND_STUDENT_CORE hsc
GROUP BY hsc.STUDENT_NO) t
SELECT ht.TEACHER_NAME, hc.COURSE_NAME, ROUND(AVG(hsc.CORE), 2) avg_core
FROM HAND_TEACHER ht
LEFT OUTER JOIN
HAND_COURSE hc ON ht.TEACHER_NO = hc.TEACHER_NO
LEFT JOIN
HAND_STUDENT_CORE hsc ON hc.COURSE_NO = hsc.COURSE_NO
GROUP BY ht.TEACHER_NAME, hc.COURSE_NAME
ORDER BY avg_core DESC
SELECT hs.*, t.COURSE_NO, t.CORE
FROM HAND_STUDENT hs
LEFT JOIN
(SELECT hsc.*,
RANK() OVER (ORDER BY hsc.CORE DESC) rank
FROM HAND_STUDENT_CORE hsc) t ON hs.STUDENT_NO = t.STUDENT_NO
JOIN HAND_COURSE hc ON hc.COURSE_NO = t.COURSE_NO
WHERE rank IN (2, 3)
--统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
SELECT COUNT(hsc.STUDENT_NO),
hc.COURSE_NO,
hc.COURSE_NAME,
(ROUND((SUM((CASE WHEN hsc.CORE > 85 AND hsc.CORE <= 100 THEN 1 ELSE 0 END))) / COUNT(1), 3) *
100) || '%' "[100, 85]",
(ROUND((SUM((CASE WHEN hsc.CORE > 70 AND hsc.CORE <= 85 THEN 1 ELSE 0 END))) / COUNT(1), 3) *
100) || '%' "[85, 70]",
(ROUND((SUM((CASE WHEN hsc.CORE >= 60 AND hsc.CORE <= 70 THEN 1 ELSE 0 END))) / COUNT(1), 3) *
100) || '%' "[60, 70]",
(ROUND((SUM((CASE WHEN hsc.CORE > 0 AND hsc.CORE < 60 THEN 1 ELSE 0 END))) / COUNT(1), 3) * 100) ||
'%' "[60, 0]"
FROM HAND_STUDENT_CORE hsc
LEFT JOIN
HAND_COURSE hc ON hsc.COURSE_NO = hc.COURSE_NO
GROUP BY hc.COURSE_NO, hc.COURSE_NAME
`
SELECT hs.*, t.avg_score, RANK() OVER (ORDER BY t.avg_score DESC) rank
FROM HAND_STUDENT hs
LEFT JOIN
(SELECT hsc.STUDENT_NO,
ROUND(AVG(hsc.CORE), 2) avg_score
FROM HAND_STUDENT_CORE hsc
GROUP BY hsc.STUDENT_NO) t
ON t.STUDENT_NO = hs.STUDENT_NO
SELECT t.*
FROM (SELECT hc.COURSE_NAME, hsc.CORE, RANK() OVER (PARTITION BY hc.COURSE_NO ORDER BY hsc.CORE DESC) rank
FROM HAND_STUDENT_CORE hsc
LEFT JOIN
HAND_COURSE hc ON hsc.COURSE_NO = hc.COURSE_NO) t
WHERE t.rank <= 3
SELECT hc.COURSE_NAME, COUNT(1) number_of_elective_courses
FROM HAND_COURSE hc
LEFT JOIN
HAND_STUDENT_CORE hsc ON hc.COURSE_NO = hsc.COURSE_NO
GROUP BY hc.COURSE_NAME
SELECT hs.STUDENT_NO, hs.STUDENT_NAME, COUNT(1) number_of_elective_courses
FROM HAND_STUDENT hs
LEFT JOIN
HAND_STUDENT_CORE hsc ON hs.STUDENT_NO = hsc.STUDENT_NO
GROUP BY hs.STUDENT_NO, hs.STUDENT_NAME
HAVING COUNT(1) = 2
--1.分组
SELECT hs.STUDENT_GENDER, COUNT(1)
FROM HAND_STUDENT hs
GROUP BY hs.STUDENT_GENDER
--2.SUM累加
SELECT SUM((CASE WHEN hs.STUDENT_GENDER = '男' THEN 1 ELSE 0 END)) "male",
SUM((CASE WHEN hs.STUDENT_GENDER = '女' THEN 1 ELSE 0 END)) "female"
FROM HAND_STUDENT hs;
SELECT hs.STUDENT_NAME, COUNT(1)
FROM HAND_STUDENT hs
WHERE hs.STUDENT_NAME LIKE '%风%'
GROUP BY hs.STUDENT_NAME
--1.分组
SELECT hs.STUDENT_NAME, hs.STUDENT_GENDER, COUNT(1)
FROM HAND_STUDENT hs
GROUP BY hs.STUDENT_NAME, hs.STUDENT_GENDER
HAVING COUNT(1) > 1
--2.自连接
SELECT hs1.STUDENT_NO, hs1.STUDENT_NAME, COUNT(1)
FROM HAND_STUDENT hs1,
HAND_STUDENT hs2
WHERE hs1.STUDENT_NAME = hs2.STUDENT_NAME
AND hs1.STUDENT_GENDER = hs2.STUDENT_GENDER
GROUP BY hs1.STUDENT_NO, hs1.STUDENT_NAME
HAVING COUNT(1) > 1
ORDER BY hs1.STUDENT_NO
SELECT hs.*
FROM HAND_STUDENT hs
WHERE TO_CHAR(hs.STUDENT_BIRTHDAY, 'yyyy') = '1990'
ORDER BY hs.STUDENT_NO
SELECT hsc.COURSE_NO, ROUND(AVG(hsc.CORE), 2) avg_core
FROM HAND_STUDENT_CORE hsc
GROUP BY hsc.COURSE_NO
ORDER BY avg_core DESC, hsc.COURSE_NO
SELECT hs.STUDENT_NO, hs.STUDENT_NAME, ROUND(AVG(hsc.CORE), 2) avg_core
FROM HAND_STUDENT hs
LEFT JOIN
HAND_STUDENT_CORE hsc ON hs.STUDENT_NO = hsc.STUDENT_NO
GROUP BY hs.STUDENT_NO, hs.STUDENT_NAME
HAVING ROUND(AVG(NVL(hsc.CORE, 2)), 2) > 85
ORDER BY avg_core
SELECT hs.STUDENT_NAME, hsc.CORE
FROM HAND_STUDENT hs
LEFT JOIN HAND_STUDENT_CORE hsc ON hs.STUDENT_NO = hsc.STUDENT_NO
LEFT JOIN HAND_COURSE hc ON hsc.COURSE_NO = hc.COURSE_NO
WHERE hc.COURSE_NAME = '数学'
AND hsc.CORE < 60
SELECT hs.STUDENT_NAME, hc.COURSE_NAME, hsc.CORE
FROM HAND_STUDENT hs
LEFT JOIN HAND_STUDENT_CORE hsc ON hs.STUDENT_NO = hsc.STUDENT_NO
LEFT JOIN HAND_COURSE hc ON hsc.COURSE_NO = hc.COURSE_NO
SELECT hs.STUDENT_NAME, hc.COURSE_NAME, hsc.CORE
FROM HAND_STUDENT hs
LEFT JOIN
HAND_STUDENT_CORE hsc ON hs.STUDENT_NO = hsc.STUDENT_NO
LEFT JOIN HAND_COURSE hc ON hsc.COURSE_NO = hc.COURSE_NO
WHERE hs.STUDENT_NO IN (SELECT hsc.STUDENT_NO
FROM HAND_STUDENT_CORE hsc
GROUP BY hsc.STUDENT_NO
HAVING MIN(hsc.CORE) > 70)
--1.子查询
SELECT hc.COURSE_NAME
FROM HAND_COURSE hc
WHERE hc.COURSE_NO IN
(SELECT hsc.COURSE_NO FROM HAND_STUDENT_CORE hsc GROUP BY hsc.COURSE_NO HAVING MIN(hsc.CORE) < 60)
--2.外连接
SELECT hc.COURSE_NO, hc.COURSE_NAME
FROM HAND_COURSE hc
LEFT JOIN HAND_STUDENT_CORE hsc ON hc.COURSE_NO = hsc.COURSE_NO
WHERE hsc.CORE < 60
SELECT hs.STUDENT_NO, hs.STUDENT_NAME
FROM HAND_STUDENT hs
LEFT JOIN
HAND_STUDENT_CORE hsc ON hs.STUDENT_NO = hsc.STUDENT_NO
WHERE hsc.COURSE_NO = '01'
AND hsc.CORE >= 80
SELECT hc.COURSE_NO, hc.COURSE_NAME, COUNT(1) "number_of_elective_course"
FROM HAND_COURSE hc
LEFT JOIN HAND_STUDENT_CORE hsc ON hc.COURSE_NO = hsc.COURSE_NO
GROUP BY hc.COURSE_NO, hc.COURSE_NAME
SELECT *
FROM (SELECT t.*,
RANK() OVER (ORDER BY t.max_core DESC) rank
FROM (SELECT hs.STUDENT_NO, hs.STUDENT_NAME, MAX(hsc.CORE) max_core
FROM HAND_STUDENT hs
LEFT JOIN HAND_STUDENT_CORE hsc ON hs.STUDENT_NO = hsc.STUDENT_NO
LEFT JOIN HAND_COURSE hc ON hsc.COURSE_NO = hc.COURSE_NO
LEFT JOIN HAND_TEACHER ht ON hc.TEACHER_NO = ht.TEACHER_NO
WHERE ht.TEACHER_NAME = '张三'
GROUP BY hs.STUDENT_NO, hs.STUDENT_NAME) t)
WHERE rank = 1
SELECT DISTINCT hsc1.*, hsc2.COURSE_NO, hsc2.CORE
FROM HAND_STUDENT_CORE hsc1
INNER JOIN HAND_STUDENT_CORE hsc2
ON (hsc1.COURSE_NO != hsc2.COURSE_NO
AND hsc1.CORE = hsc2.CORE)
--1.RANK()函数
SELECT *
FROM (SELECT hsc.COURSE_NO,
hsc.CORE,
RANK() OVER (PARTITION BY hsc.COURSE_NO ORDER BY hsc.CORE DESC) rank
FROM HAND_STUDENT_CORE hsc) t
WHERE t.rank <= 2
--2.子查询【炫技的写法】
SELECT hsc.COURSE_NO, hsc.CORE
FROM HAND_STUDENT_CORE hsc
WHERE (SELECT COUNT(1) FROM HAND_STUDENT_CORE hsc2 WHERE hsc.COURSE_NO = hsc2.COURSE_NO AND hsc.CORE < hsc2.CORE) + 1 <=
2
SELECT hsc.COURSE_NO, COUNT(1) "number_of_elective_course"
FROM HAND_STUDENT_CORE hsc
GROUP BY hsc.COURSE_NO
HAVING COUNT(1) >= 5
ORDER BY "number_of_elective_course" DESC
SELECT hsc.STUDENT_NO, COUNT(1) "number_of_elective_course"
FROM HAND_STUDENT_CORE hsc
GROUP BY hsc.STUDENT_NO
HAVING COUNT(1) >= 2
SELECT *
FROM HAND_STUDENT hs
WHERE hs.STUDENT_NO IN (SELECT hsc.STUDENT_NO
FROM HAND_STUDENT_CORE hsc
GROUP BY hsc.STUDENT_NO
HAVING COUNT(1) = (SELECT COUNT(1) FROM HAND_COURSE hc))
--1.Oracle
SELECT hs.*, ROUND(MONTHS_BETWEEN(SYSDATE, hs.STUDENT_BIRTHDAY) / 12, 1) "age"
FROM HAND_STUDENT hs
--2.MySQL
SELECT hs.*, (YEAR(NOW()) - YEAR()) "age"
FROM HAND_STUDENT hs
SELECT hs.*
FROM HAND_STUDENT hs
WHERE TO_CHAR(hs.STUDENT_BIRTHDAY, 'WW') = TO_CHAR(SYSDATE, 'WW')
SELECT hs.*
FROM HAND_STUDENT hs
WHERE TO_CHAR(hs.STUDENT_BIRTHDAY, 'WW') = TO_CHAR(SYSDATE, 'WW') + 1;
SELECT hs.*
FROM HAND_STUDENT hs
WHERE EXTRACT(MONTH FROM hs.STUDENT_BIRTHDAY) = EXTRACT(MONTH FROM SYSDATE)
SELECT hs.*
FROM HAND_STUDENT hs
WHERE EXTRACT(MONTH FROM hs.STUDENT_BIRTHDAY) = EXTRACT(MONTH FROM (ADD_MONTHS(SYSDATE, 1)))