w3resource

SQL Exercise: Produce the output of employees

SQL employee Database: Exercise-6 with Solution

[An editor is available at the bottom of the page to write and execute the scripts.]

6. Write a query in SQL to produce the output of employees as follows:
Employee
JONAS(manager).

Sample table: employees


Pictorial Presentation:

SQL exercises on employee Database: Produce the output of employees

Sample Solution:

SELECT emp_name || '('|| lower(job_name)||')' AS "Employee"
FROM employees;

Sample Output:

      Employee
--------------------
 KAYLING(president)
 BLAZE(manager)
 CLARE(manager)
 JONAS(manager)
 SCARLET(analyst)
 FRANK(analyst)
 SANDRINE(clerk)
 ADELYN(salesman)
 WADE(salesman)
 MADDEN(salesman)
 TUCKER(salesman)
 ADNRES(clerk)
 JULIUS(clerk)
 MARKER(clerk)
(14 rows)

Explanation:

The said query in SQL that retrieves a result set that includes a list of concatenated strings, where each string represents an employee name and job name enclosed in parentheses and with the job name in lowercase.

The concatenation is performed using the concatenation operator || and a string literal '(' and ')', with the lower() function used to convert the "job_name" to lowercase and the AS keyword assigns the name "Employee" to the newly created column.

Practice Online


Sample Database: employee

employee database structure

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

Previous SQL Exercise: Produce the output of employees name and job name.
Next SQL Exercise: Employees with Hire date in format February 22, 1991.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.