1. Table preparation
The SQL demonstration of the query operation will be based on the following four tables. We first create these four data tables and add data to them.
1. The first table is a department table , named as containing three fields: department number (deptno), department name (dname), department location (loc)
create table DEPT(
DEPTNO int(2) not null,
DNAME VARCHAR(14),
LOC VARCHAR(13)
);
Add a primary key constraint for the department number field (deptno):
alter table DEPT
add constraint PK_DEPT primary key (DEPTNO);
Insert data into the department table (dept):
insert into DEPT (DEPTNO, DNAME, LOC)
values (10, 'ACCOUNTING', 'NEW YORK');
insert into DEPT (DEPTNO, DNAME, LOC)
values (20, 'RESEARCH', 'DALLAS');
insert into DEPT (DEPTNO, DNAME, LOC)
values (30, 'SALES', 'CHICAGO');
insert into DEPT (DEPTNO, DNAME, LOC)
values (40, 'OPERATIONS', 'BOSTON');
2. The second table is the employee table (emp), which contains 8 fields, which are employee number (empno), employee name (ename), position (job), direct leader (mgr), and entry date (hiredate) , salary (sal), subsidy (comm), department number (deptno):
create table EMP
(
EMPNO int(4) primary key,
ENAME VARCHAR(10),
JOB VARCHAR(9),
MGR int(4),
HIREDATE DATE,
SAL double(7,2),
COMM double(7,2),
DEPTNO int(2)
);
Add a foreign key constraint to the field deptno of the employee table emp, which is associated with the field department number (deptno) of the department table (dept):
alter table EMP
add constraint FK_DEPTNO foreign key (DEPTNO)
references DEPT (DEPTNO);
Insert data into the employee table (emp):
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7788, 'SCOTT', 'ANALYST', 7566, '1987-04-19', 3000, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7876, 'ADAMS', 'CLERK', 7788, '1987-05-23', 1100, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, null, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, null, 10);
3. The third table is the salary grade table , which contains 3 fields, namely: salary grade (grade), minimum salary (losal), and maximum salary (hisal):
create table SALGRADE
(
GRADE int primary key,
LOSAL double(7,2),
HISAL double(7,2)
);
Insert data into the salary grade table (salgrade):
insert into SALGRADE (GRADE, LOSAL, HISAL)
values (1, 700, 1200);
insert into SALGRADE (GRADE, LOSAL, HISAL)
values (2, 1201, 1400);
insert into SALGRADE (GRADE, LOSAL, HISAL)
values (3, 1401, 2000);
insert into SALGRADE (GRADE, LOSAL, HISAL)
values (4, 2001, 3000);
insert into SALGRADE (GRADE, LOSAL, HISAL)
values (5, 3001, 9999);
4. The fourth table is the bonus table , which contains 4 fields, namely: employee name (ename), employee work (job), salary (sal), comm
create table BONUS
(
ENAME VARCHAR(10),
JOB VARCHAR(9),
SAL double(7,2),
COMM double(7,2)
);
Two, single table query
In database operations, single-table query is to query data in one table.
1. Simple single-table query
Query the emp table of the employee table, * represents all data
select * from emp;
Show some columns:
select empno,ename,sal from emp;
Show partial rows: where clause
select * from emp where sal > 2000;
Show some columns, some rows:
select empno,ename,job,mgr from emp where sal > 2000;
2. Alias the field of the query
You can omit the keyword as to alias the field to be queried, and you can also omit the single quotes and double quotes attached to the alias:
select empno 员工编号,ename 姓名,sal 工资 from emp;
Use the keyword as when aliasing the queried field. The alias can also carry single quotes or double quotes. There is no difference between the two:
select empno as 员工编号,ename as 姓名,sal as 工资 from emp;
select empno as '员工编号',ename as "姓名",sal as 工资 from emp;
When there are special symbols in the alias, single quotes or double quotes cannot be omitted, as follows, there is a space between employee numbers (spaces are special symbols)
select empno as 员工 编号,ename as "姓 名",sal as 工资 from emp;
If the alias employee number does not use single quotes and double quotes, it will return a 1064 error:
-- > 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '编号,ename as "姓 名",sal as 工资 from emp' at line 1
3. Arithmetic operators can be used when querying fields
select empno,ename,sal,sal+1000 as '涨薪后',deptno from emp where sal < 2500;
select empno,ename,sal,comm,sal+comm from emp;
4. Deduplication operation
To deduplicate the queried data, you need to use the keyword distinct
select distinct job from emp;
If multiple columns are included after the keyword distinct , it means to deduplicate all subsequent columns, not a single column:
select distinct job,deptno from emp;
5. Sorting
To sort the queried data, you need to use the keyword order by
By default it is sorted in ascending order
select * from emp order by sal;
asc indicates ascending order, which can be omitted by default
select * from emp order by sal asc;
desc means descending
select * from emp order by sal desc;
In the case of sal (salary) in ascending order, deptno (department number) in descending order
select * from emp order by sal asc ,deptno desc;