当前位置:网站首页>Niuke question brushing record -- MySQL

Niuke question brushing record -- MySQL

2022-07-23 11:21:00 Cheryl_ Xu

List of articles


One 、 Simple class

1、SQL15 lookup employees surface emp_no And last_name Employee information ---- Odd number 、 For a certain value

#  The job number is odd , Last name is not Mary
where emp_no%2=1 and last_name not like 'Mary'

2、SQL17 Get the salary of the employee with the second highest salary emp_no And the corresponding salary salary----limit 1,1

# limit 1,1: Choose second , Take only 1 individual 
select emp_no,salary
from salaries
where salary = (select distinct salary from salaries order by salary desc limit 1,1)

3、SQL32 take employees Table for all employees last_name and first_name Spliced together as Name----concat

concat function , take 2 Values are spliced together

select concat(last_name,' ',first_name) as Name 
from employees

4、SQL34 Bulk insert data ----insert into

insert into XX surface values (),()
Be careful :values, Use commas between data ,SQL Sentence use ; end .

insert into actor
values(1,'PENELOPE','GUINESS',' 2006-02-15 12:34:33'),(2,'NICK','WAHLBERG','2006-02-15 12:34:33')

5、SQL72 Test scores ( One )----round(x,y)

round(x,y) Aggregate functions , For data x rounding , And keep y Decimal place

select job,round(avg(score),3) as avg
from grade
group by job
order by avg desc

6、SQL77 Course order analysis of Niuke ( One )---- The variable value should be quoted in condition judgment

select *
from order_info
where date>'2025-10-15' and product_name in ('C++','Java','Python') and status='completed'

7、SQL45 take titles_test Change the table name to titles_2017----ALTER

Use ALTER Modify the table name

alter table titles_test rename to titles_2017

8、SQL84 Internship square delivery resume analysis ( One )----having And where The difference between ,>= <=

select job,sum(num) as cnt
from resume_info
 #  You can't use having, It can be used where. You can't use a comparative formula , It can be used and Connect 2 Bar formula .
 #  When select select date, It can be used having.having It doesn't have to be with group Use it with .
where date>='2025-01-01' and date<='2025-12-30' 
group by job
order by cnt desc

9、SQL64 Find everyone's task – There is 2 Table connection ,on The corresponding is different

 Insert picture description here

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

10、SQL43 Will all to_date by 9999-01-01 Update all of to NULL–update set

Update data :update XX surface set xxx=‘’, yyy=‘’
Be careful :null Is a special character , You don't need quotes

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

11、SQL42 Delete emp_no Duplicate records , Keep only the smallest id Corresponding records –delete from

A new generation of a The table is only id Table of , The reason for renaming , Because you cannot search and delete at the same time

DELETE FROM titles_test
WHERE id NOT IN(
    SELECT * FROM(
    SELECT MIN(id)
    FROM titles_test
    GROUP BY emp_no)a);

12、SQL44 take id=5 as well as emp_no=10001 Replace the row data with id=5 as well as emp_no=10005–replace

xxx=replace(xxx, The value of the original , New value )

update titles_test 
set emp_no=replace(emp_no,'10001','10005')
where id='5'

Two 、 Medium class

1、SQL36 Create a actor_name surface

The key to solving the problem is :
Use actor Table data , So available (select …) perhaps as select

Please create a actor_name surface , And will actor All in the table first_name as well as last_name Import this table .
actor_name The table structure is as follows , The title will be queried at the end actor_name Compare the results with the data in the table :

create table actor_name as select first_name,last_name from actor;
#  perhaps 
create table actor_name (select first_name,last_name from actor);

2、SQL29 Use join Search to find movies without classification id And the name

#  Query value is empty , Use  is null, Instead of  =null
where film_category.category_id is null
where film_category.category_id = null 

3、SQL22 Count the salary records of each department

Multi-table query , Be careful group by Which parameter of
It's easy to get wrong :① Should be count, instead of sum;②group by Should be dept_no, instead of salary

select departments.dept_no,departments.dept_name,count(salaries.salary) as sum 
from departments left join dept_emp on departments.dept_no=dept_emp.dept_no left join salaries on dept_emp.emp_no=salaries.emp_no
group by departments.dept_no

4、SQL19 Find all employees last_name and first_name And corresponding dept_name

Usually use left connection

select employees.last_name,employees.first_name,departments.dept_name
from employees left join dept_emp on employees.emp_no=dept_emp.emp_no left join departments on dept_emp.dept_no=departments.dept_no

3、 ... and 、 Basic knowledge records

1、 Delete table -DROP

drop table if exists XX surface 

2、 Field

smallint、datatime

3、ALTER

ALTER TABLE  Table name  ADD  Name / Indexes / Primary key / Foreign keys, etc ;
ALTER TABLE  Table name  DROP  Name / Indexes / Primary key / Foreign keys, etc ;
ALTER TABLE  Table name  ALTER  Only used to change the default value of a column ;
ALTER TABLE  Table name  RENAME  Name / Index name  TO  New column names / The new index ;
ALTER TABLE  Table name  RENAME TO/AS  The new name of the table ;
ALTER TABLE  Table name  MODIFY  The definition of the column, but does not change the column name ;
ALTER TABLE  Table name  CHANGE  Column names and definitions can be changed .
原网站

版权声明
本文为[Cheryl_ Xu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230537331304.html