SELECT语句简介:完整的SELECT语句由六个子句构成,分别是
SELECT子句、FROM子句、WHERE子句、GROUP BY子句、HAVING子句和ORDER BY子句。
六个子句的功能分别为:
SELECT子句,指定要获取表中哪些列数据。
FROM子句:指定数据来自哪些表。
WHERE子句:指定获取哪些行数据
GROUP BY子句:用于对表中数据进行分组统计。
HAVING子句:在对表中数据进行分组统计时,指定分组统计条件。
ORDER BY子句:指定使用哪来对结果进行排序。
其中SELECT子句和FROM子句为必选项,其余为可选项。
一、简单查询
1.查询指定列
--(1)指定部分列
SELECT name, title, wage, hire_date FROM teachers;
SELECT name, title, wage, TO_CHAR(hire_date, 'YYYY-MM-DD') FROM teachers; --使用其它的日期显示格式
--(2)指定全部列
SELECT * FROM students;
SELECT student_id, name, sex, specialty, dob FROM students; --列的顺序可以改变
--(3)重复记录行的处理(DISTINCT关键字,一定要放在最前)
SELECT specialty FROM students;
SELECT DISTINCT specialty FROM students;
2.改变输出
--(1)使用列别名
SELECT name AS "姓名", dob AS "生日" FROM students;
--(2)使用字符串连接
SELECT name || '生日是:' || dob AS "学生生日清单" FROM students;
3.使用算术表达式
SELECT name AS "姓名", bonus+wage AS "月总收入" FROM teachers;
4、空值处理
空值的概念:空值NULL没有数据类型,在Oracle数据库中表示未知或未确定的什,
无论是数据类型、文本类型或日期类型,均可以用统一的空值NULL表示。
为了解决NULL参加运算时出现的问题,ORACLE提供了三个函数可以对此进行处理。
--(1)函数NVL():NVL(expr1, expr2),当expr1为NULL时,函数返回expr2的值。
SELECT name AS "姓名", NVL(bonus,0)+wage AS "月总收入" FROM teachers;
--(2)函数NVL2():NVL(expr1, expr2, expr3),当expr1为NULL,返回expr3,当expr1不为NULL,返回expr2。
SELECT name AS "姓名", NVL2(bonus,bonus+wage,wage) AS "月总收入" FROM teachers;
--(3)COALESCE():COALESCE(expr1 [,expr2]...),返回参数列表中的第一个非空值,或全为NULL,则返回NULL。
SELECT name AS "姓名", COALESCE(bonus+wage, wage) AS "月总收入" FROM teachers;
二、条件查询
WHERE子句的SELECT语句格式:
SELECT <*/expression1 [AS alias1], ...> FROM table [WHERE condition(s)];
WHERE子句中使用的比较条件
算术比较条件:=,>,>=,<,<=,<>,!=。
包含测试:IN,NOT IN,范围测试,BETWEEN AND, NOT BETWEEN AND
匹配测试:LIKE, NOT LIKE。
NULL测试:IS NULL, NOT IS NULL。
逻辑运算符:AND, OR, NOT
1.单一条件查询
--(1)使用算术运算比较符
SELECT name, hire_date, title, wage
FROM teachers WHERE wage >= 2000;
SELECT student_id, name, specialty, dob
FROM students WHERE specialty = '计算机';
SELECT student_id, name, specialty, dob
FROM students WHERE dob < '1-1月-1990';
--(2)使用包含测试(IN)
SELECT name, hire_date, title, bonus
FROM teachers WHERE bonus IN(500,600);
SELECT student_id, name, specialty, dob
FROM students WHERE dob IN ('08-10月-1990','26-12月-1989');
--(3)使用范围测试(BETWEEN...AND)
SELECT name, hire_date, title, bonus
FROM teachers WHERE bonus BETWEEN 500 AND 600;
--(4)使用匹配测试(LIKE,通配符:%表示0个或多个字符,_表示单个字符)
SELECT student_id, name, specialty, dob
FROM students WHERE name LIKE '王%';
--(5)使用空值测试(IS NULL)
SELECT name, hire_date, title, bonus
FROM teachers WHERE bonus IS NULL;
SELECT name, hire_date, title, bonus
FROM teachers WHERE title IS NULL;
SELECT student_id, name, specialty, dob
FROM students WHERE dob IS NULL;
2.复合条件查询
--(1)分别使用逻辑运算符AND、OR、NOT(NOT操作符主要与BETWEEN...AND、LIKE、IN以及IS NULL结合使用)
SELECT student_id, name, sex, specialty
FROM students WHERE specialty = '计算机' AND sex = '男';
SELECT student_id, name, sex, specialty
FROM students WHERE specialty = '计算机' OR sex = '男';
SELECT student_id, name, sex, specialty
FROM students WHERE NOT specialty = '计算机';
SELECT student_id, name, specialty, dob
FROM students WHERE name NOT IN('欧阳春岚','高山');
SELECT student_id, name, specialty, dob
FROM students WHERE dob BETWEEN '1-1月-1989' AND '1-1月-1990';
SELECT student_id, name, specialty, dob
FROM students WHERE name NOT LIKE '张%';
--(2)组合使用逻辑条件
在逻辑运算符AND、OR、NOT中,NOT优先级最高,AND其次,OR最低。并且它们的优先级低于任何一种比较操作符。
SELECT student_id, name, sex, specialty
FROM students
WHERE specialty = '计算机' AND sex = '女'
OR specialty= '机电工程' AND sex = '男'; --如果要改变优先级,则需要使用括号
SELECT name, hire_date, title, bonus, wage FROM teachers
WHERE NOT title = '工程师'
AND hire_date < '1-1月-2002'
AND wage < 3000;
三、记录排序
若不指定顺序,则会按插入的先后顺序来显示数据行。
使用含有ORDER BY子句的SELECT语句格式:
SELECT <*/expression1 [AS alias1],...>
FROM table [WHERE condition(s)]
[ORDER BY expression1 [ASC | DESC,...]; --ASC为升序(默认),DESC为降序排序。
1.按单一列排序
--(1)升序排序
SELECT name, hire_date, title, bonus, wage
FROM teachers
ORDER BY wage ASC;
SELECT name, hire_date, title, bonus, wage
FROM teachers
ORDER BY wage; --升序是默认的。
--(2)降序排序
SELECT student_id, name, specialty, dob
FROM studentS
ORDER BY 2 DESC; --可以用列名、表达式别名,也可以用列序号
--(3)按多列排序
SELECT student_id, name, specialty, dob
FROM students
ORDER BY specialty, name;
SELECT student_id, name, specialty, dob
FROM students ORDER BY specialty, name DESC;
四、分组查询
含有GROUP BY子句和HAVING子句的SELECT语句
SELECT <*/expression1 [AS alias1],...> FROM table
[WHERE condition(s)]
[GROUP BY expression1, ...]
[HAVING condition(s)];
表中的数据使用列(Aggregate)函数进行统计,列函数会检查列中的所有数据。这一操作涉及原表的每一行,
总是形成一个单行的统计结果。
--(1)列(Aggregate)函数:
用于字符、数值、日期型数据的列函数:MAX(column)、MIN(column)。
用于字符、数据、日期型数据的列函数:COUNT(*)、COUNT(column)、COUNT(DISTINCT column)
只用于数值型数据的列函数:SUM(column)、AVG(column)、STDDEV(column)列的标准偏差、VARIANCE(column)列的方差。
SELECT AVG(wage) FROM teachers;
SELECT COUNT(*) FROM students;
SELECT MAX(dob), MIN(dob) FROM students;
SELECT SUM(wage) FROM teachers;
SELECT STDDEV(wage) FROM teachers;
--(2)GROUP BY子句
通过使用GROUP BY子句,可以在表中达到将数据分组的目的。
SELECT department_id FROM teachers GROUP BY department_id;
SELECT department_id, title FROM teachers GROUP BY department_id, title;
SELECT department_id, MAX(wage), MIN(wage) FROM teachers GROUP BY department_id;
SELECT department_id, SUM(wage), AVG(wage) FROM teachers GROUP BY department_id;
SELECT department_id, COUNT(*) FROM teachers GROUP BY department_id;
SELECT department_id, AVG(wage) FROM teachers WHERE wage > 1000 GROUP BY department_id;
--(3)HAVING子句
GROUP BY子句用于指定分组的依据,而HAVING子句则指定条件,用于限制分组显示结果。
SELECT department_id, AVG(wage) FROM teachers
GROUP BY department_id HAVING AVG(wage) > 2200;
SELECT department_id, AVG(wage) FROM teachers
WHERE wage < 3000
GROUP BY department_id
HAVING AVG(wage) > 2000;
SELECT department_id, AVG(wage) FROM teachers
WHERE wage < 3000
GROUP BY department_id
HAVING AVG(wage) >= 2000
ORDER BY 2;