
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Return Multiple Results in 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)
Advertisements