当前位置:网站首页>SQL daily exercise - niuke.com

SQL daily exercise - niuke.com

2022-06-13 12:33:00 EbowTang

The purpose of this article is :

It's about being right SQL After systematic learning , Conduct :
1, routine , To consolidate ;
2, To deepen the SQL Knowledge system ;
3, summary SQL Related knowledge ;
4, Or one day I can pick up the relevant items quickly SQL knowledge .
Long term update and summary ...... No deadline

Catalog

The purpose of this article is :

SQL1  Query all columns

Example 1

SQL2  Query multiple columns

Example 1

SQL3  Query result de duplication

Example 1

SQL4  Query results limit the number of rows returned

Example 1


The following are all exercises of niuke.com -mysql Database test results :

SQL1  Query all columns

subject : Now the operator wants to view all the data in the user information table , Please take out the corresponding results

Example :user_profile

iddevice_idgenderageuniversityprovince
12138male21 Peking University, Beijing
23214male Fudan University Shanghai
36543female20 Peking University, Beijing
42315female23 Zhejiang University ZheJiang
55432male25 Shandong University Shandong

According to the example , Your query should return the following results :

iddevice_idgenderageuniversityprovince
12138male21 Peking University, Beijing
23214male Fudan University Shanghai
36543female20 Peking University, Beijing
42315female23 Zhejiang University Zhejiang
55432male25 Shandong University Shandong

Example 1

Input :

drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`province` varchar(32)  NOT NULL);
INSERT INTO user_profile VALUES(1,2138,'male',21,' Peking University, ','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,' Fudan University ','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,' Peking University, ','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,' Zhejiang University ','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,' Shandong University ','Shandong');

Copy output :

1|2138|male|21| Peking University, |BeiJing
2|3214|male|None| Fudan University |Shanghai
3|6543|female|20| Peking University, |BeiJing
4|2315|female|23| Zhejiang University |ZheJiang
5|5432|male|25| Shandong University |Shandong
select * from user_profile;

 perhaps 

select 
  id,device_id,
  gender,
  age,
  university,province
from user_profile;

SQL2  Query multiple columns

subject : Now, the students want to operate the user's equipment id The corresponding gender 、 Data on age and school , Please take out the corresponding data

Example :user_profile

iddevice_idgenderageuniversityprovince
12138male21 Peking University, Beijing
23214male Fudan University Shanghai
36543female20 Peking University, Beijing
42315female23 Zhejiang University Zhejiang
55432male25 Shandong University Shandong

According to the example , Your query should return the following results

device_idgenderageuniversity
2138male21 Peking University,
3214male Fudan University
6543female20 Peking University,
2315female23 Zhejiang University
5432male25 Shandong University

Example 1

Input :

drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`province` varchar(32)  NOT NULL);
INSERT INTO user_profile VALUES(1,2138,'male',21,' Peking University, ','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,' Fudan University ','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,' Peking University, ','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,' Zhejiang University ','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,' Shandong University ','Shandong');

Copy output :

2138|male|21| Peking University, 
3214|male|None| Fudan University 
6543|female|20| Peking University, 
2315|female|23| Zhejiang University 
5432|male|25| Shandong University 
select device_id,gender,age,university 
from user_profile;

SQL3  Query result de duplication

subject : Now the operation needs to check which schools users come from , Please take the school's de duplication data from the user information table .

Example :user_profile

iddevice_idgenderageuniversityprovince
12138male21 Peking University, Beijing
23214male Fudan University Shanghai
36543female20 Peking University, Beijing
42315female23 Zhejiang University ZheJiang
55432male25 Shandong University Shandong

According to the example , Your query should return the following results :

university
Peking University,
Fudan University
Zhejiang University
Shandong University

Example 1

Input :

drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`province` varchar(32)  NOT NULL);
INSERT INTO user_profile VALUES(1,2138,'male',21,' Peking University, ','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,' Fudan University ','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,' Peking University, ','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,' Zhejiang University ','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,' Shandong University ','Shandong');

Copy output :

 Peking University, 
 Fudan University 
 Zhejiang University 
 Shandong University 
select distinct university
from user_profile;
 perhaps 
SELECT university 
from user_profile 
GROUP BY university;

SQL4  Query results limit the number of rows returned

subject : Now the operation only needs to check the front 2 User details equipment ID data , Please read the user information form  user_profile Take out the corresponding results .

Example :

iddevice_idgenderageuniversityprovince
12138male21 Peking University, Beijing
23214male Fudan University Shanghai
36543female20 Peking University, Beijing
42315female23 Zhejiang University ZheJiang
55432male25 Shandong University Shandong

According to input , Your query should return the following results :

device_id
2138
3214

Example 1

Input :

drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`province` varchar(32)  NOT NULL);
INSERT INTO user_profile VALUES(1,2138,'male',21,' Peking University, ','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,' Fudan University ','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,' Peking University, ','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,' Zhejiang University ','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,' Shandong University ','Shandong');

Copy output :

2138
3214
select device_id from user_profile limit 2;
select device_id from user_profile limit 0,2;
select device_id from user_profile limit 2 offset 0;
select device_id from user_profile where id in(1,2);
select device_id from user_profile where id <=2;
select device_id from user_profile where id=1 or id=2;

原网站

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