- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL SELECT from a subquery and then perform DELETE?
Let us first create a table −
mysql> create table DemoTable1947 ( Id int, Name varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1947 values(101,'Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1947 values(102,'David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1947 values(103,'Mike'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1947 values(104,'Bob'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1947 values(105,'Sam'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1947;
This will produce the following output −
+------+-------+ | Id | Name | +------+-------+ | 101 | Chris | | 102 | David | | 103 | Mike | | 104 | Bob | | 105 | Sam | +------+-------+ 5 rows in set (0.00 sec)
Here is the query to implement select * from subquery and then perform DELETE −
mysql> delete from DemoTable1947 where Id IN(select * from (select Id from DemoTable1947 order by Id limit 3)as t); Query OK, 3 rows affected (0.00 sec)
Let us check the table records once again −
mysql> select * from DemoTable1947;
This will produce the following output −
+------+------+ | Id | Name | +------+------+ | 104 | Bob | | 105 | Sam | +------+------+ 2 rows in set (0.00 sec)
- Related Articles
- Perform Multi-table delete in MySQL
- Perform MySQL delete under safe mode?
- How can we use a MySQL subquery with FROM clause?
- Get second largest marks from a MySQL table using subquery?
- Perform case insensitive SELECT using MySQL IN()?
- Perform MySQL SELECT INTO user-defined variable
- Perform multiple inserts with INSERT INTO SELECT and UNION in MySQL
- How to perform SELECT using COUNT in MySQL?
- Perform MySQL SELECT on fields containing null values?
- How can I use MySQL subquery as a table in FROM clause?
- Delete multiple entries from a MySQL table
- MySQL query to perform delete operation where id is the biggest?
- Perform multiplication in SELECT depending on column value in MySQL?
- Subquery to exclude a particular row in MySQL
- Delete all records from a table in MySQL?

Advertisements