/*
SQLyog 企业版 - MySQL GUI v8.14
MySQL - 5.5.62 : Database - offcnoa
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`offcnoa` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `offcnoa`;
/*Table structure for table `dept` */
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
`DEPTNO` int(2) NOT NULL,
`DNAME` varchar(14) DEFAULT NULL,
`LOC` varchar(13) DEFAULT NULL,
PRIMARY KEY (`DEPTNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `dept` */
insert into `dept`(`DEPTNO`,`DNAME`,`LOC`) values (10,'ACCOUNTING','NEW YORK'),(20,'RESEARCH','DALLAS'),(30,'SALES','CHICAGO'),(40,'OPERATIONS','BOSTON');
/*Table structure for table `emp` */
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`EMPNO` int(4) NOT NULL,
`ENAME` varchar(10) DEFAULT NULL,
`JOB` varchar(9) DEFAULT NULL,
`MGR` int(4) DEFAULT NULL,
`HIREDATE` date DEFAULT NULL,
`SAL` double(7,2) DEFAULT NULL,
`COMM` double(7,2) DEFAULT NULL,
`DEPTNO` int(2) DEFAULT NULL,
PRIMARY KEY (`EMPNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `emp` */
insert into `emp`(`EMPNO`,`ENAME`,`JOB`,`MGR`,`HIREDATE`,`SAL`,`COMM`,`DEPTNO`) values (7369,'SMITH','CLERK',7902,'1980-12-17',800.00,NULL,20),(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600.00,300.00,30),(7521,'WARD','SALESMAN',7698,'1981-02-22',1250.00,500.00,30),(7566,'JONES','MANAGER',7839,'1981-04-02',2975.00,NULL,20),(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250.00,1400.00,30),(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850.00,NULL,30),(7782,'CLARK','MANAGER',7839,'1981-06-09',2450.00,NULL,10),(7788,'SCOTT','ANALYST',7566,'1987-04-19',3000.00,NULL,20),(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000.00,NULL,10),(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500.00,NULL,30),(7876,'ADAMS','CLERK',7788,'1987-05-23',1100.00,NULL,20),(7900,'JAMES','CLERK',7698,'1981-12-03',950.00,NULL,30),(7902,'FORD','ANALYST',7566,'1981-12-03',3000.00,NULL,20),(7934,'MILLER','CLERK',7782,'1982-01-23',1300.00,NULL,10),(7988,'amithss','clerk',7934,'2021-06-29',3000.00,NULL,10),(7989,'a_thor','clerk',7902,'2021-06-29',2900.00,NULL,10);
/*Table structure for table `salgrade` */
DROP TABLE IF EXISTS `salgrade`;
CREATE TABLE `salgrade` (
`GRADE` int(11) DEFAULT NULL,
`LOSAL` int(11) DEFAULT NULL,
`HISAL` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `salgrade` */
insert into `salgrade`(`GRADE`,`LOSAL`,`HISAL`) values (1,700,1200),(2,1201,1400),(3,1401,2000),(4,2001,3000),(5,3001,9999);
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
1、查询员工的姓名
SELECT ename AS 姓名 FROM emp;
2、查询员工的薪水以及姓名
SELECT sal AS 薪水,ename AS 姓名 FROM emp
3、查询员工表所有的数据
SELECT * FROM emp;
4、查询每一个员工的年薪(包含补助)
SELECT ename AS 姓名,((sal+ IFNULL(comm,0))*12) AS 年薪(包含补助) FROM emp;
5、查询薪水为5000的员工
SELECT * FROM emp WHERE sal=5000;
6、查询职位为 MANAGER 的员工的信息
SELECT * FROM emp WHERE job='MANAGER';
7、查询薪水不等于5000的员工的信息
SELECT * FROM emp WHERE sal != 5000;
8、查询职位不是MANAGER 的员工的信息
SELECT * FROM emp WHERE job !='MANAGER';
9、薪水大于1600的员工信息
SELECT * FROM emp WHERE sal>1600;
10、薪水在1600和3000之间的员工信息
SELECT * FROM emp WHERE sal>=1600 AND sal<=3000;
11、查询部门编号为20或者30的部门信息
SELECT * FROM dept WHERE deptno IN(20,30);
12、查询部门编号为20或者30的信息
SELECT * FROM emp WHERE deptno = 20 OR deptno=30;SELECT * FROM emp WHERE deptno IN (20,30);
13、查询部门编号不为20也不为30的信息
SELECT * FROM emp WHERE deptno NOT IN (20,30);SELECT * FROM emp WHERE deptno != 20 AND deptno!=30;
14、要求第一个字母带有s的员工的信息
SELECT * FROM emp WHERE ename LIKE 's%';
15、要求第二个字母带有m的员工的信息
SELECT * FROM emp WHERE ename LIKE '_m%';
16、根据员工的薪水排序(升序)
SELECT * FROM emp ORDER BY sal ASC;
17、根据员工的薪水排序(降序)
SELECT * FROM emp ORDER BY sal DESC;
18、员工入职日期降序查询
SELECT * FROM emp ORDER BY HIREDATE DESC;
19、查询职位为MANAGER 的员工信息,并且按照薪资从高到低排序
SELECT * FROM emp WHERE job ='manager' ORDER BY sal DESC;
20、查询emp前五条数据
SELECT * FROM emp LIMIT 5;
21、员工每页5条数据,查询前三页
SELECT * FROM emp LIMIT 0,5;SELECT * FROM emp LIMIT 5,5;SELECT * FROM emp LIMIT 10,5;
22、查询JAMES的部门地址
SELECT d.loc AS 地址 FROM emp e INNER JOIN dept d WHERE e.ename='james' AND e.deptno=d.deptno;
23、查询RESEARCH部门所有员工信息
SELECT e.*,d.`DNAME` FROM dept d LEFT JOIN emp e ON d.deptno=e.deptno WHERE d.dname='RESEARCH';
数据库学习第二天