MySQL common sentences and practical training

create database database-name
  • 1

Delete database:

drop database database-name
  • 1

Modify data name:

RENAME DATABASE db_name TO new_db_name
  • 1

Create new table:

create table table-name(
col1-name type1 [not null] [primary key] [auto_increment],
col2-name type2 ,
……
)
  • 1
  • 2
  • 3
  • 4
  • 5

auto_increment ->Automatic growth, only for numerical type.

Create new table based on old table:

create table table_name like table_old_name
  • 1
create table table_name as select col1,col2... from table_old_name;
  • 1

Delete the table:

drop table table_name;
  • 1

Amend table name:

alter table table_name rename table_new_name;
  • 1

Add one column:

alter table table_name add column col type [not null] ...;
  • 1

Delete a column:

alter table table_name drop column col_name;
  • 1

Modify column name:

alter table table_name change column col_old_name col_new_name type [not null]...; 
  • 1

Modify attributes of columns:

alter table table_name change column col_old_name col_old_name type [not null]...; 
  • 1

Choice:

select * from table_name where ...
  • 1

Insert:

insert into table_name(col1_name,col2_name...) values(...);
  • 1

Copy all data:

insert into table1_name(table1_name.col1_name,table1_name.col2_name...) select table2_name.col1_name,
table2_name.col_name...
from table2_name
  • 1
  • 2
  • 3

Copy of tables across databases (using absolute paths for specific data) (Access available)

insert into b(a, b, c) select d,e,f from b in ‘Concrete databasewhere condition
  • 1

Delete:

delect from table_name where
  • 1

To update:

update table_name set col1_nameee=value ...where ...
  • 1

Find:

select * from table_name where ...
  • 1

Sort:

select * from table_name order by col1_name asc/desc;
  • 1

asc Ascending order
desc Descending order

Total:

select count(*|col_name) as count_name from table_name where ...
  • 1

Summing up:

select sum(col_name) as sum_name from table_name where ...
  • 1

Seek the mean value:

select avg(col_name) as avg_name from table_name where ...
  • 1

Maximum:

select max(col_name) as max_name from table_name where ...
  • 1

Minimum:

select min(col_name) as min_name from table_name where ...
  • 1

unionAnd union all
UNION A result set that combines two or more SELECT statements and eliminates any duplicate rows in the table.  
UNION The internal SELECT statement must have the same number of columns, and the column must have similar data types.  
At the same time, the order of columns in each SELECT statement must be the same.

**Note: In addition, the column name in the UNION result set is always equal to the column name in the first SELECT statement in UNION.  
Note: 1. The column names in the UNION result set are always equal to the column names in the first SELECT statement.
2、UNION The internal SELECT statement must have the same number of columns. Columns must also have similar data types. At the same time, the order of columns in each SELECT statement must be the same.
** 
unionUsage and points for attention
union:Union means to combine the results of two or multiple queries.  
Requirement: the number of columns for two queries must be consistent.
Recommendation: the type of column can be different, but each column of the query is recommended, and the corresponding type is the same.
Data that can come from multiple tables: The column names taken out by multiple SQL statements can be inconsistent, whichever is the first SQL statement.  
If the rows taken out of different statements are exactly the same (in this case, the values for each column are the same), then union merges the same rows, leaving only one row. It can also be understood that union will remove duplicate rows.  
If you do not want to remove duplicate rows, you can use union all.  
If there are order by and limit in clauses, we need to wrap them in brackets. Recommendation is placed after all clauses, that is, to sort or filter the final merged results.

SELECT column_name FROM table1
UNION
SELECT column_name FROM table2
  • 1
  • 2
  • 3

JoinSyntax overview
JOIN According to their functions, they can be roughly divided into three categories:

INNER JOIN(Internal join or equivalents): records that have connection matching relations in the two tables are obtained.

select * from table1_name inner join table2_name on teable1_name.col = teable2_name.col where ...
  • 1

Here is a picture describing

select * from table1_name inner join table2_name on teable1_name.col = teable2_name.col where table1_name.col is null or table2_name.col is null and...
  • 1

Here is a picture describing
LEFT JOIN(Left join: Gets the full record of the left table (table 1), that is, the right table (table 2) has no corresponding matching record.

select * from table1_name left join table2_name on teable1_name.col = teable2_name.col where ...
  • 1

Here is a picture describing

select * from table1_name inner join table2_name on teable1_name.col = teable2_name.col where teable2_name.col is null and ...
  • 1

Here is a picture describing
RIGHT JOIN(Right join: Contrary to LEFT JOIN, complete records of the right table (table 2) are obtained, i.e. there is no matching record of the left table (table 1).

Full join:

select * from A left join B on B.name = A.name 
union 
select * from A right join B on B.name = A.name;
  • 1
  • 2
  • 3

Here is a picture describing

Grouping:

select *|count,avg,sum,max,min from table_name group by table_name.col_name
  • 1

Subquery:

#in
select * from table1_name where tabel1_name.col1 in (
select table2_name.col1 from teable2_name where... 
)
#not in
select * from table1_name where tabel1_name.col1 not in (
select table2_name.col1 from teable2_name where... 
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

between Numerical value 1 and 2

select * from table_name where table_name.col between numerical value1 and numerical value2
  • 1

not between Numerical value 1 and 2

select * from table_name where table_name.col between numerical value1 and numerical value2
  • 1

The two connection table deletes the information that is not found in the secondary table in the main table:

delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
  • 1

limit Return to the first or middle row of data.

SELECT * FROM table  LIMIT [offset,] rows | rows OFFSET offset
  • 1

havingWords and sentences allow us to filter the data after a group, where sentences filter records before aggregation, that is, before groups by and having sentences. The having clause is filtered after group aggregation.  
Example:

SELECT region, SUM(population), SUM(area)
FROM bbc
GROUP BY region
HAVING SUM(area)>1000000
  • 1
  • 2
  • 3
  • 4

Go back to weight:
Sometimes you need to query out records that are not duplicated in a field, and you can use the distinct keyword provided by Mysql to filter duplicate records.

select distinct col_name from table_name;
  • 1

Title online test address: https://www.nowcoder.com/ta/sql

 

Find all the information of the latest entry staff.
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));

nothing

output Description:

emp_no birth_date first_name last_name gender hire_date
10008 1958-02-19 Saniya Kalloufi M 1994-09-15

 

The answer:select * from employees order by hire_date desc limit 0,1;
  • 1
  • 2

 

 

second questions:

 

 

Find all staff members with the third lowest ranking time of entry staff.
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));

nothing

output Description:

emp_no birth_date first_name last_name gender hire_date
10005 1955-01-21 Kyoichi Maliniak M 1989-09-12

 

select * from employees order by hire_date desc limit 2,1;
  • 1

 

 

third questions:

 

 

Find the current salary details of the heads of each department (to_date=’9999-01-01′) and the corresponding department number dept_no
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));

nothing

output Description:

emp_no salary from_date to_date dept_no
10002 72527 2001-08-02 9999-01-01 d001
10004 74057 2001-11-27 9999-01-01 d004
10005 94692 2001-09-09 9999-01-01 d003
10006 43311 2001-08-02 9999-01-01 d002
10010 94409 2001-11-23 9999-01-01 d006

 

select salaries.*,dept_manager.dept_no from salaries, dept_manager
where salaries.emp_no = dept_manager.emp_no
and salaries.to_date = "9999-01-01"
and dept_manager.to_date = "9999-01-01";
  • 1
  • 2
  • 3
  • 4

 

 

Fourth questions:

 

 

Find the last_name and first_name of all the employees assigned to the Department.
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));

nothing

output Description:

last_name first_name dept_no
Facello Georgi d001
ellipsis ellipsis ellipsis
Piveteau Duangkaew d006

 

select last_name,first_name,dept_emp.dept_no from employees,dept_emp where employees.emp_no=dept_emp.emp_no;
  • 1

 

 

fifth questions:

 

 

Find the last_name and first_name of all employees and the corresponding department number dept_no, as well as show employees who are not assigned to specific departments
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));

nothing

output Description:

last_name first_name dept_no
Facello Georgi d001
ellipsis ellipsis ellipsis
Sluis Mary NULL(In SQLite, it is empty here and MySQL is NULL.

 

select employees.last_name,employees.first_name,dept_emp.dept_no from employees left join dept_emp
on employees.emp_no=dept_emp.emp_no;
  • 1
  • 2

 

 

sixth questions:

 

 

Find all employees’salaries at the time of entry, give emp_no and salary, and reverse the order according to emp_no
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));

nothing

output Description:

emp_no salary
10011 25828
ellipsis ellipsis
10001 60117

 

select employees.emp_no,salaries.salary from employees
inner join salaries on employees.emp_no = salaries.emp_no where employees.hire_date = salaries.from_date order by employees.emp_no desc;
  • 1
  • 2

 

 

seventh questions:

 

 

Find employee number emp_no with a salary increase of more than 15 times and its corresponding increase times t
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));

nothing

output Description:

emp_no t
10001 17
10004 16
10009 18

 

select salaries.emp_no,count(salaries.salary) as t from salaries group by salaries.emp_no 
having count(salaries.salary)> 15;
  • 1
  • 2

 

 

eighth questions:

 

 

Find out all employees’current salary (to_date =’9999-01-01′) and show it in reverse order for the same salary only once
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));

nothing

output Description:

salary
94692
94409
88958
88070
74057
72527
59755
43311
25828

 

select  salaries.salary from salaries where  to_date='9999-01-01' group by salary order by salary desc;
  • 1

 

 

ninth questions:

 

 

Get the current salary of the current manager for all departments, give dept_no, emp_no, and salary, and the current expression to_date=’9999-01-01′
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));

nothing

output Description:

dept_no emp_no salary
d001 10002 72527
d004 10004 74057
d003 10005 94692
d002 10006 43311
d006 10010 94409

 

select dm.dept_no,dm.emp_no,s.salary
from salaries s, dept_manager dm
where s.emp_no=dm.emp_no and dm.to_date='9999-01-01' and s.to_date='9999-01-01'
  • 1
  • 2
  • 3

 

 

tenth questions:

 

 

Get all non manager employees emp_no
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));

nothing

output Description:

emp_no
10001
10003
10007
10008
10009
10011

 

select emp_no from employees where employees.emp_no not in (
select emp_no from dept_manager
);
  • 1
  • 2
  • 3

 

 

eleventh questions:

 

 

Get the current manager for all employees, and if the current manager is your own, the result is not displayed. The current representation is to_date=’9999-01-01′.
The first column gives the emp_no of the current employee, and the second column gives the manager corresponding manager_no.
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));

nothing

output Description:

emp_no manager_no
10001 10002
10003 10004
10009 10010

 

select dept_emp.emp_no,dept_manager.emp_no as manager_no from dept_emp left join dept_manager 
on dept_emp.dept_no = dept_manager.dept_no where dept_emp.emp_no!=dept_manager.emp_no 
and dept_emp.to_date='9999-01-01' and dept_manager.to_date='9999-01-01';
  • 1
  • 2
  • 3

 

 

twelfth questions:

 

 

Get the information about the current highest salary in all departments and give dept_no, emp_no and their corresponding salary
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));

nothing

output Description:

dept_no emp_no salary
d001 10001 88958
d002 10006 43311
d003 10005 94692
d004 10004 74057
d005 10007 88070
d006 10009 95409

 

select dept_emp.dept_no,dept_emp.emp_no,max(salaries.salary) as salary 
from dept_emp,salaries where dept_emp.emp_no=salaries.emp_no and dept_emp.to_date='9999-01-01'
and salaries.to_date='9999-01-01' group by dept_emp.dept_no;