select device_id
      ,gender
      ,age
      ,gpa
from user_profile
where university='山东大学' 
union all
select device_id
      ,gender
      ,age
      ,gpa
from user_profile
where gender='male';

UNION 和 UNION ALL 都是用于合并两个或多个 SELECT 语句的结果集的操作符,但它们在去重性能上有巨大的区别。

🚀 核心区别一句话总结

  • UNION:合并结果 + 自动去重(慢)。
  • UNION ALL:合并结果 + 保留所有重复行(快)。