Skip to content

Commit 6e20729

Browse files
committedJun 1, 2023
Add SQL SELECT solutions
1 parent 1127859 commit 6e20729

5 files changed

+204
-0
lines changed
 

‎README.md

+4
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@
344344
| 196 | Delete Duplicate Emails | [Database](./database/0196-delete-duplicate-emails.sql) | Easy |
345345
| 197 | Rising Temperature | [Database](./database/0197-rising-temperature.sql) | Easy |
346346
| 584 | Find Customer Referee | [Database](./database/0584-find-customer-referee.sql) | Easy |
347+
| 595 | Big Countries | [Database](./database/0595-big-countries.sql) | Easy |
348+
| 1148 | Article Views I | [Database](./database/1148-article-views-i.sql) | Easy |
349+
| 1683 | Invalid Tweets | [Database](./database/1683-invalid-tweets.sql) | Easy |
350+
| 1757 | Recyclable and Low Fat Products | [Database](./database/1757-recyclable-and-low-fat-products.sql) | Easy |
347351

348352
### Shell
349353
| # | Title | Solution | Difficulty |

‎database/0595-big-countries.sql

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
-- https://leetcode.com/problems/big-countries
2+
-- 595. Big Countries
3+
-- Easy
4+
5+
/*
6+
Table: World
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| name | varchar |
11+
| continent | varchar |
12+
| area | int |
13+
| population | int |
14+
| gdp | bigint |
15+
+-------------+---------+
16+
name is the primary key column for this table.
17+
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.
18+
19+
A country is big if:
20+
* it has an area of at least three million (i.e., 3000000 km2), or
21+
* it has a population of at least twenty-five million (i.e., 25000000).
22+
23+
Write an SQL query to report the name, population, and area of the big countries.
24+
Return the result table in any order.
25+
The query result format is in the following example.
26+
27+
Example 1:
28+
Input:
29+
World table:
30+
+-------------+-----------+---------+------------+--------------+
31+
| name | continent | area | population | gdp |
32+
+-------------+-----------+---------+------------+--------------+
33+
| Afghanistan | Asia | 652230 | 25500100 | 20343000000 |
34+
| Albania | Europe | 28748 | 2831741 | 12960000000 |
35+
| Algeria | Africa | 2381741 | 37100000 | 188681000000 |
36+
| Andorra | Europe | 468 | 78115 | 3712000000 |
37+
| Angola | Africa | 1246700 | 20609294 | 100990000000 |
38+
+-------------+-----------+---------+------------+--------------+
39+
Output:
40+
+-------------+------------+---------+
41+
| name | population | area |
42+
+-------------+------------+---------+
43+
| Afghanistan | 25500100 | 652230 |
44+
| Algeria | 37100000 | 2381741 |
45+
+-------------+------------+---------+
46+
*/
47+
48+
SELECT
49+
name,population,area
50+
FROM
51+
world
52+
WHERE
53+
(area >= 3000000 OR population >= 25000000);

‎database/1148-article-views-i.sql

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
-- https://leetcode.com/problems/article-views-i
2+
-- 1148. Article Views I
3+
-- Easy
4+
5+
/*
6+
Table: Views
7+
+---------------+---------+
8+
| Column Name | Type |
9+
+---------------+---------+
10+
| article_id | int |
11+
| author_id | int |
12+
| viewer_id | int |
13+
| view_date | date |
14+
+---------------+---------+
15+
There is no primary key for this table, it may have duplicate rows.
16+
Each row of this table indicates that some viewer viewed an article (written by some author) on some date.
17+
Note that equal author_id and viewer_id indicate the same person.
18+
19+
Write an SQL query to find all the authors that viewed at least one of their own articles.
20+
Return the result table sorted by id in ascending order.
21+
The query result format is in the following example.
22+
23+
Example 1:
24+
Input:
25+
Views table:
26+
+------------+-----------+-----------+------------+
27+
| article_id | author_id | viewer_id | view_date |
28+
+------------+-----------+-----------+------------+
29+
| 1 | 3 | 5 | 2019-08-01 |
30+
| 1 | 3 | 6 | 2019-08-02 |
31+
| 2 | 7 | 7 | 2019-08-01 |
32+
| 2 | 7 | 6 | 2019-08-02 |
33+
| 4 | 7 | 1 | 2019-07-22 |
34+
| 3 | 4 | 4 | 2019-07-21 |
35+
| 3 | 4 | 4 | 2019-07-21 |
36+
+------------+-----------+-----------+------------+
37+
Output:
38+
+------+
39+
| id |
40+
+------+
41+
| 4 |
42+
| 7 |
43+
+------+
44+
*/
45+
46+
SELECT
47+
DISTINCT(author_id) AS id
48+
FROM
49+
Views
50+
WHERE
51+
author_id = viewer_id
52+
ORDER BY
53+
id ASC;

‎database/1683-invalid-tweets.sql

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-- https://leetcode.com/problems/invalid-tweets
2+
-- 1683. Invalid Tweets
3+
-- Easy
4+
5+
/*
6+
Table: Tweets
7+
+----------------+---------+
8+
| Column Name | Type |
9+
+----------------+---------+
10+
| tweet_id | int |
11+
| content | varchar |
12+
+----------------+---------+
13+
tweet_id is the primary key for this table.
14+
This table contains all the tweets in a social media app.
15+
16+
Write an SQL query 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.
17+
Return the result table in any order.
18+
The query result format is in the following example.
19+
20+
Example 1:
21+
Input:
22+
Tweets table:
23+
+----------+----------------------------------+
24+
| tweet_id | content |
25+
+----------+----------------------------------+
26+
| 1 | Vote for Biden |
27+
| 2 | Let us make America great again! |
28+
+----------+----------------------------------+
29+
Output:
30+
+----------+
31+
| tweet_id |
32+
+----------+
33+
| 2 |
34+
+----------+
35+
Explanation:
36+
Tweet 1 has length = 14. It is a valid tweet.
37+
Tweet 2 has length = 32. It is an invalid tweet.
38+
*/
39+
40+
SELECT
41+
tweet_id
42+
FROM
43+
Tweets
44+
WHERE
45+
length(content) > 15;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
-- https://leetcode.com/problems/recyclable-and-low-fat-products
2+
-- 1757. Recyclable and Low Fat Products
3+
-- Easy
4+
5+
/*
6+
Table: Products
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| product_id | int |
11+
| low_fats | enum |
12+
| recyclable | enum |
13+
+-------------+---------+
14+
product_id is the primary key for this table.
15+
low_fats is an ENUM of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not.
16+
recyclable is an ENUM of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not.
17+
18+
Write an SQL query to find the ids of products that are both low fat and recyclable.
19+
Return the result table in any order.
20+
The query result format is in the following example.
21+
22+
Example 1:
23+
Input:
24+
Products table:
25+
+-------------+----------+------------+
26+
| product_id | low_fats | recyclable |
27+
+-------------+----------+------------+
28+
| 0 | Y | N |
29+
| 1 | Y | Y |
30+
| 2 | N | Y |
31+
| 3 | Y | Y |
32+
| 4 | N | N |
33+
+-------------+----------+------------+
34+
Output:
35+
+-------------+
36+
| product_id |
37+
+-------------+
38+
| 1 |
39+
| 3 |
40+
+-------------+
41+
Explanation: Only products 1 and 3 are both low fat and recyclable.
42+
*/
43+
44+
SELECT
45+
product_id
46+
FROM
47+
Products
48+
WHERE
49+
low_fats = 'Y' AND recyclable = 'Y';

0 commit comments

Comments
 (0)