SQL Exercises: View for the salesmen who belong to the city New York

SQL VIEW: Exercise-1 with Solution

1. From the following table, create a view for those salespeople who belong to the city of New York.

Sample table: salesman

Sample Solution:

-- Creating a VIEW named 'newyorkstaff'
CREATE VIEW newyorkstaff

-- Selecting all columns from the 'salesman' table

-- Filtering rows in the 'salesman' table where the 'city' column is 'New York'
FROM salesman
WHERE city = 'New York';

Sample Output:

sqlpractice=# select * from newyorkstaff;
 salesman_id |    name    |   city   | commission
        5001 | James Hoog | New York |       0.15
(1 row)

Code Explanation:

The above statement in SQL creates a view named "newyorkstaff" that includes all columns and rows from the 'salesman' table where the "city" column equals "New York".
This view can be used to easily access information about salesmen who work in New York without having to write the same SELECT statement each time.
It is to be noted that views do not store data themselves but rather provide a virtual table that can be used in subsequent queries.

Inventory database model:

Inventory database model

Contribute your code and comments through Disqus.

Previous SQL Exercise: SQL VIEW Exercises Home
Next SQL Exercise: View to show salesmen with columns id, name and city.

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.