当前位置:网站首页>SQL日常练习-牛客网
SQL日常练习-牛客网
2022-06-13 12:27:00 【EbowTang】
本文章目的:
在于对SQL系统化学习后,进行:
1,日常练习,巩固;
2,加深对SQL知识体系;
3,总结SQL相关知识;
4,或者某有朝一日能快速捡起相关SQL知识。
长期更新和总结。。。。。。无截止时间
目录
以下均是牛客网练习题-mysql数据库测试结果:
SQL1 查询所有列
题目:现在运营想要查看用户信息表中所有的数据,请你取出相应结果
示例:user_profile
| id | device_id | gender | age | university | province |
| 1 | 2138 | male | 21 | 北京大学 | Beijing |
| 2 | 3214 | male | 复旦大学 | Shanghai | |
| 3 | 6543 | female | 20 | 北京大学 | Beijing |
| 4 | 2315 | female | 23 | 浙江大学 | ZheJiang |
| 5 | 5432 | male | 25 | 山东大学 | Shandong |
根据示例,你的查询应返回以下结果:
| id | device_id | gender | age | university | province |
| 1 | 2138 | male | 21 | 北京大学 | Beijing |
| 2 | 3214 | male | 复旦大学 | Shanghai | |
| 3 | 6543 | female | 20 | 北京大学 | Beijing |
| 4 | 2315 | female | 23 | 浙江大学 | Zhejiang |
| 5 | 5432 | male | 25 | 山东大学 | Shandong |
示例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');
复制输出:
1|2138|male|21|北京大学|BeiJing 2|3214|male|None|复旦大学|Shanghai 3|6543|female|20|北京大学|BeiJing 4|2315|female|23|浙江大学|ZheJiang 5|5432|male|25|山东大学|Shandong
select * from user_profile;
或者
select
id,device_id,
gender,
age,
university,province
from user_profile;SQL2 查询多列
题目:现在运营同学想要用户的设备id对应的性别、年龄和学校的数据,请你取出相应数据
示例:user_profile
| id | device_id | gender | age | university | province |
| 1 | 2138 | male | 21 | 北京大学 | Beijing |
| 2 | 3214 | male | 复旦大学 | Shanghai | |
| 3 | 6543 | female | 20 | 北京大学 | Beijing |
| 4 | 2315 | female | 23 | 浙江大学 | Zhejiang |
| 5 | 5432 | male | 25 | 山东大学 | Shandong |
根据示例,你的查询应返回以下结果
| device_id | gender | age | university |
| 2138 | male | 21 | 北京大学 |
| 3214 | male | 复旦大学 | |
| 6543 | female | 20 | 北京大学 |
| 2315 | female | 23 | 浙江大学 |
| 5432 | male | 25 | 山东大学 |
示例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');
复制输出:
2138|male|21|北京大学 3214|male|None|复旦大学 6543|female|20|北京大学 2315|female|23|浙江大学 5432|male|25|山东大学
select device_id,gender,age,university
from user_profile;SQL3 查询结果去重
题目:现在运营需要查看用户来自于哪些学校,请从用户信息表中取出学校的去重数据。
示例:user_profile
| id | device_id | gender | age | university | province |
| 1 | 2138 | male | 21 | 北京大学 | Beijing |
| 2 | 3214 | male | 复旦大学 | Shanghai | |
| 3 | 6543 | female | 20 | 北京大学 | Beijing |
| 4 | 2315 | female | 23 | 浙江大学 | ZheJiang |
| 5 | 5432 | male | 25 | 山东大学 | Shandong |
根据示例,你的查询应返回以下结果:
| university |
| 北京大学 |
| 复旦大学 |
| 浙江大学 |
| 山东大学 |
示例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');
复制输出:
北京大学 复旦大学 浙江大学 山东大学
select distinct university
from user_profile;
或者
SELECT university
from user_profile
GROUP BY university;SQL4 查询结果限制返回行数
题目:现在运营只需要查看前2个用户明细设备ID数据,请你从用户信息表 user_profile 中取出相应结果。
示例:
| id | device_id | gender | age | university | province |
| 1 | 2138 | male | 21 | 北京大学 | Beijing |
| 2 | 3214 | male | 复旦大学 | Shanghai | |
| 3 | 6543 | female | 20 | 北京大学 | Beijing |
| 4 | 2315 | female | 23 | 浙江大学 | ZheJiang |
| 5 | 5432 | male | 25 | 山东大学 | Shandong |
根据输入,你的查询应返回以下结果:
| device_id |
| 2138 |
| 3214 |
示例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');
复制输出:
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;边栏推荐
- Opencv learning notes (II): reading MNIST datasets
- Selenium3 automatic test practice (5)
- 基于SqlSugar的开发框架循序渐进介绍(7)-- 在文件上传模块中采用选项模式【Options】处理常规上传和FTP文件上传
- Cvpr2022 | a convnet for the 2020s & how to design neural network Summary
- 10. DCN introduction
- Based on STM32F103 - as608 fingerprint module + serial port printing
- 北京市场监管局启动9类重点产品质量专项整治工作
- 想发自己的NFT,你要先搞清楚这6个问题
- Cube 技术解读 | Cube 渲染设计的前世今生
- Lucene from introduction to practice
猜你喜欢

11. PCA introduction

浅谈常见的web攻击以及如何防范

If you want to send your own NFT, you should first understand these six questions

2022年二建《建筑》参考答案汇总

Max in MySQL_ Connections and Max_ user_ Connections usage differences

机器学习服务助应用内文本语种在线和离线检测

Based on STM32F103 - as608 fingerprint module + serial port printing

我和指针那些事——初识指针
![[truth] the reason why big factories are not afraid to spend money is...](/img/db/6d40acbb0a28e9ead029f29e0ec0b3.png)
[truth] the reason why big factories are not afraid to spend money is...

Interview shock 56: what is the difference between clustered index and non clustered index?
随机推荐
云安全服务的选择
002、torchserve调用官方库模型
QT的多线程详解(包含案例)
一文说清楚ToB SaaS系统的权限管理的设计
redis实操-查询数据库信息存入redis对比两次差异
Selenium3自动化测试实战(5)
Pulsar consumer
11. PCA introduction
5 locksupport and thread interruption
我想转行程序员,上个编程培训班,能找到工作吗?我可以自学吗?
Machine learning (III) - LDA (linear discriminant analysis) theory and code explanation
基于三维GIS技术的行业发展及研究现状
selenium实操-自动化登录
Analysis of DuPont analysis method: financial analysis of the New Retail Group Co., Ltd
Max in MySQL_ Connections and Max_ user_ Connections usage differences
The answer to the subject of "Regulations" of the second construction company in 2022 has been provided. Please keep it
Lucene从入门到实战
What if the second construction fails to pass the post qualification examination? This article tells you
Camunda timer events example demo (timer events)
M1 experience win11