- 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 combine different columns of a table to yield a single column in query output in PostgreSQL?
Suppose you have a table user_info that contains the state and district of different users. An example is given below −
name | district | state |
---|---|---|
Anil | Mumbai | Maharashtra |
Joy | Jhalawar | Rajasthan |
Ron | Pune | Maharashtra |
Reena | Meerut | Uttar Pradesh |
Now, if you want to combine the state and district in a single field called location, this is how you should be able to do it −
SELECT name, district || ', ' || state as location from user_info
The || operator is the string concatenation operator. The output will be −
name | location |
---|---|
Anil | Mumbai, Maharashtra |
Joy | Jhalawar, Rajasthan |
Ron | Pune, Maharashtra |
Reena | Meerut, Uttar Pradesh |
Similar operations can also be performed on numerical values. Suppose you have a table marks containing the total marks scored by students and the maximum possible marks. An example is given below −
name | marks_scored | max_marks |
---|---|---|
Anil | 342 | 600 |
Joy | 567 | 600 |
Ron | 456 | 600 |
Reena | 543 | 600 |
Now, if you wish to output a new column containing percentage marks, based on marks_scored and max_marks, you can do that as follows −
SELECT name, marks_scored*100.0/max_marks as perc from marks
The output will look like this −
name | perc |
---|---|
Anil | 57 |
Joy | 94.5 |
Ron | 76 |
Reena | 90.5 |
- Related Articles
- MySQL query to combine two columns in a single column?
- How to alter column type of multiple columns in a single MySQL query?
- How to merge queries in a single MySQL query to get the count of different values in different columns?
- How to define and query json columns in PostgreSQL?
- Count two different columns in a single query in MySQL?
- Combine columns before matching it with LIKE in a single query in MySQL?
- How can we combine values of two or more columns of MySQL table and get that value in a single column?
- How to add column to an existing table in PostgreSQL?
- How to create a table in PostgreSQL?
- How to Query a DB in pgAdmin in PostgreSQL?
- MySQL query to sort multiple columns together in a single query
- How to sort multiple columns with a single query?
- Set all the columns of a MySQL table to a particular value with a single query
- A single MySQL query to search multiple words from different column values
- Display substrings of different length with a single MySQL query and combine the result

Advertisements