SQL - MOD , IFNULL

USE IFNULL return 0 if null,

mod (feild ,2) even/odd


SELECT a.employee_id , IFNULL(b.salary,0) as bonus FROM employees a

LEFT JOIN (SELECT * FROM Employees where mod(employee_id,2) and name NOT Like 'M%') b
ON
a.employee_id = b.employee_id
ORDER BY employee_id

Input: 
Employees table:
+-------------+---------+--------+
| employee_id | name    | salary |
+-------------+---------+--------+
| 2           | Meir    | 3000   |
| 3           | Michael | 3800   |
| 7           | Addilyn | 7400   |
| 8           | Juan    | 6100   |
| 9           | Kannon  | 7700   |
+-------------+---------+--------+
Output: 
+-------------+-------+
| employee_id | bonus |
+-------------+-------+
| 2           | 0     |
| 3           | 0     |
| 7           | 7400  |
| 8           | 0     |
| 9           | 7700  |
+-------------+-------+
Explanation: 
The employees with IDs 2 and 8 get 0 bonus because they have an even employee_id.
The employee with ID 3 gets 0 bonus because their name starts with 'M'.
The rest of the employees get a 100% bonus.

留言

此網誌的熱門文章

MAP - Sort with stream

JAVA - DSF Example