1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LeetCodeのSQL50を解いてみた

Posted at

はじめに

競技プログラミングはAtCoderしか知りませんでしたが、LeetCodeというサービスを最近知りました。
こちらは時間に縛られることなく、ひたすら問題を解くことができるのでのんびり楽しめます。

SQLもあるよ

業務で使わないPython、使わないVue、使わないFirebaseでいろいろやってますが、LeetCodeにはSQLがあります。

普段業務で使っているSQLでも遊べます!
50問厳選してくれているので、解いてみました。

記憶に残った問題

回答例
select
    b.category
    ,COUNT(a.account_id) accounts_count
from Accounts a
right join (
    values('Low Salary'),('High Salary'),('Average Salary')) as b(category) 
    on case
        when a.income < 20000 then 'Low Salary'
        when a.income > 50000 then  'High Salary'
        else 'Average Salary'
       end = b.category
group by b.category

この問題は、回答例にあるvalues (col) as tmp_tab(col)のように、
データから派生テーブルを作る使い方を完全に忘れてしまい、最初は結構長いものを書いてしまった。

おわりに

全編英語なのですが、そこまで難しくないです。
普段SQLを使っている人はぜひ解いてみてください。

肌感の難易度は、データ分析業務でバリバリウィンドウ関数書いているよって人なら楽勝だと思います。
システム開発でテーブル結合してビュー作ったり、夜間処理などでIF連携を作っている人なら、解きごたえあると思います。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?