PostgreSQL Basic SELECT Statement: Check whether the first_name column of the employees containing any number
17. Write a query to check whether the first_name column of the employees table containing any number.
Sample Solution:
Code:
-- Selecting all columns from the employees table
SELECT *
-- Selecting data from the employees table
FROM employees
-- Filtering the result set to include only records where the first name contains a digit (0-9)
WHERE first_name
SIMILAR TO '%0|1|2|3|4|5|6|7|8|9%';
Explanation:
- This SQL code selects all columns from the "employees" table.
- It filters the result set to include only records where the first name contains a digit (0-9).
- The SIMILAR TO operator is used to perform a pattern matching operation. In this case, it checks if the first name contains any digit from 0 to 9.
- The result set will contain records where the first name matches the specified pattern.
Sample table: employees
Output:
employee_id | first_name | last_name | email | phone_number | hire_date | job_id | salary | commission_pct | manager_id | department_id -------------+------------+-----------+-------+--------------+-----------+--------+--------+----------------+------------+--------------- (0 rows)
Practice Online
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a query to get the first name, last name and the length of the name, including first_name and last_name of all the employees from employees table.
Next: Write a query to select first ten records from a table.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics