【分类】:运算符、and 和 or同时使用的优先级

分析思路

select 查询结果 [设备ID,性别,年龄,学校,gpa]
from 从哪张表中查找数据 [user_profile]
where 查询条件 [gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学]
或者
(select 查询结果 [设备ID,性别,年龄,学校,gpa]
from 从哪张表中查找数据 [user_profile]
where 查询条件 [gpa在3.5以上(不包括3.5)的山东大学用户])
union
(select 查询结果 [设备ID,性别,年龄,学校,gpa]
from 从哪张表中查找数据 [user_profile]
where 查询条件 [gpa在3.8以上(不包括3.8)的复旦大学同学])
order by 对查询结果排序 [设备ID]

求解代码

方法一:

使用 and 和 or

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

方法二:

使用 union 后再排序

(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 = '山东大学'))
order by device_id