Sql

LeetcodeSQLの練習



Leetcode Sql Practice



1.持っていることによってグループ化
1.1。 https://leetcode-cn.com/problems/duplicate-emails/

2外部接続
https://leetcode-cn.com/problems/combine-two-tables/
p.FirstName、p.LastName、a.City、a.StateをPersonpから選択します。



3.CASE ... WHEN ...フロー制御ステートメント
https://leetcode-cn.com/problems/swap-salary/

Update salary SET sex= CASE sex WHEN 'm' THEN 'f' ELSE 'm' END

それ以外の場合はSQLケース
https://www.cnblogs.com/shaopang/p/6903985.html



4つのネストされたクエリ
1.1。 https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/
教科書p106の例3.57を見てください
内部結合は結合と同等であることに注意してください

select e1.Name Employee from Employee e1 inner join Employee e2 on e1.ManagerId=e2.Id where e1.Salary>e2.Salary

または

select Name Employee from Employee a where Salary>(select Salary from Employee b where b.Id=a.ManagerId)

二。 https://leetcode-cn.com/problems/customers-who-never-order/



select Name Customers from Customers where Id Not IN (select CustomerId from Orders)

5重複するメールを削除する
この質問についてもう一度考えなければなりません。
https://leetcode-cn.com/problems/delete-duplicate-emails/

DELETE p1 FROM Person p1, Person p2 WHERE p1.Email = p2.Email AND p1.Id > p2.Id