【SQL】牛客题霸SQL入门篇1(基础查询、条件查询、高级查询)

简介: 【SQL】牛客题霸SQL入门篇1(基础查询、条件查询、高级查询)

链接

基础查询

SQL1查询多列

select device_id,gender,age,university from user_profile;

SQL2查询所有列

select * from user_profile;

SQL3查询结果去重

select distinct university from user_profile;

SQL4查询结果限制返回行数

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

SQL5将查询后的列重新命名

as后面跟要新的名字

select device_id as user_infos_example 
from user_profile
where id<=2 ;

条件查询

SQL36查找后排序

默认为升序 asc可省略

select device_id,age from user_profile order by age asc;

SQL37查找后多列排序

select device_id,gpa,age from user_profile 
order by gpa asc,age asc;

SQL38查找后降序排列

select device_id,gpa,age from user_profile 
order by gpa desc,age desc;

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

like表示模糊搜素

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

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

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

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

select device_id,gender,age from user_profile where age>=20 and age<=23;

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

select device_id,gender,age,university from user_profile
where university not like '复旦大学';

SQL10用where过滤空值练习

select device_id,gender,age,university from user_profile
where age is not NULL;

SQL11高级操作符练习(1)

男性且GPA在3.5以上(不包括3.5)的用户

select device_id,gender,age,university,gpa
from user_profile
where gender like 'male' and gpa > 3.5 ;

SQL12高级操作符练习(2)

学校为北大或GPA在3.7以上(不包括3.7)的用户

select device_id,gender,age,university,gpa
from user_profile 
where university like '北京大学' or gpa > 3.7;

SQL13Where in 和Not in

学校为北大、复旦和山大的同学

select device_id,gender,age,university,gpa
from user_profile 
where university in ('北京大学','复旦大学','山东大学');

SQL14操作符混合运用

gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学

select device_id,gender,age,university,gpa
from user_profile 
where (university='山东大学' and gpa > 3.5) or (university='复旦大学' and gpa > 3.7);

SQL15查看学校名称中含北京的用户

like表示模糊搜索,%表示任意字符

regexp表示部分匹配

select device_id,age,university
from user_profile 
where university like '%北京%';
select device_id,age,university
from user_profile 
where university regexp '北京';

高级查询

SQL16查找GPA最高值

复旦大学学生gpa最高值

聚合函数max取最大值或是先降序排序然后取第一个数据

select max(gpa) from user_profile where university = '复旦大学';
select gpa from user_profile 
where university = '复旦大学'
order by gpa desc
limit 1;

SQL17计算男生人数以及平均GPA

select 
count(gender) as male_num,
avg(gpa) as avg_gpa
from user_profile
where gender = 'male';
# round(avg(gpa),1)

SQL18分组计算练习题

对每个学校不同性别的用户活跃情况和发帖数量进行分析,请分别计算出每个学校每种性别的用户数、30天内平均活跃天数和平均发帖数量。

select gender,university,
count(gender) as user_num,
avg(active_days_within_30) as avg_active_day,
avg(question_cnt) as avg_question_cnt
from user_profile
group by university,gender;

SQL19分组过滤练习题

请取出平均发贴数低于5的学校或平均回帖数小于20的学校

group by的条件判断要用having

select university,
avg(question_cnt) as avg_question_cnt,
avg(answer_cnt) as avg_answer_cnt
from user_profile
group by university
having avg(question_cnt) < 5 or avg(answer_cnt) < 20;

SQL20分组排序练习题

不同大学的用户平均发帖情况,并期望结果按照平均发帖情况进行升序排列

select university,avg(question_cnt) as  avg_question_cnt
from user_profile 
group by university
order by avg_question_cnt asc;
目录
相关文章
|
1月前
|
SQL 安全 数据库
如何在Django中正确使用参数化查询或ORM来避免SQL注入漏洞?
如何在Django中正确使用参数化查询或ORM来避免SQL注入漏洞?
141 77
|
26天前
|
SQL NoSQL Java
Java使用sql查询mongodb
通过MongoDB Atlas Data Lake或Apache Drill,可以在Java中使用SQL语法查询MongoDB数据。这两种方法都需要适当的配置和依赖库的支持。希望本文提供的示例和说明能够帮助开发者实现这一目标。
45 17
|
21天前
|
SQL Oracle 关系型数据库
如何在 Oracle 中配置和使用 SQL Profiles 来优化查询性能?
在 Oracle 数据库中,SQL Profiles 是优化查询性能的工具,通过提供额外统计信息帮助生成更有效的执行计划。配置和使用步骤包括:1. 启用自动 SQL 调优;2. 手动创建 SQL Profile,涉及收集、执行调优任务、查看报告及应用建议;3. 验证效果;4. 使用 `DBA_SQL_PROFILES` 视图管理 Profile。
|
30天前
|
SQL 存储 机器学习/深度学习
如何让SQL速度飞起来 入门YashanDB优化器
优化器,SQL引擎的核心组成部分,是数据库中用于把关系表达式转换成最优执行计划的核心组件,影响数据库系统执行性能的关键组件之一。
32 15
|
28天前
|
SQL Java 数据库连接
【潜意识Java】MyBatis中的动态SQL灵活、高效的数据库查询以及深度总结
本文详细介绍了MyBatis中的动态SQL功能,涵盖其背景、应用场景及实现方式。
91 6
|
2月前
|
SQL 存储 人工智能
Vanna:开源 AI 检索生成框架,自动生成精确的 SQL 查询
Vanna 是一个开源的 Python RAG(Retrieval-Augmented Generation)框架,能够基于大型语言模型(LLMs)为数据库生成精确的 SQL 查询。Vanna 支持多种 LLMs、向量数据库和 SQL 数据库,提供高准确性查询,同时确保数据库内容安全私密,不外泄。
379 7
Vanna:开源 AI 检索生成框架,自动生成精确的 SQL 查询
|
2月前
|
SQL NoSQL Java
Java使用sql查询mongodb
通过使用 MongoDB Connector for BI 和 JDBC,开发者可以在 Java 中使用 SQL 语法查询 MongoDB 数据库。这种方法对于熟悉 SQL 的团队非常有帮助,能够快速实现对 MongoDB 数据的操作。同时,也需要注意到这种方法的性能和功能限制,根据具体应用场景进行选择和优化。
109 9
|
3月前
|
SQL Java
使用java在未知表字段情况下通过sql查询信息
使用java在未知表字段情况下通过sql查询信息
54 8
|
3月前
|
SQL 安全 PHP
PHP开发中防止SQL注入的方法,包括使用参数化查询、对用户输入进行过滤和验证、使用安全的框架和库等,旨在帮助开发者有效应对SQL注入这一常见安全威胁,保障应用安全
本文深入探讨了PHP开发中防止SQL注入的方法,包括使用参数化查询、对用户输入进行过滤和验证、使用安全的框架和库等,旨在帮助开发者有效应对SQL注入这一常见安全威胁,保障应用安全。
106 4
|
3月前
|
SQL 监控 关系型数据库
SQL语句当前及历史信息查询-performance schema的使用
本文介绍了如何使用MySQL的Performance Schema来获取SQL语句的当前和历史执行信息。Performance Schema默认在MySQL 8.0中启用,可以通过查询相关表来获取详细的SQL执行信息,包括当前执行的SQL、历史执行记录和统计汇总信息,从而快速定位和解决性能瓶颈。
128 1