当前位置:网站首页>Leetcode SQL first day

Leetcode SQL first day

2022-07-07 21:45:00 liyatjj

1. Big country

World surface :

±------------±--------+
| 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 , The country is considered to be Power :

The area is at least 300 10,000 square kilometers ( namely ,3000000 km2), perhaps
The population is at least 2500 ten thousand ( namely 25000000)
Write a SQL Query to report Power Name of country 、 Population and area .

source :LeetCode

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

2. Recyclable and low-fat products

SQL framework
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 .

Write SQL sentence , Find product numbers that are both low-fat and recyclable .

source :LeetCode

select product_id
from Products
where low_fats='Y' && recyclable='Y'

3. Looking for user references

SQL framework
Given table 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 |
±-----±-----±----------+
Write a query statement , Return a list of customers , The number of the customer's recommender in the list is No 2.
source :LeetCode

SELECT name
FROM customer
WHERE referee_id != 2 or referee_id is NULL

This needs to pay attention to the situation when it is empty .

4. Customers who never order

SQL framework
A website contains two tables ,Customers Table and Orders surface . Write a SQL Inquire about , Find all customers who never order anything .

Customers surface :

±—±------+
| Id | Name |
±—±------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
±—±------+
Orders surface :

±—±-----------+
| Id | CustomerId |
±—±-----------+
| 1 | 3 |
| 2 | 1 |
±—±-----------+
source :LeetCode

SELECT Name  Customers
FROM Customers
WHERE Id not in
(
    SELECT CustomerId FROM Orders
);

This requires attention to the relationship between the two tables , yes Customers Of Id and Orders Of CustomerId compare .

原网站

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

随机推荐