- 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
Create a MySQL table from already created table selecting specific rows?
To create a table from an already created table, use CREATE TABLE AS SELECT statement. Let us first create a table −
mysql> create table DemoTable1318 -> ( -> Id int, -> FirstName varchar(10), -> LastName varchar(10), -> Age int -> ); Query OK, 0 rows affected (0.50 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1318 values(1,'Chris','Brown',21); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1318 values(2,'David','Miller',24); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1318 values(3,'Carol','Taylor',23); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1318;
Output
+------+-----------+----------+------+ | Id | FirstName | LastName | Age | +------+-----------+----------+------+ | 1 | Chris | Brown | 21 | | 2 | David | Miller | 24 | | 3 | Carol | Taylor | 23 | +------+-----------+----------+------+ 3 rows in set (0.00 sec)
Following is the query to create a table from an already created table selecting specific rows −
mysql> create table DemoTable1319 -> as select *from DemoTable1318 -> where Age IN(21,23); Query OK, 2 rows affected (0.81 sec) Records: 2 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select * from DemoTable1319;
Output
+------+-----------+----------+------+ | Id | FirstName | LastName | Age | +------+-----------+----------+------+ | 1 | Chris | Brown | 21 | | 3 | Carol | Taylor | 23 | +------+-----------+----------+------+ 2 rows in set (0.00 sec)
- Related Articles
- How can we create a new MySQL table by selecting specific column/s from another existing table?
- Selecting data from a MySQL table based on a specific month?
- How to create and fill a new column in an already created MySQL table?
- Selecting and displaying only some rows from a column in a MySQL table
- How to insert auto_increment in an already created table in MySQL?
- How to add a column using MySQL SELECT in an already created table?
- Fetch specific rows from a MySQL table with duplicate column values (names)?
- Delete only specific rows in a table with MySQL
- Select words from a text already in a MySQL table
- Selecting from a MySQL table based on parts of a timestamp?
- Create MySQL query to create a table from an existing table?
- Fetch random rows from a table with MySQL
- How can we create a MySQL view by selecting some range of values from a base table?
- How can we create MySQL view by selecting data based on pattern matching from base table?
- Selecting the top occurring entries in MySQL from a table with duplicate values?

Advertisements