- 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
How to add a column using MySQL SELECT in an already created table?
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.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Name,Age) values('Robert',24); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Name,Age) values('Chris',22); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+--------+------+ | Id | Name | Age | +----+--------+------+ | 1 | Robert | 24 | | 2 | Chris | 22 | +----+--------+------+ 2 rows in set (0.00 sec)
Here is the query to add a new column while using SELECT −
mysql> select Id,Name,Age,'US' AS DefaultCountryName from DemoTable;
Output
+----+--------+------+--------------------+ | Id | Name | Age | DefaultCountryName | +----+--------+------+--------------------+ | 1 | Robert | 24 | US | | 2 | Chris | 22 | US | +----+--------+------+--------------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to add NOT NULL constraint to an already created MySQL column?
- How to create and fill a new column in an already created MySQL table?
- How to insert auto_increment in an already created table in MySQL?
- How to add a “created at” column in a table to set the timestamp in MySQL?
- MongoDB query to add a document in an already created collection
- Create a MySQL table from already created table selecting specific rows?
- Select words from a text already in a MySQL table
- Inserting data into a new column of an already existing table in MySQL?
- How to check an empty table already in a MySQL database?
- How to add a column in a table in MySQL?
- How can a new column be created to a dataframe using the already present columns in Python?
- How to randomize an already created vector in R?
- How to add a column to a MySQL table in Python?
- How to add a new column to an existing table using JDBC API?
- How to add subtotal to a table column displaying NULL in MySQL?

Advertisements