@@ -71,4 +71,59 @@ case when t1.maximum<1000 then t1.sa
71
71
when t1 .maximum between 1000 and 10000 then round(t1 .sa * .76 ,0 )
72
72
else round(t1 .sa * .51 ,0 )
73
73
end as salary
74
- from t1
74
+ from t1
75
+
76
+ -- My Solution:
77
+ ** Schema (MySQL v8 .0 )**
78
+
79
+ CREATE TABLE Salaries (
80
+ ` company_id` INTEGER ,
81
+ ` employee_id` INTEGER ,
82
+ ` employee_name` VARCHAR (11 ),
83
+ ` salary` INTEGER
84
+ );
85
+
86
+ INSERT INTO Salaries
87
+ (` company_id` , ` employee_id` , ` employee_name` , ` salary` )
88
+ VALUES
89
+ (' 1' , ' 1' , ' Tony' , ' 2000' ),
90
+ (' 1' , ' 2' , ' Pronub' , ' 21300' ),
91
+ (' 1' , ' 3' , ' Tyrrox' , ' 10800' ),
92
+ (' 2' , ' 1' , ' Pam' , ' 300' ),
93
+ (' 2' , ' 7' , ' Bassem' , ' 450' ),
94
+ (' 2' , ' 9' , ' Hermione' , ' 700' ),
95
+ (' 3' , ' 7' , ' Bocaben' , ' 100' ),
96
+ (' 3' , ' 2' , ' Ognjen' , ' 2200' ),
97
+ (' 3' , ' 13' , ' Nyancat' , ' 3300' ),
98
+ (' 3' , ' 15' , ' Morninngcat' , ' 1866' );
99
+
100
+ -- -
101
+
102
+ ** Query # 1**
103
+
104
+ with cte as
105
+ (select company_id, max (salary) as max_sal from Salaries
106
+ group by company_id)
107
+
108
+ select s .company_id , s .employee_id , s .employee_name , round((case when cte .max_sal < 1000 then s .salary
109
+ when cte .max_sal between 1000 and 10000 then s .salary - (24 / 100 ) * s .salary
110
+ when cte .max_sal > 10000 then s .salary - (49 / 100 ) * s .salary end)) as salary
111
+ from Salaries s left join cte
112
+ on s .company_id = cte .company_id ;
113
+
114
+ | company_id | employee_id | employee_name | salary |
115
+ | -- -------- | ----------- | ------------- | ------ |
116
+ | 1 | 1 | Tony | 1020 |
117
+ | 1 | 2 | Pronub | 10863 |
118
+ | 1 | 3 | Tyrrox | 5508 |
119
+ | 2 | 1 | Pam | 300 |
120
+ | 2 | 7 | Bassem | 450 |
121
+ | 2 | 9 | Hermione | 700 |
122
+ | 3 | 7 | Bocaben | 76 |
123
+ | 3 | 2 | Ognjen | 1672 |
124
+ | 3 | 13 | Nyancat | 2508 |
125
+ | 3 | 15 | Morninngcat | 1418 |
126
+
127
+ -- -
128
+
129
+ [View on DB Fiddle](https:// www .db - fiddle .com / f/ gdYTR3RBd5P92nxtzygseW/ 1 )
0 commit comments