﻿ SQL SUBQUERIES: Display the employee id, name, SalaryDrawn, AvgCompare and the SalaryStatus column with a title HIGH and LOW respectively for those employees whose salary is more than and less than the average salary of all employees - w3resource

# SQL Subquery Exercises: Display the employee id, name, SalaryDrawn, AvgCompare and the SalaryStatus column with a title HIGH and LOW respectively for those employees whose salary is more than and less than the average salary of all employees

## SQL SUBQUERY: Exercise-25 with Solution

Write a query to display the employee id, name ( first name and last name ), SalaryDrawn, AvgCompare (salary - the average salary of all employees) and the SalaryStatus column with a title HIGH and LOW respectively for those employees whose salary is more than and less than the average salary of all employees.

Sample table: employees

Sample Solution:

``````SELECT  employee_id,  first_name, last_name,  salary AS SalaryDrawn,
ROUND((salary -(SELECT AVG(salary) FROM employees)),2) AS AvgCompare,
CASE  WHEN salary >=
(SELECT AVG(salary)
FROM employees) THEN 'HIGH'
ELSE 'LOW'
END AS SalaryStatus
FROM employees;
``````

Sample Output:

```employee_id	first_name	last_name	salarydrawn	avgcompare	salarystatus
100		Steven		King		24000.00	17538.32	HIGH
101		Neena		Kochhar		17000.00	10538.32	HIGH
102		Lex		De Haan		17000.00	10538.32	HIGH
103		Alexander	Hunold		9000.00		2538.32		HIGH
104		Bruce		Ernst		6000.00		-461.68		LOW
105		David		Austin		4800.00		-1661.68	LOW
106		Valli		Pataballa	4800.00		-1661.68	LOW
107		Diana		Lorentz		4200.00		-2261.68	LOW
108		Nancy		Greenberg	12000.00	5538.32		HIGH
109		Daniel		Faviet		9000.00		2538.32		HIGH
110		John		Chen		8200.00		1738.32		HIGH
111		Ismael		Sciarra		7700.00		1238.32		HIGH
112		Jose 	Manuel	Urman		7800.00		1338.32		HIGH
113		Luis		Popp		6900.00		438.32		HIGH
114		Den		Raphaely	11000.00	4538.32		HIGH
115		Alexander	Khoo		3100.00		-3361.68	LOW
116		Shelli		Baida		2900.00		-3561.68	LOW
117		Sigal		Tobias		2800.00		-3661.68	LOW
118		Guy		Himuro		2600.00		-3861.68	LOW
119		Karen		Colmenares	2500.00		-3961.68	LOW
120		Matthew		Weiss		8000.00		1538.32		HIGH
121		Adam		Fripp		8200.00		1738.32		HIGH
122		Payam		Kaufling	7900.00		1438.32		HIGH
123		Shanta		Vollman		6500.00		38.32		HIGH
124		Kevin		Mourgos		5800.00		-661.68		LOW
125		Julia		Nayer		3200.00		-3261.68	LOW
126		Irene		Mikkilineni	2700.00		-3761.68	LOW
127		James		Landry		2400.00		-4061.68	LOW
128		Steven		Markle		2200.00		-4261.68	LOW
129		Laura		Bissot		3300.00		-3161.68	LOW
130		Mozhe		Atkinson	2800.00		-3661.68	LOW
131		James		Marlow		2500.00		-3961.68	LOW
132		TJ		Olson		2100.00		-4361.68	LOW
133		Jason		Mallin		3300.00		-3161.68	LOW
134		Michael		Rogers		2900.00		-3561.68	LOW
135		Ki		Gee		2400.00		-4061.68	LOW
136		Hazel		Philtanker	2200.00		-4261.68	LOW
137		Renske		Ladwig		3600.00		-2861.68	LOW
138		Stephen		Stiles		3200.00		-3261.68	LOW
139		John		Seo		2700.00		-3761.68	LOW
140		Joshua		Patel		2500.00		-3961.68	LOW
141		Trenna		Rajs		3500.00		-2961.68	LOW
142		Curtis		Davies		3100.00		-3361.68	LOW
143		Randall		Matos		2600.00		-3861.68	LOW
144		Peter		Vargas		2500.00		-3961.68	LOW
145		John		Russell		14000.00	7538.32		HIGH
146		Karen		Partners	13500.00	7038.32		HIGH
147		Alberto		Errazuriz	12000.00	5538.32		HIGH
148		Gerald		Cambrault	11000.00	4538.32		HIGH
149		Eleni		Zlotkey		10500.00	4038.32		HIGH
150		Peter		Tucker		10000.00	3538.32		HIGH
151		David		Bernstein	9500.00		3038.32		HIGH
152		Peter		Hall		9000.00		2538.32		HIGH
153		Christopher	Olsen		8000.00		1538.32		HIGH
154		Nanette		Cambrault	7500.00		1038.32		HIGH
155		Oliver		Tuvault		7000.00		538.32		HIGH
156		Janette		King		10000.00	3538.32		HIGH
157		Patrick		Sully		9500.00		3038.32		HIGH
158		Allan		McEwen		9000.00		2538.32		HIGH
159		Lindsey		Smith		8000.00		1538.32		HIGH
160		Louise		Doran		7500.00		1038.32		HIGH
161		Sarath		Sewall		7000.00		538.32		HIGH
162		Clara		Vishney		10500.00	4038.32		HIGH
163		Danielle	Greene		9500.00		3038.32		HIGH
164		Mattea		Marvins		7200.00		738.32		HIGH
165		David		Lee		6800.00		338.32		HIGH
166		Sundar		Ande		6400.00		-61.68		LOW
167		Amit		Banda		6200.00		-261.68		LOW
168		Lisa		Ozer		11500.00	5038.32		HIGH
169		Harrison	Bloom		10000.00	3538.32		HIGH
170		Tayler		Fox		9600.00		3138.32		HIGH
171		William		Smith		7400.00		938.32		HIGH
172		Elizabeth	Bates		7300.00		838.32		HIGH
173		Sundita		Kumar		6100.00		-361.68		LOW
174		Ellen		Abel		11000.00	4538.32		HIGH
175		Alyssa		Hutton		8800.00		2338.32		HIGH
176		Jonathon	Taylor		8600.00		2138.32		HIGH
177		Jack		Livingston	8400.00		1938.32		HIGH
178		Kimberely	Grant		7000.00		538.32		HIGH
179		Charles		Johnson		6200.00		-261.68		LOW
180		Winston		Taylor		3200.00		-3261.68	LOW
181		Jean		Fleaur		3100.00		-3361.68	LOW
182		Martha		Sullivan	2500.00		-3961.68	LOW
183		Girard		Geoni		2800.00		-3661.68	LOW
184		Nandita		Sarchand	4200.00		-2261.68	LOW
185		Alexis		Bull		4100.00		-2361.68	LOW
186		Julia		Dellinger	3400.00		-3061.68	LOW
187		Anthony		Cabrio		3000.00		-3461.68	LOW
188		Kelly		Chung		3800.00		-2661.68	LOW
189		Jennifer	Dilly		3600.00		-2861.68	LOW
190		Timothy		Gates		2900.00		-3561.68	LOW
191		Randall		Perkins		2500.00		-3961.68	LOW
192		Sarah		Bell		4000.00		-2461.68	LOW
193		Britney		Everett		3900.00		-2561.68	LOW
194		Samuel		McCain		3200.00		-3261.68	LOW
195		Vance		Jones		2800.00		-3661.68	LOW
196		Alana		Walsh		3100.00		-3361.68	LOW
197		Kevin		Feeney		3000.00		-3461.68	LOW
198		Donald		OConnell	2600.00		-3861.68	LOW
199		Douglas		Grant		2600.00		-3861.68	LOW
200		Jennifer	Whalen		4400.00		-2061.68	LOW
201		Michael		Hartstein	13000.00	6538.32		HIGH
202		Pat		Fay		6000.00		-461.68		LOW
203		Susan		Mavris		6500.00		38.32		HIGH
204		Hermann		Baer		10000.00	3538.32		HIGH
205		Shelley		Higgins		12000.00	5538.32		HIGH
206		William		Gietz		8300.00		1838.32		HIGH
```

## Query Visualization:

Duration:

Rows:

Cost:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?

﻿

## SQL: Tips of the Day

How to count occurrences of a column value in SQL?

Input table:

```id | age
--------
0  | 25
1  | 25
2  | 23
```
```SELECT age, count(age)
FROM Students
GROUP by age
```

Output:

```id | age | count
----------------
0  | 25  | 2
1  | 25  | 2
2  | 23  | 1
```

Ref: https://bit.ly/3zbLPQm