SQL学习总结
SQL学习总结:星期一考试了,从小到大,大大小小的考试经历不止百次了。可这次考试,心里还是有有点紧张。今晚精力旺盛,顺便理理头绪,复习复习。
我们学习SQL课程总共4天,重点是SQL的DML(Data Manipulation Language)语句。老师说以后我们用到更多的主要就是这些语句。现在总结如下:
一、查询语句(SELECT)
1、一般用法:
SELECT Column
FROM Table;
例1:
--从员工表里,选择姓名和工资
SELECT last_name, salary
FROM employees;
例2:
--从员工表里,选择姓名和邮件,并将两列数据合成一列,以"员工邮件"为列名显示
SELECT last_name + ' 的电子邮件是: ' + last_name + '@testage.com' AS "员工电子邮件详细信息"
FROM employees;
2、过滤重复行:
SELECT DISTINCT Column
FROM Table;
例:
--统计员工表里的工种分为几类
SELECT DISTINCT job_id
FROM employees;
3、增加选择条件:
SELECT Column
FROM Table
WHERE condition;
例1:
--从员工表里,选择工资高于5000的员工
SELECT last_name, salary
FROM employees
WHERE salary > 5000;
例2:
--从员工表里,选择工资高于5000,并且属于80号部门的员工
SELECT last_name, salary, department_id
FROM employees
WHERE salary > 5000 AND department_id = 80;
例3:
--从员工表里,选择工资高于8000,或者属于80号部门的员工
SELECT last_name, department_id, salary 高于8000或者属于80号部门的员工
FROM employees
WHERE salary > 8000 OR department_id = 80;
例4:
--从员工表里,选择年薪低于36000,并且没有奖金的员工信息
SELECT salary*12年薪,*
FROM employees
WHERE salary*12 < 3000*12 AND commission_pct IS NULL;
例5:
--从员工表里,选择工资在3000到5000之间(包含3000和5000)的员工信息
SELECT *
FROM employees
WHERE salary BETWEEN 3000 AND 5000;
例6:
--从员工表里,选择属于20号部门,30号部门,80号部门的员工
SELECT last_name, department_id
FROM employees
WHERE department_id IN(20, 30, 80);--另一种写法:WHERE department_id = 20 OR department_id = 30 OR department_id = 80;
例7:
--从员工表里,选择不属于20,30,80号部门的员工
SELECT last_name, department_id
FROM employees
WHERE department_id NOT IN(20, 30, 80);
4、模糊查询:
SELECT Column
FROM Table
WHERE keywords LIKE %_;
例1:
--从员工表里,选择姓名以B打头的所有员工信息
SELECT *
FROM employees
WHERE last_name LIKE 'B%';
例2:
--从员工表里,选择姓名第二个字母是a的所有员工信息
SELECT *
FROM employees
WHERE last_name LIKE '_a%';
例3:
--从员工表里,选择姓名以A或B或C打头的员工信息
SELECT *
FROM employees
WHERE last_name LIKE '%';
5、多表查询:
SELECT Table1.Column, Table2.Column
FROM Table1, Table2
WHERE Condition;
例1:
--查询员工表里,所有员工所属的部门名字
SELECT employees.last_name, department.department_name
FROM employees, department
WHERE employees.department_id = department.department_id;
例2:
--查询查询工资高于8000的员工所属的部门
SELECT e.last_name, e.salary, d.department_name
FROM employees e
INNER JOIN department d
ON e.salary > 8000 AND e.department_id = d.department_id;
例3:
--查询所有员工所属的部门,即使该员工没有被分配任何部门
SELECT e.last_name, d.department_name
FROM employees e
LEFT OUTER JOIN department d
ON e.department_id = d.department_id;
例4:
--查询每个部门所属的员工,即使该部门没有任何员工
SELECT d.department_name, e.last_name
FROM employees e
RIGHT OUTER JOIN department d
ON e.department_id = d.department_id;
例5:
--查询所有部门以及所有员工,即使该部门没有任何员工或该员工没被分配任何部门
SELECT e.last_name, d.department_name
FROM employees e
FULL OUTER JOIN department d
ON e.department_id = d.department_id;
例6:
--查询员工Baida的经理是谁
SELECT e.last_name 雇员, m.last_name 经理
FROM employees e, employees m
WHERE e.manager_id = m.employee_id
AND e.last_name = 'Baida'; 二、组函数(AVG()、SUM()……)、分组函数(GROUP BY)、排序(ORDER BY)
1、组函数:
AVG(Column)
例:
--统计全体员工的平均工资
SELECT AVG(salary) 平均工资
FROM employees;
SUM(Column)
例:
--统计工资支出总额
SELECT SUM(salary) 工资支出总额
FROM employees;
MAX(Column)
例:
--统计最高工资
SELECT MAX(salary) 最高工资
FROM employees;
MIN(Column)
例:
--统计最低工资
SELECT MIN(salary) 最低工资
FROM employees;
COUNT(Column)
例:
--统计员工总数
SELECT COUNT(employee_id) 员工总数
FROM employees;
2、分组函数:
GROUP BY Column
HAVING Condition
--例:统计员工平均工资高于10000的部门
SELECT AVG(salary), department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) > 10000;
3、排序:
ORDER BY
--例1:将员工工资按升序排列
SELECT last_name, salary
FROM employees
ORDER BY salary;
--例2:按员工入职时间长短降序排列
SELECT last_name, hire_date
FROM employees
ORDER BY hire_date DESC;
--例3:查找工资最高的前三名员工
SELECT TOP 3 salary, last_name
FROM employees
ORDER BY salary DESC;
三、子查询
SELECT Column
FROM Table
WHERE express operator (SELECT Col
FROM Tab
WHERE condition);
例1:
--查找工资与Jones工资相同的员工信息
SELECT *
FROM employees
WHERE salary = (SELECT salary
FROM employees
WHERE last_name = 'Jones');
例2:
--查找员工表里,工资最高的员工的所有信息
SELECT *
FROM employees
WHERE salary = (SELECT MAX(salary)
FROM employees );
例3:
--查找最高工资高于10000,并且最低工资低于4000的部门
SELECT DISTINCT department_name
FROM department
WHERE department_id IN (SELECT department_id
FROM employees
GROUP BY department_id
HAVING MAX(salary) > 10000 AND MIN(salary) < 4000);
例4:
--选出表中后10行
SELECT *
FROM employees
WHERE employee_id NOT IN (SELECT TOP (((SELECT COUNT(employee_id)
FROM employees)) - 10) employee_id
FROM employees);
四、增加(INSERT INTO Table VALUES=())、删除(DELETE FROM Table)、更改数据(UPDATE Table SET Column=key)
1、增加、插入数据:
INSERT INTO Table (Column1, Column2……)
VALUES (Element1, Element2……);
例1:
--向部门表里插入编号为101的人事部
INSERT INTO department
VALUES (101, '人事部', NULL, NULL);
例2:
--向员工表里插入编号为999,名字叫“测试时代”的新员工
INSERT INTO employees (employee_id, last_name)
VALUES (999, '测试时代');
2、删除数据:
DELETE FROM Table
WHERE Condition;
例:
--删除部门表里标号为101的部门
DELETE FROM department
WHERE department_id = 101;
3、更新数据:
UPDATE Table
SET Column = Value
WHERE Condition;
例:
--将部门号为80的部门,改为部门号为101,部门名为”人力资源部“
UPDATE department
SET department_id = 101,
department_name = '人力资源部'
WHERE department_id = 80; 不错,继续努力。。。。sdlkfj3 帮顶,这是最基本的SQL知识 这是最最基本的.
没有比这再常用的,嘿嘿 常用的,实用的!
UP一下sdlkfj3 帮顶一下,继续:) 有没有创建表的脚本或者表结构啊,例如有关一、查询语句(SELECT)部分的
我想练习练习,好久都没用sql语句了 create table abc as select * from xyz;
根据表xyz创建表abc,嘿嘿. 除了这些,还要学些什么? 最基本的SQL知识 计算机专业的比较熟悉的呵呵不知道sql测试的时候 会怎么测试的嘿嘿 好好学习下 虽然现在用不到但是以后还会是用到的 学习! 基本的SQL语句
create view bbc as select * from abc ; 不错,记得几年前为部门的新员工整理过这些 right 顶了
好贴!!! 很常用的 SQL 语句 相当的实用! 谢谢! 这些都是最基本的SQL知识 bucuo
页:
[1]
2