1 Sort operation
The table structure is as follows
Find all dept_no
, and sort
select dept_no from dept_emp
order by dept_no;
逆序排序
select dept_no from dept_emp
order by dept_no desc;
2 Deduplication operation
The table structure is the same as above
Find all dept_no
, sort and remove duplicates
select distinct dept_no from dept_emp
order by dept_no desc;
It should be noted that it distinct
must be placed in the front
3 Number of queries
The table structure is the same as above
Find all dept_no
rows that appear more than or equal to 2 times
select count(dept_no) from dept_emp
group by dept_no
having count(dept_no)>=2;
It should be noted that the group by dept_no
statement cannot be omitted.
4 multiple conditions
Table structure above
lookup employees
table all emp_no
odd, and last_name
not Mary
the employee information, and follow the hire_date
reverse order
select * from employees
where emp_no %2 =1
and last_name != 'Mary'
order by hire_date desc;
It should be noted that the keywords used in parallel are and
;
if an !=
error is reported, replace it with <>
, which also means not equal.
5 The largest query
The table structure is as follows, asking for salary 第二多
of employees emp_no
and their corresponding salary salary
.
select emp_no, salary from salaries
order by salary desc
# limit 1,1;
limit 1 offset 1;
limit a,b
Equivalent to limit a offset b
, the value range is [a, a+b)
.