w3resource

Combine First and Last Names into a Computed Full Name


Create a View with Computed Columns

Write a PostgreSQL query to create a view that combines first and last names into a full name.

Solution:

-- Create a view to display full names of employees.
CREATE VIEW EmployeeFullName AS
SELECT employee_id, first_name || ' ' || last_name AS full_name, salary
FROM Employees;

Explanation:

  • Purpose of the Query:
    • The goal is to compute a new column (full_name) by concatenating first and last names.
    • This demonstrates the use of expressions and column aliases in a view.
  • Key Components:
    • first_name || ' ' || last_name AS full_name : Concatenates two columns with a space in between.
    • The SELECT statement determines the structure of the view.
  • Real-World Application:
    • Simplifies reporting by presenting data in a more user-friendly format.

Notes:

  • Computed columns in views are recalculated with each query on the view.

For more Practice: Solve these Related Problems:

  • Write a PostgreSQL query to create a view that concatenates first_name and last_name with a comma and space to form a full name.
  • Write a PostgreSQL query to create a view that computes the net price of products after applying a discount percentage.
  • Write a PostgreSQL query to create a view that calculates the number of days until each employee's next work anniversary.
  • Write a PostgreSQL query to create a view that derives a profit margin column by subtracting cost from revenue and dividing by revenue.


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

Previous PostgreSQL Exercise: Create a View with Aggregation.
Next PostgreSQL Exercise: Create a Materialized View.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.