SELECT MAX(AGE) AS 最大年龄, DEPT AS 所在班级
FROM STU
GROUP BY DEPT;
SELECT MAX(AGE) AS 最大年龄, DEPT AS 所在班级
FROM STU
WHERE GENTLE = '男'
GROUP BY DEPT;
SELECT NO AS 学号, DEPT AS 所在班级, AVG(AGE) AS 平均年龄, GROUPING(NO), GROUPING(DEPT)
FROM STU
GROUP BY CUBE(DEPT, NO);
--CUBE参数的作用除了返回GROUP BY子句指定列外,还返回按组统计的行,
--返回结果先按分组的第一个条件NO列排序显示,再按第二个条件DEPT列排序显示以此类推
--GROUPING用来判断某行数据是不是按照ROLLUP或CUBE进行汇总,返回0或1,
--1表示指定列由于汇总符产生NULL值,也表示汇总时忽略该列。
SELECT NO AS 学号, DEPT AS 所在班级, AVG(AGE) AS 平均年龄, GROUPING(NO), GROUPING(DEPT)
FROM STU
GROUP BY ROLLUP(DEPT, NO)
ORDER BY NO, DEPT;
SELECT MAX(AGE) AS 最大年龄,
DEPT AS 所在班级
FROM STU
GROUP BY DEPT HAVING MAX(AGE) >=25;
SELECT * FROM STU
ORDER BY GENTLE ASC, AGE DESC;--ASC升序,默认可省略;DESC降序
返回第一个查询结果的记录减去第二个查询结果的记录;查询列要一致
SELECT S.NO AS 学号, S.NAME AS 姓名, G.NAME AS 学科, G.SCORE AS 成绩
FROM STU S, GRADE G;
--查笛卡尔积 表行数相乘
SELECT STU.*, GRADE.*
FROM STU, GRADE
WHERE STU.NO =GRADE.NO;
--先取STU表中的第一个NO记录值与GRADE表中的所有的NO值一一比较,将相等的选出,
--再取STUdents第一个NO记录值与GRADE表中的所有的NO值一一比较,将相等的选出,以此类推,
--运算符两边至少要有一个相等的记录,否则查询记录为空。
SELECT STU.*, GRADE.*
FROM STU, GRADE
WHERE STU.NO =GRADE.NO
AND STU.NAME = '张清';
SELECT STU.*, GRADE.*
FROM STU, GRADE
WHERE STU.NO <> GRADE.NO;
--相当与无条件连接的查询结果集减去等值查询结果集
SELECT STU.*, GRADE.*
FROM STU INNER JOIN GRADE ON
STU.NO = GRADE.NO;
--INNER JOIN关键字是SQL标准写法,INNER可省略
--等值连接与非等值连接都属于内连接。指多个表的相关字段满足连接条件时,
--则从这两个表中提取数据并组合成新的记录。
SELECT S1.NO AS 学号, S1.NAME AS 姓名, S1.AGE AS 年龄
FROM STU S1 INNER JOIN STU S2
ON S1.AGE = S2.AGE
WHERE S1.NO <> S2.NO
ORDER BY S1.AGE DESC;
--查询STU表中年龄相同的结果集
SELECT STU.*, GRADE.*
FROM STU LEFT OUTER JOIN GRADE
ON STU.NO = GRADE.NO;
--OUTER可省略
--OR
SELECT STU.*, GRADE.*
FROM STU, GRADE
WHERE STU.NO = GRADE.NO(+);
--左外连接结果集包括LEFT JOIN子句中指定的左表(主表)的所有行,
--如果左表的某行在右表中没有匹配行,则在相关联的结果集中右表所有选择列均为空值
SELECT STU.*, GRADE.*
FROM STU RIGHT OUTER JOIN GRADE
ON STU.NO = GRADE.NO;
--OUTER可省略
--OR
SELECT STU.*, GRADE.*
FROM STU, GRADE
WHERE STU.NO(+) = GRADE.NO;
--右表(GRADE)为主表
SELECT STU.*, GRADE.*
FROM STU FULL OUTER JOIN GRADE
ON STU.NO = GRADE.NO;
--全外连接操作产生的结果集不仅包括符合连接条件的匹配行,
--而且包括两个连表中的所有记录,与左外连接和右外连接不同的是全外连接还返回左右表中不符合查询条件的数据行,以空值补全对应行的列值
SELECT STU.*, GRADE.*, COURSE.*
FROM STU INNER JOIN GRADE
ON STU.NO = GRADE.NO
INNER JOIN COURSE
ON GRADE.NAME = COURSE.NAME
WHERE STU.NAME = '张清';
--OR
SELECT STU.*, GRADE.*, COURSE.*
FROM STU, GRADE, COURSE
WHERE STU.NO = GRADE.NO
AND GRADE.NAME = COURSE.NAME
AND STU.NAME = '张清';
--太多表进行连接将极大降低性能,一般select语句连接的表最多为8~10个,否则就要考虑数据库设计可能不是最优
ALTER TABLE WEICK.STU
DROP PRIMARY KEY CASCADE;
DROP TABLE WEICK.STU CASCADE CONSTRAINTS;
CREATE TABLE WEICK.STU
(
NO VARCHAR2(10 BYTE),
NAME VARCHAR2(10 BYTE),
GENTLE VARCHAR2(2 BYTE),
AGE NUMBER(2),
DEPT VARCHAR2(20 BYTE)
)
TABLESPACE USERS
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
MONITORING;
CREATE INDEX WEICK.INDEX_NAME_RE ON WEICK.STU
(NAME)
LOGGING
TABLESPACE USERS
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
);
CREATE INDEX WEICK.INDEX_NO_DEPT ON WEICK.STU
(NO, DEPT)
LOGGING
TABLESPACE USERS
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
);
ALTER TABLE WEICK.STU ADD (
PRIMARY KEY
(NO)
USING INDEX WEICK.INDEX_NO_DEPT
ENABLE VALIDATE);
SET DEFINE OFF;
Insert into WEICK.STU
(NO, NAME, GENTLE, AGE, DEPT)
Values
('120006', '李飒', '男', 12, '12工商管理');
Insert into WEICK.STU
(NO, NAME, GENTLE, AGE, DEPT)
Values
('120005', '林琳', '女', 22, '12计算机');
Insert into WEICK.STU
(NO, NAME, GENTLE, AGE, DEPT)
Values
('120004', '杨过', '男', 22, '12计算机');
Insert into WEICK.STU
(NO, NAME, GENTLE, AGE, DEPT)
Values
('120003', '张清', '女', 21, '12外语');
Insert into WEICK.STU
(NO, NAME, GENTLE, AGE, DEPT)
Values
('120001', '陈诚', '男', 23, '12计算机');
Insert into WEICK.STU
(NO, NAME, GENTLE, AGE, DEPT)
Values
('120002', '李宗赫', '男', 25, '12图形');
COMMIT;
DROP TABLE WEICK.GRADE CASCADE CONSTRAINTS;
CREATE TABLE WEICK.GRADE
(
NO VARCHAR2(10 BYTE) NOT NULL,
NAME VARCHAR2(10 BYTE),
SCORE NUMBER
)
TABLESPACE WEICK_TBLSPACE
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
MONITORING;
SET DEFINE OFF;
Insert into WEICK.GRADE
(NO, NAME, SCORE)
Values
('120001', '计算机基础', 85);
Insert into WEICK.GRADE
(NO, NAME, SCORE)
Values
('120003', '计算机基础', 96);
Insert into WEICK.GRADE
(NO, NAME, SCORE)
Values
('120004', '计算机基础', 60);
COMMIT;
DROP TABLE WEICK.COURSE CASCADE CONSTRAINTS;
CREATE TABLE WEICK.COURSE
(
NAME VARCHAR2(20 BYTE),
SCORE NUMBER
)
TABLESPACE WEICK_TBLSPACE
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
MONITORING;
SET DEFINE OFF;
Insert into WEICK.COURSE
(NAME, SCORE)
Values
('计算机基础', 4);
Insert into WEICK.COURSE
(NAME, SCORE)
Values
('数据结构', 4);
Insert into WEICK.COURSE
(NAME, SCORE)
Values
('计算机英语', 2);
COMMIT;