Mysql练习1

来源:牛客网https://www.nowcoder.com/activity/oj?tab=1

SQL1 查询多列 (简单)

描述

题目:现在运营同学想要用户的设备id对应的性别、年龄和学校的数据,请你取出相应数据
 
示例:user_profile
根据示例,你的查询应返回以下结果

示例1

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,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

解答:

完成时间:  2021-12-15  13:09:30

select device_id,gender,age,university from user_profile;

 

SQL2 查询所有列 (简单)

描述

题目:现在运营想要查看用户信息表中所有的数据,请你取出相应结果
 
示例:user_profile
 
根据示例,你的查询应返回以下结果:

示例1

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,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

解答:

完成时间:  2021-12-15 13:12:34

答案1:

select * from user_profile;

答案2:

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

 

SQL3 查询结果去重

描述

题目:现在运营需要查看用户来自于哪些学校,请从用户信息表中取出学校的去重数据。
 
示例:user_profile
 
根据示例,你的查询应返回以下结果:

示例1

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,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

答案:

完成时间:2021-12-15  13:25:05

select DISTINCT university from user_profile;

 

SQL4 查询结果限制返回行数

描述

题目:现在运营只需要查看前2个用户明细设备ID数据,请你从用户信息表 user_profile 中取出相应结果。
 
示例:
 
根据输入,你的查询应返回以下结果:

示例1

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,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

答案:

完成时间:2021-12-15   13:28:33

 答案1:

LIMIT 0,2表示从0开始,取2个,默认从0开始,所以0可以省略
select device_id from user_profile LIMIT 0,2; 

答案2:

select device_id from user_profile LIMIT 2; 

 

SQL5 将查询后的列重新命名

描述

题目:现在你需要查看前2个用户明细设备ID数据,并将列名改为 'user_infos_example',,请你从用户信息表取出相应结果。
 
示例:user_profile
 
根据示例,你的查询应返回以下结果:

示例1

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,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

答案:

完成时间:2021-12-15   13:29:43

 答案1:

select device_id as user_infos_example from user_profile limit 0,2;

答案2:

as可省略

select device_id  user_infos_example from user_profile limit 2;

 

 

SQL6 查找学校是北大的学生信息

描述

题目:现在运营想要筛选出所有北京大学的学生进行用户调研,请你从用户信息表中取出满足条件的数据,结果返回设备id和学校。
 
示例:user_profile
 
根据示例,你的查询应返回以下结果:

示例1

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,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

答案:

完成时间:2021-12-16    12:32:40

 

select device_id,university from user_profile where university ='北京大学';

 

SQL7 查找年龄大于24岁的用户信息

描述

题目:现在运营想要针对24岁以上的用户开展分析,请你取出满足条件的设备ID、性别、年龄、学校。
 
 
用户信息表:user_profile
 
 
根据输入,你的 查询应返回以下结果:

示例1

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,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

 

答案:

完成时间:2021-12-16   12:38:10  

select device_id,gender,age,university from user_profile where age > 24;

 

SQL8 查找某个年龄段的用户信息

描述

题目:现在运营想要针对20岁及以上且23岁及以下的用户开展分析,请你取出满足条件的设备ID、性别、年龄。
用户信息表:user_profile
 
根据输入,你的查询应返回以下结果:

示例1

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,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

 

答案:

完成时间:2021-12-16   12:41:31

 

-- 答案1:between a and b ,范围为a到b之间的所有整数,包含a和b 
select device_id,gender,age from user_profile where age between 20 and 23;

--答案2:
select device_id,gender,age from user_profile where age >= 20 and age<=23;

 

SQL9 查找除复旦大学的用户信息

描述

题目:现在运营想要查看除复旦大学以外的所有用户明细,请你取出相应数据
 
示例:user_profile
 
根据输入,你的查询应返回以下结果:

示例1

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,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

 

答案:

完成时间:2021-12-16   12:41:41

select device_id,gender,age,university from user_profile where university != '复旦大学';

 

SQL10 用where过滤空值练习

描述

题目:现在运营想要对用户的年龄分布开展分析,在分析时想要剔除没有获取到年龄的用户,请你取出所有年龄值不为空的用户的设备ID,性别,年龄,学校的信息。
 
示例:user_profile
 
根据输入,你的 查询应返回以下结果:

示例1

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,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

 

答案:

完成时间:2021-12-16  12:48:24

-- 在mysql中,查询某字段为NULL时,切记不可用 = null,而是 is null,不为空则是 is not null,判断字符串为空字符串时用 = '',
select device_id,gender,age,university from user_profile where age is not NULL;

 

posted @ 2021-12-15 13:13  惜阙  阅读(501)  评论(0)    收藏  举报