How to exclude a specific row from a table in MySQL?


Use <> i.e. not equal in MySQL to exclude a specific row from a table. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Id int,
   -> FirstName varchar(100)
   -> );
Query OK, 0 rows affected (0.58 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(1,'John');
Query OK, 1 row affected (0.12 sec)

mysql> insert into DemoTable values(2,'Chris');
Query OK, 1 row affected (0.15 sec)

mysql> insert into DemoTable values(3,'Robert');
Query OK, 1 row affected (0.26 sec)

mysql> insert into DemoTable values(4,'David');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+------+-----------+
| Id   | FirstName |
+------+-----------+
|    1 | John      |
|    2 | Chris     |
|    3 | Robert    |
|    4 | David     |
+------+-----------+
4 rows in set (0.00 sec)

Here is the query to exclude a specific row from the table −

mysql> select Id,FirstName from DemoTable where Id <> 2;

Ouptut

This will produce the following output −

+------+-----------+
| Id   | FirstName |
+------+-----------+
| 1    | John      |
| 3    | Robert    |
| 4    | David     |
+------+-----------+
3 rows in set (0.00 sec)

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 30-Jun-2020

445 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements