📢📢📢📣📣📣
哈喽!大家好,我是【IT邦德】,10年DBA工作經驗
一位上進心十足的【大資料領域部落客】!😜😜😜
中國DBA聯盟(ACDU)成員,目前從事DBA及程式程式設計
擅長主流資料Oracle、MySQL、PG 運維開發,備份恢複,安裝遷移,性能優化、故障應急處理等。
✨ 如果有對【資料庫】感興趣的【小可愛】,歡迎關注【IT邦德】💞💞💞
❤️❤️❤️感謝各位大可愛小可愛!❤️❤️❤️
文章目錄
- 前言
- 595.大的國家
- 1757. 可回收且低脂的産品
- 584. 尋找使用者推薦人
- 183. 從不訂購的客戶
前言
SQL每個人都要用,但是用來衡量産出的并不是SQL本身,你需要用這個工具,去創造其它的價值。

595.大的國家
🚀 World 表:
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| name | varchar |
| continent | varchar |
| area | int |
| population | int |
| gdp | int |
+-------------+---------+
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 |
+-------------+------------+---------+
🐴🐴 答案
# Write your MySQL query statement below
select name,population,area from World
where area>=3000000
or population >=25000000
/* Write your T-SQL query statement below */
select name,population,area from World
where area>=3000000
or population >=25000000
/* Write your PL/SQL query statement below */
select
name "name",
population "population",
area "area"
from World
where area>=3000000
or population >=25000000
1757. 可回收且低脂的産品
🚀 表:Products
+-------------+---------+
| 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 的産品,既是低脂又是可回收的産品。
🐴🐴 答案
# Write your MySQL query statement below
select product_id from Products
where low_fats = 'Y'
and recyclable ='Y'
/* Write your T-SQL query statement below */
select product_id from Products
where low_fats = 'Y'
and recyclable ='Y'
/* Write your PL/SQL query statement below */
select product_id "product_id" from Products
where low_fats = 'Y'
and recyclable ='Y'
584. 尋找使用者推薦人
🚀 給定表 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 |
+------+
🐴🐴 答案
# Write your MySQL query statement below
select name from customer
where IFNULL(referee_id,0) <> 2
--mysql判斷非空的函數
ISNULL(expr) 如果expr為null傳回值1,否則傳回值為0
IFNULL(expr1,expr2) 如果expr1值為null傳回expr2的值,否則傳回expr1的值
/* Write your T-SQL query statement below */
select name from customer
where referee_id <> 2 OR referee_id IS NULL
/* Write your PL/SQL query statement below */
select name "name" from customer
where nvl(referee_id,0) <> 2
183. 從不訂購的客戶
🚀 某網站包含兩個表,Customers 表和 Orders 表。編寫一個 SQL 查詢,找出所有從不訂購任何東西的客戶。
Customers 表:
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Orders 表:
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
🚀 需求
例如給定上述表格,你的查詢應傳回:
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
🐴🐴 答案
# Write your MySQL query statement below
select Name "Customers" from Customers
where id not in (select CustomerId from Orders)
/* Write your T-SQL query statement below */
select Name "Customers" from Customers
where id not in (select CustomerId from Orders)
/* Write your PL/SQL query statement below */
select Name "Customers" from Customers a
where not exists (select 1 from Orders b where a.Id = b.CustomerId)
order by 1