Select one column by two different names in the same query

Posted on

Question :

I have a table named Main that contains two fields id,name.

I want to select the name column by two different names. For example:

Table Main

id:int, name:varchar(20)

I have inserted the following records:

id  | name 
  1 | Ahmad 
  2 | Sami 

I want to select the data like this:

select as n1, as n2 from Main
where n1='Ahmad' and n2='Sami'

When I execute this query, it gives the following error:

ERROR: column “n1” does not exist
LINE 1: select n1, n2 from Main where n1=’Ahmad’ …`

I use PostgreSQL as my DBMS.

Answer :

 select as "n1", as "n2"
 from main n1, main n2
 where = 'Ahmad'
     and = 'Sami';

Leave a Reply

Your email address will not be published. Required fields are marked *