w3resource

SQL Exercises: Find the last name of all employees, without duplicates

SQL Basic Select Statement: Exercise-31 with Solution.

From the following table, write a SQL query to find the unique last name of all employees. Return emp_lname.

Sample table: emp_details

 EMP_IDNO EMP_FNAME       EMP_LNAME         EMP_DEPT
--------- --------------- --------------- ----------
   127323 Michale         Robbin                  57
   526689 Carlos          Snares                  63
   843795 Enric           Dosio                   57
   328717 Jhon            Snares                  63
   444527 Joseph          Dosni                   47
   659831 Zanifer         Emily                   47
   847674 Kuleswar        Sitaraman               57
   748681 Henrey          Gabriel                 47
   555935 Alex            Manuel                  57
   539569 George          Mardy                   27
   733843 Mario           Saule                   63
   631548 Alan            Snappy                  27
   839139 Maria           Foster                  57

Sample Solution:

-- This query selects distinct values from the 'emp_lname' column of the 'emp_details' table.
SELECT DISTINCT emp_lname
-- Specifies the table from which to retrieve the data (in this case, 'emp_details').
FROM emp_details;

Output of the Query:

emp_lname
Saule
Robbin
Sitaraman
Mardy
Foster
Emily
Manuel
Dosio
Gabriel
Snappy
Snares
Dosni

Code Explanation:

The given SQL statement selects the distinct emp_lname (last name) values from the table named 'emp_details'. That is, it returns a list of unique last names of employees without any duplicates.

Relational Algebra Expression:

Relational Algebra Expression: Find the last name of all employees, without duplicates.

Relational Algebra Tree:

Relational Algebra Tree: Find the last name of all employees, without duplicates.

Practice Online


Query Visualization:

Duration:

Query visualization of Find the last name of all employees, without duplicates - Duration

Rows:

Query visualization of Find the last name of all employees, without duplicates - Rows

Cost:

Query visualization of Find the last name of all employees, without duplicates - Cost

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

Previous Python Exercise: Find the name and price of the cheapest item(s).
Next Python Exercise: Find the data of employees whose last name is 'Snares'.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/sql-exercises/sql-retrieve-exercise-31.php