w3resource

MySQL Exercise: Display the last name of employees having 'e' as the third character

MySQL Restricting and Sorting Data: Exercise-8 with Solution

Write a query to display the last name of employees having 'e' as the third character.

Sample table: employees


Code:

-- Selecting the last_name column
SELECT last_name
-- Selecting data from the employees table
FROM employees 
-- Filtering the result set to include only rows where the last_name column starts with two characters followed by 'e' and any other characters
WHERE last_name LIKE '__e%';

Explanation:

  • This SQL query selects the last_name column from the employees table.
  • The WHERE clause filters the result set to include only those rows where the last_name column:
    • Starts with any two characters (represented by the two underscores).
    • Followed by 'e'.
    • Followed by any number of characters (represented by '%').
  • The LIKE operator is used with the pattern '__e%', where each underscore represents a single character wildcard and '%' represents any number of characters.
  • This query is useful when you want to retrieve employees whose last names start with specific characters followed by 'e' and any other characters, such as finding names like 'Greene' or 'Peters'.

Relational Algebra Expression:

Relational Algebra Expression: Restricting and Sorting Data: Display the last name of employees having 'e' as the third character.

Relational Algebra Tree:

Relational Algebra Tree: Basic SELECT statement: Display the last name of employees having 'e' as the third character.

Pictorial Presentation of the above query

Pictorial: Query to display the last names of employees having 'e' as the third character

 

MySQL Code Editor:

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

Previous:Write a query to display the last name of employees whose name have exactly 6 characters.
Next:Write a query to display the jobs/designations available in the employees table.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.