Syntax error MySQL query to exclude some of the values from the table

MySQL query to exclude some of the values from the table



Use NOT IN() to exclude some of the values from the table.

Let us first create a table −

mysql> create table DemoTable791 (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FirstName varchar(100)
);
Query OK, 0 rows affected (0.61 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable791(FirstName) values('Chris');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable791(FirstName) values('Robert');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable791(FirstName) values('David');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable791(FirstName) values('Mike');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable791(FirstName) values('Bob');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable791(FirstName) values('Carol');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable791(FirstName) values('Adam');
Query OK, 1 row affected (0.09 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable791;

This will produce the following output -

+----+-----------+
| Id | FirstName |
+----+-----------+
| 1  | Chris     |
| 2  | Robert    |
| 3  | David     |
| 4  | Mike      |
| 5  | Bob       |
| 6  | Carol     |
| 7  | Adam      |
+----+-----------+
7 rows in set (0.00 sec)

Following is the query to exclude some of the values from the table using NOT IN() in MySQL −

mysql> select *from DemoTable791 where Id NOT IN(4,5,6);

This will produce the following output -

+----+-----------+
| Id | FirstName |
+----+-----------+
| 1  | Chris     |
| 2  | Robert    |
| 3  | David     |
| 7  | Adam      |
+----+-----------+
4 rows in set (0.00 sec)
Updated on: 2019-09-09T08:43:11+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements