CTEs (Common Table Expressions) [How To Write T-SQL Like A Pro Series - Ep. 3]
T-SQL Views [How To Write T-SQL Like A Pro Series - Ep. 2]
In the world of database management, T-SQL views are an essential tool that every developer should understand. In this video, Austin Libal, an expert at Pragmatic Works, introduces T-SQL views, explaining their purpose, how to use them, and how they can simplify complex database queries. A view is essentially a virtual table in SQL, not storing data itself but presenting data from one or more tables in a simplified and more accessible manner.
What is a T-SQL View?
A T-SQL view is a virtual table that is based on the result of a SELECT statement. Unlike a physical table, a view does not store data. Instead, it provides a way to look at data from one or more tables as if they were a single, unified table. Views help simplify complex database structures and improve the way data is accessed and queried.
One of the primary advantages of using a view is its ability to simplify the structure of a database. For example, if a database has several interrelated tables, a view can present data in a more intuitive way, making it easier for users to interact with the database. Additionally, views can be used to restrict access to certain columns or rows of a table, helping to manage data security effectively.
How to Create and Use Views in SQL Server Management Studio (SSMS)
Austin demonstrates how to create and work with views using SQL Server Management Studio (SSMS), showing how easy it is to set up and use views in a real-world database. Here's a simple example:
- Start by accessing your database in SSMS.
- Under the "Views" folder, you will find a list of all the views available in your database.
- To create a view, right-click on the "Views" folder and select "New View".
In the demo, Austin compares a basic table with a view that incorporates multiple joins from other related tables. The result is a much richer data set, allowing users to see additional fields like names, email addresses, and sales data, which would otherwise require multiple complex queries to retrieve.
Benefits of Using Views
Views offer numerous benefits, particularly when dealing with large, complex databases:
- Simplification: Views help simplify complex queries by predefining join conditions and data filters.
- Security: Views can restrict access to specific rows or columns, providing a layer of security for sensitive data.
- Efficiency: Views reduce the need to repeatedly write complex queries, saving time and reducing the chance for errors.
- Aggregation: Views can be used to perform aggregations (e.g., summing or averaging data), which can be dynamically updated without needing to write a new query every time.
Practical Use Case: Restricting Data Access
One of the most powerful features of views is the ability to restrict data access. For example, Austin demonstrates how to create a view that only allows a user to see data for a specific territory. This is useful when a user should not have access to certain data within the database based on their role or location.
In the demo, Austin writes the following SQL statement to create a view that filters customer data by territory:
CREATE VIEW TerritoryOne AS
SELECT CustomerID, TerritoryID
FROM Sales.Customer
WHERE TerritoryID = 1;
This view restricts access to only the data associated with territory ID 1. It is a simple yet effective way to ensure that users only see the data they are authorized to view.
Advanced Use Case: Creating Aggregated Views
Another advanced use case for views is aggregation. In the video, Austin creates a view to display products with a list price above the average price. This is done by writing a subquery to calculate the average price, which is then used in the view's SELECT statement.
CREATE VIEW ProductsAboveAveragePrice AS
SELECT ProductName, ListPrice
FROM Production.Product
WHERE ListPrice >
(SELECT AVG(ListPrice) FROM Production.Product);
By using views, the database can return dynamically updated aggregated results without requiring the user to write the aggregation query repeatedly.
Conclusion
In conclusion, T-SQL views are a powerful tool for simplifying database queries, improving security, and enabling efficient data retrieval. Whether you're working with complex joins, needing to restrict data access, or performing aggregations, views provide a convenient and effective solution. To learn more about T-SQL and other SQL-related topics, be sure to check out Pragmatic Works' on-demand training platform and courses designed to help you master SQL quickly and efficiently.
Sign-up now and get instant access
ABOUT THE AUTHOR
Austin is a Jacksonville native who graduated from The Baptist College of Florida in 2012. He previously worked as a manager in the retail service industry. He enjoys spending time with his wife and two kids. His primary focus at Pragmatic Works is on Azure Synapse Analytics and teaching the best practices for data integration, enterprise data warehousing, and big data analytics. He also enjoys helping customers learn the ins and outs of Power BI and showing people new ways to grow their business with the Power Platform.
Free Community Plan
On-demand learning
Most Recent
private training

Leave a comment