
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Subquery to exclude a particular row in MySQL
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100), -> Age int -> ); Query OK, 0 rows affected (0.87 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Name,Age) values('John',21); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Name,Age) values('Carol',22); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name,Age) values('David',23); Query OK, 1 row affected (0.54 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | John | 21 | | 2 | Carol | 22 | | 3 | David | 23 | +----+-------+------+ 3 rows in set (0.00 sec)
Here is the subquery excluding particular row, with Name Carol −
mysql> select *from DemoTable -> where Name not in (select Name from DemoTable where Name='Carol' -> );
Output
This will produce the following output −
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | John | 21 | | 3 | David | 23 | +----+-------+------+ 2 rows in set (0.00 sec)
- Related Articles
- How to exclude a specific row from a table in MySQL?
- How to correctly enclose subquery in MySQL?
- How to use actual row count (COUNT(*)) in WHERE clause without writing the same query as subquery in MySql?
- How can we fetch a particular row as output from a MySQL table?
- How do I exclude a specific record in MySQL?
- MySQL SELECT from a subquery and then perform DELETE?
- How can we create a MySQL view with a subquery?
- How do I return multiple results in a MySQL subquery with IN()?
- How can we use a MySQL subquery with INSERT statement?
- How can we use a MySQL subquery with FROM clause?
- Get second largest marks from a MySQL table using subquery?
- Exclude certain columns from SHOW COLUMNS in MySQL?
- In MySQL how to replace all NULL values in a particular field of a particular table?
- How can we nest a subquery within another subquery?
- How can I use MySQL subquery as a table in FROM clause?

Advertisements