w3resource

SQL Exercise: Employees whose last name begins with the letter D

SQL Wildcard & Special Operator: Exercise-22 with Solution

From the following table, write a SQL query to locate the employees whose last name begins with the letter 'D'. Return emp_idno, emp_fname, emp_lname and emp_dept.

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 all columns from the 'emp_details' table.
SELECT *
-- Specifies the table from which to retrieve the data (in this case, 'emp_details').
FROM emp_details
-- Filters the rows to only include those where the 'emp_lname' column starts with the letter 'D'.
WHERE emp_lname LIKE 'D%';

Output of the Query:

emp_idno	emp_fname	emp_lname	emp_dept
843795		Enric		Dosio		57
444527		Joseph		Dosni		47

Code Explanation:

The given query in SQL that retrieve all columns (denoted by "SELECT *") from the 'emp_details' table where the value in the "emp_lname" column starts with the letter 'D'.
In the "WHERE emp_lname LIKE 'D%'" condition, only rows where the value in the "emp_lname" column starts with 'D' are retrieved.
The "%" symbol is a wildcard that matches any number of characters after the specified pattern, in this case 'D'.

Relational Algebra Expression:

Relational Algebra Expression: Display all the data of employees whose last name begins with the letter 'D'.

Relational Algebra Tree:

Relational Algebra Tree: Display all the data of employees whose last name begins with the letter 'D'.

Practice Online


Query Visualization:

Duration:

Query visualization of Display all the data of employees whose last name begins with the letter 'D' - Duration

Rows:

Query visualization of Display all the data of employees whose last name begins with the letter 'D' - Rows

Cost:

Query visualization of Display all the data of employees whose last name begins with the letter 'D' - Cost

Practice Online


Contribute your code and comments through Disqus.

Previous SQL Exercise: Using where clause with not operator and NULL.
Next SQL Exercise: SQL Aggregate Functions Exercises Home

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.