w3resource

Display Only Sales Employees via a Dedicated View


Create a Filtered View

Write a PostgreSQL query to create a view that only includes employees from the Sales department.

Solution:

-- Create a view to display only Sales department employees.
CREATE VIEW SalesEmployees AS
SELECT employee_id, name, department
FROM Employees
WHERE department = 'Sales';

Explanation:

  • Purpose of the Query:
    • The goal is to filter data to include only rows meeting a specific condition (Sales department).
    • This demonstrates how to use a WHERE clause in a view definition.
  • Key Components:
    • WHERE department = 'Sales' : Filters the rows based on department.
    • The SELECT statement defines the columns to include.
  • Real-World Application:
    • Helps to quickly retrieve data for a specific subset of records without retyping the filter condition.

Notes:

  • The view will update automatically if the underlying table data changes.

For more Practice: Solve these Related Problems:

  • Write a PostgreSQL query to create a view that filters customers whose email ends with a specific domain (e.g., '@example.com').
  • Write a PostgreSQL query to create a view that filters orders with order dates within the last 30 days.
  • Write a PostgreSQL query to create a view that filters products with prices above the average price in the Products table.
  • Write a PostgreSQL query to create a view that filters transactions based on a dynamic threshold value retrieved from a subquery.


Go to:


PREV : Create a View with a Join.
NEXT : Create a View with Aggregation.

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

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.