当前位置:网站首页>[force deduction 10 days SQL introduction] Day1

[force deduction 10 days SQL introduction] Day1

2022-06-21 19:39:00 Ly methane

Li Kou learning plan address :https://leetcode.cn/study-plan/sql/

1. Big country

 surface :World 
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| name        | varchar |
| continent   | varchar |
| area        | int     |
| population  | int     |
| gdp         | int     |
+-------------+---------+
name  It's the primary key of this table .
 Each row of this table provides : Country name 、 Continent 、 area 、 Population and  GDP  value .

 If a country meets one of the following two conditions , That the country is a big country  :
 The area is at least  300  Square kilometers ( namely ,3000000 km2), Or a population of at least  2500  ten thousand ( namely  25000000)
 Write a  SQL  Query to report   Power   Name of country 、 Population and area .

answer :

SELECT name, population , area FROM World
WHERE population >= 25000000 OR area >= 3000000

2. Recyclable and low-fat products

 surface :Products
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| product_id  | int     |
| low_fats    | enum    |
| recyclable  | enum    |
+-------------+---------+
product_id  It's the primary key of this table .
low_fats  Is enumeration type , The values are the following two  ('Y', 'N'), among  'Y'  Indicates that the product is a low-fat product ,'N'  It means it's not a low-fat product .
recyclable  Is enumeration type , The values are the following two  ('Y', 'N'), among  'Y'  Indicates that the product is recyclable , and  'N'  Indicates non recyclable .

  demand 
 Write  SQL  sentence , Find product numbers that are both low-fat and recyclable .
 Return results   No sequence requirements  .

answer

SELECT product_id  FROM Products 
WHERE low_fats= 'Y' AND recyclable = 'Y'

3. Looking for user references

 surface  customer , It contains all the customer information and their references .
+------+------+-----------+
| id   | name | referee_id|
+------+------+-----------+
|    1 | Will |      NULL |
|    2 | Jane |      NULL |
|    3 | Alex |         2 |
|    4 | Bill |      NULL |
|    5 | Zack |         1 |
|    6 | Mark |         2 |
+------+------+-----------+

  demand 
 Write a query statement , Return a list of customers , The number of the customer's recommender in the list is not 2.

answer

mysql Judge non empty functions 
ISNULL(expr)	 If expr by null Return value 1, Otherwise, the return value is 0
IFNULL(expr1,expr2)	 If expr1 The value is null return expr2 Value , Otherwise return to expr1 Value 
SELECT name FROM customer 
WHERE IFNULL(referee_id, 0) != 2

4. Customers who never order

  A website contains two tables 
Customers  surface :
+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+

Orders  surface :
+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

  demand 
 Write a  SQL  Inquire about , Find all customers who never order anything . namely Orders  Not found in  CustomerId  Of  Customers surface 
+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+

answer

SELECT name "Customers" FROM Customers 
WHERE Id NOT IN(SELECT CustomerId FROM Orders) 

summary

SELECT Name FROM surface WHERE Conditions
ISNULL(expr) -------------expr by null return 1, Otherwise return to 0
IFNULL(expr1, expr2) ------------ expr1 by null return expr2, Otherwise return to expr1
NOT IN -------- The list does not contain

原网站

版权声明
本文为[Ly methane]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211804224129.html