Select Statement to retrieve the same first names with similar last name (but different case) using MySQL?


Let us first create a table −

mysql> create table DemoTable623 (FirstName varchar(100),LastName varchar(100),Age int);
Query OK, 0 rows affected (0.76 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable623 values('John','Smith',23);
Query OK, 1 row affected (0.66 sec)
mysql> insert into DemoTable623 values('Adam','smith',23);
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable623 values('Chris','Brown',24);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable623 values('Robert','brown',21);
Query OK, 1 row affected (0.22 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable623;

This will produce the following output −

+-----------+----------+------+
| FirstName | LastName | Age  | 
+-----------+----------+------+
| John      | Smith    |   23 |
| Adam      | smith    |   23 |
| Chris     | Brown    |   24 |
| Robert    | brown    |   21 |
+-----------+----------+------+
4 rows in set (0.00 sec)

Here is the query to select statement using MySQL −

mysql> select FirstName from DemoTable623 where lower(LastName) ='smith';

This will produce the following output −

+-----------+
| FirstName |
+-----------+
| John      |
| Adam      |
+-----------+
2 rows in set (0.00 sec)

Updated on: 23-Aug-2019

298 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements