- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 −
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 −
|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 −
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 −
- MySQL query to combine two columns in a single column?
- How to rename an existing column of a table in PostgreSQL?
- How to alter column type of multiple columns in a single MySQL query?
- Count two different columns in a single query in MySQL?
- 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?
- Combine columns before matching it with LIKE in a single query in MySQL?
- How to create a table in PostgreSQL?
- How can we combine values of two or more columns of MySQL table and get that value in a single column?
- Display substrings of different length with a single MySQL query and combine the result
- How to add column to an existing table in PostgreSQL?
- Set all the columns of a MySQL table to a particular value with a single query
- How to sort multiple columns with a single query?
- A single MySQL query to search multiple words from different column values
- How to Query a DB in pgAdmin in PostgreSQL?