SQL : Searching a value in all columns of a table

To search for a value in a specific data field(column) of a database table using SQL, you can use the SELECT statement with a WHERE clause to filter the results based on your search condition. Here’s an example:

SELECT *
FROM students
WHERE student_id = 101;

But, when you need to search for a value in multiple data fields(columns) of a table you need to use SELECT statement with LIKE option as follows :

SELECT *
FROM students
WHERE student_name + age + student_id + gender LIKE '%male%';

In the above example, all rows containing the value male in any one(or more) of the columns student_name, age, student_id, gender

To get the same result, you can also try the following sql statement :

SELECT *
FROM students
WHERE  'male' in (student_name , age ,student_id , gender);

You can also try the following which is a bit more manual method using OR operator and lengthy process:

SELECT *
FROM students
WHERE 
      student_name == 'male'
      OR age == 'male'
      OR student_id == 'male'
      OR gender == 'male';


Posted

in

by