

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I return multiple results in a MySQL subquery with IN()?
In MySQL, you can easily return multiple results, but also achieve this with subquery using IN(). Let us first create a table −
mysql> create table DemoTable1317 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command
mysql> insert into DemoTable1317(Name) values('Chris Brown'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1317(Name) values('John Doe'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1317(Name) values('Adam Smith'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1317(Name) values('John Smith'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1317(Name) values('David Miller'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1317(Name) values('Carol Taylor'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1317;
Output
+----+--------------+ | Id | Name | +----+--------------+ | 1 | Chris Brown | | 2 | John Doe | | 3 | Adam Smith | | 4 | John Smith | | 5 | David Miller | | 6 | Carol Taylor | +----+--------------+ 6 rows in set (0.00 sec)
Following is the query to return multiple results in a MySQL subquery −
mysql> select Name from DemoTable1317 -> where Id IN ( select Id from DemoTable1317);
Output
+--------------+ | Name | +--------------+ | Chris Brown | | John Doe | | Adam Smith | | John Smith | | David Miller | | Carol Taylor | +--------------+ 6 rows in set (0.00 sec)
- Related Questions & Answers
- How do I insert multiple values in a column with a single MySQL query?
- How can I use MySQL subquery as a table in FROM clause?
- How do we return multiple values in Python?
- How can we create a MySQL view with a subquery?
- How do I search for names starting with A in MySQL?
- How can we use a MySQL subquery with INSERT statement?
- How can we use a MySQL subquery with FROM clause?
- How do I return a document with filtered sub-documents using Mongo?
- How do I create a view in MySQL?
- How do I remove multiple elements from a list in Java?
- How do I remove a MySQL database?
- How do I loop through a JSON file with multiple keys/sub-keys in Python?
- How to correctly enclose subquery in MySQL?
- How do I insert a NULL value in MySQL?
- How do I exclude a specific record in MySQL?
Advertisements