w3resource

PostgreSQL Basic SELECT Statement: Display specific columns using alias


1. Write a query to display the names (first_name, last_name) using an alias name "First Name", "Last Name".

Sample Solution:

Code:

-- This SQL query selects the 'first_name' column and renames it as 'First Name', and selects the 'last_name' column and renames it as 'Last Name' from the 'employees' table.

SELECT first_name "First Name", last_name "Last Name" 
FROM employees;

Explanation:

  • The SELECT statement is used to retrieve data from a database table.
  • first_name and last_name are the names of the columns being selected from the employees table.
  • "First Name" and "Last Name" are aliases assigned to the selected columns, which will be displayed as column headers in the result set.
  • This query retrieves the first name and last name of employees from the 'employees' table and presents them with the specified aliases.

Sample table: employees


Output:

pg_exercises=# SELECT first_name "First Name",  last_name "Last Name" 
pg_exercises-# FROM employees;

 First Name  |  Last Name
-------------+-------------
 Steven      | King
 Neena       | Kochhar
 Lex         | De Haan
 Alexander   | Hunold
 Bruce       | Ernst
 David       | Austin
 Valli       | Pataballa
 .....
 Alexander   | Khoo
 Shelli      | Baida
 Sigal       | Tobias
 Guy         | Himuro
 Karen       | Colmenares
 Matthew     | Weiss
 Adam        | Fripp
 Payam       | Kaufling
 Shanta      | Vollman
 ....
 Mozhe       | Atkinson
 James       | Marlow
 TJ          | Olson
 Jason       | Mallin
 Michael     | Rogers
 Ki          | Gee
 Hazel       | Philtanker
 Renske      | Ladwig
 Stephen     | Stiles
 John        | Seo
 Joshua      | Patel
 Trenna      | Rajs
....

(107 rows)

Practice Online


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: PostgreSQL Basic Simple - Exercises, Practice, Solution
Next: Write a query to get a unique department ID from employee table.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/postgresql-exercises/basic/basic-select-statement-exercise-1.php