SQL语法学习(1)

一、查找大国

如果一个国家满足下述两个条件之一,则认为该国是 大国 :

  • 面积至少为 300 万平方公里(即,3000000 km2),或者
  • 人口至少为 2500 万(即 25000000

编写一个 SQL 查询以报告 大国 的国家名称、人口和面积。

输入:
World 表:
+-------------+-----------+---------+------------+--------------+
| name        | continent | area    | population | gdp          |
+-------------+-----------+---------+------------+--------------+
| Afghanistan | Asia      | 652230  | 25500100   | 20343000000  |
| Albania     | Europe    | 28748   | 2831741    | 12960000000  |
| Algeria     | Africa    | 2381741 | 37100000   | 188681000000 |
| Andorra     | Europe    | 468     | 78115      | 3712000000   |
| Angola      | Africa    | 1246700 | 20609294   | 100990000000 |
+-------------+-----------+---------+------------+--------------+
输出:
+-------------+------------+---------+
| name        | population | area    |
+-------------+------------+---------+
| Afghanistan | 25500100   | 652230  |
| Algeria     | 37100000   | 2381741 |
+-------------+------------+---------+

答案:

(1)使用 WHERE 子句和 OR

select  name, population, area
from world
where area >= 3000000 or population >= 25000000
(2)使用 WHERE 子句和 UNION(使用 or 会使索引会失效,在数据量较大的时候查找效率较低,通常建议使用 union 代替 or。)

select     name, population, area
from world
where area >= 3000000

union

select name, population, area
from  world
where    population >= 25000000

---------------------------------------------------------------------------------------------------------------------------------

二、寻找用户推荐人

写一个查询语句,返回一个客户列表,列表中客户的推荐人的编号都 不是 2

输入:

customer 表:

+------+------+-----------+
| id   | name | referee_id|
+------+------+-----------+
|    1 | Will |      NULL |
|    2 | Jane |      NULL |
|    3 | Alex |         2 |
|    4 | Bill |      NULL |
|    5 | Zack |         1 |
|    6 | Mark |         2 |
+------+------+-----------+

输出:

+------+
| name |
+------+
| Will |
| Jane |
| Bill |
| Zack |
+------+

答案:

MySQL 使用三值逻辑 —— TRUE, FALSE 和 UNKNOWN。任何与 NULL 值进行的比较都会与第三种值 UNKNOWN 做比较。这个“任何值”包括 NULL 本身!这就是为什么 MySQL 提供 IS NULL 和 IS NOT NULL 两种操作来对 NULL 特殊判断。

因此,在 WHERE 语句中我们需要做一个额外的条件判断 `referee_id IS NULL'。

select name

from customer

where referee_id <> 2 or referee_id is null

---------------------------------------------------------------------------------------------------------------------------------

三 从不订购的客户

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

输入:

Customers 表:                                                

+----+-------+                           
| Id | Name  |                  
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+
orders 表
+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

输出:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+

答案:

select name as Customers

from Customers c

where  c.id not in

(

    select CustomerId from Orders

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值