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');

复制输出:

复制代码
北京大学
复旦大学
浙江大学
山东大学

题解

二种方式

sql 复制代码
distinct 关键字
select distinct university from user_profile
distinct去重,放在列的前面使用。
sql 复制代码
分组
SELECT
university
from user_profile
group by university

以分组来筛选出去重的结果

相关推荐
Hello.Reader11 小时前
Flink SQL INSERT 语句单表写入、多表分流、分区覆盖与 StatementSet
数据库·sql·flink
占疏12 小时前
流程图编辑
java·数据库·sql
坐不住的爱码12 小时前
mybatis-动态sql语句-<foreach>
java·sql·mybatis
Jerry Lau12 小时前
Flink CDC 入门实战:从原理到踩坑全记录 (datastream/SQL 双版本)
大数据·sql·flink
Hello.Reader12 小时前
Flink SQL ANALYZE TABLE手动采集表统计信息,让优化器“更懂数据”
大数据·sql·flink
好好沉淀12 小时前
开发过程中动态 SQL 中where 1=1的作用是什么
java·服务器·开发语言·数据库·sql
小马爱打代码12 小时前
慢SQL:查询、定位分析解决的完整方案
数据库·sql
cyhysr13 小时前
oracle的model子句让sql像excel一样灵活2
sql·oracle·excel
Li.CQ1 天前
SQL学习笔记(二)
笔记·sql·学习
白衣衬衫 两袖清风1 天前
SQL联查案例
数据库·sql