Published on

LeetCode SQL 50 - Notebook

902 words5 min read
Authors
  • avatar
    Name
    Kevin Nguyen
    Twitter

Dưới đây là những ghi chú của tôi cho LeetCode SQL 50

1757. Recyclable and Low Fat Products

Table: Products

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| product_id  | int     |
| low_fats    | enum    |
| recyclable  | enum    |
+-------------+---------+


product_id is the primary key (column with unique values) for this table.
low_fats is an ENUM (category) of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not.
recyclable is an ENUM (category) of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not.

Write a solution to find the ids of products that are both low fat and recyclable.

Return the result table in any order.

Đề bài:

low_fats là 'Y' có nghĩa là ít béo và 'N' có nghĩa là béo.

recyclable là 'Y' có nghĩa là tái chế được và 'N' có nghĩa là không tái chế được.

Yêu cầu tìm các sản phẩm vừa ít chất béo vừa có thể tái chế được nên ta phải tìm giá trị cả hai cột low_fatsrecyclable đều được đặt là 'Y'.

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

584. Find Customer Referee

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
| referee_id  | int     |
+-------------+---------+
In SQL, id is the primary key column for this table.
Each row of this table indicates the id of a customer, their name, and the id of the customer who referred them.

Find the names of the customer that are not referred by the customer with id = 2.

Return the result table in any order.

Đề bài yêu cầu ta tìm tên khách hàng không được giới thiệu bởi người có id bằng 2. Với điều kiện đó ta có thể viết câu lệnh Where đơn giản là referee_id khác 2 tuy nhiên cần phải lưu ý thêm trường hợp những người không được giới thiệu bởi bất kỳ ai vì thế ta phải thêm trường hợp referee_idNull.

SELECT name
FROM Customer
WHERE referee_id != 2 OR referee_id IS NULL

Trong truy vấn này, referee_id != 2 sẽ lấy những khách hàng không được giới thiệu bởi người có ID bằng 2, và referee_id IS NULL sẽ lấy những khách hàng không được giới thiệu bởi bất kỳ ai. Cả hai điều kiện được kết hợp bằng OR để lấy tất cả các trường hợp thỏa mãn yêu cầu.

595. Big Countries

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| name        | varchar |
| continent   | varchar |
| area        | int     |
| population  | int     |
| gdp         | bigint  |
+-------------+---------+
name is the primary key (column with unique values) for this table.
Each row of this table gives information about the name of a country, the continent to which it belongs, its area, the population, and its GDP value.

A country is big if:
1. it has an area of at least three million (i.e., 3000000 km2), or
2. it has a population of at least twenty-five million (i.e., 25000000).
Write a solution to find the name, population, and area of the big countries.

Return the result table in any order.

Đề bài trên yêu cầu ta tìm "quốc gia lớn" với các điều kiện để thành một "quốc gia lớn" là có diện tích lớn hơn hoặc bằng 3000000 km2 hoặc nó có dân số lớn hơn hoặc bằng 25000000. Yêu cầu trả về tên, dân số và diện tích của quốc gia đó.

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

1148. Article Views I

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| article_id    | int     |
| author_id     | int     |
| viewer_id     | int     |
| view_date     | date    |
+---------------+---------+
There is no primary key (column with unique values) for this table, the table may have duplicate rows.
Each row of this table indicates that some viewer viewed an article (written by some author) on some date.
Note that equal author_id and viewer_id indicate the same person.

Đề bài yêu cầu chúng ta liệt kê chỉ các author_id mà đã xem ít nhất một bài viết của chính họ. Theo như mô tả, nếu author_id = viewer_id, điều này có nghĩa là tác giả đã xem công trình của mình. Chúng ta có thể thiết lập một điều kiện trong mệnh đề WHERE và chỉ lựa chọn các author_id duy nhất - kết quả SQL sẽ trông như sau:

SELECT  author_id AS id
FROM views
where author_id = viewer_id
GROUP BY author_id
ORDER BY id;