8000 Upload 1-Select solutions · rajtuts/raj-leetcode-sql-50@c4f1633 · GitHub
[go: up one dir, main page]

Skip to content

Commit c4f1633

Browse files
committed
Upload 1-Select solutions
0 parents  commit c4f1633

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed

1-Select/1148_Article_Views_I.sql

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
-- Source: https://leetcode.com/problems/article-views-i/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
-- Table: Views
4+
5+
-- +---------------+---------+
6+
-- | Column Name | Type |
7+
-- +---------------+---------+
8+
-- | article_id | int |
9+
-- | author_id | int |
10+
-- | viewer_id | int |
11+
-- | view_date | date |
12+
-- +---------------+---------+
13+
-- There is no primary key (column with unique values) for this table, the table may have duplicate rows.
14+
-- Each row of this table indicates that some viewer viewed an article (written by some author) on some date.
15+
-- Note that equal author_id and viewer_id indicate the same person.
16+
17+
-- Write a solution to find all the authors that viewed at least one of their own articles.
18+
19+
-- Return the result table sorted by id in ascending order.
20+
21+
------------------------------------------------------------------------------
22+
23+
-- SQL Schema
24+
25+
CREATE TABLE IF NOT EXISTS Views (article_id int, author_id int, viewer_id int, view_date date)
26+
TRUNCATE TABLE Views
27+
INSERT INTO Views (article_id, author_id, viewer_id, view_date) VALUES ('1', '3', '5', '2019-08-01')
28+
INSERT INTO Views (article_id, author_id, viewer_id, view_date) VALUES ('1', '3', '6', '2019-08-02')
29+
INSERT INTO Views (article_id, author_id, viewer_id, view_date) VALUES ('2', '7', '7', '2019-08-01')
30+
INSERT INTO Views (article_id, author_id, viewer_id, view_date) VALUES ('2', '7', '6', '2019-08-02')
31+
INSERT INTO Views (article_id, author_id, viewer_id, view_date) VALUES ('4', '7', '1', '2019-07-22')
32+
INSERT INTO Views (article_id, author_id, viewer_id, view_date) VALUES ('3', '4', '4', '2019-07-21')
33+
INSERT INTO Views (article_id, author_id, viewer_id, view_date) VALUES ('3', '4', '4', '2019-07-21')
34+
35+
-- MS SQL Server Code
36+
37+
SELECT DISTINCT author_id AS 'id'
38+
FROM Views
39+
WHERE author_id = viewer_id
40+
ORDER BY 1 ASC

1-Select/1683_Invalid_Tweets.sql

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- Source: https://leetcode.com/problems/invalid-tweets/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
-- Table: Tweets
4+
5+
-- +----------------+---------+
6+
-- | Column Name | Type |
7+
-- +----------------+---------+
8+
-- | tweet_id | int |
9+
-- | content | varchar |
10+
-- +----------------+---------+
11+
-- tweet_id is the primary key (column with unique values) for this table.
12+
-- This table contains all the tweets in a social media app.
13+
14+
-- 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.
15+
16+
-- Return the result table in any order.
17+
18+
------------------------------------------------------------------------------
19+
20+
-- SQL Schema
21+
22+
CREATE TABLE IF NOT EXISTS Tweets(tweet_id int, content varchar(50))
23+
TRUNCATE TABLE Tweets
24+
INSERT INTO Tweets (tweet_id, content) VALUES ('1', 'Vote for Biden')
25+
INSERT INTO Tweets (tweet_id, content) VALUES ('2', 'Let us make America great again!')
26+
27+
-- MS SQL Server Code
28+
29+
SELECT tweet_id
30+
FROM Tweets
31+
WHERE LEN(content) > 15
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- Source: https://leetcode.com/problems/recyclable-and-low-fat-products/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
-- Table: Products
4+
5+
-- +-------------+---------+
6+
-- | Column Name | Type |
7+
-- +-------------+---------+
8+
-- | product_id | int |
9+
-- | low_fats | enum |
10+
-- | recyclable | enum |
11+
-- +-------------+---------+
12+
-- product_id is the primary key (column with unique values) for this table.
13+
-- low_fats is an ENUM (category) of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not.
14+
-- recyclable is an ENUM (category) of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not.
15+
16+
-- Write a solution to find the ids of products that are both low fat and recyclable.
17+
18+
-- Return the result table in any order.
19+
20+
------------------------------------------------------------------------------
21+
22+
-- SQL Schema
23+
24+
CREATE TABLE IF NOT EXISTS Products (product_id int, low_fats ENUM('Y', 'N'), recyclable ENUM('Y','N'))
25+
TRUNCATE TABLE Products
26+
INSERT INTO Products (product_id, low_fats, recyclable) VALUES ('0', 'Y', 'N')
27+
INSERT INTO Products (product_id, low_fats, recyclable) VALUES ('1', 'Y', 'Y')
28+
INSERT INTO Products (product_id, low_fats, recyclable) VALUES ('2', 'N', 'Y')
29+
INSERT INTO Products (product_id, low_fats, recyclable) VALUES ('3', 'Y', 'Y')
30+
INSERT INTO Products (product_id, low_fats, recyclable) VALUES ('4', 'N', 'N')
31+
32+
-- MS SQL Server Code
33+
34+
SELECT product_id
35+
FROM Products
36+
WHERE low_fats = 'Y' AND recyclable = 'Y'
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
-- Source: https://leetcode.com/problems/find-customer-referee/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
-- Table: Customer
4+
5+
-- +-------------+---------+
6+
-- | Column Name | Type |
7+
-- +-------------+---------+
8+
-- | id | int |
9+
-- | name | varchar |
10+
-- | referee_id | int |
11+
-- +-------------+---------+
12+
-- In SQL, id is the primary key column for this table.
13+
-- Each row of this table indicates the id of a customer, their name, and the id of the customer who referred them.
14+
15+
16+
-- Find the names of the customer that are not referred by the customer with id = 2.
17+
18+
-- Return the result table in any order.
19+
20+
-- The result format is in the following example.
21+
22+
------------------------------------------------------------------------------
23+
24+
-- SQL Schema
25+
26+
CREATE TABLE IF NOT EXISTS Customer (id int, name varchar(25), referee_id int)
27+
TRUNCATE TABLE Customer
28+
INSERT INTO Customer (id, name, referee_id) VALUES ('1', 'Will', 'None')
29+
INSERT INTO Customer (id, name, referee_id) VALUES ('2', 'Jane', 'None')
30+
INSERT INTO Customer (id, name, referee_id) VALUES ('3', 'Alex', '2')
31+
INSERT INTO Customer (id, name, referee_id) VALUES ('4', 'Bill', 'None')
32+
INSERT INTO Customer (id, name, referee_id) VALUES ('5', 'Zack', '1')
33+
INSERT INTO Customer (id, name, referee_id) VALUES ('6', 'Mark', '2')
34+
35+
-- MS SQL Server Code
36+
37+
SELECT name
38+
FROM Customer
39+
WHERE referee_id <> 2 OR referee_id IS NULL

1-Select/595_Big_Countries.sql

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
-- Source: https://leetcode.com/problems/big-countries/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
-- Table: World
4+
5+
-- +-------------+---------+
6+
-- | Column Name | Type |
7+
-- +-------------+---------+
8+
-- | name | varchar |
9+
-- | continent | varchar |
10+
-- | area | int |
11+
-- | population | int |
12+
-- | gdp | bigint |
13+
-- +-------------+---------+
14+
-- name is the primary key (column with unique values) for this table.
15+
-- 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.
16+
17+
-- A country is big if:
18+
19+
-- it has an area of at least three million (i.e., 3000000 km2), or
20+
-- it has a population of at least twenty-five million (i.e., 25000000).
21+
-- Write a solution to find the name, population, and area of the big countries.
22+
23+
-- Return the result table in any order.
24+
25+
------------------------------------------------------------------------------
26+
27+
-- SQL Schema
28+
29+
CREATE TABLE IF NOT EXISTS World (name varchar(255), continent varchar(255), area int, population int, gdp bigint)
30+
TRUNCATE TABLE World
31+
INSERT INTO World (name, continent, area, population, gdp) VALUES ('Afghanistan', 'Asia', '652230', '25500100', '20343000000')
32+
INSERT INTO World (name, continent, area, population, gdp) VALUES ('Albania', ' 6CB7 Europe', '28748', '2831741', '12960000000')
33+
INSERT INTO World (name, continent, area, population, gdp) VALUES ('Algeria', 'Africa', '2381741', '37100000', '188681000000')
34+
INSERT INTO World (name, continent, area, population, gdp) VALUES ('Andorra', 'Europe', '468', '78115', '3712000000')
35+
INSERT INTO World (name, continent, area, population, gdp) VALUES ('Angola', 'Africa', '1246700', '20609294', '100990000000')
36+
37+
-- MS SQL Server Code
38+
39+
SELECT name, population, area
40+
FROM World
41+
WHERE area >= 3000000 OR population >= 25000000

0 commit comments

Comments
 (0)
0