w3resource

MySQL String Exercises: Find all employees where first names are in upper case

MySQL String: Exercise-8 with Solution

Write a MySQL query to find all employees where first names are in upper case.

Sample table: employees

+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
| EMPLOYEE_ID | FIRST_NAME  | LAST_NAME   | EMAIL    | PHONE_NUMBER       | HIRE_DATE  | JOB_ID     | SALARY   | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID |
+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
|         100 | Steven      | King        | SKING    | 515.123.4567       | 1987-06-17 | AD_PRES    | 24000.00 |           0.00 |          0 |   		  90 |
|         101 | Neena       | Kochhar     | NKOCHHAR | 515.123.4568       | 1987-06-18 | AD_VP      | 17000.00 |           0.00 |        100 |            90 |
|         102 | Lex         | De Haan     | LDEHAAN  | 515.123.4569       | 1987-06-19 | AD_VP      | 17000.00 |           0.00 |        100 |            90 |
|         103 | Alexander   | Hunold      | AHUNOLD  | 590.423.4567       | 1987-06-20 | IT_PROG    |  9000.00 |           0.00 |        102 |            60 |
|         104 | Bruce       | Ernst       | BERNST   | 590.423.4568       | 1987-06-21 | IT_PROG    |  6000.00 |           0.00 |        103 |            60 |
|         105 | David       | Austin      | DAUSTIN  | 590.423.4569       | 1987-06-22 | IT_PROG    |  4800.00 |           0.00 |        103 |            60 |
|         106 | Valli       | Pataballa   | VPATABAL | 590.423.4560       | 1987-06-23 | IT_PROG    |  4800.00 |           0.00 |        103 |            60 |
|         107 | Diana       | Lorentz     | DLORENTZ | 590.423.5567       | 1987-06-24 | IT_PROG    |  4200.00 |           0.00 |        103 |            60 |
|         108 | Nancy       | Greenberg   | NGREENBE | 515.124.4569       | 1987-06-25 | FI_MGR     | 12000.00 |           0.00 |        101 |           100 |
..........
|         206 | William     | Gietz       | WGIETZ   | 515.123.8181       | 1987-10-01 | AC_ACCOUNT |  8300.00 |           0.00 |        205 |           110 |
+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+

View the table

Code:

-- This SQL query selects all columns from the employees table where the first name is in uppercase.

SELECT 
    * -- Selecting all columns from the employees table.

FROM 
employees -- Specifies the table from which data is being retrieved, in this case, it's the 'employees' table.

WHERE 
first_name = BINARY UPPER(first_name);

Explanation:

  • The SELECT statement retrieves all columns from the specified table (employees).
  • The WHERE clause filters the rows where the first_name column is equal to its uppercase version.
  • UPPER(first_name) converts the first_name column values to uppercase.
  • The BINARY keyword ensures a case-sensitive comparison, ensuring that only names that are completely uppercase are selected. If it were not for the BINARY keyword, MySQL would perform a case-insensitive comparison by default.

Sample Output:

EMPLOYEE_ID	FIRST_NAME	LAST_NAME	EMAIL	PHONE_NUMBER	HIRE_DATE	JOB_ID		SALARY	COMMISSION_PCT	MANAGER_ID	DEPARTMENT_ID
132		TJ 		Olson		TJOLSON	 650.124.8234	1987-07-19T04:00:00.000Z ST_CLERK 2100	0		121		50

Go to:


PREV :Write a MySQL query to get the employee id, email id (discard the last three characters).
NEXT :Write a MySQL query to extract the last 4 character of phone numbers.

MySQL Code Editor:

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

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.