w3resource

MySQL String Exercises: Write a query to display the first eight characters of the employees

MySQL String: Exercise-16 with Solution

Write a query to display the first eight characters of the employees' first names and indicates the amounts of their salaries with '$' sign. Each '$' sign signifies a thousand dollars. Sort the data in descending order of salary.

Sample table: employees


Code:

SELECT left(first_name, 8),
REPEAT('$', FLOOR(salary/1000)) 
'SALARY($)', salary
FROM employees
ORDER BY salary DESC;

Sample Output:

left(first_name, 8)			SALARY($)		salary
Steven			$$$$$$$$$$$$$$$$$$$$$$$$		24000
Neena			$$$$$$$$$$$$$$$$$			17000
Lex			$$$$$$$$$$$$$$$$$			17000
John			$$$$$$$$$$$$$$				14000
Karen			$$$$$$$$$$$$$				13500
Michael			$$$$$$$$$$$$$				13000
Nancy			$$$$$$$$$$$$				12000
Alberto			$$$$$$$$$$$$				12000
Shelley			$$$$$$$$$$$$				12000
Lisa			$$$$$$$$$$$				11500
Den			$$$$$$$$$$$				11000
Gerald			$$$$$$$$$$$				11000
Ellen			$$$$$$$$$$$				11000
Eleni			$$$$$$$$$$				10500
Clara			$$$$$$$$$$				10500
Peter			$$$$$$$$$$				10000
Janette			$$$$$$$$$$				10000
Harrison		$$$$$$$$$$				10000
Hermann			$$$$$$$$$$				10000
Tayler			$$$$$$$$$				9600
David			$$$$$$$$$				9500
Patrick			$$$$$$$$$				9500
Danielle		$$$$$$$$$				9500
Alexande		$$$$$$$$$				9000
Daniel			$$$$$$$$$				9000
Peter			$$$$$$$$$				9000
Allan			$$$$$$$$$				9000
Alyssa			$$$$$$$$				8800
Jonathon		$$$$$$$$				8600
Jack			$$$$$$$$				8400
William			$$$$$$$$				8300
John			$$$$$$$$				8200
Adam			$$$$$$$$				8200
Matthew			$$$$$$$$				8000
Christop		$$$$$$$$				8000
Lindsey			$$$$$$$$				8000
Payam			$$$$$$$					7900
Jose Man		$$$$$$$					7800
Ismael			$$$$$$$					7700
Nanette			$$$$$$$					7500
Louise			$$$$$$$					7500
William			$$$$$$$					7400
Elizabet		$$$$$$$					7300
Mattea			$$$$$$$					7200
Oliver			$$$$$$$					7000
Sarath			$$$$$$$					7000
Kimberel		$$$$$$$					7000
Luis			$$$$$$					6900
David			$$$$$$					6800
Shanta			$$$$$$					6500
Susan			$$$$$$					6500
Sundar			$$$$$$					6400
Amit			$$$$$$					6200
Charles			$$$$$$					6200
Sundita			$$$$$$					6100
Bruce			$$$$$$					6000
Pat			$$$$$$					6000
Kevin			$$$$$					5800
David			$$$$					4800
Valli			$$$$					4800
Jennifer		$$$$					4400
Diana			$$$$					4200
Nandita			$$$$					4200
Alexis			$$$$					4100
Sarah			$$$$					4000
Britney			$$$					3900
Kelly			$$$					3800
Renske			$$$					3600
Jennifer		$$$					3600
Trenna			$$$					3500
Julia			$$$					3400
Laura			$$$					3300
Jason			$$$					3300
Julia			$$$					3200
Stephen			$$$					3200
Winston			$$$					3200
Samuel			$$$					3200
Alexande		$$$					3100
Curtis			$$$					3100
Jean			$$$					3100
Alana			$$$					3100
Anthony			$$$					3000
Kevin			$$$					3000
Shelli			$$					2900
Michael			$$					2900
Timothy			$$					2900
Sigal			$$					2800
Mozhe			$$					2800
Girard			$$					2800
Vance			$$					2800
Irene			$$					2700
John			$$					2700
Guy			$$					2600
Randall			$$					2600
Donald			$$					2600
Douglas			$$					2600
Karen			$$					2500
James			$$					2500
Joshua			$$					2500
Peter			$$					2500
Martha			$$					2500
Randall			$$					2500
James			$$					2400
Ki			$$					2400
Steven			$$					2200
Hazel			$$					2200
TJ			$$					2100

MySQL Code Editor:

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

Previous:Write a query to display the first name and salary for all employees. Format the salary to be 10 characters long, left-padded with the $ symbol.
Next:Write a query to display the employees with their code, first name, last name and hire date who hired either on seventh day of any month or seventh month in any year.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.