MySQL Practice Questions 9|| Simple Article No. 3 (including 6 questions, difficulty: simple)

Introduction: The title comes from the online programming SQL practice of Niuke. The content of the article is mainly to explain the blogger's ideas, relevant insights and summary of the main points when doing the questions. In general, it's easy to fix clear bugs, hard to find hidden bugs, and there is a long way to go. I will search for it!

Question 1 (Difficulty: Easy)

Assuming that there is a table titles_test in the test database, the data in the table is as follows:

Writing requirements:

Delete the duplicate records of emp_no, and keep only the record corresponding to the smallest id. After deletion, the titles_test table is (Note: Finally, * from the titles_test table will be selected to compare the results)

Problem solving ideas:

Problem solving code:

delete from titles_test
where id not in (
    select * from (
    select min(id) from titles_test
    group by emp_no
    ) as table1 );

Comparison summary:

  • delete syntax: delete from table name [ where filter condition ] [ limit m, n ]
  • In MySQL ( note that in MySQL, mssql and oracle will not make mistakes ), it is not allowed to select and query data while updating and deleting data in the delete table according to conditions. In other words, you cannot delete while checking.
    Otherwise, an error will be reported: SQL_ERROR_INFO: "You can't specify target table 'titles_test' for update in FROM clause"

Question 2 (Difficulty: Easy) 

Assuming that there is a table titles_test in the test database, the data in the table is as follows:

Writing requirements:

Update all to_date to 9999-01-01 to NULL, and from_date to 2001-01-01. After the update, the values ​​of the titles_test table are as follows:

Problem solving code:

update titles_test
set to_date=null,from_date='2001-01-01'
where to_date='9999-01-01';

Comparison summary:

  • Update usage: update table name set field = value, field = value [ where filter condition];

Question 3 (Difficulty: Easy) 

Assuming that there is a table titles_test in the test database, the data in the table is as follows:

Writing requirements:

Replace the row data of id=5 and emp_no=10001 with id=5 and emp_no=10005, other data remain unchanged, use replace to implement, and use update directly will report an error . After the update, the values ​​of the titles_test table are as follows:

Problem solving code:

UPDATE titles_test
set emp_no=replace(emp_no,10001,10005)
where id=5;

Comparison summary:

  • Replace function usage: replace(string,from_str,to_str) That is: replace all occurrences of from_str in string with to_str
  • In the question, set emp_no=replace(emp_no, 10001, 10005) replaces 10001 in emp_no with 10005. Due to the conditional restriction of where id=5, only the content of emp_no with id=5 is modified

Question 4 (Difficulty: Easy) 

Assuming that there is a table titles_test in the test database, the data in the table is as follows:

Writing requirements:

Change the titles_test table name to titles_2017. The table is used after the update as follows:

Problem solving code:

alter table titles_test rename to titles_2017;

Comparison summary:

  • alter table table name rename to/as new table name;
  • Other modification syntax:

Question 5 (Difficulty: Easy) 

The little friends who brush the questions in the Niu Ke have Niu Ke points. The grade table can be simplified as follows: (Explanation: id is the user's primary key id, and number represents the score)

Writing requirements:

Write a sql query, three or more points appear in the points table, and the query results are as follows:

Note: If there are multiple numbers that meet the conditions, the output will be sorted in ascending order of numbers.

Problem solving ideas:

Problem solving code:

# 解法一:
select number
from (select number,count(*) as cnt from grade group by number) as table1
where cnt>=3

# 解法二:
select number from grade
group by number
having count(*)>=3

Comparison summary:

  • where is to filter before grouping, having is to filter after grouping
  • where+subquery can replace having effect

Question 6 (Difficulty: Easy) 

There is a person table, the primary key is id, as follows:

There is a task table as follows, the primary key is also id, as follows:

Writing requirements:

Find the task status of each person, and output it. If there is no task, it will also be output, and the output results are sorted in ascending order by the person's id. The output is as follows:

Problem solving ideas:

Problem solving code:

select person.id, person.name, task.content
from person left join task
on person.id = task.person_id
order by person.id;

Comparison summary:

  • Mainly examine the connection of two tables, pay attention to the difference between the main table

Guess you like

Origin blog.csdn.net/Inochigohan/article/details/122639345