SQL入门
select * from table;
SQL实战题目
- 有下面一个表 t ,存储了每个商品类别的成交明细,我们需要通过下面这张表获取订单量大于10对应的类别,并从中取出订单量前3的商品类别,会有一些测试的订单(id=xxx的为测试),我们需要过滤掉。
id | order_id |
---|---|
c1 | 1 |
c1 | 2 |
c1 | 3 |
c2 | 4 |
c2 | 5 |
c3 | 6 |
... | ... |
c100 | 10000 |
为了满足需求,SQL可以这样子写:
select
id,
count(order_id) as sales
from
t
where id <> "xxx"
group by
id
having
count(order_id) > 10
order by
count(order_id) desc
limit 3;
代码中涉及SQL关键词:select
、from
、where
、group by
、having
、order by
、limit
执行优先顺序:from
- where
- group by
- having
- select
- order by
- limit