Introduction
LeetCode SQL50 contain 50 basic to intermediate SQL questions covering 7 SQL topics.
This is the Select
part of SQL50, which mainly focus on conditional selection using MySQL
and Pandas
.
1757. Recyclable and Low Fat Products
MySQL
Two-condition select, directly use WHERE
AND
.
|
|
Pandas
Can use bracket select or .loc
select of Pandas DataFrame.
Note the conditions in bracket should use Series logic operators &
, |
, ~
rather than AND, OR, NOT in python.
Note the return type should be DataFrame (double bracket) rather thank Series (single bracket).
|
|
|
|
584. Find Customer Referee
MySQL
Two-condition select, directly use WHERE
OR
.
|
|
Pandas
Use ~
and isin()
of Pandas Series.
|
|
595. Big Countries
MySQL
Two-condition select, directly use WHERE
OR
.
|
|
Pandas
Use |
Pandas Series.
|
|
1148. Article Views I
MySQL
Use SELECT DISTINCT
as there are duplicates. User ORDER BY
to sort.
|
|
Pandas
Use bracket select to filter by the requirement.
Note drop_duplicates
, rename
, sort_values
all have inplace
argument.
|
|
1683. Invalid Tweets
MySQL
Directly use LENGTH()
.
|
|
Pandas
Use .str.len()
method of Pandas Series, which return a Series of lengths. >15
make it a Series of Booleans.
|
|
Summary
For bracket selection of Pandas DataFrame, the conditions in bracket involve various operators or functions of Pandas Series (Common Pandas Series Operators and Functions, most of which are not only different from Python operators and functions, but a bit different from Pandas DataFrame operators and functions.
Also, always notice the inplace
argument of functions that manipulate the DataFrame. Usually the question requires a changed DataFrame as return result.
Last modified on 2024-05-20