感謝 https://www.cnblogs.com/DreamDrive/p/6193530.htmlhtml
建立表:mysql
1 DROP TABLE DEPT; 2 --部門表 3 CREATE TABLE DEPT( 4 DEPTNO int PRIMARY KEY, 5 DNAME VARCHAR(14) , --部門名稱 6 LOC VARCHAR(13) ---部門地址 7 ) ; 8 9 CREATE TABLE DEPT( 10 DEPTNO int PRIMARY KEY, 11 DNAME VARCHAR(14) , 12 LOC VARCHAR(13) 13 ) ; 14 15 16 INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK'); 17 INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS'); 18 INSERT INTO DEPT VALUES (30,'SALES','CHICAGO'); 19 INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON'); 20 21 22 23 DROP TABLE EMP; 24 --員工表 25 CREATE TABLE EMP( 26 EMPNO int PRIMARY KEY, --員工編號 27 ENAME VARCHAR(10), ---員工姓名 28 JOB VARCHAR(9), --員工工做 29 MGR int, ----員工直屬領導編號 30 HIREDATE DATE, ----入職時間 31 SAL double, ---工資 32 COMM double, --獎金 33 DEPTNO int REFERENCES DEPT); --關聯dept表 34 35 36 CREATE TABLE EMP( 37 EMPNO int PRIMARY KEY, 38 ENAME VARCHAR(10), 39 JOB VARCHAR(9), 40 MGR int, 41 HIREDATE DATE, 42 SAL double, 43 COMM double, 44 DEPTNO int , 45 foreign key EMP(DEPTNO) REFERENCES DEPT(DEPTNO) 46 ); 47 48 49 INSERT INTO EMP VALUES(7369,'SMITH','CLERK',7902,"1980-12-17",800,NULL,20); 50 INSERT INTO EMP VALUES(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30); 51 INSERT INTO EMP VALUES(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30); 52 INSERT INTO EMP VALUES(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20); 53 INSERT INTO EMP VALUES(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30); 54 INSERT INTO EMP VALUES(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30); 55 INSERT INTO EMP VALUES(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10); 56 INSERT INTO EMP VALUES(7788,'SCOTT','ANALYST',7566,'1987-07-03',3000,NULL,20); 57 INSERT INTO EMP VALUES(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10); 58 INSERT INTO EMP VALUES(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30); 59 INSERT INTO EMP VALUES(7876,'ADAMS','CLERK',7788,'1987-07-13',1100,NULL,20); 60 INSERT INTO EMP VALUES(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30); 61 INSERT INTO EMP VALUES(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20); 62 INSERT INTO EMP VALUES(7934,'MILLER','CLERK',7782,'1981-01-23',1300,NULL,10); 63 64 DROP TABLE SALGRADE; 65 //工資等級表 66 CREATE TABLE SALGRADE( 67 GRADE int,--等級 68 LOSAL double, --最低工資 69 HISAL double ); --最高工次 70 71 CREATE TABLE SALGRADE( 72 GRADE int, 73 LOSAL double, 74 HISAL double ); 75 INSERT INTO SALGRADE VALUES (1,700,1200); 76 INSERT INTO SALGRADE VALUES (2,1201,1400); 77 INSERT INTO SALGRADE VALUES (3,1401,2000); 78 INSERT INTO SALGRADE VALUES (4,2001,3000); 79 INSERT INTO SALGRADE VALUES (5,3001,9999);
emp表sql
dep表數據庫
salgrade表oracle
練習1:函數
1 1.安裝mysql數據庫 2 3 2.建立一個mydb1數據庫,並查看。 4 create database mydb1; 5 6 show mydb1; 7 show create database mydb1; 8 9 3.建立一個mydb2數據庫,指定字符編碼爲gb2312; 10 create database mydb2 chracter set gb2312 collate gb2312_bin; 11 12 4.建立一個mydb3數據庫,指定字符編碼爲utf8,而且指定校驗規則; 13 create database mydb3 character utf8 collate utf8_bin; 14 15 16 update練習 17 18 drop table if exists employee; 19 create table if not exists employee 20 ( 21 id int, 22 name varchar(20), 23 sex varchar(10), 24 birthday date, 25 salary float, 26 resume text 27 ); 28 29 30 insert into employee values (1,'zhangsan','male','1980-11-25',2000,'good body'); 31 insert into employee values(2,"lisi","male","1980-04-25",1000,"good body"); 32 insert into employee values(3,"xiaohong","female","1978-11-25",4000,"good girl"); 33 34 將全部員工薪水修改成5000元。 35 --alter table employee salary "alter這個關鍵字僅僅是用來修改表的結構的,對錶中的內容沒法修改,修改表中的內容要用update" 36 --update table employee set salary = 5000; update 是修改表中的數據,就不用在其後加上talbe關鍵字了. 37 update employee set salary = 5000; 38 39 40 將姓名爲’zhangsan’的員工薪水修改成3000元。 41 update employee set salary = 3000 where name = 'zhangsan'; 42 43 將姓名爲’lisi’的員工薪水修改成4000元,sex改成female。 44 --update employee set salary = 4000 and sex = female where name = 'lisi' ; 有多個列要修改的時候不用and鏈接列,用逗號.!!! 45 update employee set salary = 4000,sex = 'female' where name = 'lisi' ; 46 47 將xiaohong的薪水在原有基礎上增長1000元。 48 update employee set salary = salary + 1000 where name = 'xiaohong'; 49 50 51 select 練習 52 53 create table student( 54 id int, 55 name varchar(20), 56 chinese float, 57 english float, 58 math float 59 ); 60 61 insert into student(id,name,chinese,english,math) values(1,'張小明',89,78,90); 62 insert into student(id,name,chinese,english,math) values(2,'李進',67,53,95); 63 insert into student(id,name,chinese,english,math) values(3,'王五',87,78,77); 64 insert into student(id,name,chinese,english,math) values(4,'李一',88,98,92); 65 insert into student(id,name,chinese,english,math) values(5,'李來財',82,84,67); 66 insert into student(id,name,chinese,english,math) values(6,'張進寶',55,85,45); 67 insert into student(id,name,chinese,english,math) values(7,'黃蓉',75,65,30); 68 69 70 71 查詢表中全部學生的信息。 72 select * from student; 73 74 查詢表中全部學生的姓名和對應的英語成績。; 75 select name,english from student ; 76 77 78 過濾表中重複數據。 79 80 select distinct * from student; 81 82 統計每一個學生的總分。 83 --select sum(chinese+english+math) as Total from student; 這個是全部學生的總分, 貌似sum()函數是對以整列求和,對一個記錄內的求和是要單獨加 84 select name ,(chinese+english+math) as Total from student; 85 select name ,chinese+english+math as Total from student;--這兩個都對. 86 87 在全部學生總分數上加10分特長分。 88 --select name , chinese+english+math+10 as Total from student;是全部學生分數上,並非學生總成績上. 89 select name , chinese+10,english+10,math+10 as Total from student; 90 91 92 使用別名表示學生分數。 93 select name,chinese ch ,english en,math ma from student;--這個地方別名 加不加as都對. 94 95 96 查詢姓名爲李一的學生成績 97 select name,chinese, math,english from student where name = '李一'; 98 99 100 查詢英語成績大於90分的同窗 101 select name from student where english > 90 ; 102 103 查詢總分大於200分的全部同窗 104 select name from student where english+chinese+math>200; 105 106 查詢英語分數在 80-90之間的同窗。 107 --select name from student where english>=80 & english<=90; &這個符號不能表明and!!!! 108 select name from student where english>=80 and english<=90; 109 select name from student where english between 80 and 90; 110 111 112 查詢數學分數爲89,90,91的同窗。 113 select name from student where math in(89,90,91); 114 --select name from student where math=89 and math=90 and math=91;這個地方用and就不正確.檢索不到東西.能夠用關鍵字OR 115 select name from student where math=89 or math=90 or math=91; 116 117 118 119 查詢全部姓李的學生英語成績。 120 select name,english from student where name like "李%"; 121 122 123 124 查詢數學分>80而且語文分>80的同窗。 125 select name from student where math>80 and chinese >80; 126 127 查詢英語>80或者總分>200的同窗 128 select name from student where english > 80 OR english+math+chinese>200; 129 130 對數學成績排序後輸出。 131 select * from student order by math ;--默認是升序排列. 132 select * from student order by math desc; 133 select * from student order by math asc; 134 135 136 對總分排序後輸出,而後再按從高到低的順序輸出 137 select * from student order by chinese+math+english desc; 138 139 對姓李的學生成績排序輸出 140 select * from student where name like "李%" order by chinese+math+english desc;
練習2:編碼
1 1、查找部門30中員工的詳細信息。 2 --select * from emp where deptno=30; 3 select * from emp where deptno=30; 4 2、找出從事clerk工做的員工的編號、姓名、部門號。 5 -- select empno,ename,deptno from emp where job = 'clerk'; 6 7 select empno,ename,deptno from emp where job='clerk'; 8 9 3、檢索出獎金多於基本工資的員工信息。 10 --select * from emp where comm>sal; 11 select * from emp where comm >sal 12 13 4、檢索出獎金多於基本工資60%的員工信息。 14 --select * from emp where comm>sal*0.6; 15 select *from emp where comm>sal*0.6; 16 17 5、找出10部門的經理(job=Manger)、20部門的職員(job=clerk) 的員工信息。 18 --select * from dept,emp where dept.deptno=10 and emp.deptno=20; 19 20 --select * from emp where deptno=10 and job = 'manager' and deptno=20 and job='clerk'; 21 --這個地方找出10號部門的經理20部門的職員的員工信息.中間用OR鏈接.別用AND,用and是找不到這麼個條件的值的. 22 23 select * from emp where deptno=10 and job='MANAGER' or deptno=20 and job='CLERK' 24 25 6、找出10部門的經理、20部門的職員 或者既不是經理也不是職員可是工資高於2000元的員工信息。 26 27 --select * from emp where deptno=10 and job='MANAGER' or deptno=20 and job='CLERK' or 最後這個條件不會寫,原來用not 外加in 28 29 select * from emp where (deptno=10 and job='MANAGER') or ( deptno=20 and job='CLERK') or ( job not in ('MANAGER','CLERK') and sal>2000) 30 31 7、找出得到獎金的員工的工做。 32 --select * from emp where comm not null; 用is not null. 33 select * from emp where comm is not null. 34 35 select * from emp where comm is not null; 36 37 8、找出獎金少於100或者沒有得到獎金的員工的信息。 38 select * from emp where comm<100 or comm is null; 39 40 select * from emp where comm is null or comm<100; 41 42 9、找出姓名以A、B、S開始的員工信息。 43 --select * from emp where ename like "A%","B%","S%";要寫多個like語句. 44 45 select * from emp where ename like 'A%' OR ENAME LIKE 'B%' OR ENAME LIKE 'S%'; 46 47 48 10、找到名字長度爲7個字符的員工信息。 49 --select * from emp where ename.length= 7; 50 --報錯:ERROR 1054 : Unknown column 'ename.length' in 'where clause' 51 --用到了length()函數,還有就是弄like中引入對應數字的空格' '. 52 53 select * from emp where length(ename)=7; 54 select * from emp where ename like '______'; 55 56 11、名字中不包含R字符的員工信息。 57 58 --利用 not like '%R%' 59 select * from emp where ename not like '%R%'; 60 61 12、返回員工的詳細信息並按姓名排序。 62 select * from emp order by ename; 63 64 65 13、返回員工的信息並按工做降序工資升序排列。 66 select * from emp order by job desc,sal asc; 67 68 69 14、計算員工的日薪(按30天)。 70 select sal/30 from emp 71 72 73 15、找出姓名中包含A的員工信息。 74 select * from emp where ename like '%A%';
練習3:spa
//多表與分組練習 1、返回擁有員工的部門名、部門號。 --不知道這個具體幹什麼的.經過看部門dept表和emp表能夠看出來,員工並非都是在dept表中存在的地方的. --dept表中可能有四個部門號,可是員工emp表中可能只佔其中的三個部門號碼 --題乾的需求就是幹這個的.(即有的部門可能沒有"員工") select dname,deptno from dept where deptno in (select deptno from emp); +------------+--------+ | dname | deptno | +------------+--------+ | ACCOUNTING | 10 | | RESEARCH | 20 | | SALES | 30 | +------------+--------+ 2、工資水平多於smith的員工信息。 select * from emp where sal>(select sal from emp where ename = 'smith'); 3、返回員工和所屬經理的姓名。 --這個感受很簡單就是沒有想出來怎麼辦,看答案才理解了 --複用表emp,再加上使用外鏈接. --select ee.ename as eename ,mm.ename as mename from emp ee left join emp mm where ee.mgr=mm.empno; --看完答案理解了以後也寫錯了,注意外鏈接是left/right join on select ee.ename as eename ,mm.ename as mename from emp ee left join emp mm on ee.mgr=mm.empno; --如今左外鏈接還有另外的一種寫法,就是用加號"+";仔細查了一下,而且納悶爲何+的SQL語句外鏈接在navicate中沒有效果. --得知加號的外鏈接只有在oracle中才是用. +--------+--------+ | eename | mename | +--------+--------+ | SMITH | FORD | | ALLEN | BLAKE | | WARD | BLAKE | | JONES | KING | | MARTIN | BLAKE | | BLAKE | KING | | CLARK | KING | | SCOTT | JONES | | KING | NULL | | TURNER | BLAKE | | ADAMS | SCOTT | | JAMES | BLAKE | | FORD | JONES | | MILLER | CLARK | 4、返回僱員的僱傭日期早於其經理僱傭日期的員工及其經理姓名。 --感受這個也是要複用emp表,確實複用emp表 --select ee.ename as eename,mm.ename as mename from emp ee,emp mm where ee.hiredate<mm.hiredate; --這句話沒有加上是其經理的條件 --select ee.ename as eename,mm.ename as mename from emp ee left join emp mm on ee.mgr = mm.empno and ee.hiredate < mm.hiredate; --上面這句僅僅是把第三句中加上了日期小於的條件. select ee.ename as eename,mm.ename as mename from emp ee ,emp mm where ee.mgr = mm.empno and ee.hiredate < mm.hiredate; --這句爲何不用left join? 第三句用where可不能夠? 注意時間老的日期大小是小於時間老的日期的. (select ee.ename as eename ,mm.ename as mename from emp ee left join emp mm where ee.mgr=mm.empno;)第三句的寫法. 5、返回員工姓名及其所在的部門名稱。 --這句有點像第三句,不過這個牽扯到了兩個表 --select ee.ename as ename, ee.deptno as dptname from emp ee, dept dd where ee.deptno=dd.deptno; --最開始寫的,寫錯了.仍是沒有把最基本的需求高清,人家要部門名. select ename as ename ,dname from emp ee,dept dd where ee.deptno=dd.deptno; +--------+------------+ | ename | dname | +--------+------------+ | CLARK | ACCOUNTING | | KING | ACCOUNTING | | MILLER | ACCOUNTING | | SMITH | RESEARCH | | JONES | RESEARCH | | SCOTT | RESEARCH | | ADAMS | RESEARCH | | FORD | RESEARCH | | ALLEN | SALES | | WARD | SALES | | MARTIN | SALES | | BLAKE | SALES | | TURNER | SALES | | JAMES | SALES | +--------+------------+ 6、返回從事clerk工做的員工姓名和所在部門名稱。 select ename ,dname from emp ee, dept dd where ee.job='clerk' and ee.deptno = dd.deptno; +--------+------------+ | ename | dname | +--------+------------+ | MILLER | ACCOUNTING | | SMITH | RESEARCH | | ADAMS | RESEARCH | | JAMES | SALES | +--------+------------+ 7、返回部門號及其本部門的最低工資。 --這個語句不用where條件進行過濾. --找出部門的最低工資 ----想不起來.提示一下用到了min()函數. --select min(sal) from emp ; --select deptno , min(sal) from deptno ,emp --首先想需求中要用到哪幾個表.而後再寫. --由於是按照部門爲單位的因此用到分組 select deptno ,min(sal) from emp group by deptno; +--------+----------+ | deptno | min(sal) | +--------+----------+ | 10 | 1300 | | 20 | 800 | | 30 | 950 | +--------+----------+ 8、返回銷售部(sales)全部員工的姓名。 --根據需求用到了emp和dept兩個表 select ename from emp ee,dept dd where dd.dname='sales' and ee.deptno = (select deptno from dept where dname = 'sales'); +--------+ | ename | +--------+ | ALLEN | | WARD | | MARTIN | | BLAKE | | TURNER | | JAMES | +--------+ 9、返回工資水平多於平均工資的員工。 --這個用到了avg()函數. 用到表emp --select * from emp where sal > avg(sal); --一開始這樣寫的,可是報錯,提示信息爲:ERROR 1111 : Invalid use of group function --在這種狀況下要把聚合函數放在select語句中 select * from emp where sal > (select avg(sal) from emp); +-------+-------+-----------+------+------------+------+------+--------+ | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | +-------+-------+-----------+------+------------+------+------+--------+ | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975 | NULL | 20 | | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850 | NULL | 30 | | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450 | NULL | 10 | | 7788 | SCOTT | ANALYST | 7566 | 1987-07-03 | 3000 | NULL | 20 | | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL | 10 | | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000 | NULL | 20 | +-------+-------+-----------+------+------------+------+------+--------+ 10、返回與SCOTT從事相同工做的員工。 --只用到表emp --select * from emp where job = '(select job from emp where ename = 'scott')'; --上一句在語法上是沒有問題的,可是有個小問題就是job是varchar字符串類型的,可是在其後是select語句 -- 的時候其後是不能加上單引號,或者是單引號的,不然都會報錯的. --還要把搜索到的結果去掉scott他自己這條記錄. select * from emp where job = (select job from emp where ename = 'scott') and ename<> 'scott'; 11、返回與30部門員工工資水平相同的員工姓名與工資。 --這個也只用到了emp表 select ename ,sal from emp where sal in( select sal from emp where deptno = 30); +--------+------+ | ename | sal | +--------+------+ | ALLEN | 1600 | | WARD | 1250 | | MARTIN | 1250 | | BLAKE | 2850 | | TURNER | 1500 | | JAMES | 950 | +--------+------+ 12、返回工資高於30部門全部員工工資水平的員工信息。 --從題意中就能夠看出要使用到關鍵字all 也只用到了emp表 --all關鍵字怎麼使用. select * from emp where sal>all(select sal from emp where deptno = 30); +-------+-------+-----------+------+------------+------+------+--------+ | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | +-------+-------+-----------+------+------------+------+------+--------+ | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975 | NULL | 20 | | 7788 | SCOTT | ANALYST | 7566 | 1987-07-03 | 3000 | NULL | 20 | | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL | 10 | | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000 | NULL | 20 | +-------+-------+-----------+------+------------+------+------+--------+ 13、返回部門號、部門名、部門所在位置及其每一個部門的員工總數。 --須要用到emp和dept兩個表 select deptno ,dname ,loc ,count(*) from emp ee,dept dd where ee.deptno = dd.deptno 14、返回員工的姓名、所在部門名及其工資。 --須要用到emp表和dept表. select ee.ename , dd.dname ,ee.sal from emp ee,dept dd where ee.deptno = dd.deptno; +--------+------------+------+ | ename | dname | sal | +--------+------------+------+ | CLARK | ACCOUNTING | 2450 | | KING | ACCOUNTING | 5000 | | MILLER | ACCOUNTING | 1300 | | SMITH | RESEARCH | 800 | | JONES | RESEARCH | 2975 | | SCOTT | RESEARCH | 3000 | | ADAMS | RESEARCH | 1100 | | FORD | RESEARCH | 3000 | | ALLEN | SALES | 1600 | | WARD | SALES | 1250 | | MARTIN | SALES | 1250 | | BLAKE | SALES | 2850 | | TURNER | SALES | 1500 | | JAMES | SALES | 950 | +--------+------------+------+ 15、返回員工的詳細信息。(包括部門名) --須要用到兩個表emp和dept --在用select * 的基礎上還要加上部門名這一列,這咋辦???? --看了答案明白了既然是還要加上部門名而且是詳細信息,那麼就對這三個表都select* 操做 --select * from emp ee,dept dd,salgrade ss where ee.deptno = dd.deptno; --一開始沒有加上工資的限制. select * from emp ee,dept dd,salgrade ss where ee.deptno = dd.deptno and sal between losal and hisal; +-------+--------+-----------+------+------------+------+------+--------+--------+------------+----------+-------+-------+-------+ | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | DEPTNO | DNAME | LOC | GRADE | LOSAL | HISAL | +-------+--------+-----------+------+------------+------+------+--------+--------+------------+----------+-------+-------+-------+ | 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800 | NULL | 20 | 20 | RESEARCH | DALLAS | 1 | 700 | 1200 | | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600 | 300 | 30 | 30 | SALES | CHICAGO | 3 | 1401 | 2000 | | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250 | 500 | 30 | 30 | SALES | CHICAGO | 2 | 1201 | 1400 | | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975 | NULL | 20 | 20 | RESEARCH | DALLAS | 4 | 2001 | 3000 | | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250 | 1400 | 30 | 30 | SALES | CHICAGO | 2 | 1201 | 1400 | | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850 | NULL | 30 | 30 | SALES | CHICAGO | 4 | 2001 | 3000 | | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450 | NULL | 10 | 10 | ACCOUNTING | NEW YORK | 4 | 2001 | 3000 | | 7788 | SCOTT | ANALYST | 7566 | 1987-07-03 | 3000 | NULL | 20 | 20 | RESEARCH | DALLAS | 4 | 2001 | 3000 | | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL | 10 | 10 | ACCOUNTING | NEW YORK | 5 | 3001 | 9999 | | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500 | 0 | 30 | 30 | SALES | CHICAGO | 3 | 1401 | 2000 | | 7876 | ADAMS | CLERK | 7788 | 1987-07-13 | 1100 | NULL | 20 | 20 | RESEARCH | DALLAS | 1 | 700 | 1200 | | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950 | NULL | 30 | 30 | SALES | CHICAGO | 1 | 700 | 1200 | | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000 | NULL | 20 | 20 | RESEARCH | DALLAS | 4 | 2001 | 3000 | | 7934 | MILLER | CLERK | 7782 | 1981-01-23 | 1300 | NULL | 10 | 10 | ACCOUNTING | NEW YORK | 2 | 1201 | 1400 | +-------+--------+-----------+------+------------+------+------+--------+--------+------------+----------+-------+-------+-------+ 16、返回員工工做及其從事此工做的最低工資。 --須要用到表emp,很明顯須要 用到分組group by. select job ,min(sal) from emp group by job; --原來能夠在from前面直接用聚合函數. +-----------+----------+ | job | min(sal) | +-----------+----------+ | ANALYST | 3000 | | CLERK | 800 | | MANAGER | 2450 | | PRESIDENT | 5000 | | SALESMAN | 1250 | +-----------+----------+ 17、返回不一樣部門經理的最低工資。 --須要用到一個表emp,須要用到group by select deptno , min(sal) from emp where job = 'manager' group by deptno; +--------+----------+ | deptno | min(sal) | +--------+----------+ | 10 | 2450 | | 20 | 2975 | | 30 | 2850 | +--------+----------+ 18、計算出員工的年薪,而且以年薪排序。 --須要用到一個表emp select sal*12 as ysal from emp order by ysal asc; --上面的語句沒有問題,關鍵是年薪的計算方法,這裏還牽扯到獎金的問題. 19、返回工資處於第四級別的員工的姓名。 --這裏須要兩個表,一個是emp一個是salgrade --select ename form emp ee,salgrade where grade =4; --這句話仔細一想就能夠看出不會有任何結果,where過濾不掉任何東西 select ename from emp ,salgrade where grade =4 and sal>=losal and sal <= hisal; select ename from emp ,salgrade where grade =4 and sal between losal and hisal; +-------+ | ename | +-------+ | JONES | | BLAKE | | CLARK | | SCOTT | | FORD | +-------+ 20、返回工資爲二等級的職員名字、部門所在地、和二等級的最低工資和最高工資 --從題意中可知須要用到三個表 emp dept salgrade --二等級的最低工資和最高工資.並非二等級員工的工資.這個是和第21問的區別. --select ename ,loc ,min(sal) ,max(sal) from emp ee,dept dd, salgrade ss where grade = 2 and sal between losal and hisal; --二等級的最低工資和最高工資並非二等級員工的最低工資和最高工資. select ename ,loc ,losal,hisal from emp ee,dept dd,salgrade ss where grade = 2 and sal between losal and hisal; 21、返回工資爲二等級的職員名字、部門所在地、二等級員工工資的最低工資和最高工資 --須要用到salgrade , dept表和emp表 mysql> select ename , loc ,min(sal) ,max(sal) from emp ee, dept dd, salgrade ss where grade = 2 and sal between losal and hisal; +-------+----------+----------+----------+ | ename | loc | min(sal) | max(sal) | +-------+----------+----------+----------+ | WARD | NEW YORK | 1250 | 1300 | +-------+----------+----------+----------+ --這樣寫是錯誤的.到底在select後加上聚合函數的應用????? --select e.ename,loc,max(m.sal) ,min(m.sal),grade from emp e,emp m ,dept salgrade where e.deptno=dept.deptno and e.sal beteeen losal and hissal --m.sal between losal and hisal and grade = 2 group by e.ename , loc, grade; select ename , loc ,(select min(sal) from emp,salgrade where sal between losal and hisal and grade = 2) minsal,(select max(sal) from emp,salgrade where sal between losal and hisal and grade = 2) maxsal from emp,salgrade ,dept where emp.deptno = dept.deptno and sal between losal and hisal and grade = 2; +--------+----------+--------+--------+ | ename | loc | minsal | maxsal | +--------+----------+--------+--------+ | WARD | CHICAGO | 1250 | 1300 | | MARTIN | CHICAGO | 1250 | 1300 | | MILLER | NEW YORK | 1250 | 1300 | +--------+----------+--------+--------+ 22.工資等級多於smith的員工信息。 --得到員工信息,三個表中有的信息都獲取出來,就 用到三個表. --須要用到兩個表 emp表和salgrade 表 --select * from emp,salgrade where grade >(select sal from emp,salgrade where sal between losal and hisal); --select * from emp ee,salgrade ss,dept dd where grade > (select grade from salgrade, emp where ename = 'smith' and sal between losal and ss.hisal ) --and sal > (select sal from em ); select * from emp,salgrade where sal between losal and hisal and grade >(select grade from emp,salgrade where sal between losal and hisal and ename = 'smith'); +-------+--------+-----------+------+------------+------+------+--------+-------+-------+-------+ | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | GRADE | LOSAL | HISAL | +-------+--------+-----------+------+------------+------+------+--------+-------+-------+-------+ | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600 | 300 | 30 | 3 | 1401 | 2000 | | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250 | 500 | 30 | 2 | 1201 | 1400 | | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975 | NULL | 20 | 4 | 2001 | 3000 | | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250 | 1400 | 30 | 2 | 1201 | 1400 | | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850 | NULL | 30 | 4 | 2001 | 3000 | | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450 | NULL | 10 | 4 | 2001 | 3000 | | 7788 | SCOTT | ANALYST | 7566 | 1987-07-03 | 3000 | NULL | 20 | 4 | 2001 | 3000 | | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL | 10 | 5 | 3001 | 9999 | | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500 | 0 | 30 | 3 | 1401 | 2000 | | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000 | NULL | 20 | 4 | 2001 | 3000 | | 7934 | MILLER | CLERK | 7782 | 1981-01-23 | 1300 | NULL | 10 | 2 | 1201 | 1400 | +-------+--------+-----------+------+------------+------+------+--------+-------+-------+-------+