Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Задача 1
- ```postgresql
- SELECT employee_id,
- salary,
- MAX(salary) OVER () AS max_salary,
- ROUND(MAX(salary) OVER () / salary, 2) AS t
- FROM employees;
- ```
- | employee_id | salary | max_salary | t |
- |-------------|----------|------------|-------|
- | 100 | 24000.00 | 24000 | 1.00 |
- | 101 | 17000.00 | 24000 | 1.41 |
- | 102 | 17000.00 | 24000 | 1.41 |
- | 103 | 9000.00 | 24000 | 2.67 |
- | 104 | 6000.00 | 24000 | 4.00 |
- | 105 | 4800.00 | 24000 | 5.00 |
- | 107 | 4200.00 | 24000 | 5.71 |
- | 108 | 12008.00 | 24000 | 2.00 |
- | 109 | 9000.00 | 24000 | 2.67 |
- | 110 | 8200.00 | 24000 | 2.93 |
- | 111 | 7700.00 | 24000 | 3.12 |
- | 112 | 7800.00 | 24000 | 3.08 |
- | 113 | 6900.00 | 24000 | 3.48 |
- | 114 | 11000.00 | 24000 | 2.18 |
- | 115 | 3100.00 | 24000 | 7.74 |
- | 116 | 2900.00 | 24000 | 8.28 |
- | 117 | 2800.00 | 24000 | 8.57 |
- | 118 | 2600.00 | 24000 | 9.23 |
- | 119 | 2500.00 | 24000 | 9.60 |
- | 120 | 8000.00 | 24000 | 3.00 |
- | 121 | 8200.00 | 24000 | 2.93 |
- | 122 | 7900.00 | 24000 | 3.04 |
- | 123 | 6500.00 | 24000 | 3.69 |
- | 124 | 5800.00 | 24000 | 4.14 |
- | 125 | 3200.00 | 24000 | 7.50 |
- | 126 | 2700.00 | 24000 | 8.89 |
- | 127 | 2400.00 | 24000 | 10.00 |
- | 128 | 2200.00 | 24000 | 10.91 |
- | 129 | 3300.00 | 24000 | 7.27 |
- | 130 | 2800.00 | 24000 | 8.57 |
- | 131 | 2500.00 | 24000 | 9.60 |
- | 132 | 2100.00 | 24000 | 11.43 |
- | 133 | 3300.00 | 24000 | 7.27 |
- | 134 | 2900.00 | 24000 | 8.28 |
- | 135 | 2400.00 | 24000 | 10.00 |
- | 136 | 2200.00 | 24000 | 10.91 |
- | 137 | 3600.00 | 24000 | 6.67 |
- | 138 | 3200.00 | 24000 | 7.50 |
- | 139 | 2700.00 | 24000 | 8.89 |
- | 140 | 2500.00 | 24000 | 9.60 |
- | 141 | 3500.00 | 24000 | 6.86 |
- | 142 | 3100.00 | 24000 | 7.74 |
- | 143 | 2600.00 | 24000 | 9.23 |
- | 144 | 2500.00 | 24000 | 9.60 |
- | 145 | 14000.00 | 24000 | 1.71 |
- | 146 | 13500.00 | 24000 | 1.78 |
- | 147 | 12000.00 | 24000 | 2.00 |
- | 148 | 11000.00 | 24000 | 2.18 |
- | 149 | 10500.00 | 24000 | 2.29 |
- | 150 | 10000.00 | 24000 | 2.40 |
- | 151 | 9500.00 | 24000 | 2.53 |
- | 152 | 9000.00 | 24000 | 2.67 |
- | 153 | 8000.00 | 24000 | 3.00 |
- | 154 | 7500.00 | 24000 | 3.20 |
- | 155 | 7000.00 | 24000 | 3.43 |
- | 156 | 10000.00 | 24000 | 2.40 |
- | 157 | 9500.00 | 24000 | 2.53 |
- | 158 | 9000.00 | 24000 | 2.67 |
- | 159 | 8000.00 | 24000 | 3.00 |
- | 160 | 7500.00 | 24000 | 3.20 |
- | 161 | 7000.00 | 24000 | 3.43 |
- | 162 | 10500.00 | 24000 | 2.29 |
- | 163 | 9500.00 | 24000 | 2.53 |
- | 164 | 7200.00 | 24000 | 3.33 |
- | 165 | 6800.00 | 24000 | 3.53 |
- | 166 | 6400.00 | 24000 | 3.75 |
- | 167 | 6200.00 | 24000 | 3.87 |
- | 168 | 11500.00 | 24000 | 2.09 |
- | 169 | 10000.00 | 24000 | 2.40 |
- | 170 | 9600.00 | 24000 | 2.50 |
- | 171 | 7400.00 | 24000 | 3.24 |
- | 172 | 7300.00 | 24000 | 3.29 |
- | 173 | 6100.00 | 24000 | 3.93 |
- | 174 | 11000.00 | 24000 | 2.18 |
- | 175 | 8800.00 | 24000 | 2.73 |
- | 176 | 8600.00 | 24000 | 2.79 |
- | 177 | 8400.00 | 24000 | 2.86 |
- | 179 | 6200.00 | 24000 | 3.87 |
- | 180 | 3200.00 | 24000 | 7.50 |
- | 181 | 3100.00 | 24000 | 7.74 |
- | 182 | 2500.00 | 24000 | 9.60 |
- | 183 | 2800.00 | 24000 | 8.57 |
- | 184 | 4200.00 | 24000 | 5.71 |
- | 185 | 4100.00 | 24000 | 5.85 |
- | 186 | 3400.00 | 24000 | 7.06 |
- | 187 | 3000.00 | 24000 | 8.00 |
- | 188 | 3800.00 | 24000 | 6.32 |
- | 189 | 3600.00 | 24000 | 6.67 |
- | 190 | 2900.00 | 24000 | 8.28 |
- | 191 | 2500.00 | 24000 | 9.60 |
- | 192 | 4000.00 | 24000 | 6.00 |
- | 193 | 3900.00 | 24000 | 6.15 |
- | 194 | 3200.00 | 24000 | 7.50 |
- | 195 | 2800.00 | 24000 | 8.57 |
- | 196 | 3100.00 | 24000 | 7.74 |
- | 197 | 3000.00 | 24000 | 8.00 |
- | 198 | 2600.00 | 24000 | 9.23 |
- | 199 | 2600.00 | 24000 | 9.23 |
- | 200 | 4400.00 | 24000 | 5.45 |
- | 201 | 13000.00 | 24000 | 1.85 |
- | 202 | 6000.00 | 24000 | 4.00 |
- | 203 | 6500.00 | 24000 | 3.69 |
- | 204 | 10000.00 | 24000 | 2.40 |
- | 205 | 12008.00 | 24000 | 2.00 |
- | 206 | 8300.00 | 24000 | 2.89 |
- | 106 | 7000.00 | 24000 | 3.43 |
- | 500 | 1000.00 | 24000 | 24.00 |
- | 178 | 7000.00 | 24000 | 3.43 |
- ## Задача 2
- ```postgresql
- SELECT employee_id,
- salary,
- AVG(salary) OVER (PARTITION BY department_id) AS avg_salary_by_department,
- ROUND(salary / AVG(salary) OVER (PARTITION BY department_id), 2) AS t
- FROM employees;
- ```
- | employee_id | salary | avg_salary_by_department | t |
- |-------------|----------|--------------------------|------|
- | 200 | 4400.00 | 4400 | 1.00 |
- | 201 | 13000.00 | 9500 | 1.37 |
- | 202 | 6000.00 | 9500 | 0.63 |
- | 115 | 3100.00 | 4150 | 0.75 |
- | 114 | 11000.00 | 4150 | 2.65 |
- | 119 | 2500.00 | 4150 | 0.60 |
- | 118 | 2600.00 | 4150 | 0.63 |
- | 117 | 2800.00 | 4150 | 0.67 |
- | 116 | 2900.00 | 4150 | 0.70 |
- | 203 | 6500.00 | 6500 | 1.00 |
- | 127 | 2400.00 | 3421.74 | 0.70 |
- | 120 | 8000.00 | 3421.74 | 2.34 |
- | 121 | 8200.00 | 3421.74 | 2.40 |
- | 122 | 7900.00 | 3421.74 | 2.31 |
- | 123 | 6500.00 | 3421.74 | 1.90 |
- | 124 | 5800.00 | 3421.74 | 1.70 |
- | 125 | 3200.00 | 3421.74 | 0.94 |
- | 126 | 2700.00 | 3421.74 | 0.79 |
- | 128 | 2200.00 | 3421.74 | 0.64 |
- | 129 | 3300.00 | 3421.74 | 0.96 |
- | 130 | 2800.00 | 3421.74 | 0.82 |
- | 131 | 2500.00 | 3421.74 | 0.73 |
- | 132 | 2100.00 | 3421.74 | 0.61 |
- | 133 | 3300.00 | 3421.74 | 0.96 |
- | 134 | 2900.00 | 3421.74 | 0.85 |
- | 135 | 2400.00 | 3421.74 | 0.70 |
- | 136 | 2200.00 | 3421.74 | 0.64 |
- | 137 | 3600.00 | 3421.74 | 1.05 |
- | 138 | 3200.00 | 3421.74 | 0.94 |
- | 139 | 2700.00 | 3421.74 | 0.79 |
- | 140 | 2500.00 | 3421.74 | 0.73 |
- | 141 | 3500.00 | 3421.74 | 1.02 |
- | 142 | 3100.00 | 3421.74 | 0.91 |
- | 143 | 2600.00 | 3421.74 | 0.76 |
- | 144 | 2500.00 | 3421.74 | 0.73 |
- | 180 | 3200.00 | 3421.74 | 0.94 |
- | 181 | 3100.00 | 3421.74 | 0.91 |
- | 182 | 2500.00 | 3421.74 | 0.73 |
- | 183 | 2800.00 | 3421.74 | 0.82 |
- | 184 | 4200.00 | 3421.74 | 1.23 |
- | 185 | 4100.00 | 3421.74 | 1.20 |
- | 186 | 3400.00 | 3421.74 | 0.99 |
- | 187 | 3000.00 | 3421.74 | 0.88 |
- | 188 | 3800.00 | 3421.74 | 1.11 |
- | 189 | 3600.00 | 3421.74 | 1.05 |
- | 190 | 2900.00 | 3421.74 | 0.85 |
- | 191 | 2500.00 | 3421.74 | 0.73 |
- | 192 | 4000.00 | 3421.74 | 1.17 |
- | 193 | 3900.00 | 3421.74 | 1.14 |
- | 194 | 3200.00 | 3421.74 | 0.94 |
- | 195 | 2800.00 | 3421.74 | 0.82 |
- | 196 | 3100.00 | 3421.74 | 0.91 |
- | 197 | 3000.00 | 3421.74 | 0.88 |
- | 198 | 2600.00 | 3421.74 | 0.76 |
- | 199 | 2600.00 | 3421.74 | 0.76 |
- | 500 | 1000.00 | 3421.74 | 0.29 |
- | 103 | 9000.00 | 6200 | 1.45 |
- | 104 | 6000.00 | 6200 | 0.97 |
- | 106 | 7000.00 | 6200 | 1.13 |
- | 105 | 4800.00 | 6200 | 0.77 |
- | 107 | 4200.00 | 6200 | 0.68 |
- | 204 | 10000.00 | 10000 | 1.00 |
- | 163 | 9500.00 | 8900 | 1.07 |
- | 156 | 10000.00 | 8900 | 1.12 |
- | 158 | 9000.00 | 8900 | 1.01 |
- | 159 | 8000.00 | 8900 | 0.90 |
- | 160 | 7500.00 | 8900 | 0.84 |
- | 161 | 7000.00 | 8900 | 0.79 |
- | 162 | 10500.00 | 8900 | 1.18 |
- | 146 | 13500.00 | 8900 | 1.52 |
- | 147 | 12000.00 | 8900 | 1.35 |
- | 148 | 11000.00 | 8900 | 1.24 |
- | 149 | 10500.00 | 8900 | 1.18 |
- | 150 | 10000.00 | 8900 | 1.12 |
- | 151 | 9500.00 | 8900 | 1.07 |
- | 152 | 9000.00 | 8900 | 1.01 |
- | 153 | 8000.00 | 8900 | 0.90 |
- | 154 | 7500.00 | 8900 | 0.84 |
- | 155 | 7000.00 | 8900 | 0.79 |
- | 157 | 9500.00 | 8900 | 1.07 |
- | 164 | 7200.00 | 8900 | 0.81 |
- | 165 | 6800.00 | 8900 | 0.76 |
- | 166 | 6400.00 | 8900 | 0.72 |
- | 167 | 6200.00 | 8900 | 0.70 |
- | 168 | 11500.00 | 8900 | 1.29 |
- | 169 | 10000.00 | 8900 | 1.12 |
- | 170 | 9600.00 | 8900 | 1.08 |
- | 171 | 7400.00 | 8900 | 0.83 |
- | 172 | 7300.00 | 8900 | 0.82 |
- | 173 | 6100.00 | 8900 | 0.69 |
- | 174 | 11000.00 | 8900 | 1.24 |
- | 175 | 8800.00 | 8900 | 0.99 |
- | 176 | 8600.00 | 8900 | 0.97 |
- | 177 | 8400.00 | 8900 | 0.94 |
- | 179 | 6200.00 | 8900 | 0.70 |
- | 178 | 7000.00 | 8900 | 0.79 |
- | 145 | 14000.00 | 8900 | 1.57 |
- | 102 | 17000.00 | 19333.33 | 0.88 |
- | 101 | 17000.00 | 19333.33 | 0.88 |
- | 100 | 24000.00 | 19333.33 | 1.24 |
- | 113 | 6900.00 | 8601.33 | 0.80 |
- | 112 | 7800.00 | 8601.33 | 0.91 |
- | 110 | 8200.00 | 8601.33 | 0.95 |
- | 109 | 9000.00 | 8601.33 | 1.05 |
- | 111 | 7700.00 | 8601.33 | 0.90 |
- | 108 | 12008.00 | 8601.33 | 1.40 |
- | 206 | 8300.00 | 10154 | 0.82 |
- | 205 | 12008.00 | 10154 | 1.18 |
- ## Задача 3
- ```postgresql
- SELECT e.employee_id,
- e.salary,
- AVG(e.salary) OVER (PARTITION BY e.department_id) AS avg_salary_by_department,
- AVG(e.salary) OVER (PARTITION BY e.job_id) AS avg_salary_by_job,
- ROUND(AVG(e.salary) OVER (PARTITION BY e.department_id) /
- AVG(e.salary) OVER (PARTITION BY e.job_id), 2) AS ratio
- FROM employees e;
- ```
- | employee_id | salary | avg_salary_by_department | avg_salary_by_job | ratio |
- |-------------|----------|--------------------------|-------------------|-------|
- | 200 | 4400.00 | 4400 | 4400 | 1.00 |
- | 202 | 6000.00 | 9500 | 6000 | 1.58 |
- | 201 | 13000.00 | 9500 | 13000 | 0.73 |
- | 117 | 2800.00 | 4150 | 2483.33 | 1.67 |
- | 119 | 2500.00 | 4150 | 2483.33 | 1.67 |
- | 118 | 2600.00 | 4150 | 2483.33 | 1.67 |
- | 116 | 2900.00 | 4150 | 2483.33 | 1.67 |
- | 115 | 3100.00 | 4150 | 2483.33 | 1.67 |
- | 114 | 11000.00 | 4150 | 11000 | 0.38 |
- | 203 | 6500.00 | 6500 | 6500 | 1.00 |
- | 132 | 2100.00 | 3421.74 | 2785 | 1.23 |
- | 144 | 2500.00 | 3421.74 | 2785 | 1.23 |
- | 143 | 2600.00 | 3421.74 | 2785 | 1.23 |
- | 124 | 5800.00 | 3421.74 | 7280 | 0.47 |
- | 123 | 6500.00 | 3421.74 | 7280 | 0.47 |
- | 142 | 3100.00 | 3421.74 | 2785 | 1.23 |
- | 500 | 1000.00 | 3421.74 | 2483.33 | 1.38 |
- | 141 | 3500.00 | 3421.74 | 2785 | 1.23 |
- | 140 | 2500.00 | 3421.74 | 2785 | 1.23 |
- | 139 | 2700.00 | 3421.74 | 2785 | 1.23 |
- | 138 | 3200.00 | 3421.74 | 2785 | 1.23 |
- | 137 | 3600.00 | 3421.74 | 2785 | 1.23 |
- | 136 | 2200.00 | 3421.74 | 2785 | 1.23 |
- | 135 | 2400.00 | 3421.74 | 2785 | 1.23 |
- | 134 | 2900.00 | 3421.74 | 2785 | 1.23 |
- | 125 | 3200.00 | 3421.74 | 2785 | 1.23 |
- | 126 | 2700.00 | 3421.74 | 2785 | 1.23 |
- | 127 | 2400.00 | 3421.74 | 2785 | 1.23 |
- | 128 | 2200.00 | 3421.74 | 2785 | 1.23 |
- | 133 | 3300.00 | 3421.74 | 2785 | 1.23 |
- | 187 | 3000.00 | 3421.74 | 3215 | 1.06 |
- | 186 | 3400.00 | 3421.74 | 3215 | 1.06 |
- | 185 | 4100.00 | 3421.74 | 3215 | 1.06 |
- | 184 | 4200.00 | 3421.74 | 3215 | 1.06 |
- | 183 | 2800.00 | 3421.74 | 3215 | 1.06 |
- | 182 | 2500.00 | 3421.74 | 3215 | 1.06 |
- | 181 | 3100.00 | 3421.74 | 3215 | 1.06 |
- | 196 | 3100.00 | 3421.74 | 3215 | 1.06 |
- | 199 | 2600.00 | 3421.74 | 3215 | 1.06 |
- | 195 | 2800.00 | 3421.74 | 3215 | 1.06 |
- | 180 | 3200.00 | 3421.74 | 3215 | 1.06 |
- | 194 | 3200.00 | 3421.74 | 3215 | 1.06 |
- | 188 | 3800.00 | 3421.74 | 3215 | 1.06 |
- | 189 | 3600.00 | 3421.74 | 3215 | 1.06 |
- | 190 | 2900.00 | 3421.74 | 3215 | 1.06 |
- | 192 | 4000.00 | 3421.74 | 3215 | 1.06 |
- | 191 | 2500.00 | 3421.74 | 3215 | 1.06 |
- | 193 | 3900.00 | 3421.74 | 3215 | 1.06 |
- | 198 | 2600.00 | 3421.74 | 3215 | 1.06 |
- | 197 | 3000.00 | 3421.74 | 3215 | 1.06 |
- | 122 | 7900.00 | 3421.74 | 7280 | 0.47 |
- | 121 | 8200.00 | 3421.74 | 7280 | 0.47 |
- | 120 | 8000.00 | 3421.74 | 7280 | 0.47 |
- | 129 | 3300.00 | 3421.74 | 2785 | 1.23 |
- | 130 | 2800.00 | 3421.74 | 2785 | 1.23 |
- | 131 | 2500.00 | 3421.74 | 2785 | 1.23 |
- | 104 | 6000.00 | 6200 | 6000 | 1.03 |
- | 105 | 4800.00 | 6200 | 6000 | 1.03 |
- | 107 | 4200.00 | 6200 | 6000 | 1.03 |
- | 103 | 9000.00 | 6200 | 6000 | 1.03 |
- | 106 | 7000.00 | 6200 | 7000 | 0.89 |
- | 204 | 10000.00 | 10000 | 10000 | 1.00 |
- | 177 | 8400.00 | 8900 | 8350 | 1.07 |
- | 176 | 8600.00 | 8900 | 8350 | 1.07 |
- | 175 | 8800.00 | 8900 | 8350 | 1.07 |
- | 174 | 11000.00 | 8900 | 8350 | 1.07 |
- | 173 | 6100.00 | 8900 | 8350 | 1.07 |
- | 172 | 7300.00 | 8900 | 8350 | 1.07 |
- | 171 | 7400.00 | 8900 | 8350 | 1.07 |
- | 170 | 9600.00 | 8900 | 8350 | 1.07 |
- | 169 | 10000.00 | 8900 | 8350 | 1.07 |
- | 178 | 7000.00 | 8900 | 8350 | 1.07 |
- | 166 | 6400.00 | 8900 | 8350 | 1.07 |
- | 165 | 6800.00 | 8900 | 8350 | 1.07 |
- | 164 | 7200.00 | 8900 | 8350 | 1.07 |
- | 163 | 9500.00 | 8900 | 8350 | 1.07 |
- | 162 | 10500.00 | 8900 | 8350 | 1.07 |
- | 161 | 7000.00 | 8900 | 8350 | 1.07 |
- | 160 | 7500.00 | 8900 | 8350 | 1.07 |
- | 159 | 8000.00 | 8900 | 8350 | 1.07 |
- | 158 | 9000.00 | 8900 | 8350 | 1.07 |
- | 157 | 9500.00 | 8900 | 8350 | 1.07 |
- | 156 | 10000.00 | 8900 | 8350 | 1.07 |
- | 155 | 7000.00 | 8900 | 8350 | 1.07 |
- | 154 | 7500.00 | 8900 | 8350 | 1.07 |
- | 153 | 8000.00 | 8900 | 8350 | 1.07 |
- | 152 | 9000.00 | 8900 | 8350 | 1.07 |
- | 151 | 9500.00 | 8900 | 8350 | 1.07 |
- | 150 | 10000.00 | 8900 | 8350 | 1.07 |
- | 168 | 11500.00 | 8900 | 8350 | 1.07 |
- | 145 | 14000.00 | 8900 | 12200 | 0.73 |
- | 147 | 12000.00 | 8900 | 12200 | 0.73 |
- | 148 | 11000.00 | 8900 | 12200 | 0.73 |
- | 149 | 10500.00 | 8900 | 12200 | 0.73 |
- | 146 | 13500.00 | 8900 | 12200 | 0.73 |
- | 167 | 6200.00 | 8900 | 8350 | 1.07 |
- | 179 | 6200.00 | 8900 | 8350 | 1.07 |
- | 101 | 17000.00 | 19333.33 | 17000 | 1.14 |
- | 102 | 17000.00 | 19333.33 | 17000 | 1.14 |
- | 100 | 24000.00 | 19333.33 | 24000 | 0.81 |
- | 112 | 7800.00 | 8601.33 | 7920 | 1.09 |
- | 111 | 7700.00 | 8601.33 | 7920 | 1.09 |
- | 113 | 6900.00 | 8601.33 | 7920 | 1.09 |
- | 108 | 12008.00 | 8601.33 | 12008 | 0.72 |
- | 110 | 8200.00 | 8601.33 | 7920 | 1.09 |
- | 109 | 9000.00 | 8601.33 | 7920 | 1.09 |
- | 205 | 12008.00 | 10154 | 12008 | 0.85 |
- | 206 | 8300.00 | 10154 | 8300 | 1.22 |
- ## Задача 4
- ```postgresql
- WITH min_salary_by_dept AS (SELECT department_id,
- MIN(salary) AS min_salary
- FROM employees
- GROUP BY department_id)
- SELECT e.employee_id, e.last_name, e.salary, m.department_id
- FROM employees e
- JOIN min_salary_by_dept m
- ON e.department_id = m.department_id AND e.salary = m.min_salary
- ORDER BY e.department_id, e.last_name;
- ```
- | employee_id | last_name | salary | department_id |
- |-------------|------------|----------|---------------|
- | 200 | Whalen | 4400.00 | 10 |
- | 202 | Fay | 6000.00 | 20 |
- | 119 | Colmenares | 2500.00 | 30 |
- | 203 | Mavris | 6500.00 | 40 |
- | 500 | B | 1000.00 | 50 |
- | 107 | Lorentz | 4200.00 | 60 |
- | 204 | Baer | 10000.00 | 70 |
- | 173 | Kumar | 6100.00 | 80 |
- | 102 | De Haan | 17000.00 | 90 |
- | 101 | Kochhar | 17000.00 | 90 |
- | 113 | Popp | 6900.00 | 100 |
- | 206 | Gietz | 8300.00 | 110 |
- ## Задача 5
- ```postgresql
- SELECT manager_id,
- COUNT(employee_id) AS subordinates_count
- FROM employees
- WHERE manager_id IS NOT NULL
- GROUP BY manager_id
- ORDER BY subordinates_count DESC
- LIMIT 3;
- ```
- | manager_id | subordinates_count |
- |------------|--------------------|
- | 100 | 15 |
- | 121 | 8 |
- | 122 | 8 |
- ## Задача 6
- ```postgresql
- WITH sorted_employees AS (SELECT e.employee_id,
- e.salary,
- e.last_name,
- NTILE(5) OVER (ORDER BY e.last_name) AS group_num
- FROM employees e)
- SELECT employee_id,
- last_name,
- salary,
- group_num,
- AVG(salary) OVER (PARTITION BY group_num) AS avg_salary_by_group,
- salary - AVG(salary) OVER (PARTITION BY group_num) AS salary_diff_from_group_avg
- FROM sorted_employees;
- ```
- | employee_id | last_name | salary | group_num | avg_salary_by_group | salary_diff_from_group_avg |
- |-------------|-------------|----------|-----------|---------------------|----------------------------|
- | 174 | Abel | 11000.00 | 1 | 6336.36 | 4663.64 |
- | 166 | Ande | 6400.00 | 1 | 6336.36 | 63.64 |
- | 130 | Atkinson | 2800.00 | 1 | 6336.36 | -3536.36 |
- | 105 | Austin | 4800.00 | 1 | 6336.36 | -1536.36 |
- | 500 | B | 1000.00 | 1 | 6336.36 | -5336.36 |
- | 204 | Baer | 10000.00 | 1 | 6336.36 | 3663.64 |
- | 116 | Baida | 2900.00 | 1 | 6336.36 | -3436.36 |
- | 167 | Banda | 6200.00 | 1 | 6336.36 | -136.36 |
- | 172 | Bates | 7300.00 | 1 | 6336.36 | 963.64 |
- | 192 | Bell | 4000.00 | 1 | 6336.36 | -2336.36 |
- | 151 | Bernstein | 9500.00 | 1 | 6336.36 | 3163.64 |
- | 129 | Bissot | 3300.00 | 1 | 6336.36 | -3036.36 |
- | 169 | Bloom | 10000.00 | 1 | 6336.36 | 3663.64 |
- | 185 | Bull | 4100.00 | 1 | 6336.36 | -2236.36 |
- | 187 | Cabrio | 3000.00 | 1 | 6336.36 | -3336.36 |
- | 148 | Cambrault | 11000.00 | 1 | 6336.36 | 4663.64 |
- | 154 | Cambrault | 7500.00 | 1 | 6336.36 | 1163.64 |
- | 110 | Chen | 8200.00 | 1 | 6336.36 | 1863.64 |
- | 188 | Chung | 3800.00 | 1 | 6336.36 | -2536.36 |
- | 119 | Colmenares | 2500.00 | 1 | 6336.36 | -3836.36 |
- | 142 | Davies | 3100.00 | 1 | 6336.36 | -3236.36 |
- | 102 | De Haan | 17000.00 | 1 | 6336.36 | 10663.64 |
- | 186 | Dellinger | 3400.00 | 2 | 6582.18 | -3182.18 |
- | 189 | Dilly | 3600.00 | 2 | 6582.18 | -2982.18 |
- | 160 | Doran | 7500.00 | 2 | 6582.18 | 917.82 |
- | 104 | Ernst | 6000.00 | 2 | 6582.18 | -582.18 |
- | 147 | Errazuriz | 12000.00 | 2 | 6582.18 | 5417.82 |
- | 193 | Everett | 3900.00 | 2 | 6582.18 | -2682.18 |
- | 109 | Faviet | 9000.00 | 2 | 6582.18 | 2417.82 |
- | 202 | Fay | 6000.00 | 2 | 6582.18 | -582.18 |
- | 197 | Feeney | 3000.00 | 2 | 6582.18 | -3582.18 |
- | 181 | Fleaur | 3100.00 | 2 | 6582.18 | -3482.18 |
- | 170 | Fox | 9600.00 | 2 | 6582.18 | 3017.82 |
- | 121 | Fripp | 8200.00 | 2 | 6582.18 | 1617.82 |
- | 190 | Gates | 2900.00 | 2 | 6582.18 | -3682.18 |
- | 135 | Gee | 2400.00 | 2 | 6582.18 | -4182.18 |
- | 183 | Geoni | 2800.00 | 2 | 6582.18 | -3782.18 |
- | 206 | Gietz | 8300.00 | 2 | 6582.18 | 1717.82 |
- | 199 | Grant | 2600.00 | 2 | 6582.18 | -3982.18 |
- | 178 | Grant | 7000.00 | 2 | 6582.18 | 417.82 |
- | 108 | Greenberg | 12008.00 | 2 | 6582.18 | 5425.82 |
- | 163 | Greene | 9500.00 | 2 | 6582.18 | 2917.82 |
- | 152 | Hall | 9000.00 | 2 | 6582.18 | 2417.82 |
- | 201 | Hartstein | 13000.00 | 2 | 6582.18 | 6417.82 |
- | 205 | Higgins | 12008.00 | 3 | 6941.27 | 5066.73 |
- | 118 | Himuro | 2600.00 | 3 | 6941.27 | -4341.27 |
- | 103 | Hunold | 9000.00 | 3 | 6941.27 | 2058.73 |
- | 175 | Hutton | 8800.00 | 3 | 6941.27 | 1858.73 |
- | 179 | Johnson | 6200.00 | 3 | 6941.27 | -741.27 |
- | 195 | Jones | 2800.00 | 3 | 6941.27 | -4141.27 |
- | 122 | Kaufling | 7900.00 | 3 | 6941.27 | 958.73 |
- | 115 | Khoo | 3100.00 | 3 | 6941.27 | -3841.27 |
- | 100 | King | 24000.00 | 3 | 6941.27 | 17058.73 |
- | 156 | King | 10000.00 | 3 | 6941.27 | 3058.73 |
- | 101 | Kochhar | 17000.00 | 3 | 6941.27 | 10058.73 |
- | 173 | Kumar | 6100.00 | 3 | 6941.27 | -841.27 |
- | 137 | Ladwig | 3600.00 | 3 | 6941.27 | -3341.27 |
- | 127 | Landry | 2400.00 | 3 | 6941.27 | -4541.27 |
- | 165 | Lee | 6800.00 | 3 | 6941.27 | -141.27 |
- | 177 | Livingston | 8400.00 | 3 | 6941.27 | 1458.73 |
- | 107 | Lorentz | 4200.00 | 3 | 6941.27 | -2741.27 |
- | 133 | Mallin | 3300.00 | 3 | 6941.27 | -3641.27 |
- | 128 | Markle | 2200.00 | 3 | 6941.27 | -4741.27 |
- | 131 | Marlow | 2500.00 | 3 | 6941.27 | -4441.27 |
- | 164 | Marvins | 7200.00 | 3 | 6941.27 | 258.73 |
- | 143 | Matos | 2600.00 | 3 | 6941.27 | -4341.27 |
- | 203 | Mavris | 6500.00 | 4 | 5942.86 | 557.14 |
- | 194 | McCain | 3200.00 | 4 | 5942.86 | -2742.86 |
- | 158 | McEwen | 9000.00 | 4 | 5942.86 | 3057.14 |
- | 126 | Mikkilineni | 2700.00 | 4 | 5942.86 | -3242.86 |
- | 124 | Mourgos | 5800.00 | 4 | 5942.86 | -142.86 |
- | 125 | Nayer | 3200.00 | 4 | 5942.86 | -2742.86 |
- | 198 | OConnell | 2600.00 | 4 | 5942.86 | -3342.86 |
- | 153 | Olsen | 8000.00 | 4 | 5942.86 | 2057.14 |
- | 132 | Olson | 2100.00 | 4 | 5942.86 | -3842.86 |
- | 168 | Ozer | 11500.00 | 4 | 5942.86 | 5557.14 |
- | 146 | Partners | 13500.00 | 4 | 5942.86 | 7557.14 |
- | 106 | Pataballa | 7000.00 | 4 | 5942.86 | 1057.14 |
- | 140 | Patel | 2500.00 | 4 | 5942.86 | -3442.86 |
- | 191 | Perkins | 2500.00 | 4 | 5942.86 | -3442.86 |
- | 136 | Philtanker | 2200.00 | 4 | 5942.86 | -3742.86 |
- | 113 | Popp | 6900.00 | 4 | 5942.86 | 957.14 |
- | 141 | Rajs | 3500.00 | 4 | 5942.86 | -2442.86 |
- | 114 | Raphaely | 11000.00 | 4 | 5942.86 | 5057.14 |
- | 134 | Rogers | 2900.00 | 4 | 5942.86 | -3042.86 |
- | 145 | Russell | 14000.00 | 4 | 5942.86 | 8057.14 |
- | 184 | Sarchand | 4200.00 | 4 | 5942.86 | -1742.86 |
- | 111 | Sciarra | 7700.00 | 5 | 6328.57 | 1371.43 |
- | 139 | Seo | 2700.00 | 5 | 6328.57 | -3628.57 |
- | 161 | Sewall | 7000.00 | 5 | 6328.57 | 671.43 |
- | 159 | Smith | 8000.00 | 5 | 6328.57 | 1671.43 |
- | 171 | Smith | 7400.00 | 5 | 6328.57 | 1071.43 |
- | 138 | Stiles | 3200.00 | 5 | 6328.57 | -3128.57 |
- | 182 | Sullivan | 2500.00 | 5 | 6328.57 | -3828.57 |
- | 157 | Sully | 9500.00 | 5 | 6328.57 | 3171.43 |
- | 176 | Taylor | 8600.00 | 5 | 6328.57 | 2271.43 |
- | 180 | Taylor | 3200.00 | 5 | 6328.57 | -3128.57 |
- | 117 | Tobias | 2800.00 | 5 | 6328.57 | -3528.57 |
- | 150 | Tucker | 10000.00 | 5 | 6328.57 | 3671.43 |
- | 155 | Tuvault | 7000.00 | 5 | 6328.57 | 671.43 |
- | 112 | Urman | 7800.00 | 5 | 6328.57 | 1471.43 |
- | 144 | Vargas | 2500.00 | 5 | 6328.57 | -3828.57 |
- | 162 | Vishney | 10500.00 | 5 | 6328.57 | 4171.43 |
- | 123 | Vollman | 6500.00 | 5 | 6328.57 | 171.43 |
- | 196 | Walsh | 3100.00 | 5 | 6328.57 | -3228.57 |
- | 120 | Weiss | 8000.00 | 5 | 6328.57 | 1671.43 |
- | 200 | Whalen | 4400.00 | 5 | 6328.57 | -1928.57 |
- | 149 | Zlotkey | 10500.00 | 5 | 6328.57 | 4171.43 |
- ## Задача 7
- ```postgresql
- WITH hire_stats AS (SELECT e.employee_id,
- e.hire_date,
- COUNT(*)
- FILTER (WHERE e2.hire_date BETWEEN e.hire_date - INTERVAL '1 year'
- AND e.hire_date + INTERVAL '1 year') AS hire_count_within_1_year,
- COUNT(*) FILTER (WHERE e2.hire_date > e.hire_date AND
- EXTRACT(YEAR FROM e2.hire_date) =
- EXTRACT(YEAR FROM e.hire_date) OR
- (e2.hire_date = e.hire_date AND e2.employee_id >
- e.employee_id)) AS hire_count_same_year
- FROM employees e
- JOIN employees e2 ON e2.employee_id != e.employee_id
- GROUP BY e.employee_id, e.hire_date)
- SELECT *
- FROM hire_stats;
- ```
- | employee_id | hire_date | hire_count_within_1_year | hire_count_same_year |
- |-------------|------------|--------------------------|----------------------|
- | 184 | 2004-01-27 | 16 | 9 |
- | 116 | 2005-12-24 | 51 | 0 |
- | 179 | 2008-01-04 | 29 | 10 |
- | 138 | 2005-10-26 | 48 | 4 |
- | 181 | 2006-02-23 | 50 | 18 |
- | 205 | 2002-06-07 | 8 | 4 |
- | 146 | 2005-01-05 | 39 | 28 |
- | 190 | 2006-07-11 | 49 | 5 |
- | 201 | 2004-02-17 | 19 | 6 |
- | 162 | 2005-11-11 | 49 | 2 |
- | 105 | 2005-06-25 | 46 | 15 |
- | 132 | 2007-04-10 | 39 | 11 |
- | 174 | 2004-05-11 | 26 | 4 |
- | 107 | 2007-02-07 | 44 | 17 |
- | 134 | 2006-08-26 | 44 | 4 |
- | 144 | 2006-07-09 | 49 | 6 |
- | 170 | 2006-01-24 | 51 | 21 |
- | 192 | 2004-02-04 | 18 | 7 |
- | 176 | 2006-03-24 | 48 | 14 |
- | 169 | 2006-03-23 | 47 | 15 |
- | 168 | 2005-03-11 | 39 | 20 |
- | 163 | 2007-03-19 | 42 | 13 |
- | 101 | 2005-09-21 | 48 | 8 |
- | 175 | 2005-03-19 | 40 | 19 |
- | 180 | 2006-01-24 | 51 | 20 |
- | 115 | 2003-05-18 | 18 | 4 |
- | 114 | 2002-12-07 | 12 | 0 |
- | 102 | 2001-01-13 | 0 | 0 |
- | 167 | 2008-04-21 | 21 | 1 |
- | 112 | 2006-03-07 | 49 | 17 |
- | 109 | 2002-08-16 | 10 | 2 |
- | 108 | 2002-08-17 | 10 | 1 |
- | 158 | 2004-08-01 | 27 | 1 |
- | 156 | 2004-01-30 | 18 | 8 |
- | 197 | 2006-05-23 | 48 | 9 |
- | 155 | 2007-11-23 | 30 | 3 |
- | 135 | 2007-12-12 | 29 | 1 |
- | 118 | 2006-11-15 | 38 | 1 |
- | 133 | 2004-06-14 | 26 | 3 |
- | 195 | 2007-03-17 | 42 | 14 |
- | 111 | 2005-09-30 | 49 | 6 |
- | 127 | 2007-01-14 | 42 | 18 |
- | 124 | 2007-11-16 | 30 | 4 |
- | 199 | 2008-01-13 | 29 | 9 |
- | 123 | 2005-10-10 | 48 | 5 |
- | 128 | 2008-03-08 | 25 | 3 |
- | 500 | 2022-01-01 | 1 | 1 |
- | 126 | 2006-09-28 | 43 | 3 |
- | 194 | 2006-07-01 | 49 | 7 |
- | 173 | 2008-04-21 | 21 | 0 |
- | 149 | 2008-01-29 | 28 | 7 |
- | 200 | 2003-09-17 | 15 | 1 |
- | 166 | 2008-03-24 | 23 | 2 |
- | 103 | 2006-01-03 | 51 | 22 |
- | 189 | 2005-08-13 | 47 | 12 |
- | 161 | 2006-11-03 | 39 | 2 |
- | 142 | 2005-01-29 | 40 | 27 |
- | 121 | 2005-04-10 | 44 | 17 |
- | 117 | 2005-07-24 | 48 | 13 |
- | 188 | 2005-06-14 | 46 | 16 |
- | 152 | 2005-08-20 | 47 | 9 |
- | 206 | 2002-06-07 | 8 | 3 |
- | 125 | 2005-07-16 | 49 | 14 |
- | 119 | 2007-08-10 | 34 | 6 |
- | 113 | 2007-12-07 | 30 | 2 |
- | 153 | 2006-03-30 | 47 | 13 |
- | 157 | 2004-03-04 | 21 | 5 |
- | 147 | 2005-03-10 | 39 | 22 |
- | 202 | 2005-08-17 | 47 | 11 |
- | 196 | 2006-04-24 | 47 | 10 |
- | 183 | 2008-02-03 | 28 | 6 |
- | 120 | 2004-07-18 | 26 | 2 |
- | 100 | 2003-06-17 | 15 | 3 |
- | 136 | 2008-02-06 | 28 | 5 |
- | 150 | 2005-01-30 | 40 | 26 |
- | 140 | 2006-04-06 | 47 | 12 |
- | 139 | 2006-02-12 | 51 | 19 |
- | 193 | 2005-03-03 | 39 | 23 |
- | 187 | 2007-02-07 | 44 | 16 |
- | 164 | 2008-01-24 | 28 | 8 |
- | 137 | 2003-07-14 | 15 | 2 |
- | 191 | 2007-12-19 | 29 | 0 |
- | 160 | 2005-12-15 | 51 | 1 |
- | 159 | 2005-03-10 | 39 | 21 |
- | 172 | 2007-03-24 | 42 | 12 |
- | 141 | 2003-10-17 | 16 | 0 |
- | 122 | 2003-05-01 | 17 | 5 |
- | 186 | 2006-06-24 | 50 | 8 |
- | 154 | 2006-12-09 | 41 | 0 |
- | 131 | 2005-02-16 | 39 | 25 |
- | 198 | 2007-06-21 | 38 | 7 |
- | 177 | 2006-04-23 | 47 | 11 |
- | 182 | 2007-06-21 | 38 | 8 |
- | 204 | 2002-06-07 | 8 | 5 |
- | 106 | 2022-12-15 | 1 | 0 |
- | 104 | 2007-05-21 | 39 | 10 |
- | 185 | 2005-02-20 | 38 | 24 |
- | 110 | 2005-09-28 | 49 | 7 |
- | 171 | 2007-02-23 | 44 | 15 |
- | 178 | 2007-05-24 | 38 | 9 |
- | 145 | 2004-10-01 | 33 | 0 |
- | 148 | 2007-10-15 | 32 | 5 |
- | 130 | 2005-10-30 | 48 | 3 |
- | 129 | 2005-08-20 | 47 | 10 |
- | 165 | 2008-02-23 | 26 | 4 |
- | 143 | 2006-03-15 | 46 | 16 |
- | 203 | 2002-06-07 | 8 | 6 |
- | 151 | 2005-03-24 | 42 | 18 |
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement