1. is null
And is not null
:
- Example: Query the employee number empno, name ename, and salary sal of the superior leader mgr in the emp table:
select empno,ename,sal from emp where mgr is null;
- Example: Query the name, salary, and bonus of employees without bonus comm in the emp table:
select ename,sal,comm from emp where comm is null;
- Example: Query the information of all employees with bonuses:
select * from emp where comm is not null;
2. Alias: as
or 省略
:
- Example: Change the name ename of the inquired employee to'name':
select ename as 姓名 from emp;
select ename 姓名 from emp;
3. Removal distinct
:
- Example: Query all positions job appearing in the emp table:
select distinct job from emp;
4. Comparison operator:>、<、>=、<=、=、!=、<>
- Example: Query the names and salaries of all employees whose salary is less than or equal to 1600:
select ename,sal from emp where sal <= 1600;
- Example: Query the name, position and department number deptno of all employees whose department label is 20:
select ename,job,deptno from emp where deptno = 20;
- Example: Query the name and department number of all employees whose department is not department 10: (two ways of writing)
select ename,deptno from emp where deptno != 10;
select ename,deptno from emp where deptno <> 10;
5. and
And or
: and
equivalent to in java &&
, or
equivalent to in java ||
:
- Example: Query the information of employees whose salary is greater than 2000 and is in department 10:
select * from emp where sal > 2000 and deptno = 10;
- Example: The query part is the name and position of the employee in department 30 or the superior leader whose mgr is 7698, the superior leader and department number:
select ename,job,mgr,deptno from emp where deptno = 30 or mgr = 7698;
6、in
:
- Example: Query the information of employees whose salary is 5000, 1500, 3000 in the emp table:
select * from emp where sal = 1500 or sal = 5000 or sal = 3000;
select * from emp where sal in(1500,5000,3000);
7. between x and y
(including x and y):
- Example: Query the name and salary of employees whose salary is between 2000 and 3000:
select ename,sal from emp where sal >= 2000 and sal <= 3000;
select ename,sal from emp where sal between 2000 and 3000;
8. Fuzzy query: like
:
_
: Represents a single unknown character%
: Represents 0 or more unknown characters
- Example: string beginning with a:
a%
- Example: end with m:
%m
- Example: contains x:
%x%
- Example: The second character is a:
_a
- Example: The third letter from the bottom is m:
%m__
- Example: start with a and the penultimate letter is b:
a%b_
- Case: Query the name and salary of all employees whose name contains a:
select ename,sal from emp where ename like '%a%';
- Case: Query notepads whose unit price is less than 100 (title includes notepads):
select * from t_item where price < 100 and title like '%记事本%';
- Case: Query effective product information with images:
select * from t_item where title like '%得力%' and image is not null;
- Case: Query product information that has nothing to do with Deli: (title does not include Deli):
select * from t_item where title not like '%得力%';
9. Sort: order by
Keyword:
Write the character name to sort after by (the default is ascending order) asc
:: ascending,: desc
descending
- Example: Query the names and salaries of all employees in ascending order of salary:
select ename,sal from emp order by sal;
- Example: Query the information of all employees in department 10, sorted in descending order of salary:
select * from emp where deptno = 10 order by sal desc;
- Example: Query all employees in ascending order by department, and if the departments are consistent, they will be sorted in descending order by salary:
select * from emp order by deptno,sal desc;
- Example: Query all product categories and unit prices in descending order of category, if the categories are the same, sort them in descending order of unit price:
select category_id,price from t_item order by category_id desc,price;
10. Paging query: limit
:
limit the number of skipped, the number of requests (the number of each page)
- Example: Query wage employees table in descending order of the first five data:
select * from emp order by sal desc limit 0,5
; - Example: Query 4 pieces of data on page 3 in descending order of salary in the employee table:
select * from emp order by sal desc limit 8,4;
- Example: Query the third data on the third page of the product information whose price is less than 100 yuan in the product table
select * from t_item where price < 100 order by price desc limit 6,3;
- Example: Query the information of the top three employees in departments 10 and 30:
select * from emp where deptno in(10,30) order by sal limit 0,3;