SQL Server BasicsWhat is SQL Server database?What is RDBMS?What is Normalization?Why we use Denormalization?What_is_SQL?What is PL/SQL?Difference between SQL and PL/SQLDatabase TableOne to One RelationshipOne to Many RelationshipMany to Many RelationshipMany to One RelationshipString Data TypesNumber Data TypesDate Data TypesOther Data TypesCreate DatabaseDrop DatabaseCreating and Managing Users in SQL ServerCreate TableAlter TableDrop TableConstraints in SQL serverPrimary KeyForeign KeyUnique KeyCandidate KeyComposite KeyDifference between primary key and candidate keyPrimary key and foreign key relationshipSurrogate KeyCascading Referential Integrity ConstraintsSelf Referential Integrity ConstraintsInsert into statementInsert multiple recordsUpdate statementDelete statementTruncate statementDifference between Delete and TruncateAlias in SQL ServerSelect statementSelect DistinctSelect TopSelect IntoNull Functions(ISNULL(),NULLIF(),COALESCE())Sub QueryIdentity ColumnSequence objectDifference between sequence and identity columnSQL Server ClausesWHERE ClauseOrder By ClauseTop N ClauseGroup By ClauseHaving ClauseDifference between Where and HavingSQL Server OperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAny OperatorsAll OperatorsUnion OperatorsUnion All OperatorsDifference between Union and Union AllIntersect OperatorExcept OperatorDifference between Except and IntersectJoinsInner JoinLeft JoinRight JoinFull JoinSelf JoinCross JoinViewsWhat are views?Create views using SSMSIndexed ViewsComplex ViewsCheck Option in ViewCheck Encryption in ViewSchema Binding Option in ViewRead-only ViewsUpdatable ViewsAdvantages and disadvantages of viewsCreate multiple views on one tableCan we implement index on views?Can we Perform Insert, update, delete operation on views?Stored Procedure and FunctionsWhat are Stored Procedures?Why we use stored procedures?Passing parameters to Stored procedureUser-Defined FunctionsDifference between UDF and Stored procedurePre-Defined Functions@@Indentity and Scope_IndentityNULLIF, ISNULL and COALESCE

What are views?

In SQL Server, a view is a virtual table that is derived from the result of a SQL query. It does not store any data itself but represents a logical representation of the data stored in one or more underlying tables. Views provide a way to encapsulate complex queries, simplify data retrieval, and enhance security by controlling the access to the underlying tables.

Let's consider an example to illustrate the concept of views in SQL Server:

Suppose we have two tables: Employees and Departments. The Employees table contains information about employees, such as their ID, name, department ID, and salary. The Departments table contains information about different departments, including the department ID and department name.

Now, let's say we want to create a view that displays the name, salary, and department name of each employee. We can create a view called EmployeeDetails using the following SQL statement:


CREATE VIEW EmployeeDetails AS
SELECT e.Name, e.Salary, d.DepartmentName
FROM Employees e
JOIN Departments d ON e.DepartmentID = d.DepartmentID;

In this example, the EmployeeDetails view is created by selecting the employee's name and salary from the Employees table and joining it with the Departments table based on the department ID. The resulting view will contain columns for the employee's name, salary, and department name.

Once the view is created, we can treat it like a regular table in subsequent queries. For instance, we can retrieve the details of employees by querying the EmployeeDetails view:


SELECT *
FROM EmployeeDetails;

The query above will fetch the data from the EmployeeDetails view, which internally executes the underlying query defined in the view's creation statement. This allows us to access the consolidated information without needing to write the join logic every time.

Views in SQL Server offer flexibility, as they can be used to restrict access to certain columns, filter data, or even aggregate information from multiple tables. They provide an abstraction layer over the underlying data and enable efficient data retrieval and management.