💡
Các câu hỏi trong phần đầu của bài SQL50 khá đơn giản, chỉ cần sử dụng lệnh SELECT và các toán tử so sánh cơ bản trong SQL. Để lại bình luận nếu bạn cần giải thích thêm nhé.
Câu 1757. Recyclable and Low Fat Products
+-------------+--------------------+
| Column Name | Type |
+-------------+--------------------+
| product_id | int |
| low_fats | enum ('Y', 'N') |
| recyclable | enum ('Y', 'N') |
+-------------+--------------------+
Write a solution to find the ids of products that are both low fat and recyclable.
Đáp án
-- Write your PostgreSQL query statement below
select product_id from Products where low_fats = 'Y' and recyclable = 'Y'
Câu 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.
Đáp án
-- Write your PostgreSQL query statement below
select name from Customer where referee_id != 2 or referee_id is null;
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:
- it has an area of at least three million (i.e., 3000000 km2), or
- 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.
Đáp án
-- Write your PostgreSQL query statement below
select name, population, area from World where area >= 3000000 or population >= 25000000
Câu 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.
Write a solution to find all the authors that viewed at least one of their own articles.
Đáp án
-- Write your PostgreSQL query statement below
select distinct(author_id) as id from Views where author_id = viewer_id order by 1 asc
Câu 1683. Invalid Tweets
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| tweet_id | int |
| content | varchar |
+----------------+---------+
tweet_id is the primary key (column with unique values) for this table.
content consists of characters on an American Keyboard, and no other special characters.
This table contains all the tweets in a social media app.
Write a solution to find the IDs of the invalid tweets. The tweet is invalid if the number of characters used in the content of the tweet is strictly greater than 15.
Đáp án
-- Write your PostgreSQL query statement below
select tweet_id from Tweets where CHAR_LENGTH(content) > 15;