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
+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | 100 | Steven | King | SKING | 515.123.4567 | 1987-06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 | | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 1987-06-18 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 1987-06-19 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 1987-06-20 | IT_PROG | 9000.00 | 0.00 | 102 | 60 | | 104 | Bruce | Ernst | BERNST | 590.423.4568 | 1987-06-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 | | 105 | David | Austin | DAUSTIN | 590.423.4569 | 1987-06-22 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 1987-06-23 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 1987-06-24 | IT_PROG | 4200.00 | 0.00 | 103 | 60 | | 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 1987-06-25 | FI_MGR | 12000.00 | 0.00 | 101 | 100 | .......... | 206 | William | Gietz | WGIETZ | 515.123.8181 | 1987-10-01 | AC_ACCOUNT | 8300.00 | 0.00 | 205 | 110 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
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)
Go to:
PREV : PostgreSQL Basic Simple - Exercises, Practice, Solution
NEXT : Write a query to get a unique department ID from employee table.
Practice Online
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
