UPDATE salary SET sex = CASE WHEN sex='m'THEN'f' ELSE'm' END;
学习要点: SELECT OrderID, Quantity, CASE WHEN Quantity >30THEN'The quantity is greater than 30' WHEN Quantity =30THEN'The quantity is 30' ELSE'The quantity is under 30' ENDAS QuantityText FROM OrderDetails;
626. Exchange Seats 换座位
1 2 3 4 5 6 7 8 9 10 11 12 13 14
SELECT (CASE WHENMOD(id, 2) !=0AND counts != id THEN id +1 WHENMOD(id, 2) !=0AND counts = id THEN id ELSE id -1 END) AS id, student FROM seat, (SELECT COUNT(*) AS counts FROM seat) AS seat_counts ORDERBY id ASC;
1 2 3 4
DELETE p1 FROM Person p1, Person p2 WHERE p1.Email = p2.Email AND p1.Id > p2.Id
CREATEFUNCTION getNthHighestSalary(N INT) RETURNSINT BEGIN SET N := N-1; // 赋值语句 N = N-1 RETURN ( # Write your MySQL query statement below. SELECT salary FROM employee GROUPBY salary ORDERBY salary DESC LIMIT N, 1 ); END
180. Consecutive Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13
SELECTDISTINCT #l2.*, l1.Num AS ConsecutiveNums FROM Logs l1, Logs l2, Logs l3 WHERE l1.Id = l2.Id -1 AND l2.Id = l3.Id -1 AND l1.Num = l2.Num AND l2.Num = l3.Num ;
185. Department Top Three Salaries
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Write your MySQL query statement below SELECT t2.Name as Department, t1.Name as Employee, t1.Salary FROM ( SELECT DepartmentId,Name,Salary FROM ( SELECT*, dense_rank() over (partitionby DepartmentId orderby Salary desc) as ranking FROM Employee) as a WHERE ranking <=3 ) t1 JOIN Department t2 ON t1.DepartmentId = t2.Id
184. Department Highest Salary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
SELECT Department.name AS'Department', Employee.name AS'Employee', Salary FROM Employee JOIN Department ON Employee.DepartmentId = Department.Id WHERE (Employee.DepartmentId , Salary) IN ( SELECT DepartmentId, MAX(Salary) FROM Employee GROUPBY DepartmentId ) ;
181. Employees Earning More Than Their Managers
1 2 3 4 5 6
SELECT a.NAME AS Employee FROM Employee AS a JOIN Employee AS b ON a.ManagerId = b.Id AND a.Salary > b.Salary ;
1179. Reformat Department Table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Write your MySQL query statement below select id, sum(casemonthwhen'Jan'then revenue end) as Jan_Revenue, sum(casemonthwhen'Feb'then revenue end) as Feb_Revenue, sum(casemonthwhen'Mar'then revenue end) as Mar_Revenue, sum(casemonthwhen'Apr'then revenue end) as Apr_Revenue, sum(casemonthwhen'May'then revenue end) as May_Revenue, sum(casemonthwhen'Jun'then revenue end) as Jun_Revenue, sum(casemonthwhen'Jul'then revenue end) as Jul_Revenue, sum(casemonthwhen'Aug'then revenue end) as Aug_Revenue, sum(casemonthwhen'Sep'then revenue end) as Sep_Revenue, sum(casemonthwhen'Oct'then revenue end) as Oct_Revenue, sum(casemonthwhen'Nov'then revenue end) as Nov_Revenue, sum(casemonthwhen'Dec'then revenue end) as Dec_Revenue from Department groupby id
Checking if Disqus is accessible...