-- 案例1:查询部门编号是30-90之间的部门编号、员工姓名 -- 方式1: SELECT department_id,last_name FROM employees WHERE department_id BETWEEN30AND90;
-- 方式2: SELECT department_id,last_name FROM employees WHERE department_id>=30AND department_id<=90;
-- 案例2:查询年薪不是100000-200000之间的员工姓名、工资、年薪 -- 方式1: SELECT last_name,salary,salary*12*(1+IFNULL(commission_pct,0)) 年薪 FROM employees WHERE salary*12*(1+IFNULL(commission_pct,0))<100000OR salary*12*(1+IFNULL(commission_pct,0))>200000;
-- 方式2: SELECT last_name,salary,salary*12*(1+IFNULL(commission_pct,0)) 年薪 FROM employees WHERE salary*12*(1+IFNULL(commission_pct,0)) NOTBETWEEN100000AND200000;
-- 案例:查询员工表的姓名,要求格式:姓首字符大写,其他字符小写,名所有字符大写,且姓和名之间用_分割,最后起别名“OUTPUT” SELECTUPPER(SUBSTR(first_name,1,1)),first_name FROM employees; SELECTLOWER(SUBSTR(first_name,2)),first_name FROM employees; SELECTUPPER(last_name) FROM employees;
SELECT CONCAT(UPPER(SUBSTR(first_name,1,1)),LOWER(SUBSTR(first_name,2)),'_',UPPER(last_name)) "OUTPUT" FROM employees;
SELECT employee_id,last_name,salary,salary*1.2 "new salary" FROM employees;
-- 3. 将员工的姓名按首字母排序,并写出姓名的长度(length)
SELECT LENGTH(last_name) 长度 FROM employees ORDERBY SUBSTR(last_name,1,1) ASC;
-- 4. 做一个查询,产生下面的结果 /* <last_name> earns <salary> monthly but wants <salary*3> Dream Salary King earns 24000 monthly but wants 72000 */
SELECT CONCAT(last_name,' earns ',salary,' monthly but wants ',salary*3) "Dream Salary" FROM employees;
-- 5. 使用 case-when,按照下面的条件: /* job grade AD_PRES A ST_MAN B IT_PROG C SA_REP D ST_CLERK E 产生下面的结果 Last_name Job_id Grade king AD_PRES A */
SELECT last_name,job_id, CASE job_id WHEN'AD_PRES'THEN'A' WHEN'ST_MAN'THEN'B' WHEN'IT_PROG'THEN'C' WHEN'SA_REP'THEN'D' WHEN'ST_CLERK'THEN'E' END Grade FROM employees;
-- 案例1:查询女神名和对应的男神名 SELECT NAME,boyName FROM boys,beauty WHERE beauty.boyfriend_id= boys.id;
-- 案例2:查询员工名和对应的部门名 SELECT last_name,department_name FROM employees,departments WHERE employees.`department_id`=departments.`department_id`;
为表起别名: 注意:如果为表起了别名,则查询的字段就不能使用原来的表名去限定
提高语句的简洁度
区分多个重名的字段
1 2 3 4
-- 查询员工名、工种号、工种名 SELECT e.last_name,e.job_id,j.job_title FROM employees e,jobs j WHERE e.`job_id`=j.`job_id`;
两个表的顺序可以调换
1 2 3 4
-- 查询员工名、工种号、工种名 SELECT e.last_name,e.job_id,j.job_title FROM jobs j,employees e WHERE e.`job_id`=j.`job_id`;
可以加筛选
1 2 3 4 5 6 7 8 9 10 11
-- 案例:查询有奖金的员工名、部门名 SELECT last_name,department_name,commission_pct FROM employees e,departments d WHERE e.`department_id`=d.`department_id` AND e.`commission_pct` ISNOTNULL;
-- 案例2:查询城市名中第二个字符为o的部门名和城市名 SELECT department_name,city FROM departments d,locations l WHERE d.`location_id` = l.`location_id` AND city LIKE'_o%';
可以加分组
1 2 3 4 5 6 7 8 9 10 11 12
-- 案例1:查询每个城市的部门个数 SELECTCOUNT(*) 个数,city FROM departments d,locations l WHERE d.`location_id`=l.`location_id` GROUPBY city;
-- 案例2:查询有奖金的每个部门的部门名和部门的领导编号和该部门的最低工资 SELECT department_name,d.`manager_id`,MIN(salary) FROM departments d,employees e WHERE d.`department_id`=e.`department_id` AND commission_pct ISNOTNULL GROUPBY department_name,d.`manager_id`;
可以加排序
1 2 3 4 5 6
-- 案例:查询每个工种的工种名和员工的个数,并且按员工个数降序 SELECT job_title,COUNT(*) FROM employees e,jobs j WHERE e.`job_id`=j.`job_id` GROUPBY job_title ORDERBYCOUNT(*) DESC;
可以实现三表连接
1 2 3 4 5 6 7
-- 案例:查询员工名、部门名和所在的城市 SELECT last_name,department_name,city FROM employees e,departments d,locations l WHERE e.`department_id`=d.`department_id` AND d.`location_id`=l.`location_id` AND city LIKE's%' ORDERBY department_name DESC;
非等值连接
1 2 3 4 5
-- 案例1:查询员工的工资和工资级别 SELECT salary,grade_level FROM employees e,job_grades g WHERE salary BETWEEN g.`lowest_sal` AND g.`highest_sal` AND g.`grade_level`='A';
自连接
1 2 3 4
-- 案例:查询 员工名和上级的名称 SELECT e.employee_id,e.last_name,m.employee_id,m.last_name FROM employees e,employees m WHERE e.`manager_id`=m.`employee_id`;
SELECT 查询列表 FROM 表名1 别名 【INNER】 JOIN 表名2 别名 ON 连接条件 WHERE 筛选条件 GROUPBY 分组列表 HAVING 分组后筛选 ORDERBY 排序列表;
等值连接
简单连接
1 2 3 4 5
-- 案例:查询员工名和部门名 SELECT last_name,department_name FROM departments d JOIN employees e ON e.department_id =d.department_id;
添加筛选条件
1 2 3 4 5 6
-- 案例1:查询部门编号>100的部门名和所在的城市名 SELECT department_name,city FROM departments d JOIN locations l ON d.`location_id` = l.`location_id` WHERE d.`department_id`>100;
添加分组+筛选
1 2 3 4 5 6
-- 案例1:查询每个城市的部门个数 SELECTCOUNT(*) 部门个数,l.`city` FROM departments d JOIN locations l ON d.`location_id`=l.`location_id` GROUPBY l.`city`;
添加分组+筛选+排序
1 2 3 4 5 6 7 8
-- 案例1:查询部门中员工个数>10的部门名,并按员工个数降序 SELECTCOUNT(*) 员工个数,d.department_name FROM employees e JOIN departments d ON e.`department_id`=d.`department_id` GROUPBY d.`department_id` HAVING 员工个数>10 ORDERBY 员工个数 DESC;
非等值连接
1 2 3 4 5 6 7
-- 案例:查询部门编号在10-90之间的员工的工资级别,并按级别进行分组 SELECTCOUNT(*) 个数,grade FROM employees e JOIN sal_grade g ON e.`salary` BETWEEN g.`min_salary` AND g.`max_salary` WHERE e.`department_id` BETWEEN10AND90 GROUPBY g.grade;
自连接
1 2 3 4 5
-- 案例:查询员工名和对应的领导名 SELECT e.`last_name`,m.`last_name` FROM employees e JOIN employees m ON e.`manager_id`=m.`employee_id`;
-- 案例1:查询所有女神记录,以及对应的男神名,如果没有对应的男神,则显示为null -- 左连接 SELECT b.*,bo.* FROM beauty b LEFTJOIN boys bo ON b.`boyfriend_id` = bo.`id`;
-- 右连接 SELECT b.*,bo.* FROM boys bo RIGHTJOIN beauty b ON b.`boyfriend_id` = bo.`id`;
-- 案例2:查哪个女神没有男朋友 -- 左连接 SELECT b.`name` FROM beauty b LEFTJOIN boys bo ON b.`boyfriend_id` = bo.`id` WHERE bo.`id` ISNULL;
-- 右连接 SELECT b.*,bo.* FROM boys bo RIGHTJOIN beauty b ON b.`boyfriend_id` = bo.`id` WHERE bo.`id` ISNULL;
-- 案例3:查询哪个部门没有员工,并显示其部门编号和部门名 SELECTCOUNT(*) 部门个数 FROM departments d LEFTJOIN employees e ON d.`department_id` = e.`department_id` WHERE e.`employee_id` ISNULL;
-- 案例 /* 一、查询编号>3 的女神的男朋友信息,如果有则列出详细,如果没有,用 null 填充 4 小红 大飞 5 小白 大黄 6 小绿 NULL */ SELECT b.id,b.name,bo.* FROM beauty b LEFTJOIN boys bo ON b.boyfriend_id = bo.id WHERE b.id>3;
-- 二、查询哪个城市没有部门 SELECT l.city FROM departments d RIGHTJOIN locations l ON l.location_id = d.location_id WHERE d.`department_id` ISNULL;
-- 三、查询部门名为 SAL 或 IT 的员工信息 SELECT d.*,e.* FROM departments d LEFTJOIN employees e ON d.`department_id` = e.`department_id` WHERE d.`department_name` ='SAL'OR d.`department_name`='IT';
/*三、 已知学员信息表stuinfo stuId stuName gender majorId 已知专业表major id majorName 已知成绩表result id成绩编号 majorid stuid score */ -- 1、查询所有男生的姓名、专业名和成绩,使用SQL92和SQL99两种语法方式实现 -- SQL92 select stuname,majorname,score from stuinfo s,major m,result r where s.majorid = m.id and r.stuid = s.stuid and s.gender ='男';
-- SQL99 select stuname,majorname,score from stuinfo s join major m on s.majorid = m.id joinresult r on r.stuid = s.stuid where s.gender ='男';
-- 2、查询每个性别的每个专业的平均成绩,并按平均成绩降序 selectavg(score) 平均成绩,gendeer,s.majorid from stuinfo s joinresult r on s.stuid = r.stuid groupby gender,s.majorid orderby 平均成绩 desc;
-- 案例1:谁的工资比 Abel 高? -- (1)查询Abel的工资 SELECT salary FROM employees WHERE last_name ='Abel' -- (2)查询salary>(1)的员工信息 SELECT last_name,salary FROM employees WHERE salary>( SELECT salary FROM employees WHERE last_name <>'Abel' );
-- 案例2:返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id 和工资 -- (1)查询141号员工的job_id SELECT job_id FROM employees WHERE employee_id =141 -- (2)查询143号员工的salary SELECT salary FROM employees WHERE employee_id =143 -- (3)查询job_id=(1) and salary>(2)的信息 SELECT last_name,job_id,salary FROM employees WHERE job_id = ( SELECT job_id FROM employees WHERE employee_id =141 ) AND salary>( SELECT salary FROM employees WHERE employee_id =143 );
-- 1. 查询和 Zlotkey 相同部门的员工姓名和工资 -- (1)查询Zlotkey的部门编号 SELECT department_id FROM employees WHERE last_name ='Zlotkey' -- (2)查询department_id = (1)的员工姓名和工资 SELECT last_name,salary FROM employees WHERE department_id = ( SELECT department_id FROM employees WHERE last_name ='Zlotkey' );
-- 2. 查询工资比公司平均工资高的员工的员工号,姓名和工资。 -- (1)查询平均工资 SELECTAVG(salary) FROM employees -- (2)查询salary>(1)的信息 SELECT employee_id,last_name,salary FROM employees WHERE salary>( SELECTAVG(salary) FROM employees );
-- 案例1:返回location_id是1400或1700的部门中的所有员工姓名 -- (1)查询location_id是1400或1700的部门 SELECT department_id FROM departments WHERE location_id IN(1400,1700) -- (2)查询department_id = (1)的姓名 SELECT last_name FROM employees WHERE department_id IN( SELECTDISTINCT department_id FROM departments WHERE location_id IN(1400,1700) );
-- 题目:返回其它部门中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及salary -- (—1)查询job_id为‘IT_PROG’部门的工资 SELECTDISTINCT salary FROM employees WHERE job_id ='IT_PROG' -- (2)查询其他部门的工资<任意一个(1)的结果 SELECT employee_id,last_name,job_id,salary FROM employees WHERE salary<ANY( SELECTDISTINCT salary FROM employees WHERE job_id ='IT_PROG' ); -- 等价于 SELECT employee_id,last_name,job_id,salary FROM employees WHERE salary<( SELECTMAX(salary) FROM employees WHERE job_id ='IT_PROG' );
-- 案例3:返回其它部门中比job_id为‘IT_PROG’部门所有工资都低的员工 的员工号、姓名、job_id 以及salary -- (1)查询job_id为‘IT_PROG’部门的工资 SELECTDISTINCT salary FROM employees WHERE job_id ='IT_PROG' -- (2)查询其他部门的工资<所有①的结果 SELECT employee_id,last_name,job_id,salary FROM employees WHERE salary<ALL( SELECTDISTINCT salary FROM employees WHERE job_id ='IT_PROG' ); -- 等价于 SELECT employee_id,last_name,job_id,salary FROM employees WHERE salary<( SELECTMIN(salary) FROM employees WHERE job_id ='IT_PROG' );
放在select后面
1 2 3 4 5 6 7 8
-- 不知道什么场景用 -- 案例;查询部门编号是50的员工个数 SELECT ( SELECTCOUNT(*) FROM employees WHERE department_id =50 ) 个数;
放在from后面
1 2 3 4 5 6 7 8 9 10 11 12 13
-- 案例:查询每个部门的平均工资的工资级别 -- (1)查询每个部门的平均工资 SELECTAVG(salary),department_id FROM employees GROUPBY department_id -- (2)将(1)和sal_grade两表连接查询 SELECT dep_ag.department_id,dep_ag.ag,g.grade FROM sal_grade g JOIN ( SELECTAVG(salary) ag,department_id FROM employees GROUPBY department_id ) dep_ag ON dep_ag.ag BETWEEN g.min_salary AND g.max_salary;
-- 3. 查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资 -- (1)查询各部门的平均工资 SELECT department_id,AVG(salary) ag FROM employees GROUPBY department_id -- (2)将(1)结果和employees表连接查询 SELECT employee_id,last_name,salary FROM employees e JOIN ( SELECT department_id,AVG(salary) ag FROM employees GROUPBY department_id ) dep_ag ON e.department_id = dep_ag.department_id WHERE e.salary>dep_ag.ag;
-- 4. 查询和姓名中包含字母 u 的员工在相同部门的员工的员工号和姓名 -- (1)查询姓名中包含字母 u 的员工的部门编号 SELECTDISTINCT department_id FROM employees WHERE last_name LIKE'%u%' -- (2)查询部门号是①的员工号和姓名 SELECT employee_id,last_name FROM employees WHERE department_id IN( SELECTDISTINCT department_id FROM employees WHERE last_name LIKE'%u%' );
-- 6. 查询管理者是 King 的员工姓名和工资 -- (1)查询管理者是king的编号 SELECT employee_id FROM employees WHERE last_name ='k_ing' -- (2)查询哪个员工的领导编号是(1) SELECT last_name,salary FROM employees WHERE manager_id IN( SELECT employee_id FROM employees WHERE last_name ='k_ing' );
-- 3. 查询平均工资最低的部门信息和该部门的平均工资 -- (1)查询各部门的平均工资 SELECTAVG(salary) ag,department_id FROM employees GROUPBY department_id -- (2)查询哪个部门的平均工资最低 SELECTAVG(salary) ag,department_id FROM employees GROUPBY department_id ORDERBY ag LIMIT 1 -- (3)连接(2)和departments表 SELECT d.*,dep_ag.ag FROM departments d JOIN ( SELECTAVG(salary) ag,department_id FROM employees GROUPBY department_id ORDERBY ag LIMIT 1 ) dep_ag ON d.department_id = dep_ag.department_id
-- 7. 各个部门中 最高工资中最低的那个部门的 最低工资是多少 -- (1)查询各部门的最高工资 SELECTMAX(salary) mx,department_id FROM employees GROUPBY department_id -- (2)查询各部门的最高工资最低的那个部门 SELECT department_id FROM employees GROUPBY department_id ORDERBYMAX(salary) LIMIT 1 -- (3)查询部门编号是②的部门的最低工资 SELECTMIN(salary),department_id FROM employees WHERE department_id = ( SELECT department_id FROM employees GROUPBY department_id ORDERBYMAX(salary) LIMIT 1 );
DDL语言
说明:Data Define Language数据定义语言,用于对数据库和表的管理和操作
库的管理
创建数据库
1 2
CREATE DATABASE stuDB; CREATE DATABASE IF NOTEXISTS stuDB;
删除数据库
1 2
DROP DATABASE stuDB; DROP DATABASE IF EXISTS stuDB;
-- 1.使用分页查询实现,查询员工信息表中部门为50号的工资最低的5名员工信息 select* from employees where deparrment_id =50 orderby salary asc limit 0,5;
-- 2.使用子查询实现城市为Toroto的,且工资>10000的员工姓名 -- (1)查询城市为Toroto的部门编号 select department_id from departments d join locations l on d.location_id = l.location_id where city ='Toroto' -- (2)查询部门号在(1)里面的员工姓名 select last_name from employees where salary>10000and department_id in( select department_id from departments d join locations l on d.location_id = l.location_id where city ='Toroto' );