使用SQL对数据库进行查询操作

  • 查询语句 Select
  • 习题练习


查询语句 Select

使用select语句可以实现对数据库的查询操作,为高频语句。

select *   // 查询全部
from table   

select name //查询表中某个字段,这里代表查询学生表中姓名。
from  student

习题练习

—— 题目来源于Leetcode,建议自己去刷一下。

  1. 可回收且低脂的产品
±------------±--------+
 | Column Name | Type |
 ±------------±--------+
 | product_id | int |
 | low_fats | enum |
 | recyclable | enum |
 ±------------±--------+
 product_id 是这个表的主键。 low_fats 是枚举类型,取值为以下两种 (‘Y’, ‘N’),其中 ‘Y’ 表示该产品是低脂产品,‘N’ 表示不是低脂产品。 recyclable 是枚举类型,取值为以下两种
 (‘Y’, ‘N’),其中 ‘Y’ 表示该产品可回收,而 ‘N’ 表示不可回收。写出 SQL 语句,查找既是低脂又是可回收的产品编号。
 返回结果 无顺序要求 。 查询结果格式如下例所示: Products 表:
 ±------------±---------±-----------+
 | product_id | low_fats | recyclable |
 ±------------±---------±-----------+
 | 0 | Y | N |
 | 1 | Y | Y |
 | 2 | N |Y |
 | 3 | Y | Y |
 | 4 | N | N |
 ±------------±---------±-----------+
 Result 表:
 ±------------+
 | product_id |
 ±------------+
 | 1 |
 | 3 |
 ±------------+
 只有产品 id 为 1 和 3 的产品,既是低脂又是可回收的产品。

sql语句:

select product_id
from Products
where low_fats='Y' and recyclable='Y';

简单题,不做分析。

  1. 寻找用户推荐人
给定表 customer ,里面保存了所有客户信息和他们的推荐人。
 ±-----±-----±----------+
 | id | name | referee_id|
 ±-----±-----±----------+
 | 1 | Will | NULL |
 | 2 | Jane | NULL |
 | 3 | Alex | 2 |
 | 4 | Bill | NULL |
 | 5 | Zack | 1 |
 | 6 | Mark | 2 |
 ±-----±-----±----------+写一个查询语句,返回一个客户列表,列表中客户的推荐人的编号都 不是 2。
 对于上面的示例数据,结果为:
 ±-----+
 | name |
 ±-----+
 | Will |
 | Jane |
 | Bill |
 | Zack |
 ±-----+

sql语句:

select name
from customer
where referee_id != "2" or referee_id is null ;

简单题,不做分析。

  1. 大的国家
World 表:
±------------±--------+
 | Column Name | Type |
 ±------------±--------+
 | name | varchar |
 | continent | varchar |
 | area | int |
 | population | int |
 | gdp | bigint |
 ±------------±--------+
 name 是这张表的主键。 这张表的每一行提供:国家名称、所属大陆、面积、人口和 GDP 值。
 如果一个国家满足下述两个条件之一,则认为该国是 大国 :
 面积至少为 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 |
 ±------------±-----------±--------+

sql语句:

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

简单题。注意where语句时运算符的优先级。

  1. 文章浏览①
Views 表:
±--------------±--------+
 | Column Name | Type |
 ±--------------±--------+
 | article_id | int |
 | author_id | int |
 | viewer_id | int |
 | view_date | date |
 ±--------------±--------+
 此表无主键,因此可能会存在重复行。 此表的每一行都表示某人在某天浏览了某位作者的某篇文章。 请注意,同一人的 author_id 和 viewer_id 是相同的。请编写一条 SQL 查询以找出所有浏览过自己文章的作者,结果按照 id 升序排列。
 查询结果的格式如下所示:
 Views 表:
 ±-----------±----------±----------±-----------+
 | article_id | author_id | viewer_id | view_date |
 ±-----------±----------±----------±-----------+
 | 1 | 3 | 5 | 2019-08-01 |
 | 1 | 3 | 6 |2019-08-02 |
 | 2 | 7 | 7 | 2019-08-01 |
 | 2 | 7 | 6 | 2019-08-02 |
 | 4 | 7 | 1 | 2019-07-22 |
 | 3 | 4 | 4 | 2019-07-21 |
 | 3 | 4 | 4 | 2019-07-21 |
 ±-----------±----------±----------±-----------+结果表:
 ±-----+
 | id |
 ±-----+
 | 4 |
 | 7 |
 ±-----+

sql语句:

select distinct viewer_id AS id
from views
where author_id = viewer_id
order by id ASC

xx as yy:将xx作为yy,表中实际列为yy,值取xx
order by:排序,ASC为增量排序;DESC为减量排序

  1. 无效的推文
表:Tweets
±---------------±--------+
 | Column Name | Type |
 ±---------------±--------+
 | tweet_id | int |
 | content | varchar |
 ±---------------±--------+
 tweet_id 是这个表的主键。 这个表包含某社交媒体 App 中所有的推文。写一条 SQL 语句,查询所有无效推文的编号(ID)。当推文内容中的字符数严格大于 15 时,该推文是无效的。
 以任意顺序返回结果表。
 查询结果格式如下示例所示:
 Tweets 表:
 ±---------±---------------------------------+
 | tweet_id | content |
 ±---------±---------------------------------+
 | 1 | Vote for Biden |
 | 2 | Let us make America great again! |
 ±---------±---------------------------------+
 结果表:
 ±---------+
 | tweet_id |
 ±---------+
 | 2 |
 ±---------+
 推文 1 的长度 length = 14。该推文是有效的。 推文 2 的长度 length = 32。该推文是无效的。

sql语句:

select tweet_id
from tweets
where LENGTH(content) >15

length():计算字符长度