当前位置:网站首页>SQL每日一练(牛客新题库)——第4天:高级操作符
SQL每日一练(牛客新题库)——第4天:高级操作符
2022-07-28 12:33:00 【无 羡ღ】
1. 高级操作符练习(2)
题目:现在运营想要找到学校为北大或GPA在3.7以上(不包括3.7)的用户进行调研,请你取出相关数据(使用OR实现)
建表语句:
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,
`gpa` float);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing',3.4);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai',4.0);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing',3.2);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang',3.6);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong',3.8);
解题答案:
SELECT
device_id,
gender,
age,
university,
gpa
FROM
user_profile
WHERE
gpa > '3.7'
OR
university = '北京大学'

2. Where in 和Not in
题目:现在运营想要找到学校为北大、复旦和山大的同学进行调研,请你取出相关数据。
建表语句:
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,
`gpa` float);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing',3.4);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai',4.0);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing',3.2);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang',3.6);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong',3.8);
解题答案:
select device_id,gender,age,university,gpa
from user_profile
WHERE university="北京大学" or university="复旦大学" or university="山东大学"

3. 操作符混合运用
题目:现在运营想要找到gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学进行用户调研,请你取出相应数据

建表语句:
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,
`gpa` float);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing',3.4);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai',4.0);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing',3.2);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang',3.6);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong',3.8);
解题答案:
SELECT device_id, gender, age, university,gpa from user_profile where gpa > 3.8 and university = '复旦大学' UNION
SELECT device_id, gender, age, university,gpa from user_profile where gpa > 3.5 and university = '山东大学'

4. 查看学校名称中含北京的用户
题目:现在运营想查看所有大学中带有北京的用户的信息,请你取出相应数据。
建表语句:
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,
`gpa` float);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8);
INSERT INTO user_profile VALUES(6,2131,'male',28,'北京师范大学',3.3);
解题答案:
select device_id,age,university
from user_profile
-- where university like '%北京%';
WHERE university regexp "北京";
-- 正则表达式用来匹配文本的特殊的串(字符集合)(匹配文本,将一个模式(正则表达式)与一个文本串进行比较)。
-- LIKE 和 REGEXP之间的重要差别
-- LIKE 匹配整个列,如果被匹配的文本在列值中出现,LIKE 将不会找到它,相应的行也不会被返回(除非使用通配符)。而 REGEXP 在列值内进行匹配,如果被匹配的文本在列值中出现,REGEXP 将会找到它,相应的行将被返回,并且 REGEXP 能匹配整个列值(与 LIKE 相同的作用)。

5. 如何让刷题变得更高效?
最近很多学了基础的小伙伴问我该怎么提升编程水平?学了基础该上哪刷题?明明学了很多,做项目却不知道怎么上手,其实这就是练得太少,只注重了学,却忽视了刷题,只有不断练习才能提高和巩固编程思维和能力!
链接地址:牛客网 | SQL刷题篇,废话少说速度上号!!!
边栏推荐
- Change password, confirm password verification antd
- C语言:优化后的归并排序
- Realize the mutual value transfer between main window and sub window in WPF
- Leetcode-136. numbers that appear only once
- [C language] the difference between structure pointer and structure variable as formal parameters
- vim常用命令详解(vim使用教程)
- 基于深度学习的超分辨率重建
- nport串口服务器配置网址(串口服务器是不是网口转串口)
- 如何配置adb环境变量(环境变量在哪打开)
- I miss the year of "losing" Li Ziqi
猜你喜欢

Risk analysis of option trading

Beyond Istio OSS——Istio服务网格的现状与未来

Change password, confirm password verification antd

夜神模拟器抓包微信小程序

Customized template in wechat applet

管理区解耦架构见过吗?能帮客户搞定大难题的

今日睡眠质量记录75分

Can second uncle cure young people's spiritual internal friction?

org.apache.ibatis.exceptions.TooManyResultsException的异常排查过程

我抄底了被清算的NFT,却被OpenSea上了锁
随机推荐
《暗黑破坏神4》PS4/PS5测试版已加入PlayStation数据库
IP电话系统和VoIP系统使用指南
【C语言】结构体指针与结构体变量作形参的区别
Using auto.js to realize fifaol3 brush teaching assistant
Is azvudine, a domestic oral new coronal drug, safe? Expert authority interpretation
Leetcode 笔记 118. 杨辉三角
Getderivedstatefromprops lifecycle
Protective bearish strategy
Intra prediction and transform kernel selection based on Neural Network
验证码暴力破解测试[通俗易懂]
Black Scholes Merton European option pricing formula
二舅能治好年轻人的精神内耗吗?
vim常用命令详解(vim使用教程)
Use non recursive method to realize layer traversal, preorder traversal, middle order traversal and post order traversal in binary tree
.NET的求复杂类型集合的差集、交集、并集
Beyond istio OSS -- current situation and future of istio Service Grid
基于神经网络的帧内预测和变换核选择
PHP generates random numbers (nickname random generator)
Gamestop bear market entered NFT trading, and established game retailers took advantage of Web3 to make a second spring
What is the optimization method of transaction and database
