力扣sql基础篇(四)
1 每位学生的最高成绩
1.1 题目内容
1.1.1 基本题目信息
1.1.2 示例输入输出
1.2 示例sql语句
SELECT e.student_id,e.course_id,e.grade
FROM
(
SELECT *,rank() over(partition by student_id order by grade desc,course_id asc) rk
FROM Enrollments
)e
WHERE e.rk=1;
1.3 运行截图
2 购买了产品A和产品B却没有购买产品c的顾客
2.1 题目内容
2.1.1 基本题目信息
2.1.2 示例输入输出
2.2 示例sql语句
SELECT customer_id,customer_name
FROM Customers
WHERE customer_id in(
SELECT customer_id
FROM Orders
GROUP BY customer_id
HAVING sum(product_name='A')>=1 AND sum(product_name='B')>=1 AND sum(product_name='c')=0
)
2.3 运行截图
3 计算布尔表达式的值
3.1 题目内容
3.1.1 基本题目信息
3.1.2 示例输入输出
3.2 示例sql语句
SELECT e.left_operand,e.operator,e.right_operand,case
when e.operator='>' AND e.t1>e.t2 then "true"
when e.operator='<' AND e.t1<e.t2 then "true"
when e.operator='=' AND e.t1=e.t2 then "true"
else "false" end value
FROM
(
SELECT left_operand,operator,right_operand,(SELECT value FROM Variables WHERE name=e1.left_operand) t1,(SELECT value FROM Variables WHERE name=e1.right_operand) t2
FROM Expressions e1
)e
3.3 运行截图