Leetcode SQL 50 -- 1378. Replace Employee ID With The Unique Identifier

Question

Table name: Employees

Column Name Type
id int
name varchar

id is the primary key (column with unique values) for this table.
Each row of this table contains the id and the name of an employee in a company.

Table name: EmployeeUNI

Column Name Type
id int
unique_id int

(id, unique_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the id and the corresponding unique id of an employee in the company.

Write a solution to show the unique ID of each user,
If a user does not have a unique ID replace just show null.
Return the result table in any order.

Explaination

In this question, we need to use JOIN:

Read more

Leetcode SQL 50 -- 1683. Invalid Tweets

Question

Table name: Tweets

Column Name Type
tweet_id int
content varchar

tweet_id is the primary key (column with unique values) for this table.
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.
Return the result table in any order.

Explaination

To check the number of character, we can use the LENGTH(<string>) function.

If you use this function on numbers (e.g. int), it will turn it into string and return number of digits.

Solution

SELECT tweet_id FROM Tweets WHERE LENGTH(content)>15;
Read more

Leetcode SQL 50 -- 1148. Article Views I

Question

Table name: Views

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.
Return the result table sorted by id in ascending order.

Explaination

We have two tasks:

  1. Authors viewed their own articles → WHERE author_id = viewer_id
  2. Sort table by id in ascending order → ORDER BY author_id ASC
Read more

Leetcode SQL 50 -- 595. Big Countries

Question

Table name: World

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.

Big Country

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.
Return the result table in any order.

Explaination

Read more

Leetcode SQL 50 -- 584. Find Customer Referee

Question

Table name: Customer

Column Name Type
id int
name varchar
referee_id int

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.

Explaination

Again we use SELECT <column_name> FROM <table_name> WHERE <conditions> to get the solution.

The tricky part is some of the referee_id may contain NULL value.

NULL handling

We may simply think that the solution is

Read more

Leetcode SQL 50 -- 1757. Recyclable and Low Fat Products

Question

Table name: 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 of type (Y, N) where Y means this product is low fat and N means it is not.

recyclable is an ENUM 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.

Explaination

This is one the most basic SQL SELECT query scenario,
by using SELECT <column_name> FROM <table_name> WHERE <conditions> you may get the solution easily.

Solution

Read more
Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×