0% found this document useful (0 votes)
122 views98 pages

SQL Server Student Guide-1

Uploaded by

Madhaw Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
122 views98 pages

SQL Server Student Guide-1

Uploaded by

Madhaw Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 98

Trademark Acknowledgements

All products are registered trademarks of their respective


organizations.
All software is used for educational purposes only.

QDUS - I_SG /12-M11-V02


Copyright ©NIIT. All rights reserved.

No part of this publication may be reproduced, stored in


retrieval system or transmitted in any form, or by any
means, electronic, mechanical, photocopying, recording or
otherwise, without the prior written permission of the
publisher.
After completing this course, the student should be able to:
 Query and manage databases using SQL Server.

About This Course


Conventions
Prologue
Convention Indicates...
Note
Description
The Querying Data Using SQL Server - I course is Animation
designed to provide students the necessary skills to query
and manage databases using SQL Server. The course starts
with an introduction to client-server architecture and an Just a Minute
overview of SQL Server. Next, it familiarizes the students
with Structured Query Language (SQL) and builds on the
complexity of query handling in SQL Server with every
progressing session. It also introduces the students to some
advanced concepts in SQL Server such as implementing
managed code and implementing services for message-
based communication.

Rationale
Databases are an integral part of an organization. Aspiring
database developers should be able to efficiently
implement and maintain databases. This knowledge will
enable them to build robust database solutions.

Objectives
After completing this course, the student should be able to:
 Identify SQL Server tools
 Query data from a single table
 Query data from multiple tables
 Manage databases and tables
 Manipulate data in tables
 Implement indexes, views, and full-text search
 Implement stored procedures and functions
 Implement triggers and transactions
 Implement managed code
 Implement services for message-based
communication

Entry Profile
The students who want to take this course should have:
 Knowledge of RDBMS concepts.
 Basic knowledge of computers.

Exit Profile
Role of a Database Server
Chapter 1 Organizations have always been storing and managing
business data. Earlier, organizations used to store data on
Overview of SQL Server paper. With an increase in the usage of computers,
organizations started maintaining the same information in
In today’s competitive environment, an organization needs
computers. Data was stored in an organized way, and it
a secure, reliable, and productive data platform for its
was also easy to retrieve data faster than before. As data
business applications. SQL Server provides a platform to
retrieval became easy and fast, organizations started using
build and manage data applications. In addition, it
business or Web applications to support the business
combines data analysis, reporting, integration, and
operations.
notification services that enable organizations to build and
deploy efficient Business Intelligence (BI) solutions. Consider a scenario. A Web application might be used by
an organization to manage the data of the employees. This
This chapter discusses the importance of a database
application retrieves and manipulates the data stored in a
server. In addition, it provides an overview of SQL Server,
data source, such as MS SQL Server and Oracle. To
its components, and features. Further, this chapter
retrieve and manipulate the data stored in a data store, you
introduces the Structured Query Language (SQL) that is
need to use a data access technology in the application. A
used to manipulate the data in a database server. Lastly, it
data access technology interacts with a data source and
discusses the tools provided by SQL Server to improve the
enables you to retrieve and manipulate data through the
productivity of the database developer and manage the
application. The application can retrieve data from the
server.
data source using any of the following data access
Objectives technologies:
 Language-Integrated Query (LINQ)
In this chapter, you will learn to:  ADO.NET Entity Framework
 Appreciate SQL Server as a database server  WCF Data Services
 Identify the SQL Server tools  Sync Framework
All these technologies use ADO.NET at the core level.
ADO.NET further implements SQL to access data from
Introduction to SQL Server the data store.
Every organization needs to maintain information related Business or Web applications accept data as input, process
to employees, customers, business partners, or business the data based on business requirements, and provide data
transactions. Organizations build business applications or information as output. For example, an application
with a user-friendly interface to store and manipulate this maintains the details of the number of cars sold for a
information and to generate reports. For this, they need a particular brand, such as Ferrari. Each car has a unique
platform to store and maintain this information in an identification number that is already stored in an
efficient way. Various Database Management Systems application. Whenever a sale happens, the application
(DBMS) and Relational Database Management Systems checks whether the unique identification number provided
(RDBMS), such as SQL Server, Oracle, and Sybase, for the car is correct or not. If the unique identification
provide the platforms for storing and maintaining this number is correct then the sale details for the same is
information. updated in the application. The data is saved and an output
message confirming that the data has been saved is
SQL Server is a database engine introduced by Microsoft.
displayed to the user. This process of checking whether the
It provides an environment used to create and manage
unique identification number exists in the system or not is
databases. It allows secure and efficient storage and
called a business rule.
management of data. In addition, it provides other
components and services that support the business Consider another scenario. The Human Resource
intelligence platform to generate reports and help in department of an organization uses an application to
analyzing historical data and predicting future trends. manage the employee data. The users need to add the
details of new employees. For this, the application
As a database developer, it is important for you to identify
provides an interface to enter the employee details. These
the role of a database server in an organization. You can
details are validated for accuracy based on business rules.
design a database effectively if you know all the
For example, a business rule is defined to check that the
components and services of SQL Server. In addition, you
date of joining of the new employee is less than or equal to
need to understand the basics of SQL, a language that is
the current date. If the data meets the requirements, it is
used to query and manage data.
saved in the data store. only on the server and not on all the clients.
Based on the preceding scenario, a business application A two-tier architecture is also called the client-server
can have the following elements: architecture. A client sends the request for a service and a
 The User Interface (UI) or the presentation server provides that service. Most RDBMSs, such as
element through which data is entered. Microsoft Access, SQL Server, and Oracle, support client-
 The application logic or the business rule server architecture. RDBMS provides centralized
element, which helps in validating the entered functionality while supporting many users.
data. Three-Tier Architecture
 The data storage or the data management When implementing complex business solutions in a two-
element, which manages the storage and retrieval tier architecture, the tier on which the business logic is
of data. implemented becomes over loaded. As a result, it takes
These elements form the base of the models or more time to execute. Therefore, to provide further
architectures used in application development. All these flexibility, the two-tier architecture can be split into three
elements can exist on the same computer as a single tiers. In three-tier architecture, the first tier is the client
process or on different computers as different processes. tier. The second or the middle tier is called the business
Depending on the placement of these elements, the tier. The third tier is called the server tier. The server tier
application architecture can be categorized as: contains a database server that manages the data.
 Single-tier architecture In this architecture, an additional tier called a business tier
 Two-tier architecture has been added between the client tier and the server tier,
 Three-tier architecture as shown in the following figure.
 N-tier architecture
Single-Tier Architecture
In a single-tier architecture, all elements of a business
application are combined as a single process. If multiple
users need to work on this application then it needs to be
installed on the computer of every user. This type of
architecture has one disadvantage. In case some errors are
identified in the application then after rectifying the same,
the application has to be installed again on the system of
every user. This is a time-consuming process.
Two-Tier Architecture
To solve the problems of single-tier application, two-tier
architecture was introduced. In two-tier architecture, the
application is divided into two parts. One part handles the
data, while the other provides the user interface. A Three-Tier Client/Server Architecture
Therefore, this architecture is called two-tier architecture.
The business tier consists of all the business rules. It
These two parts can be located on a single computer or on
consists of the application logic that implements business
separate computers over a network. The part that handles
rules and validates the data. The advantage of a three-tier
the UI is called the client tier. The part that implements
application is that it allows you to change the business
the application logic and validates the input data based on
rules without affecting the other two tiers.
the business rules is called the server tier, as shown in the
For example, in a banking application for loans, the user
following figure.
tier is the frontend used by the customer to specify the
loan details. The server tier can consist of an RDBMS in
which the data is stored. The business tier lies between the
other two tiers and consists of business rules, such as the
loan limit and the interest rate charged to a customer. If
there is a change in the rate of interest, only the middle tier
component needs to be modified.
N-Tier Architecture
As the business complexities increased, the business tier
A Two-Tier Architecture became larger and unmanageable. This led to the evolution
In the preceding architecture, the maintenance, upgrade, of n-tier architecture, where the business services model
and general administration of data is easier, as it exists was divided into smaller manageable units. N-tier
architecture is also called a multi-tier architecture.
In this architecture, one component near the client tier is
responsible to do the client side validation and send the
data to the presentation tier. Therefore, it is possible to
keep the UI-centric processing component on a computer
near the client. The UI-centric processing component is
responsible for processing the data retrieved from and sent
to the presentation tier. In addition, you may have another
component near the database server to manipulate and
validate the data. You can keep the data-centric processing
components on another computer near the database server,
so that you gain significant performance benefits. Data-
centric processing components are responsible for
accessing the data tier to retrieve, modify, and delete data
to and from the database server.
The n-tier architecture consists of the following layers:
 Client tier The Architecture of the Web Applications
 UI-centric processing components Depending on the type of business rules, the applications
 Data-centric processing objects can be implemented on any of the tiers, such as Web
 Database server clients, Web server, or the database server.
The banking application, when further expanded, can To provide support to applications where users can send
represent an example of n tier architecture. The client tier requests simultaneously, the database server needs to be
would consist of the user interface, which would include fast, reliable, and secure. SQL Server is one such complete
the user interface controls, such as forms, menus, and database platform that provides a fast, reliable, and secure
toolbars. The server tier would consist of data-handling RDBMS. It also helps in data analysis with integrated BI
including saving data to the database server. tools. The BI tools are used to prepare reports that are
The business logic would include the rules and guidelines analyzed further to make efficient business decisions.
for different types of accounts, interest rates, fixed
deposits, ATMs, and loan rules. All of these would form
the middle tier. However, there would be some rules that SQL Server Elements
need to be implemented both on the user interface and on SQL Server contains a number of elements. Each element
the database. You can place these rules either on the UI- in SQL Server provides various tools and services to
centric processing components or data-centric processing improve the database efficiency. The various elements in
components, based on the functionality. SQL Server, such as SQL Server Database Engine,
Applications that follow multi-tier architecture can be Analysis services, and SQL Server Data tools, working
used across various locations. For example, in Web together constitute its architecture. However, you need to
applications, an application is stored on the Web server. understand the most important and basic elements of SQL
The clients access the application from any location Server that are frequently used while querying data.
through a browser. The clients make requests to the Web SQL Server has the following elements:
server and receive responses.
The following figure shows the architecture of the Web
Services
Services are the applications that are started automatically
applications.
during the startup of the computer system. Services
provide support to create and manage databases in
different environments. Multiple services are set up on the
computer system when the SQL Server is installed, which
includes some of the following services:
 SQL Server Database Engine: It provides
support to store, query, process, and secure data
on a database server. It allows you to create and
manage database objects such as tables, views,
stored procedures, and triggers.
 SQL Server Agent: It is used to automate
administrative tasks by creating and managing
jobs, alerts, and operators.
 SQL Server Data Tools: It allows developers to
create, debug, and maintain databases and
database objects using an integrated development
environment offered by Microsoft Visual Studio.
Instances
An instance is the basic unit of program installation and
program execution. It is a copy of the .EXE file that
represents the programs and resources supported on SQL
Server. You can install multiple instances of SQL Server
on a single computer. Each instance is segregated from
other instances based on disk files usage and resources
allocation. SQL Server provides the following types of
instances:
 Default Instance: The default instance of SQL The Components of SQL Server
Server is identified by the name of the computer As shown in the preceding figure, SQL Server consists of
on which the instance is running. SQL Server the following core components:
allows you to install only one default instance of  Database engine
SQL Server on a system.  Integration services
 Named Instances: The named instance of SQL  Analysis services
Server is identified by both, the computer name  Reporting services
and the instance name. SQL Server allows you to  Master Data services
create more than one named instance of SQL Database Engine
Server on a system. A database engine provides support to store, query,
Tools process, and secure data on a database server. It allows
SQL Server provides various tools that enable you to you to create and manage database objects, such as tables,
query and manage the database engine. These tools are: views, stored procedure, and triggers. Apart from
 SQL Server Management Studio: It is a providing support for data management, a database engine
powerful tool that provides a simple and also provides the following background services:
integrated environment to develop and manage  Service Broker: Provides support for
the SQL Server database objects. asynchronous communication between clients
 SQLCMD Tool: It allows you to execute T-SQL and the database server, thereby enabling reliable
statements at the command prompt mode. query processing. The client application sends a
 SQL Server Configuration Manager Tool: It request to the database server and continues to
manages utility services, such as client network work. These requests are queued up at the server
utility, server network utility, and service manager in case the server is not available to process the
utility services. request immediately. A Service Broker ensures
 SQL Server Installation Center: It provides you that the request is processed whenever the server
the ability to add, delete, or modify the features of is available.
SQL Server, such as adding a new feature to an The following figure shows the implementation
existing server or upgrading SQL Server. of Service Broker in the order processing system.

SQL Server Components


SQL Server contains a number of components. Each
component provides specific services and support to the
clients connected to the server.
The following figure displays the components of SQL The Implementation of Service Broker in the Order
Server. Processing System
The preceding figure describes the example of the
order processing system. The client applications
are sending orders to the database server to enter
the order details. All these orders are placed in a
queue, which is managed by the Service Broker.
 Replication: Allows you to copy and distribute
data and database objects from one database as computers or mobile devices. Notification
server to another. These servers can be located at service is a platform for developing and
remote locations to provide fast access to users at deploying highly scalable notification
widely distributed locations. After replicating applications. It allows developers to build
data, SQL Server allows you to synchronize notification applications that send timely,
different databases to maintain data consistency. personalized information updates, helping to
For example, the database servers for your enhance customer relationships. For example, a
organization might be located at different brokerage firm sends stock and fund prices based
locations around the world, but all the servers on the customer’s preferences.
store common data. To ensure that the data in all Integration Services
the servers is synchronized, you can implement Data in different sources might be stored in different
data replication. Replication follows the formats and structures. Integration services allow you to
publisher/subscriber model. In this model, the gather and integrate this varied data in a consistent format
changes are sent out by one database server in a common database called the data warehouse. A data
(“publisher”) and are received by others warehouse consists of integrated databases, which can be a
(“subscribers”). DBMS, text files, or flat files. A data warehouse is similar
The following figure depicts the replication to a physical warehouse that stores raw material or
process. products for further distribution. The organization does not
store useless materials or products in its warehouse
because it costs money and affects the ability to get
products in and out of the warehouse. Similarly, a data
warehouse should not contain useless data. The data
should be meaningful so that it could be processed
quickly. A data warehouse is a large central repository of
data that helps in decision-making.
Consider a telecommunications company, where the CEO
notices that the frequency of cancellation of services by its
customers in the past one year has increased considerably.
The company is unable to analyze the service preferences
of the customers because data is scattered across disparate
data sources. These data sources contain data spanning
two decades. In such a case, a data warehouse can be
implemented to integrate two decades of historical data
The Replication Process from disparate data sources. The integrated data will
In the preceding figure, articles are the database provide a holistic view of the customers to the CEO.
objects to be replicated. These articles are stored SQL Server Integration Services (SSIS) Import and Export
in a database called publication and are located Wizard provides a series of dialogs to help you complete
on the publisher server. The distributor server the process of selecting the data source, the destination,
takes the publications from the publisher server and the objects that will be transferred to create a data
and distributes them to the subscribers. warehouse.
 Full-text search: Allows you to implement fast Analysis Services
and intelligent search in large databases. It allows Data warehouses are designed to facilitate reporting and
you to search records containing certain words analysis. Enterprises are increasingly using data stored in
and phrases. You can search for different forms of data warehouses for analytical purposes to assist them in
a specific word, such as ‘produce’, ‘produces’, or making quick business decisions. The applications used
‘production’. In addition, you can search for for such analysis are termed as BI applications. Data
synonyms of a given word, such as ‘garment’, analysis assists in determining past trends and formulating
‘cloth’, or ‘fabric’. future business decisions. This type of analysis requires a
 Notification services: Allow you to generate and large volume of data to reach a consistent level of
send notification messages to the users or sampling.
administrators about any event. For example, the In the telecommunications company scenario, with the
database administrator should be notified when a help of the analysis tools querying on the data warehouse,
table is created or deleted. The notification the CEO can identify the customers who are canceling
messages can be sent to a variety of devices, such their services. The company can then use this information
to provide attractive offers to the identified customers and entities can be the order placed, name, or quantity. MDS
build loyalty. This kind of information analysis proves to implementation ensures that correct values are provided
be beneficial to the enterprise in the long run. The for each attribute of an entity.
enterprise can retain its customers by offering loyalty MDS is actually used to formulate a data model. It
programs and schemes on the basis of analysis on the includes the following features to manage data, and
historical data. enhances the quality and performance:
Consider another example of a soft drink manufacturer  Versioning: Organizations have different views
that uses data of the past few years to forecast the quantity of particular data on the basis of time, business
of bottles to be manufactured in the current month. These entities, or external changes. For example, an
forecasts are based on various parameters, such as the organization wants to view the monthly, weekly,
average temperature during the last few years, purchasing and quarterly sales to calculate the profitability
capacity of the customers, age group of the customers, and ratio. MDS provides the ability to view the sales
past trends of consumption. The requirements for such an record at different time intervals, so that the users
analysis include: can work easily on various versions.
 A large volume of data  Security: MDS ensures that the sensitive data is
 Historical data, that is, data stored over a period protected. The data is protected by limiting user
of time access for performing specific administrative
Therefore, analysis services help in data analysis in a BI tasks, and viewing or updating selected entities or
application. Microsoft SQL Server Analysis Services attributes.
(SSAS) provide Online Analytical Processing (OLAP) for  Global Access to Master Data: MDS provides a
BI applications. OLAP arranges the data in the data Web application that acts as a portal to enable
warehouse in an easily accessible format. This technology users to access master data without installing any
enables data warehouse to do online analysis of the data. client application on their desktops.
Reporting Services  Business Rules: MDS enforces business rules to
Reporting services provide support to generate complete ensure the accuracy of master data by updating
reports on data in the database engine or in the data values, setting default values, and sending an
warehouse. These services provide a set of tools that help email when some new data is added or removed.
in creating and managing different types of reports in The following figure shows the usage of the various SQL
different formats. Using these services, you can create Server core components in a BI application.
centralized reports that can be saved to a common server.
Reporting services provide secure and restricted access to
these reports.
Microsoft SQL Server Reporting Services (SSRS) help in
creating Web-based reports based on the content stored in
a variety of data sources. You can also publish these
reports in different formats.
Master Data Services
Master Data Services (MDS) is used to manage the master
data for an organization. Master Data includes information
that is vital for an organization’s business operations. It
generally consists of data, which is transactional in nature
and can be used by multiple users, systems, applications,
and processes across an organization. It may include data
about customers, products, employees, assets, locations,
and vendors, who require continuous quality management,
ease of access, and effective sharing of data.
MDS is a Microsoft technology for implementing master
data management. It is a part of the Enterprise and
Business Intelligence editions of SQL Server. One of the
fundamental tasks in MDS implementation is to identify
the data that is critical for an organization. This data is
referred to as entities. Further, the attributes associated
with each entity are identified. For example, an entity can
be a customer, a student, or a product. The attributes of
SQL Server Editions
SQL Server supports various editions that provide
different features targeting different business
requirements. SQL Server has the following editions:
 Enterprise: The SQL Server Enterprise edition
includes the core database engine and supports
the advanced services such as table partitioning,
parallel indexing, and indexed views. The
Enterprise edition is designed to provide high
performance and security, optimized productivity,
and high scalability of data.
 Standard: The SQL Server Standard edition has
core database engine and basic business
The Core Components of SQL Server
intelligence capabilities. It differs from the
Enterprise Edition in that it does not support all
the security and data warehousing features of the
Enterprise edition. The Standard edition provides
easy integration, improved manageability, and
easy extension of data across various platforms,
such as .Net or Linux.
 Business Intelligence: The BI edition has been Development Tools and Languages
introduced in SQL Server 2012. It consists of all Development tools and languages are used to create the
the Standard Edition capabilities and supports the interface for the Windows forms, Web forms, and console
business intelligence tools, such as Master Data applications. The tools include Visual Studio and Visual
Services and Data Quality Services. The Business C# Developer. The languages that can be used are Visual
Intelligence edition provides rapid exploration of Basic.NET, C#, or F#. These components are based on
data and greater consistency, manageability, and the .NET Framework base classes. These tools also enable
security of data. you to create database-related codes and objects without
switching to the SQL Server database engine. This enables
Data Quality Services helps the database you to work with database objects quickly and easily.
administrator to maintain the quality of the data. Base Class Library
The .NET Framework consists of a class library that acts
as a base for any .NET language, such as Visual
SQL Server Integration with the .NET Basic, .NET, and C#. This class library is built on the
Framework object-oriented nature of the runtime. It provides classes
that can be used in the code to accomplish a range of
Microsoft SQL Server is integrated with the .NET common programming tasks, such as string management,
Framework, as shown in the following figure. data collection, database connectivity, and file access. In
addition, it enables you to connect with a database and
generate scripts or create queries for accessing data.
CLR
CLR is one of the most essential components of the .NET
Framework. It provides an environment for the application
to run. CLR or the runtime provides functionalities, such
as exception handling, security, debugging, and versioning
support to the applications.
Some of the features provided by CLR are:
 Automatic memory management: Allocates and
de-allocates memory to the application as and
when required.
 Standard type system: Provides a set of
common data types in the form of Common Type
Integration of SQL Server with the .NET Framework System (CTS). This means that the size of integer
The .NET Framework is an environment used to build, and long variables is the same across all
deploy, and run business applications. These applications programming languages.
can be built in various programming languages supported  Language interoperability: Provides the ability
by the .NET Framework. It has its own collection of of an application to interact with another
services and classes. It exists as a layer between the .NET application written in a different programming
applications and the underlying operating system. language. This also helps maximize code reuse.
SQL Server uses various services provided by the .NET  Platform independence: Allows execution of a
Framework. For example, the notification services code from any platform that supports the .NET
component is based on the .NET Framework. This CLR.
component uses the .NET Framework services to generate  Security management: Applies restrictions on
and send notification messages. the code to access the resources of a computer.
The .NET Framework is also designed to make CLR can host a variety of languages. It offers a common
improvements in code reuse, code specialization, resource set of tools across these languages, ensuring
management, multilanguage development, security, interoperability between the codes. The code developed
deployment, and administration. Therefore, it helps bridge with a language compiler that targets CLR is called a
the gap of interoperability between different applications. managed code.
The .NET Framework consists of the following Alternatively, the code that is developed without
components: considering the rules and requirements of the common
 Development tools and languages language runtime is called unmanaged code. Unmanaged
 Base class library code executes in the common language runtime
 Common Language Runtime (CLR) environment with minimal services. For example,
unmanaged code may run with limited debugging and services by implementing the HTTP endpoints.
without the garbage collection process. For example, sales executives of an organization
With CLR integration in SQL Server, you can implement need to access the data on the database server
programming logics that involve complex operations in through their Personal Desktop Assistant (PDA)
the database by using the programs written in any of devices. However, providing direct access from a
the .NET-supported languages. CLR integration allows PDA device to the database server involves a high
you to create objects in a .NET-supported language and cost. Therefore, organization can implement Web
embed these objects in the database. You can embed services through which each sales executive can
the .NET code in database objects such as stored log the sales details online from anywhere using
procedure, function, or trigger. Such a database object is any device.
called a managed database object.  High level of security: Implements high security
by enforcing policies for log on passwords.
Administrators can also manage permissions on
Features of SQL Server database objects granted to different users.
 High availability: Ensures that the database
The components of SQL Server help improve the database server is available to all users at all times. This
management and developer productivity by the following reduces the downtime of the server. In SQL
features of SQL Server: Server, high availability is implemented with the
 Built-in support for Extensible Markup help of database mirroring, failover clustering,
Language (XML) data: Allows you to store and and database snapshots.
manage XML data in variables or columns of the  Support for data migration and analysis:
XML data type. The XML feature of SQL Server Provides tools to migrate data from different data
enables you to write code to retrieve data from sources to a common database. In addition, it
the database in the form of XML. In addition, it allows building the data warehouse on this data
allows you to read an XML document and store that can support BI applications for data analysis
the XML data in the database. and decision-making.
 CLR integration: Allows you to use the CLR  Intellisense: Provides the feature of auto
features of .NET Framework in the SQL Server completion of code written to create or
database. It enables you to use the code written in manipulate database objects in the Query Editor
any of the .NET supported languages for window.
implementing complex programming logics in the  Policy-based management: Used to define a set
database. For example, you need to write a code of policies for configuring and managing SQL
for the verification of the credit card number Server. For example, you can define a policy to
entered by the user. It will be complex to write set a naming convention for tables and stored
the code for the verification of the credit card procedures. When a user tries to create a table,
number in T-SQL. However, the same code can the table name must map with the naming
be written effectively using a .NET programming convention defined in the policy, else an error
language. Therefore, you can write the code in will be raised.
a .NET programming language and use that code  Resource governor: Used to manage the
in SQL Server to verify the credit card workload of SQL Server by allocating and
information. managing the server resources, such as CPU time
 Scalability: Allows you to distribute the data in and memory. The resource pool represents the
large tables into several filegroups. This enables server resource. You can specify the minimum
SQL Server to access all the filegroups and maximum values of the CPU and memory
simultaneously and retrieve the data quickly. This utilization in a resource pool. These resources are
makes the database scalable and helps improve used for running and performing various assigned
the performance of queries. tasks in SQL Server.
 Service-oriented architecture: Provides
distributed and asynchronous application
framework for large-scale applications. This Types of SQL Statements
allows the database clients to send requests to the
database server even if the server is not available As a database developer, you need to manage the database
to process the request immediately. to store, access, and modify data. SQL is the core language
 Support for Web services: Allows you to used to perform these operations on the data. SQL,
provide direct access to the data from the Web pronounced as ‘sequel’, is a language that is used to
manage data in an RDBMS. This language was developed
by IBM in the 1970s. It follows the International
Organization for Standardization (ISO) and American
National Standards Institute (ANSI) standards.
Most database systems have created customized versions
of the SQL language. For example, Transact-SQL (T-
SQL) is a scripting language used in SQL Server for
programming. The applications may have different user
interfaces but have a common way to communicate with
SQL Server by sending T-SQL statements to the server.
The SQL statements can be divided into the following
categories:
 Data Definition Language (DDL): Is used to
define the database, data types, structures, and
constraints on the data. Some of the DDL
statements are:
 CREATE: Used to create a new database
object, such as a table.
 ALTER: Used to modify the database Identifying SQL Server Tools
objects.
 DROP: Used to delete the objects. SQL Server provides various tools that help improve the
 Data Manipulation Language (DML): Is used efficiency of database developers. SQL Server
to manipulate the data in database objects. Some Management Studio is one such tool that helps in creating
of the DML statements are: and maintaining database objects. SQL Server Data Tools
 INSERT: Used to insert a new data record help in creating and implementing BI solutions. SQL
in a table. Server also provides tools, such as Database Engine
 UPDATE: Used to modify an existing Tuning Advisor and SQL Server Configuration Manager
record in a table. that help the database administrator in configuring the
 DELETE: Used to delete a record from a server and optimizing its performance.
table. Before you start working on SQL Server, it is important to
 Data Control Language (DCL): Is used to identify the various tools and their features provided by
control the data access in the database. Some of SQL Server.
the DCL statements are:
 GRANT: Used to assign permissions to
users to access a database object. SQL Server Management Studio
 REVOKE: Used to deny permissions to SQL Server Management Studio is a powerful tool
users to access a database object. associated with SQL Server. It provides a simple and
 Data Query Language (DQL): Is used to query integrated environment for developing and managing the
data from database objects. SELECT is the DQL SQL Server database objects. The various components of
statement that is used to select data from the SQL Server Management Studio, such as query editor,
database in different ways and formats. object explorer, and solution explorer are used to create,
SQL is not a case-sensitive language. Therefore, you can store, and execute queries. SQL Server Management
write the statements in any case, lowercase or uppercase. Studio provides the facility to view the execution plans of
For example, you can use the SELECT statement in queries in different formats.
lowercase as ‘select’ or in title case as ‘Select’. The following table lists the main components of the SQL
Server Management Studio interface.

Components Description
Object Explorer An Object Explorer window
provides the ability to
register, browse, and
manage servers. Using
Object Explorer, you can
also create, browse, and
manage server components. window provides an
The Object Explorer allows organized view of your
you to configure the projects and files. In this
following components: explorer, you can right-click
 Security: Used to on a project or file to
create login and manage or set their
users, and to assign properties.
permissions. Query Editor The Query Editor window
 Replication: Used provides the ability to
to create and execute queries written in T-
manage publishers SQL. It can be invoked by
and subscribers. selecting the New Query
 SQL Server Agent: option from the File menu or
Used to automate the New Query button from
administrative tasks the Standard toolbar.
by creating and Template Explorer The Template Explorer
managing jobs, window provides a set of
alerts, and templates of SQL queries to
operators. perform standard database
 Management: operations. You can use
Used to configure these queries to reduce the
Distributed time spent in creating
Transaction queries.
Coordinator, Dynamic Help The Dynamic Help window
Database Mail is available from the Help
service, or SQL menu of SQL Server
Server logs. In Management Studio. This
addition, it is used tool automatically displays
for managing links to relevant information
policies and while users work in the
governing Management Studio
resources of SQL environment.
Server.
 Server Objects: The Components of the SQL Server Management Studio
Used to create and Interface
manage backups,
endpoints, and
triggers. Task 1.1: Opening SQL Server
Object Explorer Details The Object Explorer Details Management Studio
provide the detailed
description of all the objects
in SQL Server.
Registered Servers The Registered Servers
SQL Server Data Tools
window displays all the SQL Server Data Tools (SSDT) is a replacement for
servers registered with the Business Intelligence Development Studio and is used to
management studio. It also work with relational databases and BI projects. It allows
helps record connection developers to create, debug, and maintain databases and
information for each database objects by using an integrated development
registered server including environment offered by MS Visual Studio. SQL Server
the authentication type, Object Explorer in Visual Studio gives a view of the
default database, network database objects. This view is similar to as offered by SQL
protocol characteristics, Server management studio. Database objects like tables,
encryption, and time-out views, or procedures can be created, modified, or deleted
parameters. by using the options available in SQL Server Object
Solution Explorer The Solution Explorer Explorer. SSDT offers tools and project types to work with
the Analysis, Reporting, and Integration services.
Therefore, SSDT has combined various database
development tools into a single IDE.

Database Engine Tuning Advisor


Database Engine Tuning Advisor helps database
administrators to analyze and tune the performance of the
server. To analyze the performance of the server, the
administrator can execute a set of T-SQL statements
against a database. After analyzing the performance of
these statements, the tool provides recommendations to
add, remove, or modify database objects, such as indexes
or indexed views to improve performance. These
recommendations help in executing the given T-SQL
statements in the minimum possible time.

SQL Server Configuration Manager Summary


SQL Server Configuration Manager helps the database In this chapter, you learned that:
administrators to manage the services associated with SQL  A business application can have three elements:
Server. These services include SQL Server Agent, SQL user interface, business logic, and data storage.
Server Analysis Services, SQL Server Reporting Services,  A database server is used to store and manage the
SQL Server Integration Services, and SQL Server Browser database in a business application.
service. Administrators can start, pause, resume, or stop  SQL Server consists of the five core components:
these services by using this tool. In addition, it allows you database engine, integration services, analysis
to configure certain properties, such as server alias, services, reporting services, and master data
network protocols, and connection parameters. services.
In addition, the tool allows you to manage the network  A database engine provides support to store,
connectivity configuration from the SQL Server client query, process, and secure data on a database
computers. It allows you to specify the protocols through server. Integration services allow you to gather
which the client computers can connect to the server. and integrate this varied data in a consistent
format in a common database called the data
warehouse.
SQL Server Profiler  Analysis services assist in determining past trends
and formulating future business decisions.
SQL Server profiler helps in monitoring the events, such  Reporting services provide support to generate
as login connections, execution of DML statements, stored comprehensive reports on the data stored in the
procedures, batches, and security permission checks that database engine or the data warehouse.
are generated within an instance of a database engine.  Master Data services provide support to manage
Data captured from these events are used for analysis master data.
purposes. In addition, Server Profiler provides an in depth  Microsoft SQL Server is integrated with the .NET
view of the query submission, access of the database Framework.
against the queries, and return of results after processing  The .NET Framework is an environment used to
of queries. By using SQL Server Profiler, database can be build, deploy, and run business applications.
changed and the outcomes of the changes on different  The .NET Framework consists of three
database objects are analyzed. components: development tools and languages,
base class library, and CLR.
 SQL Server provides the following benefits:
 Built-in support for XML data
 CLR integration
 Scalability
 Service-oriented architecture
 Support for Web services
 High level of security
 High availability
 Support for data migration and analysis
 Intellisense
 Policy-based management
 Resource governor
 The SQL statements can be divided into the
following categories:
 DDL: To create and manage database
objects.
 DML: To store and manage data in
database objects.
 DCL: To allow or deny access to database
objects.
 DQL: To query data from database objects.
 SQL Server provides the following tools to
improve the efficiency of the database developers
and manage the server:
 SQL Server Management Studio
 SQL Server Data Tools
 Database Engine Tuning Advisor
 SQL Server Configuration Manager
 SQL Server Profile

Reference Reading
Introduction to SQL Server

Reference Reading: Books Reference Reading: URLs


Introducing Microsoft SQL http://msdn.microsoft.com/
Server 2012 by Ross Mistry en-us/library/
and Stacia Minser ms144275.aspx

Identifying SQL Server Tools

Reference Reading: Books Reference Reading: URLs


Beginning Microsoft SQL http://msdn.microsoft.com/
Server2012 Programming en-us/library/hh272686%
by Paul Atkinson and Robert 28v=vs.103%29.aspx
Vieira
 Structured data: Is the data that has definite
structure. It follows a defined format and has a
predefined length, such as string or numeric data.
Chapter 2 The structured data can be easily stored in a table.
For example, the roll number of a student is a
Querying Data numeric value and can be easily stored in the
RollNo column of the Student table. In addition,
As a database developer, you need to regularly retrieve you can store the spatial data, such as
data for various purposes, such as creating reports and geographical data with longitude and latitude, and
manipulating data. You can retrieve data from a database geometrical data with coordinates.
server by using SQL queries.  Semi-structured data: Is the data that has an
This chapter explains how to retrieve specific data from indefinite structure and cannot be easily stored in
database tables by executing SQL queries. Further, it a database table. The attributes of semi-structured
discusses how to incorporate functions to customize the data can change. For example, the details of the
data values returned by the queries. In addition, the customers contain various attributes, such as
chapter explains how to retrieve summarized and grouped customer ID and customer name. These attributes
data from database tables. change for every customer. Semi-structured data
is generally stored in the XML format.
Objectives  Unstructured data: Is the data that does not have
a defined structure. It does not necessarily follow
In this chapter, you will learn to:
any format or sequence. In addition, it does not
 Retrieve data
follow any rule and is not predictable. For
 Use functions to customize the result set
example, text files, image files, streaming videos,
 Summarize and group data
and sound clips represent unstructured data.
Depending upon the type of data to be stored, SQL Server
supports the various data types. These data types can be
Retrieving Data associated with the columns, local variables, or
At times, database users might need to retrieve complete expressions defined in the database. You need to specify
or selected data from a table. Depending on the the data type of a column according to the data to be
requirements, they might need to extract only selected stored in the column. For example, you can specify the
columns or rows from a table. For example, an character data type to store the employee name, date data
organization stores the employee data in the Employee type to store the hire date of employees. Similarly, you can
table of the SQL Server database. If the HR Manager of specify money data type to store the salary of the
the organization needs to send a letter to employee, only employees.
selected information, such as name and address details, of The data types supported in SQL Server can be organized
the employee needs to be retrieved from the Employee into the following categories:
table. However, if the HR Manager has to prepare a report  Numeric
of the employees, all the employee details need to be  Character string
retrieved from the Employee table.  Date and time
Depending on these requirements, you will need to run  Binary
different SQL queries to specify the criteria for selecting  Others
data from the tables. Therefore, it is important for you to Numeric
learn how to query databases to retrieve the required The numeric data types allow you to store integer values
information. in the database. The following table lists the numeric data
Databases can contain different types of data. Therefore, types supported by SQL Server.
before querying the data, it is important to identify the
various data types.

Identifying Data Types


Data type represents the type of data that a database object
can contain. This data can be in the form of characters or
numbers. You can store the following types of data in a
database:
type with 100 character size. Now, you store a
word containing 50 characters in that variable. In
this case, the system will allocate memory space
required to store 50 characters only. The space for
the rest 50 characters that can be stored in the
variable will be freed. This way, there is no
memory loss. It stores only nonunicode
characters.
 nvarchar: It allocates memory similar to the
varchar data type. However, it stores only unicode
characters. It requires twice the storage space as
required by varchar.
 nchar: It supports fixed-length unicode data with
the maximum length of 4000 characters. It
requires twice as much storage space as required
by the char data type.

The Numeric Data Types Unicode is an industry wide computing standard for
representing characters. It provides unique number to every
Character and String character so that these characters are represented in the
The character and string data types allow you to store text same way across different platforms and languages.
values in the database. The following table lists the
character and string data types supported by SQL Server. Date and Time
The date and time data types allow you to store date and
time values in the database. The following table lists the
date and time data types supported by SQL Server.

The Character and String Data Types


The preceding table lists various data types, such as char,
varchar, nchar, and nvarchar, which are used for storing
character data. However, you must know the difference in
using these data types. The following list describes the
differences between these data types: The Date and Time Data Types
 char: It is used to store fixed length character
Binary
data in a variable. For example, you declare a
The binary data types allow you to store binary data in the
variable of char data type with 50 character size.
database. The following table lists the binary data types
This declaration reserves the memory space
supported by SQL Server.
required to store 50 characters in that variable. If
you store 10 characters in that variable, then the
remaining 40 character memory space will be
wasted.
 varchar: It is used to store variable length
character data in a variable. It does not reserve
the memory space at the time of variable
declaration. Instead, it allocates memory as per
the size of the data stored in the variable. For
example, you declare a variable of varchar data
The Binary Data Types select_column_list is the list of columns or
Others aggregate columns for which the data is to be listed.
The other data types allow you to store unique identifiers, INTO creates a new table and inserts the resulting rows
cursors, table, XML, and spatial data in the database. The from the query into it.
following table lists the other data types supported by SQL new_table_name is the name of the new table to be
Server. created.
FROM table_name is the name of the table from
which the data is to be retrieved.
WHERE specifies the search condition for the rows
returned by the query.
search_condition specifies the condition to be
satisfied to return the selected rows.

The SELECT statement contains some more clauses,


such as GROUP BY, ROLLUP, CUBE, and ORDER BY.
These clauses will be explained later in this chapter.

All the examples in this book are based on the


AdventureWorks, Inc. case study given in the Appendix of the
book, Querying Data Using SQL Server - Activity Book.
For example, the Employee table is stored in the
HumanResources schema of the AdventureWorks
database. To display all the details of the employees, you
can use the following query:
USE AdventureWorks
GO
SELECT * FROM HumanResources.Employee
The Other Data Types GO
The following figure shows the output of the preceding
query.
Retrieving Specific Attributes
While retrieving data from tables, you can display one or
more columns. For example, the AdventureWorks
database stores the employee details, such as EmployeeID,
ManagerID, Title, HireDate, and BirthDate in the
Employee table. You may want to view all the columns of
the Employee table or a few columns, such as
EmployeeID and ManagerID. You can retrieve the
required data from the database tables by using the
The Output Derived After Using the Select Query
SELECT statement.
The result set displays the records in the same order as
The SELECT statement is used to access and retrieve data
they are stored in the source table.
from a database. The syntax of the SELECT statement is:
SELECT [ALL | DISTINCT]
select_column_list The data in the output window may vary depending on
[INTO [new_table_name]] the modifications done on the database.
FROM {table_name | view_name}
[WHERE search_condition]
where, Schema is a namespace that acts as a container of
ALL is represented with an (*) asterisk symbol and objects in a database. A schema can be owned by any user,
and its ownership is transferable. A single schema can
displays all the columns of the table.
contain objects owned by multiple database users.
DISTINCT specifies that only the unique rows should
appear in the result set. If you need to retrieve specific columns from a table, you
can specify the column names in the SELECT statement.
For example, to view specific details such as EmployeeID,
ContactID, LoginID, and Title of the employees of
AdventureWorks, you can specify the column names in the The Report
SELECT statement, as shown in the following query: You can write the query to accomplish the required task in
SELECT EmployeeID, ContactID, LoginID, any of the following ways:
Title FROM HumanResources.Employee 1. SELECT 'Department Number'=
The following figure shows the output of the preceding DepartmentID, ' Department Name'=
query. Name FROM
HumanResources.Department
2. SELECT DepartmentID 'Department
Number', Name ' Department Name'
FROM HumanResources.Department
3. SELECT DepartmentID AS
'Department Number', Name AS '
Department Name' FROM
HumanResources.Department
The following figure shows the output of the preceding
query.

The Output Derived After Using the Select Query


In the preceding output, the result set shows the column
names in the way they are present in the table definition.
You can customize these column names, if required.

The Output Derived After Using the Select Query


In the preceding figure, the columns are displayed with
user-defined headings, but the original column names in
the database table remain unchanged.
Similarly, you might need to make results more
explanatory. In such a case, you can add more text to the
values displayed in the columns by using literals. Literals
are string values that are enclosed in single quotes and
added to the SELECT statement. The literal values are
printed in a separate column as they are written in the
SELECT list. Therefore, literals are used for display
purpose.
The following SQL query retrieves the employee ID and
Customizing the Display their titles from the Employee table along with a literal
Sometimes, you may want to change the way data is
“Designation:”:
displayed. For example, if the names of columns are not
SELECT EmployeeID, 'Designation:',
descriptive, you might need to change the default column
Title
headings by creating user-defined headings. FROM HumanResources.Employee
For example, you need to display the Department ID and The literals are created by specifying the string within
Department Names from the Department table of the quotes and placing the same inside the SELECT query.
AdventureWorks database. You want that the column The following figure shows the output of the preceding
headings in the report should be different from those query.
stored in the table, as shown in the following table.

Department Number Department Name


Calculating Column Values
Sometimes, you might also need to show calculated values
for the columns. For example, the Orders table stores the
order details such as OrderID, ProductID, OrderDate,
UnitPrice, and Units. To find the total amount of an order,
you need to multiply the UnitPrice of the product with the
Units. In such cases, you can apply arithmetic operators.
Arithmetic operators are used to perform mathematical
operations, such as addition, subtraction, division, and
multiplication, on numeric columns or on numeric
constants.
SQL Server supports the following arithmetic operations:
 + (for addition)
The Output Derived After Using the Select Query  - (for subtraction)
In the preceding figure, the result set displays a virtual  / (for division)
column with Designation as a value in each row. This  * (for multiplication)
column does not physically exist in the database table.  % (for modulo - the modulo arithmetic operator is
Concatenating the Text Values in the Output used to obtain the remainder of two divisible
Concatenation is the operation where two strings are numeric integer values)
joined to make one string. For example, the strings, snow All arithmetic operators can be used in the SELECT
and ball can be concatenated to display the output, statement with column names and numeric constants in
snowball. any combination.
As a database developer, you have to manage the When multiple arithmetic operators are used in a single
requirements from various users, who might want to view query, the processing of the operation takes place
results in different ways. You may require to display the according to the precedence of the arithmetic operators.
values of multiple columns in a single column and also The precedence level of arithmetic operators in an
add a description with the column value. In such a case, expression is multiplication (*), division (/), modulo (%),
you can use the concatenation operator. The subtraction (-), and addition (+). You can change the
concatenation operator is used to concatenate string precedence of the operators by using parentheses [()].
expressions. It is represented by the + sign. To concatenate When an arithmetic expression uses the same level of
two strings, you can use the following query: precedence, the order of execution is from left to right.
SELECT 'snow ' + 'ball' For example, the EmployeePayHistory table in the
The preceding query will display snowball as the output. HumanResources schema contains the hourly rate of the
The following SQL query concatenates the data of the employees. The following SQL query retrieves the per day
Name and GroupName columns of the Department table rate of the employees from the EmployeePayHistory table:
into a single column: SELECT EmployeeID, Rate, Per_Day_Rate
SELECT Name + ' department comes under = 8 * Rate FROM
' + GroupName + ' group' AS Department HumanResources.EmployeePayHistory
FROM HumanResources.Department In the preceding query, Rate is multiplied by 8, assuming
In the preceding query, literals such as ‘department comes that an employee works for 8 hours in a day. The
under’ and ‘group’, are concatenated to increase the following figure shows the output of the preceding query.
readability of the output. The following figure shows the
output of the preceding query.

The Output Derived After Using the Select Query


retrieve selected rows by using various comparison
operators. Comparison operators test for similarity
between two expressions. They allow row retrieval from a
table based on the condition specified in the WHERE
clause. Comparison operators cannot be used on text,
ntext, or image data type expressions. The syntax for using
comparison operators in the SELECT statement is:
SELECT column_list
FROM table_name
WHERE expression1 comparison_operator
expression2

where,
expression1 and expression2 are any valid
combination of a constant, a variable, a function, or a
column-based expression.
The Output Derived After Using the Select Query In the WHERE clause, you can use a comparison operator
to specify a condition. The following SQL query retrieves
records from the Employee table where the vacation hour
Retrieving Selected Rows is less than 5:
SELECT EmployeeID, NationalIDNumber,
In a given table, a column can contain different values in Title, VacationHours FROM
different records. At times, you might need to view only HumanResources.Employee WHERE
those records that match a condition. For example, in a VacationHours < 5
manufacturing organization, an employee wants to view a The preceding query retrieves all the rows that satisfy the
list of products from the Products table that are priced specified condition by using the comparison operator, as
between $ 100 and $ 200. Consider another example, shown in the following figure.
where a teacher wants to view the names and the scores of
the students who scored more than 80%. Therefore, the
query must select the names and the scores from the table
with a condition added to the score column.
To retrieve selected rows based on a specific condition,
you need to use the WHERE clause in the SELECT
statement. Using the WHERE clause selects the rows that
satisfy the condition.
The following SQL query retrieves the department details
from the Department table, where the group name is
Research and Development:
SELECT * FROM
HumanResources.Department WHERE
GroupName = 'Research and Development' The Output Derived After Using the Select Query
The following figure shows the output of the preceding The following table lists the comparison operators
query. supported by SQL Server.

Operators Description
= Equal to
> Greater than
The Output Derived After Using the Select Query < Less than
In the preceding figure, the rows containing the Research >= Greater than or equal to
and Development group name are displayed. <= Less than or equal to
Using Comparison Operators to <>, != Not equal to
!< Not less than
Specify Conditions
!> Not greater than
You can specify conditions in the SELECT statement to
The Comparison Operators Supported by SQL Server The following SQL query retrieves records from
Sometimes, you might need to view records for which one the Department table when the GroupName is not
or more conditions hold true. Depending on the Quality Assurance:
requirements, you can retrieve records based on the SELECT * FROM
following conditions: HumanResources.Department WHERE
 Records that match one or more conditions NOT GroupName = 'Quality
 Records that contain values in a given range Assurance'
 Records that contain any value from a given set The preceding query retrieves all the rows except
of values the rows that match the condition specified after
 Records that match a pattern the NOT conditional expression.
 Records that contain NULL values Retrieving Records That Contain
 Records to be displayed in a sequence
 Records from the top of a table
Values in a Given Range
 Records without duplication of values Range operators retrieve data based on a range. The syntax
for using range operators in the SELECT statement is:
Retrieving Records that Match One or SELECT column_list
More Conditions FROM table_name
Logical operators are used in the SELECT statement to WHERE expression1 range_operator
retrieve records based on one or more conditions. While expression2 AND expression3
querying data, you can combine more than one logical
operator to apply multiple search conditions. In a SELECT
statement, the conditions specified in the WHERE clause where,
is connected by using the logical operators. The syntax for
using logical operators in the SELECT statement is:
SELECT column_list expression1, expression2, and expression3
FROM table_name are any valid combination of constants, variables,
WHERE conditional_expression1 {AND/OR} functions, or column-based expressions.
[NOT] range_operator is any valid range operator.
conditional_expression2 Range operators are of the following types:
where,  BETWEEN: Specifies an inclusive range to
conditional_expression1 and search.
conditional_expression2 are any conditional The following SQL query retrieves records from
expressions. the Employee table where the number of hours
The three types of logical operators are: that the employees can avail to go on a vacation
 OR: Returns a true value when at least one is between 20 and 50:
condition is satisfied. For example, the following SELECT EmployeeID, VacationHours
SQL query retrieves records from the Department FROM HumanResources.Employee
table when the GroupName is either WHERE VacationHours BETWEEN 20
Manufacturing or Quality Assurance: AND 50
SELECT * FROM  NOT BETWEEN: Excludes the specified range
HumanResources.Department WHERE from the result set.
GroupName = 'Manufacturing' OR The following SQL query retrieves records from
GroupName = 'Quality Assurance' the Employee table where the number of hours
 AND: Is used to join two conditions and returns a that the employees can avail to go on a vacation
true value when both the conditions are satisfied. is not between 40 and 50:
To view the details of all the employees of SELECT EmployeeID,VacationHours
AdventureWorks who are married and working as FROM HumanResources.Employee
a Production Technician – WC60, you can use the WHERE VacationHours NOT BETWEEN
AND logical operator, as shown in the following 40 AND 50
query: Retrieving Records That Contain Any
SELECT * FROM
HumanResources.Employee WHERE
Value from a Given Set of Values
Title = 'Production Technician - Sometimes, you might want to retrieve data after
WC60' AND MaritalStatus = 'M' specifying a set of values to check whether the specified
value matches any data of the table. This type of operation
 NOT: Reverses the result of the search condition.
is performed by using the IN and NOT IN keywords. The
syntax for using the IN and NOT IN keywords in the name is five characters long and begins with ‘Sale’,
SELECT statement is: whereas the fifth character can be anything. For this, you
SELECT column_list need to use the ‘_’ wildcard character, as shown in the
FROM table_name following query:
WHERE expression list_operator SELECT * FROM
(‘value_list’) HumanResources.Department WHERE Name
where, LIKE 'Sale_'
expression is any valid combination of constants, The following table describes the wildcard characters that
variables, functions, or column-based expressions. are used with the LIKE keyword in SQL server.
list_operator is any valid list operator, IN or NOT
IN. Wildcard Description
value_list is the list of values to be included or % Represents any string of
excluded in the condition. zero or more character(s).
The IN keyword selects the values that match any one of _ Represents any single
the values given in a list. The following SQL query character.
retrieves the records of employees who are Recruiter, [] Represents any single
Stocker, or Buyer from the Employee table: character within the
SELECT EmployeeID, Title, LoginID FROM specified range.
HumanResources.Employee WHERE Title IN
[^] Represents any single
('Recruiter', 'Stocker', 'Buyer')
character not within the
Alternatively, the NOT IN keyword restricts the selection
specified range.
of values that match any one of the values in a list. The
following SQL query retrieves records of employees
The Wildcard Characters Supported by SQL Server
whose designation is not Recruiter, Stocker, or Buyer:
SELECT EmployeeID, Title, LoginID FROM The wildcard characters can be combined into a single
HumanResources.Employee WHERE Title expression with the LIKE keyword. The wildcard
NOT IN ('Recruiter', 'Stocker', characters themselves can be searched using the LIKE
'Buyer') keyword by putting them into square brackets ([]).
The following table describes the use of the wildcard
Retrieving Records That Match a characters with the LIKE keyword.
Pattern
When retrieving data, you can view selected rows that Expression Returns
match a specific pattern. For example, you are asked to LIKE ‘LO%’ All names that begin with
create a report that displays the names of all the products “LO”
of AdventureWorks beginning with the letter P. You can do LIKE ‘%ion’ All names that end with
this by using the LIKE keyword. The LIKE keyword is “ion”
used to search a string by using wildcards. Wildcards are
LIKE ‘%rt%’ All names that have the
special characters, such as ‘*’ and ‘%’. These characters
letters “rt” in them
are used to match patterns.
LIKE ‘_rt’ All three letter names
The LIKE keyword matches the given character string
ending with “rt”
with the specified pattern. The pattern can include
combination of wildcard characters and regular characters. LIKE ‘[DK]%’ All names that begin with
While performing a pattern match, regular characters must “D” or “K”
match the characters specified in the character string. LIKE ‘[A D]ear’ All four letter names that
However, wildcard characters are matched with fragments end with “ear” and begin
of the character string. with any letter from “A”
For example, if you want to retrieve records from the through “D”
Department table where the values of Name column begin LIKE ‘D[^c]%’ All names beginning with
with ‘Pro’, you need to use the ‘%’ wildcard character, as “D” and not having “c” as
shown in the following query: the second letter.
SELECT * FROM
HumanResources.Department WHERE Name The Use of the Wildcard Characters with the LIKE Keyword
LIKE 'Pro%'
Consider another example, where you want to retrieve the
The Like operator is not case-sensitive. For example,
rows from the Department table in which the department Like 'LO%' and Like 'lo%' will return the same result.
The Contact Table
The contact details contain the residential, office, and
mobile number of an employee. If the employee does not
have any of the contact numbers, it is substituted with a
null value. Now, you want to display a result set by
substituting all the null values with zero. To perform this
task, you can use the ISNULL() function. The ISNULL()
function replaces the null values with the specified
replacement value. The syntax for using the ISNULL()
Retrieving Records That Contain NULL function in the SELECT statement is:
ISNULL ( check_expression ,
Values replacement_value )
A NULL value in a column implies that the data value for
where,
the column is not available. You might be required to find
check_expression is the expression to be checked
records that contain null values or records that do not
contain NULL values in a particular column. In such a for null.
case, you can use the unknown_value_operator in your replacement_value is the value that replaces the
queries. null values in the resultset.
The syntax for using the unknown_value_operator in the For example, the following SQL query replaces the null
SELECT statement is: values with zero in the query output:
SELECT column_list SELECT EmployeeID, ISNULL(Residence,
FROM table_name 0) AS Residence, ISNULL(Office, 0.00)
WHERE column_name AS Office, ISNULL(Mobile Number, 0.00)
unknown_value_operator AS Mobile Number FROM Contact
where, The following figure displays the output of the preceding
unknown_value_operator is either the keyword IS query.
NULL or IS NOT NULL.
The following SQL query retrieves only those rows from
the EmployeeDepartmentHistory table for which the value
in the EndDate column is NULL:
SELECT EmployeeID, EndDate FROM
HumanResources.EmployeeDepartmentHisto
ry WHERE EndDate IS NULL
The Output Derived After Using the ISNULL()
No two NULL values are equal. You cannot compare Consider another example, the following SQL query
one NULL value with another. replaces the null values with zero in the SalesQuota
column in the query output:
NULL values are always the first item to be displayed in the SELECT SalesPersonID, ISNULL
output that is sorted in an ascending order. (SalesQuota, 0.00) AS 'Sales Quota'
At times, you might need to handle the null values in a FROM Sales.SalesPerson
table quiet differently. For example, the contact details of The following figure displays the output of the preceding
the employees are stored in the following Contact table. query.
the Contact table:
SELECT EmployeeID, COALESCE(Residence,
Office ,Mobile_Number)AS
Contact_Number FROM Contact

Retrieving Records to be Displayed in a


The Output Derived After Using the ISNULL()
Sequence
You can use the ORDER BY clause of the SELECT
At times, you might need to retrieve the first non null
statement to display the data in a specific order.Data
value from a list of values in each row. For example, the
can be displayed in the ascending or
Contact table contains three columns to store the
residence, office, and mobile number details of employees. descending order of values in a given column.
You want to generate a report that displays the first non The syntax for using the ORDER BY clause in the
null contact number in each row for every employee, as SELECT statement is:
shown in the following table. SELECT select_list
FROM table_name
[ORDER BY order_by_expression [ASC|
EmployeeID Contact_Number
DESC]
1 945673561 [, order_by_expression [ASC|DESC]...]
2 23456 where,
3 912345678 order_by_expression is the column name on
4 908087657 which the sort operation is to be performed.
ASC specifies that the values need to be sorted in
The Report ascending order.
In the preceding table, the employee with ID, 1, has only DESC specifies that the values need to be sorted in
the office number. Therefore, it is displayed in the descending order.
Contact_Number column. The employee with ID, 2, has The following SQL query retrieves the records from the
the residence and office numbers. Therefore, the residence Department table by setting ascending order on the Name
number, as it is coming first in the row, is displayed in the column:
result set. SELECT DepartmentID, Name FROM
To display such type of reports, you can use the HumanResources.Department ORDER BY
COALESCE() function. The COALESCE() function Name ASC
checks the values of each column in a list and returns the Optionally, you can sort the result set based on more than
first non null contact number. The null value is returned one column. For this, you need to specify the sequence of
only if all the values in a list are null. The syntax for using the sort columns in the ORDER BY clause, as shown in
the COALESCE() function is: the following query:
COALESCE ( column_name [ ,...n ] ) SELECT GroupName, DepartmentID, Name
For example, you can use the following SQL query to FROM HumanResources.Department ORDER
display the very first contact number of the employees in BY GroupName, DepartmentID
The preceding query sorts the Department table in Employee table:
ascending order of GroupName, and then ascending order SELECT TOP 10 PERCENT * FROM
of DepartmentID, as shown in the following figure. HumanResources.Employee
In the output of the preceding query, 29 rows will be
returned where the total number of rows in the Employee
table is 290.
If the SELECT statement including TOP has an ORDER
BY clause, then the rows to be returned are selected after
the ORDER BY clause has been applied.
For example, you want to retrieve the top three records
from the Employee table where the HireDate is greater
than or equal to 1/1/98 and less than or equal to 12/31/98.
Further, the record should be displayed in the ascending
order based on the SickLeaveHours column. To
accomplish this task, you can use the following query:
SELECT TOP 3 * FROM
The Output Derived After Using the ORDER BY Clause HumanResources.Employee WHERE HireDate
>= ‘1/1/98’ AND HireDate <= ‘12/31/98’
If you do not specify the ASC or DESC keywords with ORDER BY SickLeaveHours ASC
the column name in the ORDER BY clause, the records are Consider another example, where you want to retrieve the
sorted in ascending order. details of top 10 employees who have the highest sick
leave hours. In addition, the result set should include all
The ORDER BY clause does not sort the table physically. those employees whose sick leave hours matches the
lowest sick leave hours included in the result set:
Retrieving Records from the Top of a SELECT TOP 10 WITH TIES EmployeeID,
Table Title, SickLeaveHours FROM
You can use the TOP keyword to retrieve only the first set HumanResources.Employee
of rows from the top of a table. This set of records can be ORDER BY SickLeaveHours DESC
either a number of records or a percent of rows that will be The following figure displays the output of the preceding
returned from a query result. query.
For example, you want to view the product details from
the product table, where the product price is more than $
50. There might be various records in the table, but you
want to see only the top 10 records that satisfy the
condition. In such a case, you can use the TOP keyword.
The syntax for using the TOP keyword in the SELECT
statement is:
SELECT [TOP n [PERCENT] [WITH TIES]]
column_name [,column_name...]
FROM table_name
WHERE search_conditions
[ORDER BY [column_name
[,column_name...]]
where,
n is the number of rows that you want to retrieve. If the
PERCENT keyword is used, then “n” percent of the rows
are returned.
WITH TIES specifies that result set includes all the The Output Derived After Using the WITH TIES Clause
additional rows that matches the last row returned by the In the preceding figure, the number of rows returned is 13.
TOP clause. It is used along with the ORDER BY clause. The last three rows has the sick leave hours as 68, which is
The following query retrieves the top 10 rows of the the same as the sick leave hours for the 10th row, which is
Employee table: the 1ast row returned by the TOP clause.
SELECT TOP 10 * FROM Retrieving Records from a Particular
HumanResources.Employee
Position
The following query retrieves the top 10% rows of the
At times, you might need to retrieve a specific number of
records starting from a particular position in a table. For
example, you may want to retrieve only five records at a
time, starting from a specified position, and in a particular
order.
With the ORDER BY clause, you can retrieve the records
in a specific order but cannot limit the number of records
returned.
With the TOP clause, you can limit the number of records
returned but cannot retrieve the records from a specified
position.
In such a case, you can use the OFFSET and FETCH The Output Derived After Using the OFFSET Clause
clause to retrieve a specific number of records, starting You may want to retrieve the 10 records from the
from a particular position, in the result set. The result set Employee table, excluding the first 15 records. In such a
should be sorted on a particular column. case, you can use the FETCH clause along with the
The syntax for using the OFFSET-FETCH clause in the OFFSET clause, as shown in the following query:
SELECT statement is: Select
SELECT select_list EmployeeID,NationalIDNumber,ContactID
FROM table_name ,HireDate
[ORDER BY order_by_expression [ASC| from HumanResources.Employee
DESC] Order By EmployeeID
[, order_by_expression [ASC|DESC]...] OFFSET 15 ROWS
[ <offset_fetch> ] FETCH NEXT 10 ROWS ONLY

<offset_fetch> ::=
OFFSET [ integer_constant |
offset_row_count_expression ] [ ROW |
ROWS]
FETCH [ FIRST | NEXT ]
[integer_constant |
fetch_row_count_expression ] [ ROW
| ROWS ] ONLY
where,
OFFSET specifies the number of rows to be excluded
before the query execution. The value can be an integer
constant or an expression. The Output Derived After Using the OFFSET and FETCH
CLAUSES
FETCH specifies the number of rows to be returned in the
query execution. The value can be an integer constant or
an expression. FIRST and NEXT are similar. You can use It is mandatory to use ORDER BY clause with the
either of them with the FETCH clause. Similarly, ROW OFFSET and FETCH clause.
and ROWS are same, use either of them.
For example, you want to retrieve the records of
employees from the Employee table. But, you do not want FETCH clause can be used only with the OFFSET
to include the first 15 records in the result set. In such a clause.
case, you can use the OFFSET clause to exclude the first
15 records from the result set, as shown in the following
query:
Select
EmployeeID,NationalIDNumber,ContactID
,HireDate from HumanResources.Employee
Order By EmployeeID
OFFSET 15 ROWS
The following figure displays the output of the preceding
query.
Retrieving Records Without Duplication
of Values Task 2.1: Displaying Query
You can use the DISTINCT keyword when you need to Results in Different Formats
eliminate rows with duplicate values in a column. The
DISTINCT keyword eliminates the duplicate rows from
the result set. The syntax of the DISTINCT keyword is:
SELECT [ALL|DISTINCT] column_names Activity 2.1: Retrieving Data
FROM table_name
WHERE search_condition
where, Using Functions to Customize the
DISTINCT keyword specifies that only the records
containing non-duplicated values in the specified column Result Set
are displayed. While querying data from SQL Server, you can use
The following SQL query retrieves all the Titles beginning various in-built functions provided by SQL Server to
with PR from the Employee table: customize the result set. Customization includes changing
SELECT DISTINCT Title FROM the format of the string or date values or performing
HumanResources.Employee WHERE Title calculations on the numeric values in the result set. For
LIKE 'PR%' example, if you need to display all the text values in upper
The execution of the preceding query displays a title only case, you can use the upper() string function. Similarly, if
once. you need to calculate the square of the integer values, you
If the DISTINCT keyword is followed by more than one can use the power() mathematical function.
column name, then it is applied to all the columns. You Depending on the utility, the in-built functions provided
can specify the DISTINCT keyword only before the select by SQL Server are categorized as string functions, date
list. functions, mathematical functions, ranking functions, and
system functions.

Using String Functions


You can use the string functions to manipulate the string
values in the result set. For example, to display only the
first eight characters of the values in a column, you can
use the left() string function.
String functions are used with the char and varchar data
types. SQL Server provides string functions that can be
used as a part of any character expression. These functions
are used for various operations on strings.
The syntax for using a function in the SELECT statement
is: without using any
SELECT function_name (parameters) wildcard
where, characters.
function_name is the name of the function. Difference SELECT Returns 4, the
parameters are the required parameters for the string (character_expres difference DIFFERENCE
function. sion1, ('HELLO', function compares
For example, you want to retrieve the Name, character_expressi 'hell') two strings and
DepartmentID, and GroupName columns from the on2) evaluates the
Department table and the data of the Name column should similarity between
be displayed in uppercase with a user-defined heading, them, returning a
Department Name. For this, you can use the upper() string value from 0
function, as shown in the following query: through 4. The
SELECT 'Department Name'= upper(Name), value 4 is the best
DepartmentID, GroupName FROM match.
HumanResources.Department Left SELECT left Returns 'RICH',
The following SQL query uses the left() string function to (character_expres ('RICHARD',4 which is a part of
extract the specified characters from the left side of a sion, ) the character
string: integer_expression string equal in size
SELECT Name = Title + ' ' + left ) to the
(FirstName,1) + '. ' + LastName, integer_expression
EmailAddress FROM Person.Contact characters from
The following figure shows the output of the preceding the left.
query. Len SELECT len Returns 7, the
(character_expres ('RICHARD') number of
sion) characters in the
character_expressi
on.
Lower SELECT lower Returns 'richard',
(character_expres ('RICHARD') after converting
sion) character_expressi
on to lower case.
Ltrim SELECT ltrim Returns
(character_expres (' RICHARD') 'RICHARD'
sion) without leading
The Output Derived After Using the Query spaces. It removes
The following table lists the string functions provided by leading blanks
SQL Server. from the character
expression.
Function name Example Description Replace SELECT Returns ‘Universe’
Ascii SELECT ascii Returns 65, the (string_expression, REPLACE after replacing
(character_expres ('ABC') ASCII code of the string_pattern, ('University ‘ity’ with ‘e’.
sion) leftmost character string_replacemen ','ity','e')
'A'. t) ;
Char (integer SELECT char Returns 'A', the Replicate SELECT Returns
_expression) (65) character (string_expression Replicate ‘UniversityUniver
equivalent of the ,integer_expressio ('University
sity’. It repeats the
ASCII code value. n) ',2) string value for a
Charindex SELECT Returns 2, the specified number
(‘pattern’, charindex starting position of of times.
expression) ('E','HELLO' the specified Patindex (‘% Select Returns 7, the
) pattern in the pattern%’, patindex ('% starting position of
expression. The expression) BOX the first
pattern should be %','ACTIONBO occurrence of the
a string literal X') pattern in the
specified char_expression2
expression, or into
zeros if the pattern character_expressi
is not found. It on1 at the start
uses wildcard position.
characters to Substring SELECT Returns 'ea', which
specify the pattern (expression, start, substring is part of the
to be searched. length) ('Weather', character string. It
Reverse SELECT Returns 'NOITCA', 2,2) returns the part of
(character_expres reverse the reverse of the the source
sion) ('ACTION') character_expressi character string
on. from the start
Right SELECT right Returns 'HARD', a position of the
(character_expres ('RICHARD',4 part of the expression.
sion, ) character string, Upper SELECT upper Returns
integer_expression after extracting (character_expres ('Richard') 'RICHARD'. It
) from the right the sion) converts lower
number of case characters to
characters upper case.
specified in the Concat SELECT Returns
integer_expression (string_value1, concat ‘RichardHill
. string_value2 [, ('Richard', ’. It
Rtrim SELECT rtrim Returns string_valueN ] ) 'Hill') concatenates
(character_expres ('RICHARD ') 'RICHARD', after two or more
sion) removing any strings into
trailing blanks a single
from the character string.
expression.
Space SELECT Returns The String Functions Provided by SQL Server
(integer_expressio 'RICHARD'+ 'RICHARD HILL'.
n) space (2) Two spaces are
+'HILL' inserted between Using Conversion Functions
the first and You can use the conversion functions to convert data from
second word. one type to another. For example, you want to convert a
Str SELECT str Returns '123.45'. It string value into a numeric format. You can use the parse()
(float_expression, (123.45,6,2) converts numeric function to convert string values to numeric or date time
[length, data to character format, as shown in the following query:
[decimal]]) data where the SELECT parse(‘219’ as decimal)
length is the total The following table lists the conversion functions
length, including provided by SQL Server.
the decimal point,
the sign, the digits,
and the spaces and
the decimal is the
number of places
to the right of the
decimal point.
Stuff SELECT stuff Returns 'Wither'. It
(character_expres ('Weather', deletes the number
sion1, start, 2,2, 'i') of characters as
length, specified in the
character_expressi character_expressi
on2) on1 from the start
and then inserts
The Conversion Functions Provided by SQL Server date) 3,‘2009-01-0 3 months to the
The following table lists the style values for displaying 1’) date.
datetime expressions in different formats. Datediff (date SELECT Returns 4,
part, date1, date2) datediff calculates the
Style value Format (year, number of date
1 mm/dd/yyyy convert parts between two
2 yy.mm.dd (datetime,'2 dates.
005-06-06'),
3 dd/mm/yyyy
convert
4 dd.mm.yy (datetime,
5 dd-mm-yy '2009-01-01'
))
The Style Values Datename (date SELECT Returns June, date
part, date) datename part from the listed
The TRY_CONVERT() may give an error at the time (month, date as a
of execution that it is not a recognized built-in function name. convert character value.
Perform the following steps to resolve this problem: Right- (datetime,'2
click the AdventureWorks database in the Object-Explorer 005-06-06'))
window. Select Properties. The Database Properties- Datepart (date SELECT Returns 9, the
AdventureWorks dialog box is displayed. Select Options part, date) datepart(mm, month, from the
in the Select a page pane of the dialog box. Select SQL '2009-09-01' listed date as an
Server 2012(110) in the Compatibility level drop-down ) integer.
list. Click OK to close the dialog box. Getdate() SELECT Returns the
getdate() current date and
time.
Using Date Functions Day (date) SELECT day Returns 5, an
('2009 integer, which
You can use the date functions of SQL Server to 01-05') represents the day.
manipulate date and time values. You can either perform SELECT
Getutcdate() Returns the
arithmetic operations on date values or parse the date getutcdate() current date of the
values. Date parsing includes extracting components, such
system in
as the day, the month, and the year from a date value.
Universal Time
You can also retrieve the system date and use the value in Coordinate (UTC)
the date manipulation operations. To retrieve the current time. UTC time is
system date, you can use the getdate() function. The also known as the
following query displays the current date: Greenwich Mean
SELECT getdate()
Time (GMT).
The datediff() function is used to calculate the difference SELECT month
Month (date) Returns 1, an
between two dates. For example, the following SQL query ('2009 01 integer, which
uses the datediff() function to calculate the age of the 05') represents the
employees:
month.
SELECT datediff (yy, BirthDate,
getdate()) AS 'Age' Year (date) SELECT year Returns 2009, an
('2009-01-05 integer, which
FROM HumanResources.Employee
') represents the
The preceding query calculates the difference between the
current date and the date of birth of employees, whereas, year.
the date of birth of employees is stored in the BirthDate Datefromparts Select Returns
column of the Employee table in the AdventureWorks (year, date, month) datefrompart 2009-01-05, which
database. s is the value for the
(2009,01,05) specified year,
The following table lists the date functions provided by
SQL Server. month, and day.
Datetimefromparts SELECT Returns
Function name Example Description (year, month, day, DATETIMEFROM 2009-01-09
hour, minute, PARTS (2009, 12:25:45.050,
Dateadd (date SELECT Returns
seconds, 01, 09, which is the full
part, number, dateadd(mm, 2009-04-01, adds
milliseconds ) 12,25, 45, datetime value.
50 )
Eomonth SELECT Returns
(start_date [, EOMONTH 2009-03-31.It
month_to_add ]) ('2009-01-09 displays the last
', 2) date of the month
specified in
start_date. The
month_to_add
arguments
specifies the The Output Derived After Using the Query
number of months
to add to
start_date.

The Date Functions Provided by SQL Server


To parse the date values, you can use the datepart()
function in conjunction with the date functions. For
example, the datepart() function retrieves the year when an
employee was hired, along with the employee title, from
the Employee table, as shown in the following query:
SELECT Title, datepart (yy, HireDate)
AS 'Year of Joining'
FROM HumanResources.Employee
SQL Server provides the abbreviations and values of the
of the first parameter in the datepart() function, as shown
in the following table.

Date part Abbreviation Values


Year yy, yyyy 1753-9999
Quarter qq, q 1-4 Using Mathematical Functions
Month mm, m 1-12 You can use mathematical functions to manipulate the
day of year dy, y 1-366 numeric values in a result set. You can perform various
Day dd, d 1-31 numeric and arithmetic operations on the numeric values.
Week wk, ww 0-51 For example, you can calculate the absolute value of a
Weekday Dw 1-7(1 is Sunday) number or you can calculate the square or square root of a
Hour Hh (0-23) value.
Minute mi, n (0-59) The following table lists the mathematical functions
Second ss, s 0-59 provided by SQL Server.
Millisecond Ms 0-999
Function name Example Description
The Abbreviations Used to Extract Different Parts of a Date Abs SELECT abs Returns 87, an
The following SQL query uses datename() and datepart() (numeric_expressi (-87) absolute value.
functions to retrieve the month name and year from a on)
given date: Ceiling SELECT Returns 15, the
SELECT EmployeeID,datename(mm, (numeric_expressi ceiling smallest integer
hiredate)+ ', ' + convert on) (14.45) greater than or
(varchar,datepart(yyyy, hiredate)) as equal to the
'Joining' specified value.
FROM HumanResources.Employee Exp SELECT exp Returns
The following figure shows the output of the preceding (float_expression) (4.5) 90.017131300521
query. 8, the exponential
value of the
specified value. length is the precision to which the expression is to be
Floor SELECT floor Returns14, the rounded off.
(numeric_expressi (14.45) largest integer less The following SQL query retrieves the EmployeeID and
on) than or equal to Rate for the specified employee ID from the
the specified EmployeePayHistory table:
value. SELECT EmployeeID, 'Hourly Pay Rate' =
Log SELECT log Returns round(Rate,2)
(float_expression) (5.4) 1.6863989535702 FROM HumanResources.EmployeePayHistory
3, the natural The following figure shows the output of the preceding
logarithm of the query.
specified value.
log10 SELECT log10 Returns the
(float_expression) (5.4) base-10 logarithm
of the specified
value.
Pi () SELECT pi() Returns the
constant value of
3.1415926535897
93.
Power SELECT power Returns 64, which
(numeric_expressi (4,3) is 4 to the power
on, y) of 3. The Output Derived After Executing the Query
Radians SELECT Returns 3, In the preceding figure, the value of the Hourly Pay Rate
(numeric_expressi radians(180) converts from column is rounded off to two decimal places.
on) degrees to radians. While using the round() function, if the length is positive,
Rand ([seed]) SELECT rand Returns a random then the expression is rounded to the right of the decimal
() float number point. If the length is negative, then the expression is
between 0 and 1. rounded to the left of the decimal point.
Round SELECT round Returns 15.790, a The following table lists the usage of the round() function
(numeric_expressi (15.789, 2) numeric provided by SQL Server.
on, length) expression
rounded off to the Function Output
length specified as round (1234.567,2) 1234.570
an integer round (1234.567,1) 1234.600
expression. round (1234.567,0) 1235.000
Sign SELECT sign Returns positive, round (1234.567,-1) 1230.000
(numeric_expressi (-15) negative, or zero. round (1234.567,-2) 1200.000
on) Here, it returns -1. round (1234.567,-3) 1000.000
Sqrt SELECT sqrt Returns 8, the
(float_expression) (64) square root of the The Usage of the round() Function
specified value.

The Mathematical Functions Provided by SQL Server Using Logical Functions


For example, to calculate the round off value of any
number, you can use the round() mathematical function. You can use the logical functions to perform logical
The round() mathematical function calculates and returns operations on a result set. For example, to return a value
the numeric value based on the input values provided as an from a list of values, you can use the Choose() function.
argument. The logical functions return a Boolean value as output.
The syntax of the round() function is: The following table lists the logical functions provided by
round(numeric_expression, length) SQL Server.
where,
numeric_expression is the numeric expression to
be rounded off.
The row_number() function returns the sequential
numbers, starting at 1, for the rows in a result set based on
a column.
For example, the following SQL query displays the
sequential number on a column by using the row_number
() function:
SELECT EmployeeID, Rate, row_number()
The Logical Functions OVER(ORDER BY Rate desc) AS RANK FROM
HumanResources.EmployeePayHistory
The following figure displays the output of the preceding
query.

The Output Derived After Using the row_number() Function


The EmployeeID and Rate column are retrieved from the
EmployeePayHistory table, where the Rate column is
ranked by using the row_number() function. The ORDER
BY keyword in the OVER clause specifies that the result
Using Ranking Functions set will appear in the descending order of the Rate column.

You can use ranking functions to generate sequential


The rank() Function
The rank() function returns the rank of each row in a result
numbers for each row or to give a rank based on specific
set based on the specified criteria. For example, you want
criteria. For example, in a manufacturing organization, the
to rank the products based on the sales made during a year.
management wants to rank the employees based on their
For this, you can use the rank() function. This function
salary. To rank the employees, you can use the rank()
will consider the ORDER BY clause and the record with
function.
maximum value will get the highest rank if the ORDER
Consider another example, where a teacher wants to see
BY clause is asc.
the names and scores of all students according to their
For example, you want to create the report of all the
ranks. Ranking functions return a ranking value for each
employees with their salary rates. The salary rates should
row. However, based on the criteria, more than one row
also contain the rank of the salary. The highest ranked
can get the same rank. You can use the following functions
employee should be given the rank as 1. In addition, if two
to rank the records:
employees have the same salary rate, they should be given
 row_number()
the same rank.
 rank()
To perform this task, you need to use the following query:
 dense_ rank()
SELECT EmployeeID, Rate, rank() OVER
 ntile()
(ORDER BY Rate desc) AS Rank FROM
All these functions make use of the OVER clause. This
HumanResources.EmployeePayHistory
clause determines the ascending or descending sequence
The following figure displays the output of the preceding
in which rows are assigned a rank. You can use the
query.
PARTITION BY clause with the OVER clause to partition
the rows on which the ranking is performed. The
PARTITION BY clause divides the result set returned into
partitions based on the specified condition, and then the
RANK function is applied to each partition.
The row_number() Function
The Output Derived After Using the rank() Function The Output Derived After Using the dense_rank()
In the preceding output, the salary rates for the employee Consider another example, where you want to rank the
ID 158 and 42 are same. Therefore, both of them have sales person‘s data according to the value of the sale
been ranked 6 but the rank for the employee ID 140 is 8, amount. However, you want that the ranking should be
not 7. To provide the consecutive ranks, you need to use performed for each sales territory. You can perform this
the dense_rank() function. task by using the PARTITION BY clause on the sales
The dense_rank() Function territory column, as shown in the following query:
SELECT SalesPersonID, TerritoryID,
The dense_rank() function is used where consecutive
ranking values need to be given based on the specified SalesYTD,
dense_rank() OVER(PARTITION BY
criteria. It performs the same ranking task as the rank()
TerritoryID ORDER BY SalesYTD desc)
function, but provides consecutive ranking values to an
as Rank FROM Sales.SalesPerson where
output. For example, you want to rank the products based
TerritoryID IS NOT NULL
on the sales done for that product during a year. If two
products A and B have same sale values, both will be The following figure displays the output of the preceding
assigned a common rank. The next product in the order of query.
sales values will be assigned the next rank value.
If in the preceding example of the rank() function, you
need to give the same rank to the employees with the same
salary rate and the consecutive rank to the next one, you
need to use the following query:
SELECT EmployeeID, Rate, dense_rank()
OVER(ORDER BY Rate desc) AS Rank FROM
HumanResources.EmployeePayHistory
The following figure displays the output of the preceding
query.
The Output Derived After Using the ntile() Function
In the preceding figure, the records in the Employee table
are divided into four groups after arranging the data in the
The Output Derived After Using the PARTITION BY Clause BirthDate column in ascending order. Each group contains
In the preceding figure, first the sales territory are four rows and each row in a group is given the same
partitioned on the basis of TerritoryID, and then ranking is ranking.
applied on the SalesYTD column for each territory. The number of rows in a group depends on the total
While using the dense_rank() function, the ranks assigned number of rows in the table. If the number of rows in a
to the rows are consecutive and depend on the distinct table is not divisible by the number specified in the ntile()
values of the column on which ranking is performed. function, the groups will be of two sizes, one larger or
However, if you need to divide the rows into the specific smaller and the rest equal. Larger groups come before
number of rank groups, you can use the ntile() function. smaller groups in the order specified by the OVER clause.
The ntile() Function For example, if the number of rows in the table is 16 and
The ntile() function is used to divide the result set into a the result set is partitioned into 5 groups, the first group
specific number of groups. This function accepts a will contain 4 records. However, the rest of the groups will
positive integer to distribute the rows into the number of contain 3 records. Continuing with the preceding example,
groups. The distributed groups are numbered, starting change the number of groups to 5 in the ntile() function, as
from 1. For each row retrieved from the database, the ntile shown in the following query:
() function returns the number of group to which the row SELECT EmployeeID, BirthDate,
belongs. HireDate, ntile(5) OVER(ORDER BY
For example, you want to retrieve the details of those BirthDate) AS Rank
employees who are hired after April 2001. The employees FROM HumanResources.Employee
WHERE datepart(mm, HireDate) >= 04 and
need to be grouped into four groups, and then ranked
datepart(yy, HireDate) >= 2001
based on their birth date. You can perform this task by
using the ntile() function with 4 as parameter, as shown in The following figure displays the output of the preceding
the following query: query.
SELECT EmployeeID, BirthDate,
HireDate, ntile(4) OVER(ORDER BY
BirthDate) AS Rank
FROM HumanResources.Employee
WHERE datepart(mm, HireDate) >= 04 AND
datepart(yy, HireDate) >= 2001
The following figure displays the output of the preceding
query.
Using Analytical Functions
At times, you may want to perform analytical comparison
of the data stored in a table and compute some aggregate
result value like average or percentage. For example,
consider the following Emp_SalesData table.

The Output Derived After Using the ntile() Clause


As shown in the preceding figure, the first group contains
four records. However, the rest of the records contain three The Emp_SalesData Table
records. The preceding table stores the month wise sales details for
the products, P1, P2, and P3. You want to display the sales
amount for each month as well as for the following and
preceding months to analyze the increase or decrease in
the sales. SQL Server provides the analytical functions to
perform such analysis. Unlike aggregate functions,
analytical functions can return multiple rows for each
group specified. Some of the analytical functions are:
 lead()
 lag()
 first_value()
 last_value()
lead()
The lead() function returns the values from the subsequent
rows in the same result set. This analytical function is used
to compare values in the current row with the values in the
subsequent rows. The syntax of the lead() function is:
lead ( scalar_expression [ ,offset ] ,
[ default ] )
OVER ( [ partition_by_clause ]
order_by_clause )
where,
scalar_expression is the value to be returned
based on the specified offset.
offset is the number of rows to be forwarded from the
current row. If the offset is not specified, it will take the
default value, 1.
Default is the value to return when scalar_expression
at offset is NULL. If it is not specified, it will take the
default value, NULL.
OVER ( [ partition_by_clause ]
order_by_clause) specifies the order of the data
before the function is applied.
For example, you want to prepare a sales report that
displays the comparison of sales happened in a particular
month with the next month sales. For this, you need to
display the total sales amount for each month along with
the total sales amount for the following month. To perform
this task, you can use the following query:
SELECT
MONTH, SUM(amount) AS TotalSale,lead
(sum(amount), 1) OVER (ORDER BY month)
AS Next_Month_Sales The Output Derived by Using the lag() Function
FROM EMP_SALESDATA first_value()
GROUP BY MONTH The first_value() function returns the first value in an
ORDER BY MONTH;
ordered set of records. For example, you may want to
The following figure displays the output of the preceding retrieve the first month of the year when the sales amount
query. for each product was collected for the first time. To
perform this task, you can use the first_value() function.
The syntax of this function is:
first_value ( [scalar_expression )
OVER ( [ partition_by_clause ]
order_by_clause [
rows_range_clause ] )
where,
rows_range_clause limits the rows within the
partition by specifying the start and end points.
The Output Derived by Using the lead() Function For example, consider the following query:
lag() SELECT DISTINCT ProductID, first_value
The lag() function returns the values from the preceding (Month) OVER (PARTITION BY ProductID
rows in the same result set. This analytical function is used ORDER BY Month ) AS
to compare the values in the current row with the values in 'FirstMonthOfCollection' FROM
the previous rows. The syntax of the lag() function is: EMP_SALESDATA
lag (scalar_expression [,offset] The preceding query retrieves the first month of the year
[,default]) when the sales amount for each product was collected for
OVER ( [ partition_by_clause ] the very first time, as shown in the following figure.
order_by_clause )
For example, you want to prepare a sales report that
displays the comparison of sales happened in the current
month with the previous month sales. For this, you need to
display the total sales amount for each month along with
the total sales amount for the previous month. To perform
this task, you can use the following query:
SELECT
MONTH, SUM(amount) AS TotalSale,lag
(sum(amount), 1) OVER (ORDER BY month)
AS Previous_Month_Sales
FROM EMP_SALESDATA
GROUP BY MONTH
ORDER BY MONTH; The Output Derived by Using the first_value() Function
The following figure displays the output of the preceding last_value()
query. The last_value() function returns the last value in an
ordered set of records. For example, you may want to
retrieve the most recent month of the year when the sales
amount for each product was collected. To perform this
task, you can use the last_value() function. The syntax of
using this function is:
last_value ( [scalar_expression ) OVER
( [ partition_by_clause ] process ID number 'HostID'
order_by_clause of a client process.
[ rows_range_clause ] ) host_name () Returns the SELECT
For example, consider the following query: current host host_name()
SELECT DISTINCT ProductID, last_value computer name of AS
(Month) OVER (PARTITION BY ProductID a client process. 'HostName'
ORDER BY Month ROWS BETWEEN CURRENT suser_sid Returns the SELECT
ROW AND UNBOUNDED FOLLOWING) AS ([‘login_name’]) Security suser_sid
'RecentMonthOfCollection' FROM Identification ('sa') AS
EMP_SALESDATA (SID) number SID
The preceding query retrieves the most recent month of corresponding to
the year when the sales amount for each product was the log on name of
collected, as shown in the following figure. the user.
suser_id Returns the log on SELECT
([‘login_name’]) Identification (ID) suser_id
number ('sa') AS
corresponding to USERID
the log on name of
the user.
suser_sname Returns the log on SELECT
([server_user_id]) name of the user suser_sname
corresponding to (0x01) AS
the security SUSER
identification
number.
The Output Derived by Using the last_value() Function user_id Returns the SELECT
([‘name_in_db’]) database user_id
identification ('Robert')
Using System Functions number AS USERID
corresponding to
The system functions are used to query the system tables. the user name.
System tables are a set of tables that are used by SQL user_name Returns the user SELECT
Server to store information about users, databases, tables, ([user_id]) name user_name
and security. The system functions are used to access the corresponding to (13)AS
SQL Server databases or user-related information. For the database UserName
example, to view the host ID of the terminal on which you identification
are logged on, you can use the following query: number.
SELECT host_id() AS 'HostID'
db_id Returns the SELECT db_id
The preceding query displays the output as shown in the ('AdventureW
(['db_name']) database
following figure. orks') AS
identification
number of the DatabaseID
database.
db_name ([db_id]) Returns the SELECT
database name. db_name(17)
The Output Derived After Executing the host_id()
AS
DatabaseName
The system functions may display different output on object_id Returns the SELECT
different computers. (‘objname’) database object ID object_id
The following table lists the system functions provided by number. ('AdventureW
SQL Server. orks.HumanRe
sources.Empl
Functions Description Example oyee') AS
ObjectID
host_id() Returns the SELECT
host_id() AS object_name Returns the SELECT
current host
(‘obj_id’) database object object_name Aggregate Functions
name. (901578250)
AS At times, you need to calculate the summarized values of a
ObjectName column based on a set of rows. For example, the salary of
cast ( expression Converts an SELECT cast employees is stored in the Rate column of the
AS data_type expression of one (NationalIDN EmployeePayHistory table and you need to calculate the
[ (length ) ]) data type to the umber AS average salary earned by the employees.
specified data CHAR(20)) AS The aggregate functions, on execution, summarize the
type. IDNUMBER values of a column or a group of columns, and produce a
FROM single value. The syntax of an aggregated function is:
HumanResourc SELECT aggregate_function([ALL|
es.Employee DISTINCT] expression)
convert Converts an SELECT FROM table_name
( data_type expression of one convert(CHAR where,
[ ( length ) ], data type to the (20), ALL specifies that the aggregate function is applied to all
expression [, specified data NationalIDNu the values in the specified column.
style ] ) type. mber) AS DISTINCT specifies that the aggregate function is
IDNUMBER applied to only unique values in the specified column.
FROM expression specifies a column or an expression with
HumanResourc operators.
es.Employee You can calculate summary values by using the following
datalength Returns the SELECT aggregate functions:
( expression ) number of bytes datalength  Avg(): Returns the average of values in a numeric
used to represent ('Test expression, either all or distinct.
the specified Expression') The following SQL query retrieves the average
expression. AS value from the Rate column of the
LengthOfData EmployeePayHistory table with a user-defined
heading:
The System Functions Provided by SQL Server SELECT 'Average Rate' = avg
(Rate) FROM
HumanResources.EmployeePayHistory
Activity 2.2: Customizing the  Count(): Returns the number of values in an
Result Set expression, either all or distinct. The following
SQL query retrieves the unique rate values from
the EmployeePayHistory table with a user-
defined heading:
Summarizing and Grouping Data SELECT 'Unique Rate' = count
At times, the users need to view a summary of the data. (DISTINCT Rate)
Summary of the data contains aggregated values that help FROM
in data analysis at a broader level. For example, to analyze HumanResources.EmployeePayHistory
the sales, the users might want to view the average sales or The count() function also accepts (*) as its
total sales for a specified time period. SQL Server parameter, but it counts the number of rows
provides aggregate functions to generate summarized data. returned by the query.
The users might also want to view the summarized data in  Min(): Returns the lowest value in the
different groups based on specific criteria. For example, expression. The following SQL query retrieves
the users want to view the average sales data regionwise or the minimum value from the Rate column of the
productwise. In such a case, the sales data of each region EmployeePayHistory table with a user-defined
will be displayed together. You can group the data by heading:
using the GROUP BY clause of the SELECT statement. SELECT 'Minimum Rate' = min
You can also use aggregate functions to summarize data (Rate)
when grouping it. FROM
HumanResources.EmployeePayHistory
 Max(): Returns the highest value in the
Summarizing Data by Using expression. The following SQL query retrieves
the maximum value from the Rate column of the
EmployeePayHistory table with a user-defined The following figure displays the output of the preceding
heading: query.
SELECT 'Maximum Rate' = max
(Rate)
FROM
HumanResources.EmployeePayHistory
 Sum(): Returns the sum total of values in a
numeric expression, either all or distinct. The
following SQL query retrieves the sum value of
all the unique rate values from the
EmployeePayHistory table with a user-defined
heading:
SELECT 'Sum' = sum (DISTINCT
Rate) FROM
HumanResources.EmployeePayHistory

Grouping Data
The Output Derived After Executing the Query
At times, you need to view data matching specific criteria The GROUP BY...HAVING clause is same as the
to be displayed together in the result set. For example, you SELECT...WHERE clause. The GROUP BY clause
want to view a list of all the employees with details of collects data that matches the condition and summarizes it
employees of each department displayed together. into an expression to produce a single value for each
You can group the data by using the GROUP BY, group. The HAVING clause eliminates all those groups
ROLLUP, CUBE, and PIVOT clauses of the SELECT that do not match the specified condition.
statement. The following query retrieves all the titles along with their
GROUP BY average vacation hours when the vacation hours are more
The GROUP BY clause summarizes the result set into than 30 and the group average value is greater than 55:
groups, as defined in the SELECT statement, by using SELECT Title, 'Average Vacation Hours'
aggregate functions. The HAVING clause further restricts = avg(VacationHours) FROM
the result set to produce the data based on a condition. The HumanResources.Employee WHERE
syntax of the GROUP BY clause is: VacationHours > 30 GROUP BY Title
SELECT column_list HAVING avg(VacationHours) >55
FROM table_name The GROUP BY clause can be applied on multiple fields.
WHERE condition You can use the following query to retrieve the average
[GROUP BY [ALL] expression [, value of the vacation hours that is grouped by Title and
expression] ManagerID in the Employee table:
[HAVING search_condition] SELECT Title, 'Manager ID' =
where, ManagerID, Average = avg
ALL is a keyword used to include those groups that do not (VacationHours) FROM
meet the search condition. HumanResources.Employee GROUP BY
expression specifies the column name(s) or Title, ManagerID
expression(s) on which the result set of the SELECT The following figure displays the output of the preceding
statement is to be grouped. query.
search_condition is the conditional expression on
which the result is to be produced.
The following SQL query returns the minimum and
maximum values of vacation hours for the different types
of titles where the number of hours that the employees can
avail to go on a vacation is greater than 80:
SELECT Title, Minimum = min
(VacationHours), Maximum = max
(VacationHours) FROM
HumanResources.Employee WHERE
VacationHours > 80 GROUP BY Title
The EmpTable Table
You want to view the average salary of the employees
combined for each region and department. You also want
to view the average salary of the employees regionwise
and departmentwise. To view the average salary of the
employees combined for each region and department, you
can use the following query:
The Output Derived After Executing the Query
SELECT Region, Department, avg(sal)
If you want to display all those groups that are excluded
AverageSalary
by the WHERE clause, then you can use the ALL keyword FROM EmpTable
along with the GROUP BY clause. GROUP BY
For example, the following query retrieves the records for Region, Department
the employee titles that are eliminated in the WHERE To view the average salary of the employees for each
condition: region, you can use the following query:
SELECT Title, VacationHours = sum SELECT Region, avg(sal) AverageSalary
(VacationHours) FROM FROM EmpTable
HumanResources.Employee WHERE Title IN GROUP BY
('Recruiter', 'Stocker', 'Design Region
Engineer') GROUP BY ALL Title ORDER BY
To view the average salary of the employees for each
sum (VacationHours)DESC
department, you can use the following query:
The following figure displays the output of the preceding SELECT Department, avg(sal)
query. AverageSalary
FROM EmpTable
GROUP BY
Department
Using the preceding queries, you can view the average
salary of the employees based on different grouping
criteria. If you want to view the results of all the previous
three queries in a single result set, you need to perform the
union of the results generated from the preceding queries.
However, instead of performing the union of the results,
you can use the GROUPING SET clause, as shown in the
following query:
SELECT Region, Department, AVG(sal)
AverageSalary
FROM EmpTable
GROUP BY
The Output Derived After Executing the Query GROUPING SETS
(
(Region, Department),
If you write the preceding query using GROUP BY
(Region),
clause, it will display three records.
(Department)
The GROUPING SETS clause is used to combine the )
result generated by multiple GROUP BY clauses into a The following figure displays the output of the preceding
single result set. For example, the employee details of the query.
organization are stored in the following EmpTable table.
102 2008 120000.00
102 2009 110000.00
103 2007 105000.00
103 2008 180000.00
103 2009 160000.00
104 2007 170000.00
104 2008 120000.00
104 2009 150000.00

The Data Stored in the SalesHistory Table


You have been assigned the task to generate a report that
shows the total sales amount earned by each employee
during the previous years and the total sales amount
The Output Derived After Using the GROUPING SET Clause earned by all the employees in the previous years.
In the preceding figure, the rows that do not have NULL To generate the report required to accomplish the
values represent the average salary of the employees preceding task, you need to apply the following levels of
grouped for each region and department. The rows that aggregation:
contain NULL values in the Department column represent  Sum of sales amount earned by each employee in
the average salary of the employees for each region. The the previous years
rows that contain NULL values in the Region column  Sum of sales amount earned by all the employees
represent the average salary of the employees for each in the previous years
department. Therefore, you can use the ROLLUP operator to apply the
Working with the ROLLUP and CUBE preceding levels of aggregation and generate the required
Operators result set, as shown in the following query:
Consider a scenario, where the management of Tebisco, SELECT EmployeeID, YearOfSale, SUM
Inc. wants to view the details of the sales data of previous (SalesAmount) AS SalesAmount
years. The management wants to view a report that shows FROM SalesHistory
the total sales amount earned by each employee during the GROUP BY ROLLUP(EmployeeID,
previous years and the total sales amount earned by all the YearOfSale)
employees in the previous years. In addition, the In the preceding query, the EmployeeID and YearOfSale
management wants to view a report that shows the columns are specified with the ROLLUP operator because
yearwise sum of sales amount earned by each employee the result is to be generated for each employee as well as
during the previous years. for each year of sale.
To generate the result sets required in the preceding The following figure shows the output of the preceding
scenario, you need to apply multiple levels of aggregation. query.
You can use the ROLLUP and CUBE operators to apply
multiple levels of aggregation on result sets and generate
the required report.
Using the ROLLUP Operator
The ROLLUP operator is an extension of the GROUP BY
clause. This operator can be used to apply multiple levels
of aggregation on results retrieved from a table. The
ROLLUP operator generates a result set that contains a
subtotal for each group and a grand total of all the groups.
Consider a scenario. The sales data of Tebisco, Inc. is
stored in the SalesHistory table. The data stored in the
SalesHistory table is shown in the following table.

EmployeeID YearOfSale SalesAmount


101 2007 120000.00
101 2008 140000.00
101 2009 250000.00
102 2007 150000.00
aggregation:
 Sum of sales amount for each year
 Sum of sales amount for all years
 Sum of sales amount for each employee in all
years
Therefore, you can use the CUBE operator to apply the
preceding levels of aggregation and generate the required
result set, as shown in the following query:
SELECT EmployeeID, YearOfSale, SUM
(SalesAmount) AS SalesAmount
FROM SalesHistory
GROUP BY CUBE(EmployeeID, YearOfSale)
The following figure shows the result of the preceding
query.

The Output Derived After Using the ROLLUP Operator


The preceding figure displays the sum of the sales amount
earned by each employee during the previous years. The
amount earned by employee 101 is displayed in row
number 4. The NULL in this row represents that it
contains the sum of the preceding rows. Similarly, the
amount earned by employee 102 is displayed in row
number 8, by employee 103 is displayed in row number
12, and by employee 104 is displayed in row number 16.
In addition, the output displays the grand total of the sales
amount earned by all the employees during the previous
years in row number 17.
Using the CUBE Operator
The CUBE operator is also used to apply multiple levels
of aggregation on the result retrieved from a table.
However, this operator extends the functionality of the
ROLLUP operator and generates a result set with all the
possible combination of the records retrieved from a table.
For example, you have to generate a report by retrieving
data from the SalesHistory table. The generated report The Output Derived After Using the CUBE Operator
should show the yearwise total amount of sale by all the The preceding figure displays the sum of the sales amount
employees, total amount of sale of all the previous years, earned by all the employees during each year. The amount
and total amount of sale earned by each employee during earned by all the employees in year 2007 is displayed in
all the previous years. row number 5. Similarly, amount earned by all the
To generate a result set required to accomplish the employees in year 2008 and 2009 is displayed in row
preceding task, you need to apply the following levels of number 10 and 15, respectively. Further, it displays the
grand total of the sales amount earned by all the addition, it performs aggregations on the remaining
employees during the previous years in row number 16. In column values if required in the output. The syntax of the
addition, the output displays the sum of sales amount PIVOT operator is:
earned by each employee during the previous years in row SELECT * from table_name
numbers 17 to 20. The output in the last four rows is PIVOT (aggregation_function
similar to the output in row numbers, 4, 8, 12, and 16 (value_column)
displayed by using the ROLLUP operator. FOR pivot_column
Differences Between the ROLLUP and CUBE IN (column_list)
Operators ) table_alias
Both the ROLLUP and CUBE operators are used to apply Consider an example, where you want to display the
multiple levels of aggregation on the result set retrieved number of purchase orders placed by certain employees,
from a table. However, these operators are different in laid down with the vendors. The following query provides
terms of result sets they produce. The differences between this report:
the ROLLUP and CUBE operators are: SELECT VendorID, [164] AS Emp1, [198]
 For each value in the columns on the right side of AS Emp2, [223] AS Emp3, [231] AS Emp4,
the GROUP BY clause, the CUBE operator [233] AS Emp5
reports all possible combinations of values from FROM
the columns on the left side. However, the (SELECT PurchaseOrderID, EmployeeID,
ROLLUP operator does not report all such VendorID
possible combinations. FROM Purchasing.PurchaseOrderHeader) p
PIVOT
 The number of groupings returned by the
(
ROLLUP operator equals the number of columns
count (PurchaseOrderID)
specified in the GROUP BY clause plus one.
FOR EmployeeID IN
However, the number of groupings returned by
( [164], [198], [223], [231], [233] )
the CUBE operator equals the double of the
) AS pvt
number of columns specified in the GROUP BY
ORDER BY VendorID
clause.
The following figure displays the output of the preceding
query.

PIVOT
The database users might need to view data in a user-
defined format. These reports might involve summarizing
data on the basis of various criteria. SQL Server allows The Output Derived After Using the PIVOT Operator
you to generate summarized data reports using the PIVOT Unpivot
clause of the SELECT statement. The UNPIVOT operator allows database users to
The PIVOT clause is used to transform a set of columns normalize the data that has earlier been pivoted. This
into values. PIVOT rotates a table-valued expression by operator transforms the multiple column values of a record
turning the unique values from one column in the into multiple records with the same values in a single
expression into multiple columns in the output. In column. For example, consider a table named Applicant
that stores the details of applicants. This table stores the
applicant‘s name and grade secured by the applicants in
matriculation, higher secondary, and graduation.
The following table shows the data stored in the Applicant
table.

The Data Stored in the Applicant Table


In the preceding table, the Matriculation,
HigherSecondary, and Graduation columns show the grade
secured by the applicants. However, if you want to display
different rows for each qualification secured by the
applicants, you can use the UNPIVOT operator, as shown
in the following query:
SELECT ApplicantName, Qualification,
Grade
FROM
(SELECT ApplicantName,
Matriculation, HigherSecondary,
Graduation
FROM Applicant) Ap
UNPIVOT
(Grade FOR Qualification IN
(Matriculation, HigherSecondary,
Graduation)
) AS unpivotedtbl
The following figure shows the output of the preceding
query.

Activity 2.3: Summarizing and


Grouping Data

Summary
In this chapter, you learned that:
 Data type represents the type of data that a
database object can contain.
 You can store the following types of data in a
database:
 Structured data
 Semi-structured data
 Unstructured data
 Data can be retrieved from a database by using
The Output Derived After Using the UNPIVOT Operator
the SELECT statement.
In the preceding figure, the table contains different rows  Data of all the columns of a table can be retrieved
for each qualification that the applicant has completed. by specifying * in the SELECT statement.
 Data that has to be retrieved based on a condition
is specified by adding the WHERE clause.
 Synonyms are created to refer to a database tables.
object easily.  The aggregate functions, such as avg(), count(),
 Literals and user-defined headings are added to min(), max(), and sum() are used to retrieve
change the display. summarized data.
 The concatenation operator is used to concatenate  The GROUP BY, GROUP BY ALL, and PIVOT
a string expression. clauses are used to group the result set.
 Arithmetic operators are used to perform  The GROUPING SETS clause is used to combine
mathematical operations. the results generated by multiple GROUP BY
 Comparison operators test the similarity between clauses into a single result set.
two expressions.  The ROLLUP and CUBE operators are used to
 Logical operators are used in the SELECT apply multiple levels of aggregation on result
statement to retrieve records based on one or sets.
matching conditions. The logical operators are  The UNPIVOT operator allows to normalize the
AND, OR, and NOT. data that has earlier been pivoted.
 Range operators retrieve data based on a range.
There are two types of range operators,
BETWEEN and NOT BETWEEN. Reference Reading
 The IN keyword allows the selection of values
that match any one of the values in a list. Retrieving Data
 The NOT IN keyword restricts the selection of
values that match any one of the values in a list. Reference Reading: Books Reference Reading: URLs
 The LIKE keyword is used to specify the pattern Sams Teach Yourself SQL in http://msdn.microsoft.com/
search. 24 Hours by Ryan Stephens en-us/library/
 The IS NULL keyword is used to retrieve missing and Ron Plew ms189499.aspx
values.
 The ORDER BY clause is used to retrieve data in
a specific order.
Using Functions to Customize the
 The TOP keyword retrieves only the first set of Result Set
rows, which can either be a number or a percent
of rows that will be returned from a query result. Reference Reading: Books Reference Reading: URLs
 The OFFSET-FETCH clause is used to retrieve a Microsoft SQL Server 2012 http://msdn.microsoft.com/
number of records from anywhere in the result Step by Step by Patrick en-us/library/
set. LeBlanc ms174318.aspx
 The DISTINCT keyword eliminates duplicate
rows. Summarizing and Grouping Data
 The string functions are used to format data in the
result set.
 The date functions are used to manipulate date Reference Reading: Books Reference Reading: URLs
values. Sams Teach Yourself SQL in http://msdn.microsoft.com/
 The mathematical functions are used to perform 24 Hours by Ryan Stephens en-us/library/ms191500%
numerical operations. and Ron Plew 28v=SQL.105%29.aspx
 The conversion functions are used to convert a Microsoft SQL Server 2012
data from one type to another. Step by Step by Patrick
 The ranking functions are used to generate LeBlanc
sequential numbers for each row or to give a rank
based on specific criteria.
 You can use the following functions to rank the
records:
 row_number()
 rank()
 dense_ rank()
 ntile()
 The analytical functions are used to perform
comparisons between rows in the same table.
 The system functions are used to query system
inner join is applied, only the rows with values satisfying
the join condition in the common column are displayed.
The rows in both the tables that do not satisfy the join
Chapter 3 condition are not displayed.
For example, the details of movies are stored in the
Querying Data by Using Joins and following Movies table.
Subqueries MovieID MovieName YearMade
In a normalized database, data related to an entity may be 1 My Fair Lady 1964
stored in multiple tables. When you need to view data 2 Unforgiven 1992
from related tables together, you can join the tables with 3 Time Machine 1997
the help of common attributes.
You can also use subqueries where the result of a query is The Movies Table
used as an input for the condition of another query. In The details of actors are stored in the following Actors
addition, you can store the result of a query as a temporary table.
result set by using Common Table Expressions (CTE). This
temporary result set exists only within the scope of a
MovieID FirstName LastName
single SQL statement. In addition you can also use
1 Rex Harrison
Derived Tables to simplify the queries involving complex
aggregate calculations. 1 Audrey Hepburn
This chapter discusses how to query data from multiple 2 Clint Eastwood
tables by applying various types of joins, such as inner 5 Humphrey Bogart
join, outer join, cross join,equi join, or self join. Further, it
explains how to use subqueries. Lastly, this chapter The Actors Table
discusses about temporary result sets created by using If you join the Movies table and Actors table using an
CTE. inner join, the result set will be retrieved as shown in the
following table.
Objectives
In this chapter, you will learn to:
 Query data by using joins
 Query data by using subqueries
 Manage result sets

Querying Data by Using Joins The Result Set Obtained After Joining the Movies Table and the
Actors Table
When an SQL query executes, it returns a result set. A Notice that the last row of the Actors table is not present in
result set is a set of rows retrieved from a table. As a the result set because the corresponding matching record is
database developer, you may need to retrieve data from not found in the Movies table.
more than one table and display it in a single result set. In A join is implemented by using the SELECT statement,
such a case, different columns in the result set can obtain where the SELECT list contains the names of the columns
data from different tables. To retrieve data from multiple to be retrieved from the tables. The FROM clause contains
tables, SQL Server allows you to apply joins. Joins allow the names of the tables from which combined data is to be
you to view data from related tables in a single result set. retrieved. The WHERE clause specifies the condition,
You can join more than one table based on a common with a comparison operator, based on which the tables will
attribute. be joined.
Depending on the requirements to view data from multiple The syntax of applying an inner join in the SELECT
tables, you can apply different types of joins, such as inner statement is:
join, outer join, cross join, equi join, or self join. SELECT column_name, column_name
[,column_name]
FROM table1_name JOIN table2_name
Using an Inner Join ON table1_name.ref_column_name
join_operator
An inner join retrieves records from multiple tables after
table2_name.ref_column_name
comparing values present in a common column. When an
[WHERE search_condition] The following figure displays the data of the
where, EmployeePayHistory table.
table1_name and table2_name are the names of the
tables that are joined.
join_operator is the comparison operator based on
which the join is applied.
table1_name.ref_column_name and
table2_name.ref_column_name are the names of
the columns on which the join is applied.
WHERE specifies the search condition for the rows
returned by the query.
search_condition specifies the condition to be
satisfied to return the selected rows. The EmployeePayHistory Table
The following figure displays the output of the preceding
inner join query.
An inner join is the default join. Therefore, you can
also apply an inner join by using the JOIN keyword. In
addition, you can use the INNER JOIN keyword.
Whenever a column is mentioned in a join condition, it
should be referred by prefixing it with the table name to
which it belongs or with a table alias. A table alias is used
to refer to the table with another name or to uniquely
identify the table. The table alias is defined in the FROM
clause of the SELECT statement.
When listing the column names in the SELECT statement,
it is mandatory to use a table name or a table alias if an
ambiguity arises due to duplicate column names in
multiple tables.
The following query displays the Employee ID and Title
for each employee from the Employee table, and the Rate
and PayFrequency columns from the EmployeePayHistory
table:
SELECT e.EmployeeID,e.Title, The Output Derived by Executing the Inner Join Query
eph.Rate,eph.PayFrequency Based on the relationship between tables, you need to
FROM HumanResources.Employee e JOIN select the common column to set the join condition. In the
HumanResources.EmployeePayHistory eph preceding inner join query, the common column is
ON e.EmployeeID = eph.EmployeeID EmployeeID, which are the primary key of the Employee
In the preceding query, the Employee table and the table and the foreign key of the EmployeePayHistory
EmployeePayHistory table are joined on the common table.
column, EmployeeID. The query also assigns e as an alias Observe the result set after the join. The record of the
of the Employee table and eph as an alias of the employee with EmployeeID 1 from the Employee table is
EmployeePayHistory table. The column names are also joined with the record of the employee with EmployeeID
listed with the table alias names. 1 from the EmployeePayHistory table.
The following figure displays the data of the Employee While applying joins, you can also check for other
table. conditions. The following query retrieves the employee ID
and the designation from the Employee table for all the
employees whose pay rate is greater than 40:
SELECT ‘Employee ID’ = e.EmployeeID,
‘Designation’ = e.Title
FROM HumanResources.Employee e INNER
JOIN HumanResources.EmployeePayHistory
eph
ON e.EmployeeID = eph.EmployeeID
WHERE eph.Rate>40
The Employee Table In the preceding query, the tables are joined based on the
EmployeeID column, which is common in both the tables. Actors Table
In addition, only those records are selected where the In the preceding figure, the FirstName and LastName
value in the Rate column of the EmployeePayHistory table columns have the value NULL against the movie, Time
is greater than 40. Machine. This is because there are no records of actors in
the Actors table for the Time Machine movie.
Consider another example from the AdventureWorks, Inc.
Using an Outer Join The SpecialOfferProduct table contains a list of products
that are on special offer and the SalesOrderDetail table
In comparison to an inner join, an outer join displays the
stores the details of all the sales transactions. The users at
result set containing all the rows from one table and the
AdventureWorks, Inc. need to view the transaction details
matching rows from the other table. For example, if you
of the special offer products. In addition, they want to
create an outer join on table A and table B, it will show
view the ProductID of the special offer products for which
you all the records of table A and only those records from
no transaction has been done.
table B for which the condition on the common column
To perform this task, you can use the LEFT OUTER JOIN
holds true.
keyword, as shown in the following query:
An outer join displays NULL for the columns of the
SELECT DISTINCT (p.ProductID),
related table where it does not find any matching records.
p1.SpecialOfferID, p1.SalesOrderID,
The syntax of applying an outer join is:
p1.OrderQty, p1.UnitPrice
SELECT column_name, column_name
FROM Sales.SpecialOfferProduct p LEFT
[,column_name]
OUTER JOIN
FROM table1_name [LEFT | RIGHT| FULL]
[Sales].[SalesOrderDetail] p1 ON
OUTER JOIN table2_name
p.ProductID =p1.ProductID
ON table1_name.ref_column_name
ORDER BY p.ProductID
join_operator
The following figure displays the output of the preceding
table2_name.ref_column_name
query.
[WHERE search_condition]
An outer join is of the following types:
 Left outer join
 Right outer join
 Full outer join
Using a Left Outer Join
A left outer join returns all rows from the table specified
on the left side of the LEFT OUTER JOIN keyword and
the matching rows from the table specified on the right
side. It displays NULL for the columns of the table
specified on the right side where it does not find any
matching records.
For example, you need to display the details of all the
movies and the corresponding details of the actors who
acted in those movies. To perform this task, you can join The Output Derived After Executing the Query
the Movies table and the Actors table by using a left outer The preceding figure displays NULL for the ProductIDs
join. The Movies table will be on the left side of the LEFT for which no transaction was performed.
OUTER JOIN keyword in the query. The following figure Using a Right Outer Join
displays the result set of using the left outer join between A right outer join returns all the rows from the table
the Movies table and the Actors table. specified on the right side of the RIGHT OUTER JOIN
keyword and the matching rows from the table specified
on the left side.
For example, you need to display the details of all the
actors and the corresponding details of the movies in
which they have acted. To perform this task, you can join
the Movies table and the Actors table by using a right
outer join. The Actors table will be on the right side of the
RIGHT OUTER JOIN keyword in the query.
The Result Set Obtained After Joining the Movies Table and the The following figure displays the result set of using the
right outer join between the Movies table and the Actors
table. matching rows from both the tables. However, the
matching records are displayed only once. In case of non-
matching rows, a NULL value is displayed for the
columns for which data is not available.
For example, you need to display the details of all the
movies along with all the actors combined in a single
result set. To perform this task, you can join the Movies
table and the Actors table by using a full outer join. The
following figure displays the result set of using the full
The Result Set Obtained After Joining the Movies Table and the outer join between the Movies table and the Actors table.
Actors Table
In the preceding figure, NULL is displayed against the
MovieID, MovieName, and YearMade columns for the
actor, Humphrey Bogart. This is because there are no
records for the actor, Humphrey Bogart in the Movies
table.
Consider the example of AdventureWorks, Inc. You want
to display the IDs of all the sales persons along with the
territory assigned to them. In addition, you need to display
the IDs of all those sales persons who have not been
assigned any territory. To perform this task, you can apply The Result Set Obtained After Joining the Movies Table and the
a right outer join between the Sales.SalesTerritory table Actors Table
and the Sales.SalesPerson table, as shown in the following Consider another example of AdventureWorks, Inc. You
query: want to display the IDs of all the sales persons along with
SELECT st.Name AS Territory, the territory assigned to them. In addition, you need to
sp.SalesPersonID display the IDs of all those sales persons who have not
FROM Sales.SalesTerritory st been assigned any territory. Similarly, you need to display
RIGHT OUTER JOIN Sales.SalesPerson sp all those territories which have not been assigned to any
ON st.TerritoryID = sp.TerritoryID sales person. To perform this task, you can apply a full
The following figure displays the output of the preceding outer join between the Sales.SalesTerritory table and the
query. Sales.SalesPerson table, as shown in the following query:
SELECT st.Name AS Territory,
sp.SalesPersonID
FROM Sales.SalesTerritory st
FULL OUTER JOIN Sales.SalesPerson sp
ON st.TerritoryID = sp.TerritoryID
The following figure displays the output of the preceding
query.

The Output Derived After Executing the Query


In the preceding figure, NULL is displayed in the Territory
column for those sales persons who have not been
assigned any territory.
Using a Full Outer Join
A full outer join is a combination of left outer join and
right outer join. This join returns all the matching and non-
The Output Derived After Executing the Query
The preceding query will display NULL values in the
Territory column for those sales persons who have not
been assigned any territory. Similarly, it will display
NULL values in the SalesPersonID column for those
territories which have not been assigned to any sales
person.

The Result Set Obtained After Cross Joining the Shape Table
and Color Table
Consider another example of a storehouse that sells
computers. As a database developer, you have saved the
configuration and price details of computers in the
ComputerDetails table, as shown in the following figure.

Using a Cross Join


A cross join is also known asthe Cartesian Product. It
joins each row of one table with each row of the other
table. The number of rows in the result set is equal to the The ComputerDetails Table
number of rows in the first table multiplied by the number The store also sells peripheral devices. You have saved the
of rows in the second table. This implies that if table A has details of these devices in the AddOnDetails table, as
10 rows and table B has 5 rows, then all the 10 rows of shown in the following figure.
table A are joined with all the 5 rows of table B. Therefore,
the result set will contain 50 rows.
For example, table A contains three shapes: Circle,
Rectangle, and Line. Table B contains three colors: Red,
Blue, and Green.
If you join these two tables using the cross join, the result
set will contain nine rows, as shown in the following The AddOnDetails Table
figure. To identify the total price of a computer with all the
combinations of add-on devices, you can use the CROSS
JOIN keyword, as shown in the following query:
SELECT A.CompDescription,
B.AddOnDescription, A.Price + B.Price
AS ‘Total Cost’ FROM ComputerDetails A
CROSS JOIN AddOnDetails B
The preceding query combines the records of both the
tables to display the total price of a computer with all the
possible combinations, as shown in the following figure.
The Output Derived After Executing the Query

Using a Self Join


In a self join, a table is joined with itself. As a result, one
row in a table correlates with other rows in the same table.
The Output Derived After Executing the Query
In a self join, a table name is used twice in the query.
Therefore, to differentiate between the two instances of a
single table, the table is given two alias names.
Consider the example of the Employee table. The
Employee table contains records as shown in the following
figure.

Using an Equi Join


An equi join is the same as an inner join and joins tables
with the help of a foreign key. However, in an equi join, The Employee Table
only the equality operator is used to specify the join You want to display the employee details along with their
condition, whereas you can use conditional operators to manager details, such as manager ID and manager
specify the join condition in an inner join. designation. However, in the preceding table, designation
Consider an example where you apply an equi join of the manager is not displayed. In this case, the manager
between the EmployeeDepartmentHistory table, the is also an employee. Therefore, the designation of the
Employee table, and the Department table by using a manager can be retrieved from the title column, which
common column, EmployeeID. To perform this task, you stores the designation of an employee. Therefore, you
can use the following query: need to join the table with itself to obtain the required
SELECT * FROM result.
HumanResources.EmployeeDepartmentHisto To perform a self join, you need to divide the physical
ry d JOIN HumanResources.Employee e ON table into two logical tables, as shown in the following
d.EmployeeID = e.EmployeeID JOIN figure.
HumanResources.Department p ON
p.DepartmentID = d.DepartmentID
The output of the preceding query displays the
EmployeeID column from all the tables, as shown in the
following figure.

The Physical Table Divided into Logical Tables


In the preceding figure, the Employee (emp) table While querying data from multiple tables, you might need
represents the EmployeeID, title, and ManagerID of the to use the result of one query as an input for the condition
employee, whereas the Employee (mgr) table represents of another query. For example, in the AdventureWorks
the EmployeeID and title of the managers. Using the database, you need to view the designation of all the
preceding figure, you can easily identify the designations employees who earn more than the average salary. For
of the managers. this, you have to perform two steps. First, you have to find
The following query joins the Employee table with itself the average salary. Second, you have to find the
to display the employee IDs and the designations of all the employees who earn more than the average salary. In such
employees with the employee IDs and designations of cases, you can combine more than one statement in a
their managers: subquery.
SELECT emp.EmployeeID, emp.Title AS A subquery is an SQL statement that is used within
Employee_Designation, emp.ManagerID, another SQL statement. Subqueries are nested inside the
mgr.Title AS Manager_Designation FROM WHERE or HAVING clause of the SELECT, INSERT,
HumanResources.Employee emp, UPDATE, and DELETE statements. The query that
HumanResources.Employee mgr represents the parent query is called the outer query, and
WHERE emp.ManagerID = mgr.EmployeeID the query that represents the subquery is called theinner
The following figure shows the output of the preceding query. The database engine executes the inner query first
query. and returns the result to the outer query to calculate the
result set.
For example, the EmployeeDetails table contains five
columns: EmployeeID, EmpName, Designation, Salary,
and DeptNo. The following figure represents the
EmployeeDetails table.

The Output Derived After Executing the Query

The EmployeeDetails Table


Activity 3.1: Using Joins You want to find all the employees who have the same
designation as John. Notice that the designation of John is
not known in advance. Therefore, first you have to execute
the following query to find the designation of John:
SELECT Designation FROM
EmployeeDetails
WHERE EmpName = ‘John’
Consider that the output of the preceding query is ?
Executive?. Then, you have to execute the following query
to get the desired output:
SELECT * from EmployeeDetails
WHERE Designation = ‘Executive’
Instead of executing two queries, you can combine these
two queries to get the desired output, as shown in the
following query:
SELECT * FROM EmployeeDetails
WHERE Designation = (SELECT
Designation FROM EmployeeDetails
WHERE EmpName = ‘John’)
In the preceding query, the inner query is executed first
and returns the designation of John to the outer query.
Querying Data by Using Subqueries Then, the outer query is executed using the return value
and displays the desired output, as shown in the following The syntax for using the IN keyword is:
figure. SELECT column, column [,column]
FROM table_name
WHERE column [ NOT ] IN
( SELECT column FROM table_name
[WHERE conditional_expression])
For example, you need to retrieve the EmployeeID
The Output Derived After Executing the Query attribute of all the employees who live in Bothell from the
Depending on the output generated by the subquery and EmployeeAddress table. To perform this task, you need to
the purpose for which it is to be used in the outer query, use a query to obtain the AddressID of all the addresses
you can use different keywords, operators, and functions that contain the word Bothell. You can then obtain the
in the subqueries. EmployeeID from the Employee table where the
AddressID matches any of the AddressIDs returned by the
previous query.
Using the IN and EXISTS Keywords To perform this task, you can use the following query:
SELECT EmployeeID FROM
A subquery returns values that are used by the outer query. HumanResources.EmployeeAddress WHERE
A subquery can return one or more values. Depending on AddressID IN (SELECT AddressID FROM
the requirement, you can use these values in different Person.Address WHERE City = ‘Bothell’)
ways in the outer query. The following figure shows the output of the preceding
For example, in the AdventureWorks database, you need query.
to display the department name for an employee whose
EmployeeID is 46. To perform this task, you can use the
following query:
SELECT Name FROM
HumanResources.Department
WHERE DepartmentID =
(SELECT DepartmentID FROM
HumanResources.EmployeeDepartmentHisto
ry
WHERE EmployeeID = 46 AND EndDate IS
NULL)
In the preceding query, the inner subquery returns the
DepartmentID column of the employee with EmployeeID
as 46. Using this DepartmentID, the outer query returns
the name of the department from the Department table.
The EndDate is NULL signifying that you need to extract
The Output Derived After Executing the Query
the ID of the department where the employee is currently
working. Using the EXISTS Keyword
In the preceding query, the subquery returns a single You can also use a subquery to check if a set of records
value. However, at times, you need to return more than exists. For this, you need to use the EXISTS clause with a
one value from the subquery. In addition, you might need subquery. The EXISTS keyword always returns a TRUE
to use a subquery only to check the existence of some or FALSE value.
records and based on that you need to execute the outer The EXISTS clause checks for the existence of rows
query. according to the condition specified in the inner query and
You can specify different kinds of conditions on passes the existence status to the outer query. The inner
subqueries by using the following keywords: query in this case returns the result set in the form of a
 IN table. Therefore, such queries are also called table-valued
 EXISTS subqueries. The EXISTS keyword returns a TRUE value if
the result of the inner query contains any row.
Using the IN Keyword The query introduced with the EXISTS keyword differs
If a subquery returns more than one value, you might need from other queries. The EXISTS keyword is not preceded
to match a column value with any of the values in the list by any column name, constant, or other expression; and it
returned by the inner query. To perform this task, you need contains an asterisk (*) in the SELECT list of the inner
to use the IN keyword. query.
The syntax for using the EXISTS keyword in the SELECT existing comparison operators.
statement is: The ALL keyword returns a TRUE value if all the values
SELECT column, column [,column] that are retrieved by the subquery satisfy the comparison
FROM table_name operator. It returns a FALSE value if only some values
WHERE EXISTS ( SELECT column FROM satisfy the comparison operator or if the subquery does not
table_name [WHERE return any rows to the outer statement.
conditional_expression] ) The ANY keyword returns a TRUE value if any value that
For example, the users of AdventureWorks, Inc. need a list is retrieved by the subquery satisfies the comparison
containing the employee ID and title of all the employees operator. It returns a FALSE value if no values in the
who have worked in the marketing department at any subquery satisfy the comparison operator or if the
point in time. The department ID of the marketing subquery does not return any rows to the outer statement.
department is 4. The following table shows the operators that can be used
To generate the required list, you can write the following with the ALL and ANY keywords.
query by using the EXISTS keyword:
SELECT EmployeeID, Title FROM Operator Description
HumanResources.Employee
>ALL Means greater than the
WHERE EXISTS
maximum value in the list.
(SELECT * FROM
The expression |
HumanResources.EmployeeDepartmentHisto
column_name >ALL (10,
ry WHERE EmployeeID =
20, 30) means ‘greater than
HumanResources.Employee.EmployeeID AND
30’.
DepartmentID = 4)
The following figure displays the output generated by the >ANY Means greater than the
preceding query. minimum value in the list.
The expression |
column_name >ANY (10,
20, 30) means ‘greater than
10’.
=ANY Means any of the values in
the list. It acts in the same
way as the IN clause.
The expression |
column_name =ANY (10,
20, 30) means ‘equal to
either 10 or 20 or 30’.
<>ANY Means not equal to any
value in the list.
The expression |
The Output Derived After Executing the Query column_name <>ANY (10,
20, 30) means ‘not equal to
A subquery must be enclosed within parentheses and 10 or 20 or 30’.
cannot use the ORDER BY or the COMPUTE BY clause. <>ALL Means not equal to all the
values in the list. It acts in
the same way as the NOT IN
Using Modified Comparison clause.
The expression |
Operators column_name <>ALL (10,
While using subqueries, you can use the =, >, and < 20, 30) means ‘not equal to
comparison operators to create a condition that checks the 10 and 20 and 30’.
value returned by the subquery. When a subquery returns
more than one value, you might need to apply the The Operators that can be used with the ALL and ANY
operators to all the values returned by the subquery. To Keywords
perform this task, you can modify the comparison The following query displays the employee ID and the
operators in the subquery. SQL Server provides the ALL title of all the employees whose vacation hours are more
and ANY keywords that can be used to modify the than the vacation hours of employees designated as
Recruiter:
SELECT EmployeeID, Title
FROM HumanResources.Employee
WHERE VacationHours >ALL (SELECT
VacationHours
FROM HumanResources.Employee WHERE
Title =‘Recruiter’)
In the preceding query, the inner query returns the
vacation hours of all the employees who are titled as
Recruiter. The outer query uses the ‘>ALL’ comparison
operator. It retrieves the details of those employees who
have vacation hours greater than all the employees titled
as Recruiter.
The following figure shows the output of the preceding
query.

The Output Derived After Executing the Query


Consider another example of the EmployeeDetails table.
You want to display the details of employees whose salary
is more than the lowest salary of department having ID 10.
To perform this task, you can use the following query:
SELECT * FROM EmployeeDetails
WHERE Salary > ANY ( SELECT DISTINCT
Salary FROM EmployeeDetails
WHERE DeptNo = 10)
In the preceding query, the ANY operator compares the
salary of each employee to each value returned by the
subquery. The following figure displays the output of the
preceding query.

The Output Derived After Executing the Query


Using Aggregate Functions
While using subqueries, you can also use aggregate
functions in the subqueries to generate aggregated values
from the inner query. For example, in a manufacturing SELECT DepartmentID FROM
organization, the management wants to view the sales HumanResources.EmployeeDepartmentHisto
records of the items whose sales records are higher than ry
the average sales record of a particular product. For this, WHERE EmployeeID = /* Level 1 inner
you first need to obtain the average sales record of a query */
particular product, and then find all the records whose (SELECT EmployeeID FROM
sales records exceed the average value. For this, you can HumanResources.Employee
use aggregate functions inside the subquery. WHERE ContactID = /* Level 2 inner
The following query displays the employee ID of the query */
employees whose vacation hours are greater than the (SELECT ContactID FROM Person.Contact
average vacation hours of employees with title as WHERE EmailAddress =
Marketing Assistant: ‘taylor0@adventure-works.com’)
SELECT EmployeeId FROM ) AND EndDate IS NULL
HumanResources.Employee In the preceding query, two queries are nested within
WHERE VacationHours >(SELECT AVG another query. The level 2 inner query returns the contact
(VacationHours) FROM ID of an employee based on the e-mail address of the
HumanResources.Employee employee from the Person table. The value in EndDate
WHERE Title = ‘Marketing Assistant’) column of the
In the preceding query, the inner query returns the average HumanResources.EmployeeDepartmentHistory table will
vacation hours of all the employees who are titled as be NULL for that department ID in which the employee is
Marketing Assistant. The outer query uses the comparison currently working.
operator ‘>’ to retrieve the employee ID of all those The level 1 inner query uses this contact ID to search for
employees who have vacation hours more than the average the employee ID of the employee with the given e-mail
vacation hours assigned for a Marketing Assistant. address. The main query uses the employee ID returned by
The following figure shows the output of the preceding the level 1 inner query to search for the department ID
query. from the EmployeeDepartmentHistory table.
The following figure shows output of the preceding query.

The Output Derived After Executing the Query


You can implement subqueries upto 32 levels. However,
the number of levels that can be used depends on the
memory available on the database server.
Consider another example where you want to display the
details of employees whose salary is greater than the
highest salary in the Admin department. To perform this
task, you need to execute the following query:
SELECT * FROM EmployeeDetails
WHERE Salary > ( SELECT max(Salary)
FROM EmployeeDetails
WHERE DeptNo = ( SELECT DeptNo from
The Output Derived After Executing the Query DeptDetails
WHERE DeptName = ‘Admin’))
In the preceding query, the level 2 inner query returns the
Using Nested Subqueries department number of the admin department. The level 1
A subquery can contain one or more subqueries. inner query uses this department number to retrieve the
Subqueries are used when the condition of a query is highest salary in this department. The main query uses the
dependent on the result of another query, which, in turn, is highest salary returned by the level 1 inner query to
dependent on the result of another subquery. display the employee details. The preceding query
For example, you need to view the department ID of an displays the output as shown in the following figure.
employee whose e-mail address is taylor0@adventure-
works.com. To perform this task, you can use the
following query:
The Output Derived After Executing the Query

Using Correlated Subqueries


A correlated subquery can be defined as a query that
depends on the outer query for its evaluation. In a normal
nested subquery, the inner query executes only once. The
main query is executed using the value returned by the
inner query. On the other hand, in a correlated subquery,
the inner query is driven by the outer query. In this case,
the inner query executes once for each row returned by the
outer query. For example, if the outer query returns 10
rows, the inner query will be executed 10 times. The Output Derived After Executing the Query
In a correlated subquery, the WHERE clause references a
table in the FROM clause of the outer SELECT statement.
This means that the inner query is evaluated for each row Using the APPLY Operator
of the table specified in the outer query.
Sometimes, you need to combine the result set of two
For example, you can use the following query to find the queries such that for each row of the first query, the
employees who earn the highest salary in their department: second query is evaluated to determine if any rows are
SELECT * FROM EmployeeDetails e returned. For example, you need to know the dividends
WHERE Salary = (SELECT max(Salary)
paid to each employee of the organization. The dividends
FROM EmployeeDetails
are required to be paid on the basis of the salary of the
WHERE DeptNo = e.DeptNo)
employees. The Employee table contains the records of all
The preceding query can be identified as a correlated the employees. The Pay table contains the salary details of
subquery since the e.DeptNo column in the WHERE each employee. To display the required data, you need to
clause of the inner query is referencing the table specified retrieve employee details from the Employee table. For
in the FROM clause of the outer SELECT statement. The each row of the Employee table, you need to calculate the
preceding query will display the output as shown in the dividend based on salary from the Pay table. Therefore,
following figure. you need to combine the result sets of two queries to
display the required data. SQL Server provides you with
the APPLY operator to perform this task in a single SQL
statement.
There are two types of APPLY operators supported by
SQL Server. These are:
The Output Derived After Executing the Query  CROSS APPLY
Consider another example of AdventureWorks database.  OUTER APPLY
The following query displays the employee ID, The syntax for using the APPLY operator is:
designation, and number of hours spent on vacation for all left_table_expression {CROSS | OUTER}
the employees whose vacation hours are greater than the APPLY right_table_expression
average vacation hours identified for their title: where,
SELECT EmployeeID, Title, left_table_expression represents the outer result
VacationHours set.
FROM HumanResources.Employee e1 WHERE right_table_expression represents the inner
e1.VacationHours > result set, which is evaluated for each record of the outer
(SELECT AVG(e2.VacationHours) result set.
FROM HumanResources.Employee e2 WHERE
e1.Title = e2.Title) CROSS APPLY
In the preceding query, the inner query returns the The CROSS APPLY operator returns only those rows from
titlewise average vacation hours from the Employee table. the outer result set that matches with the inner result set.
The outer query retrieves the employee ID, title, and For example, customers can deposit their money in a bank.
vacation hours of all the employees whose vacation hours The information regarding the deposits made by the
are greater than the average vacation hours retrieved by customers is stored in the Depositor table, as shown in the
the inner query. The following figure shows the output of following figure.
the preceding query.
the following query:
SELECT d.Customer_name, d.Acc_num,
br.Loan_num FROM Depositor d
OUTER APPLY
(SELECT * FROM Borrower b WHERE
d.Customer_name = b.Customer_name)br
The Depositor Table The following figure displays the output of the preceding
The bank also grants loans to the customers for their query.
personal use. This information is stored in the Borrower
table, as shown in the following figure.

The Borrower Table


The Output Derived After Executing the Query
You want to view the name, account number, and loan
In the preceding figure, Jennifer does not have a loan
number of the customers who are borrowers as well as
account with the bank. Therefore, NULL is displayed
depositors. The following query uses the CROSS APPLY
against the loan number for Jennifer in the result set.
operator on the Depositor table and the Borrower table to
view the required result:
SELECT d.Customer_name, d.Acc_num, Activity 3.2: Using Subqueries
br.Loan_num FROM Depositor d
CROSS APPLY
(SELECT * FROM Borrower b WHERE
d.Customer_name = b.Customer_name) br Managing Result Sets
The following figure displays the output of the preceding
In an organization, data related to different entities is
query.
stored in multiple tables. If you want to view data from the
multiple tables together, you need to combine or compare
the result sets. SQL Server provides the various operators,
such as UNION, EXCEPT, and INTERSECT, to combine
as well as compare the result sets.
Sometimes, you need to reference the result set of the
previous query to manipulate the data in other tables.
However, the result set of a query is not stored in a
database. Therefore, if you want to use the result set of a
The Output Derived After Executing the Query
query, you need to first store that result set in a table and
In the preceding figure, the record of Jenne is not
then use that table to manipulate the data in other tables.
displayed as it does not match any row in the outer result
This task has been made easy by using CTEs provided by
set.
SQL Server.
OUTER APPLY
The OUTER APPLY operator returns all rows from the
outer result set even if the corresponding row is not found Combining Result Sets
in the inner result set. Therefore, the final result set
obtained after performing the OUTER APPLY operation As a database developer, you often need to create the
contains NULL values when the inner result set does not result sets after combining data from multiple tables.
return a matching value for each or either of the rows from While combining data from multiple tables, you may need
the outer result set. to either store only the common records from the
combined tables or exclude some records from the result
For example, you want to view the details of all the
sets based on different criteria.
customers who are depositors. In addition, you want to
view the loan numbers of the customers who are SQL Server provides you with the UNION operator to
borrowers also. You can view the desired result by using combine data from multiple tables. In addition, you can
use the EXCEPT and INTERSECT operators to exclude
records from the result sets based on different criteria.
You must follow some basic rules while using the
UNION, EXCEPT, and INTERSECT operators to
combine the result sets. These rules are:
 The number and the sequence of the columns
must be the same in all queries.
 The data types of the columns in all the queries
must be compatible.
Using the UNION Operator
The UNION operator is used to combine data of two or
more queries into a single result set. The syntax of the The Output Derived After Executing the Query
UNION operator is: In the preceding figure, the result set contains all the rows
<query_expression> from both the tables. This resulted in duplicate rows in the
UNION [ ALL ] result set.
<query_expression> Using the EXCEPT and INTERSECT
[ UNION [ ALL ] Operators
<query_expression>
Sometimes, you need to display the result set after
[ ...n ] ]
comparing the results of two queries. You can use the
where, EXCEPT and INTERSECT operators to perform this task.
query_expression is the query expression that The EXCEPT operator compares two result sets and
returns the data to be combined with the data from returns the data from the first result set that is not found in
another query expression. The definitions of the columns the second result set. The number and order of columns
that are part of a UNION operation do not have to be the should be same in the result sets on which the EXCEPT
same, but they must be compatible through implicit operator is applied.
conversion. However, the INTERSECT operator returns the common
UNION specifies that result sets from multiple queries rows after comparing two result sets. If the common rows
need to be combined and returned as a single result set. are not found, then NULL value is returned. The syntax
ALL specifies that all the rows need to be incorporated for using the INTERSECT and EXCEPT operators is:
into the result set. This can result in duplicate rows. If it is <query_expression>
not specified, duplicate rows are removed. EXCEPT | INTERSECT
For example, you can use the following query to view the <query_expression>
details of all customers who are either depositors or where,
borrowers with the bank: query_expression specifies the query that returns the
SELECT Customer_name FROM Depositor
data to be compared with the data from another query
UNION
expression.
SELECT Customer_name FROM Borrower
For example, you can use the following query to return the
The following figure displays the output of the preceding
name of those customers who are depositors but not
query.
borrowers:
SELECT Customer_name FROM Depositor
EXCEPT
Select Customer_name FROM Borrower
The following figure displays the output of the preceding
query.

The Output Derived After Executing the Query


In the preceding figure, there are no duplicate rows in the The Output Derived After Executing the Query
result set. Now, consider the following query: The preceding figure returns the name of the customer
SELECT Customer_name FROM Depositor who is not a borrower.
UNION ALL Similarly, you can use the following query to find the
SELECT Customer_name FROM Borrower name of the customers who are depositors and taken loan
The following figure displays the output of the preceding from the bank as well:
query. SELECT Customer_name FROM Depositor
INTERSECT However, SQL Server allows you to access the temporary
SELECT Customer_name FROM Borrower result sets by using CTEs.
The following figure displays the output of the preceding A CTE is used to create a temporary named result set. It is
query. defined within the execution scope of a single SQL
statement. The syntax for using CTE is:
WITH expression_name [(column_name1
[,...n] )]
AS
(CTE_Query_defnition)
where,
The Output Derived After Executing the Query expression_name is the name of the CTE.
(column_name1 [,...n] ) specifies the name of
columns included in the temporary result set.
CTE_Query_defnition specifies the query whose
result set populates the CTE.
For example, you want to prepare a report that displays the
top 10 rates of employees. In addition, the report should
also display the maximum rate among these top 10 rates.
For this, you need to use the CTE, as shown in the
following statements:
WITH RateCTE(Rate)
AS
(
SELECT TOP 10 Rate = Rate FROM
HumanResources.EmployeePayHistory
)
SELECT Rate, Max_Rate = (SELECT max
(Rate) FROM RateCTE)
FROM RateCTE
The following figure displays the output of the preceding
statements.

The Output Derived After Executing the Statements


Sometimes, you need to reference a CTE multiple times in
Working with Temporary Result Sets the same query. Such queries are referred to as recursive
CTEs. In a recursive CTE, result sets of more than one
Sometimes, a user wants to create queries on the result query are combined to populate the CTE.
sets that are obtained earlier. The result sets returned after A recursive CTE consists of the following members to
executing the queries are known as temporary result sets. produce the required result:
These temporary result sets are not stored in the database  Anchor query: Includes one or more query
and remain in the memory till the execution of the query. expressions joined by the UNION ALL, UNION,
After the execution of the query, the table containing the EXCEPT, or INTERSECT operators. As these
temporary result set is automatically dropped.
query expressions produce the base result set for
the recursive CTE, they are referred to as anchor
query.
 Recursive query: Includes one or more query
expressions joined by the UNION ALL operator
that uses the CTE itself to produce the result. It
takes the result of the anchor query as an input to The Output Derived After Executing the Statements
produce an output. This output further acts as an Consider another example. The Employee table of the
input for the recursive query to produce another AdventureWorks database stores the employee details
output. This process continues until an empty along with the information about their respective
result set is produced. supervisors. You want to find the hierarchy of employees
The syntax of recursive CTE is: starting from the top level employee. To accomplish this
WITH expression_name [(column_name1 task, you can execute the following statements:
[,...n] )] WITH Rec_CTE(LoginID, ManagerID,
AS EmployeeID) AS
( (
(CTE_Query_defnition) --anchor query SELECT LoginID, ManagerID, EmployeeID
UNION ALL FROM HumanResources.Employee
(Recursive_Query_Expression) WHERE ManagerID IS NULL
) UNION ALL
where, SELECT e.LoginID, e.ManagerID,
CTE_Query_defnition represents the anchor query e.EmployeeID
that is used as an input for the recursive query. FROM HumanResources.Employee e
Recursive_Query_Expression represents the INNER JOIN Rec_CTE d
recursive query. ON e.ManagerID = d.EmployeeID
The process of execution of a recursive CTE is: )
1. The anchor query is executed first to create the SELECT *
first invocation or base result set (T0). FROM Rec_CTE
2. The recursive query is executed with Ti as an The following steps describe the execution of the
input and Ti+1 as an output. Here, i starts from 0. preceding statements:
3. Step 2 is repeated until an empty set is returned. 1. The anchor query returns the top level employee
4. The final result set is displayed. This result set is where the ManagerID is null, as shown in the
a UNION ALL of T0 to Tn. following figure.
For example, consider the following statements:
WITH List_CTE(i)
AS The Top Level Employee
( 2. This result is referenced by the recursive query to
SELECT i = CONVERT(varchar find the direct subordinates at the next level, 109.
(8000),‘Welcome’) The direct subordinates at the next level are
UNION ALL retrieved by performing the JOIN operation
SELECT i + ‘A’ FROM List_CTE WHERE between the Employee table and the CTE.
LEN(i) < 10 3. The following figure shows the direct
) subordinates at the next level.
SELECT i FROM List_CTE
ORDER BY i
In the preceding statements, the anchor query populates
the CTE with the result, Welcome. The recursive query
uses this result and produces the output, WelcomeA. This
output further acts as an input for the recursive query to
produce the result, WelcomeAA. This process continues
until the recursive query produces no rows.
The Direct Subordinates at the Next Level
The following figure displays the output of the preceding
statements. 4. Retrieval of direct subordinates at the next level
occurs recursively, until an empty result set is
returned by the JOIN operation. The final result
set returned by the running query is the union of The Output Derived After Executing the Statement
all result sets generated by the anchor and Now, to view the total orders placed for a particular
recursive queries. The following figure displays product, you need to execute the following statement:
the final result set of the preceding query. SELECT ProductID,SUM(OrderQty) AS
TotalSalesOrdersPlaced FROM
Sales.SalesOrderDetail
GROUP BY ProductId,SalesOrderId
The following figure displays the output of the preceding
statement.

The Final Result Set Obtained After Executing the


Statement
In the preceding figure, the hierarchical
representation of employees is displayed.

Working with Derived Tables The Output Derived After Executing the Statement
Now, you want to view the output of both the preceding
Sometimes, obtaining data in a result set is a complicated two queries in a single result set. To perform this task,
process as it might include some complex aggregate SQL Server provides you, with the concept of derived
calculations, involving fields of one or more tables. A tables. The derived tables is a virtual table, created by
simple query might not help in such a case. Therefore, you using a SELECT statement and is given an alias name
may need to simplify the process by using temporary using the AS clause. It is also referred to as the inline
tables or views. For example, the SalesOrderDetail table views. It can be referenced as a regular table or a view in
of the AdventureWorks database contains the details of the the SELECT statement. The derived table has its scope
orders placed by the customers. You want to find out the restricted within the query, can be used only within the
total quantity ordered and the total orders placed for a query, and ceases to exist once the query has finished
particular product. To accomplish this task, you need to executing. Unlike temporary tables or views, there is no
first use the following SQL statement to retrieve the total overhead of creation, deletion, or data insertion.
quantity ordered for the product: For example, consider the following statements:
SELECT ProductID, SUM(OrderQty) AS SELECT
TotalQuantityOrdered FROM Table1.Productid,TotalQuantityOrdered,
Sales.SalesOrderDetail TotalSalesOrdersPlaced
GROUP BY ProductId FROM
The following figure displays the output of the preceding (SELECT ProductID,SUM(OrderQty) AS
statement. TotalQuantityOrdered FROM
Sales.SalesOrderDetail
GROUP BY ProductId
)AS Table1
INNER JOIN
(SELECT ProductID,SUM(OrderQty) AS
TotalSalesOrdersPlaced FROM
Sales.SalesOrderDetail
GROUP BY ProductId ,SalesOrderId
) AS Table2
ON Table1.ProductID=Table2.ProductID
The following steps describe the execution of the
preceding statements:
1. The FROM clause of the preceding statement
creates two derived tables named, Table1 and joined with each row from the second table.
Table2.  In an equi join, only the equality operator is used
2. The derived table, Table1, selects the total to specify the join condition.
number of orders placed for each product from  A self join correlates one row in a table with other
the Sales.SalesOrderDetail table. rows in the same table.
3. The derived table, Table2, selects the total sales  The IN clause in a subquery returns zero or more
orders placed for each product from the values.
Sales.SalesOrderDetail table.  The EXISTS clause in a subquery returns data in
4. Finally, an inner join is used to obtain data from terms of a TRUE or FALSE value.
both the derived tables into a common result set.  The ALL and ANY keywords are used in a
The following figure displays the output of the preceding subquery to modify the existing comparison
statements. operator.
 Aggregate functions can also be used in
subqueries to generate aggregated values from the
inner query.
 Subqueries that contain one or more queries are
specified as nested subqueries.
 A correlated subquery can be defined as a query
that depends on the outer query for its evaluation.
 SQL Server provides the APPLY operator that
allows you to combine the result sets retrieved
from table expressions.
 There are two types of APPLY operators
supported by SQL Server. These are:
 CROSS APPLY
 OUTER APPLY
The Output Derived After Executing the Statement  The CROSS APPLY operator returns only those
rows from the outer result set that match with the
inner result set.
Activity 3.3: Using CTE  The OUTER APPLY operator returns all rows
from the outer result set even if the corresponding
row is not found in the inner result set.
 The UNION operator is used to combine the data
Activity 3.4: Using Derived of two or more queries into a single result set.
 The EXCEPT operator compares two result sets
Tables and returns the data from the first result set that is
not found in the second result set.
 The INTERSECT operator returns the common
Summary rows after comparing two result sets. If the
common rows are not found, then NULL value is
In this chapter, you learned that: returned.
 Joins and subqueries are used to retrieve data  A CTE is used to create a temporary named result
from multiple tables. set.
 An inner join combines records from multiple  In a recursive CTE, result sets of more than one
tables by using a comparison operator on a query are combined to populate the CTE.
common column.  A recursive CTE consists of the following
 A left outer join returns all the rows from the left members to produce the required result:
table and the matching rows from the right table.  Anchor query
 A right outer join returns all the rows from the  Recursive query
right table and the matching rows from the left  A derived table is a virtual table, which is created
table. by using the SELECT statement with an alias in
 A full outer join returns all the matching and non- the AS clause.
matching rows from both the tables on which the  Derived table acts as a regular table or a view.
join is applied.
 A cross join returns each row from the first table
Reference Reading
Querying Data by Using Joins

Reference Reading: Books Reference Reading: URLs


Sams Teach Yourself SQL in http://msdn.microsoft.com/
24 Hours by Ryan Stephens en-us/library/
and Ron Plew ms191472.aspx

Querying Data by Using Subqueries

Reference Reading: Books Reference Reading: URLs


Microsoft SQL Server 2012 http://msdn.microsoft.com/
Bible by Adam Jorgensen, en-us/library/ms189575%
Jorge Segarra, and Patrick 28v=SQL.105%29.aspx
LeBlanc

Managing Result Sets

Reference Reading: Books Reference Reading: URLs


Beginning SQL Server 2012 http://msdn.microsoft.com/
for Developers by Robin en-us/library/
Dewson ms190766.aspx
Server
System databases are the standard databases that exist in
Chapter 4 every instance of SQL Server. These databases contain a
specific set of tables that are used to store server-specific
Managing Databases and Tables configurations and templates for other databases. In
addition, these databases contain a temporary storage area
As a database developer, you are responsible for creating required to query the database.
and managing databases and tables. While creating tables, SQL Server contains the following system databases:
it is important for you to maintain data integrity. This  master
implies that data in tables is accurate, consistent, and  tempdb
reliable. SQL Server provides various checks that you can  model
apply on tables to enforce data integrity.  msdb
This chapter introduces different types of system  Resource
databases. It explains how to create and drop user-defined
The master Database
databases. Further, it explains how to create and manage
The master database consists of system tables that keep
user-defined tables by using DDL statements. In addition,
track of the server installation as a whole and all the other
the chapter focuses on various checks and rules that you
databases. It records all the server-specific configuration
can apply to tables to ensure data integrity.
information, including authorized users, databases, system
Objectives configuration settings, and remote servers. In addition, it
records the instance-wide metadata, such as logon
In this chapter, you will learn to: accounts, endpoints, and system configuration settings.
 Manage databases The master database contains critical data that controls the
 Manage tables SQL Server operations. It is advisable not to give any
permission to users on the master database. It is also
important to update the backups of the master database to
Managing Databases reflect the changes that take place in the database as the
master database records the existence of all other
A database is a collection of tables and objects such as
databases and the location of those database files.
views, indexes, stored procedures, and triggers. The data
The master database also stores the initialization
stored in a database may be related to a process, such as an
information of SQL Server. Therefore, if the master
inventory or a payroll.
database is unavailable, the SQL Server database engine
As a database developer, you might need to create
will not be started.
databases to store information. At times, you might also
need to delete a database, if it is not required. Therefore, it
is essential to know how to create and delete a database. The query window of SQL Server Management Studio
SQL Server supports many databases and contains some defaults to the master database context. Any queries executed
standard system databases. Before creating a database, it is from the query window will execute in the master database
important to identify the system databases supported by unless you change the context.
SQL Server and their importance. The tempdb Database
The tempdb database is a temporary database that holds all
A view is a virtual table that provides access to a temporary tables and stored procedures. It is automatically
subset of columns from one or more tables. used by the server to resolve large or nested queries or to
An index is an internal table structure that SQL Server uses sort data before displaying results to the user.
to provide quick access to the rows of a table based on the All the temporary tables and results generated by the
values of one or more columns. GROUP BY, ORDER BY, and DISTINCT clauses are
A stored procedure is a collection or batch of T-SQL stored in the tempdb database. The tempdb database is re-
statements and control-of-flow language that is stored under created every time SQL Server is started so that the system
one name and executed as a single unit. always starts with a clean copy of the database. Temporary
A trigger is a block of code that constitutes a set of T-SQL tables and stored procedures are dropped automatically on
statements. These statements are activated in response to disconnect or when the system is shut down. Backup and
certain actions. restore operations are not allowed on tempdb database.
Therefore, you should not save any database object in the
tempdb database because this database is recreated every
Identifying System Databases in SQL
time SQL Server starts. This results in losing the data you
saved earlier.

Stored procedures will be discussed later in Chapter


7. Endpoints will be discussed later in Chapter 10.
The model Database
The model database acts as a template or a prototype for
new databases. Whenever a database is created, the
contents of the model database are copied to the new
database.
If you modify the model database, all databases created
after the modification will inherit those changes. The
changes include setting permissions or database options,
or adding objects such as tables, functions, or stored
procedures. For example, if you want every new database
to contain a particular database object, you can add the
object to the model database. After this, whenever you
create a new database, the object will be added to the
database.
The msdb Database
The msdb database supports SQL Server Agent, which is a
tool to schedule periodic activities of SQL Server, such as
backup and database mailing. SQL Server Agent is used to
schedule a job to run in response to a specific event or on
demand. For example, if you want to back up all the
company servers every weekday, you can automate this
task. Schedule the backup to run after 12.00 P.M., Monday
through Friday. If the backup encounters a problem, SQL
Server Agent can record the event and notify you. The
msdb database contains task scheduling, exception
handling, alert management, and system operator
information needed for the SQL Executive Service. The
msdb database contains a few system-defined tables that
are specific to the database. Identifying the Database Files
As a database developer, you can query this database to
retrieve information on alerts, exceptions, and schedules. SQL Server maps a database over a set of operating-
For example, you can query this database to know the system files. Each database is stored as a set of files on the
schedule for the next backup and to know the history of hard disk of the computer. These files include:
previously scheduled backups. You can also query this  Primary data file: The primary data file contains
database to know how many database e-mail messages database objects. It can be used for the system
have been sent to the administrator. tables and objects. It is the starting point of the
database and points to other files in the database.
The Resource Database
Every database has one primary data file. It has
The Resource database is a read-only, hidden system
a .mdf extension.
database that contains all the system objects, such as
 Secondary data file: The secondary data file is
system-defined procedures and views, included with SQL
used to store user-defined database objects. Very
Server. It does not contain user data or user metadata. It
large databases may need multiple secondary data
makes upgrading to a new version of SQL Server easier
files spread across multiple disks. Databases need
and faster. By design, it is not visible in the Object
not have secondary data files if the primary data
Explorer window of SQL Server Management Studio. It
file is large enough to hold all the data in the
also does not have an entry in master.sys.databases.
database. The secondary data file has a .ndf
extension.
 Transaction log file: The transaction log file
records all modifications that have occurred in the
database and the transactions that caused those {
modifications. The transaction log files hold all FILEGROUP filegroup_name [ CONTAINS
the transaction information and can be used to FILESTREAM ] [ DEFAULT ]
recover a database. At least one transaction log <filespec> [ ,...n ]
file must exist for a database. However, there can }
be more than one transaction log files. The where,
minimum size of a transaction log file is 512KB. database_name is the name of the new database.
The size of the transaction log file should be 25 - ON specifies the disk files used to store the data portion of
40 percent of the size of the database. The log the database (data files).
files have a .ldf extension. PRIMARY specifies the associated <filespec> list that
defines files in the primary filegroup.
A database must consist of a primary data file and one LOG ON specifies the disk files used to store the log files.
transaction log file. In SQL Server, the locations of all the NAME=logical_file_name specifies the logical
files in a database are recorded in the primary data file of the name for the file.
database and in the master database. FILENAME=os_file_name specifies the operating-
What are Filegroups? system file name for the file.
Database files are grouped together in filegroups for SIZE=size specifies the initial size of the file defined in
allocation and administration purposes. A filegroup is a the <filespec>list.
collection of files. A database comprises a primary MAXSIZE=max_size specifies the maximum size to
filegroup and any user-defined filegroup. A primary which the file defined in the <filespec> list can grow.
filegroup contains the primary data file and any other files FILEGROWTH=growth_increment specifies the
that are not put into any other filegroup. It also contains growth increment of the file defined in the <filespec>
the system tables. When objects are created in a database list. The FILEGROWTH setting for a file cannot exceed the
without specifying the filegroup, they are assigned to the MAXSIZE setting.
default filegroup. Only one filegroup in a database can be filegroup_name specifies the logical name of the
the default filegroup. filegroup. It must be unique in the database and cannot be
A user-defined filegroup is a filegroup that is created by the system-provided name.
users. You can create filegroups to distribute data amongst CONTAINS FILESTREAM specifies that the filegroup
more than one filegroups to improve the performance of a
stores the FILESTREAM data in the file system.
database.
DEFAULT specifies that it is the default filegroup in the
database.
Creating a User-Defined Database
In addition to system databases, SQL Server also contains To create a database, you must be a member of the
dbcreator server role. In addition, you must have the
user-defined databases where users store and manage their
CREATE DATABASE, CREATE ANY DATABASE, or ALTER
information. When a user creates a database, it is stored as
ANY DATABASE permissions.
a set of files on the hard disk of the computer.
The following statement creates a database named
To create a user-defined database, you can use the
Personnel to store the data related to all the employees:
CREATE DATABASE statement. The syntax of the
CREATE DATABASE Personnel
CREATE DATABASE statement is:
CREATE DATABASE database_name The preceding statement creates a database named
[ ON [ PRIMARY ] [ < filespec >]] Personnel in the C:\Program Files\Microsoft SQL Server
[ , <filegroup> [ ,...n ] ] \MSSQL10.MSSQLSERVER\MSSQL\DATA folder. The
[ LOG ON [ < filespec > ]] data file name of the database is Personnel.mdf and the log
< filespec > ::= file name is Personnel_log.ldf.
( [ NAME = logical_file_name , ] Specifying FileGroups While Creating
FILENAME = ‘os_file_name’ Databases
[ , SIZE = size ] When you specify only the name of the database while
[ , MAXSIZE = { max_size | creating a database, it is created with the default filegroup.
UNLIMITED } ] However, you can also specify the name of the filegroup
[ , FILEGROWTH = growth_increment ] ) to be used for a database while creating it. For example,
[ ,...n ] consider the following statements:
} USE master
<filegroup> ::=
GO
CREATE DATABASE MyDB
ON PRIMARY
( NAME=‘MyDB_Primary’,
FILENAME=
‘c:\Data\MyDB_Prm.mdf’,
SIZE=4MB,
MAXSIZE=10MB,
FILEGROWTH=1MB),
FILEGROUP MyDB_FG1
( NAME = ‘MyDB_FG1_Dat1’,
FILENAME =
‘c:\Data\MyDB_FG1_1.ndf’,
SIZE = 1MB,
MAXSIZE=10MB,
FILEGROWTH=1MB),
( NAME = ‘MyDB_FG1_Dat2’,
FILENAME = The MyDB Database
‘c:\Data\MyDB_FG1_2.ndf’, You can change the default filegroup by using the
SIZE = 1MB, following statement:
MAXSIZE=10MB, ALTER DATABASE MyDB
FILEGROWTH=1MB) MODIFY FILEGROUP MyDB_FG1 DEFAULT
LOG ON GO
( NAME=‘MyDB_log’,
The preceding statement makes MyDB_FG1 as the default
FILENAME =
filegroup for the MyDB database.
‘c:\Data\MyDB.ldf’,
SIZE=1MB,
MAXSIZE=10MB, Task 4.1: Creating a Database
FILEGROWTH=1MB) Using the Object Explorer Window
GO
The preceding statements create a database MyDB where
the primary data file is stored in the primary filegroup,
After a database is created, you may also need to view
which is the default filegroup. The two secondary data
the details of the database, such as database size, database
files are stored in the user-defined filegroup named owner, date of creation, and compatibility level. For this
MyDB_FG1. It also creates a log file by the name purpose, you can use the sp_helpdb statement. The syntax of
MyDB.ldf. the sp_helpdb statement is:
sp_helpdb [database name]
The Data folder must exist in the C drive for the Creating FILESTREAM-Enabled
preceding code to execute successfully. Databases
The following figure represents the creation of the At times, you need to store unstructured data such as
primary, log, and secondary files of the MyDB database. videos, graphic files, sound clips, Word documents, or
Excel spreadsheets in a database. This unstructured data is
called a Binary Large Object (BLOB). SQL Server
provides the FILESTREAM feature that enables you to
store and manage BLOBs effectively.
The FILESTREAM feature allows BLOBs to be stored
directly in the Windows file system. This feature
integrates the SQL Server Database Engine with New
Technology File System (NTFS). The BLOBs are stored in
the NTFS file system and the SQL Server database engine
manages the link between the column declared with the
FILESTREAM feature and the actual file located in the
NTFS.
The company wants to store unstructured data such as
photos of the employees in the database. Therefore, being
NTFS is the file system used by the Windows
a database developer, you can use the following statement
operating system to store and retrieve files from a hard disk.
to create the FILESTREAM-enabled database named
To use the FILESTREAM feature, a database needs to TekSoft:
contain a FILESTREAM filegroup. In addition, you need CREATE DATABASE TekSoft
to create a table that contains a varbinary(max) column ON
along with the FILESTREAM attribute. Setting a column PRIMARY ( NAME = TekSoft,
with the FILESTREAM attribute causes the database FILENAME = ‘C:\Data\TekSoft.mdf’),
engine to store all the data for that column in the Windows FILEGROUP FileStreamGroup CONTAINS
file system, and not in the database file. FILESTREAM(NAME = TekSoft_Data,
For a database to contain the FILESTREAM filegroup, the FILENAME = ‘C:\Data\TekSoft_Data’)
FILESTREAM feature of the SQL Server instance, on LOG ON ( NAME = Log1,
which the database is created, must be enabled. You can FILENAME = ‘C:\Data\TekSoft.ldf’)
enable the FILESTREAM feature of an SQL Server GO
instance during its installation. In addition, you can enable In the preceding statement, the CONTAINS
the FILESTREAM feature of an SQL Server instance by FILESTREAM clause is used to specify that the database
using the SQL Server Configuration Manager. Once the will store the FILESTREAM data. After the execution of
FILESTREAM feature is enabled for an instance of SQL the preceding statement, the TekSoft_Data folder and the
Server, you need to configure the FILESTREAM access TekSoft.ldf and TekSoft.mdf files are created in the Data
level. folder of the C drive.
The FILESTREAM access level specifies the accessibility The TekSoft_Data folder contains the $FSLOG folder and
of the FILESTREAM data on an instance of SQL Server. the filestream.hdr file. The $FSLOG folder contains the
The following table describes the possible values of access log information of the FILESTREAM-enabled tables in
levels and their accessibility. the TekSoft database. However, the filestream.hdr is a
system file that contains the FILESTREAM header
Access Levels Accessibility information and must not be deleted.
0 FILESTREAM data access
is disabled. This is the
default value.
1 FILESTREAM data is
enabled only for T-SQL
access.
2 FILESTREAM data is
enabled only for T-SQL and
local file system access.
3 FILESTREAM data is
enabled for T-SQL, local file
system access, and remote
file system access.

The Access Levels and the Accessibility of FILESTREAM Data


You can execute the following statement in the Query
Editor window to configure the FILESTREAM data
access:
EXEC sp_configure
filestream_access_level, 2 Renaming a User-Defined Database
Then, execute the following statement to apply the You can rename a database whenever required. Only a
configuration changes: system administrator or the database owner can rename a
RECONFIGURE database. The sp_renamedb stored procedure is used to
Once the FILESTREAM data access has been configured rename a database. The syntax of the sp_renamedb
successfully, you can create the FILESTREAM-enabled statement is:
database to store the unstructured data. For example, sp_renamedb old_database_name,
TekSoft, Inc. is a leading software development company. new_database_name
where,
old_database_name is the current name of the Creating a Table
database. In SQL Server, you can create a table by using the
new_database_name is the new name of the database. CREATE TABLE statement. The syntax of the CREATE
For example, the following statement renames the TABLE statement is:
Personnel database to the Employee database: CREATE TABLE
sp_renamedb Personnel, Employee [ database_name. [ schema_name ] .]
table_name
( { <column_definition> |
Dropping a User-Defined Database <computed_column_definition> }
You can delete a database when it is no longer required. [IDENTITY (SEED, INCREMENT)]
This causes all the database files and data to be deleted. [ <table_constraint> ] [ ,...n ] )
Only the users with sysadmin role and the database owner [ ON { partition_scheme_name
( partition_column_name ) | filegroup
have the permissions to delete a database. The DROP
| "default" } ]
DATABASE statement is used to delete a database. The
[ { TEXTIMAGE_ON { filegroup |
syntax of the DROP DATABASE statement is:
"default" } ]
DROP DATABASE database_name
[ ; ]
where,
where,
database_name is the name of the database.
database_name specifies the name of the database
The following statement deletes the Employee database:
where the table is created. If you do not specify a database
DROP DATABASE Employee
name, the table is created in the current database.
schema_name specifies the schema name where the new
You cannot delete a system-defined database. table belongs. Schema is a logical group of database
objects in a database. Schemas help in improving
manageability of objects in a database.
You can rename or delete a database by right-clicking table_name specifies the new table name. The table
the database to be renamed or deleted under the Databases
name can be a maximum of 128 characters.
folder in the Object Explorer window, and then selecting the
Rename or Delete option from the pop-up menu. column_name specifies the name of the column and
must be unique in the table. It can be a maximum of 128
characters.
Managing Tables computed_column_definition specifies the
expression that produces the value of a computed column.
A table is a database object used to store data. Data in a A computed column does not exist physically in the
table is organized in rows and columns. Each row in a memory, but it is used to generate a computed value. The
table represents a unique record and each column other columns in a table, which are not computed
represents an attribute of the record. The column names columns, are called persisted columns.
within a table must be unique, but the same column name IDENTITY is used for those columns that need
can be used in different tables within a database. automatically generated, unique system values. This
As a database developer, you need to create tables to store property can be used to generate sequential numbers.
data. While creating tables in a relational database, you SEED is the starting or the initial value for the IDENTITY
must ensure that no one enters invalid data in it. For this, column.
you need to apply certain rules and constraints on columns INCREMENT is the step value used to generate the next
that specify the kind of data to be stored. In addition, you value for the column. This value can also be negative.
need to specify the relationships between various tables.
table_constraint is an optional keyword that
If you want to store a large volume of data in a table, you
specifies the PRIMARY KEY, NOT NULL, UNIQUE,
can create a partitioned table. This helps in improving
FOREIGN KEY, or CHECK constraint.
query performance.
partition_scheme_name specifies the partition
In addition to creating tables, you are responsible for
scheme name that defines the filegroups on which the
managing tables. Management of tables involves
partition of a table is mapped. Ensure that the partition
modifying tables to add columns or to change the rules
scheme exists within the database.
imposed on the table. It also involves deleting tables,
partition_column_name specifies the column name
when they are no longer required.
on which a partitioned table will be partitioned.
TEXTIMAGE_ON { filegroup | "default" } can use the following statement to create an IDENTITY
are keywords that specify that the text, ntext, image, xml, column in a table:
varchar(max), nvarchar(max), varbinary(max), and CLR CREATE TABLE Emp
user-defined type columns are stored on the specified (EmpCode int IDENTITY(100,1),
filegroup. If there are no large value columns in the table, EmpName char(25) NOT NULL,
TEXTIMAGE_ON is not allowed. DeptNo char(4) NOT NULL)
GO
The preceding statement creates a table Emp with the
You need to have the CREATE TABLE permissions to IDENTITY column. The EmpCode column of the Emp
create a table in a database.
table is the IDENTITY column with the starting value
Consider the following example. The management of (SEED) as 100 and the step value (INCREMENT) as 1.
AdventureWorks, Inc. needs to maintain the leave details Therefore, the EmpCode column will store values as 100,
of the employees. For this, you need to create a table 101, 102, and so on.
named EmployeeLeave in the HumanResources schema. Consider another example where the management of
The following table shows the structure of the AdventureWorks, Inc. needs to maintain the project details
EmployeeLeave table. assigned to their employees. For this, you need to create a
Project table in the HumanResources schema. The
Columns Data Type Checks following table shows the structure of the Project table.
EmployeeID Int NOT NULL
LeaveStartDate Date NOT NULL Columns Data Type
LeaveEndDate Date NOT NULL ProjectCode Int
LeaveReason varchar(100) NOT NULL ProjectManagerID Int
LeaveType char(2) NOT NULL Description varchar(50)
StartDate Datetime
The Structure of the EmployeeLeave Table EndDate Datetime
You can use the following statement to create the table:
CREATE TABLE The Structure of the Project Table
HumanResources.EmployeeLeave You can use the following statement to create the Project
( table:
EmployeeID int NOT NULL, CREATE TABLE HumanResources.Project
LeaveStartDate datetime NOT NULL, (
LeaveEndDate datetime NOT NULL, ProjectCode int,
LeaveReason varchar(100), ProjectManagerID int,
LeaveType char(2)NOT NULL Description varchar(50),
) StartDate datetime,
GO EndDate datetime
You can use the system stored procedure named sp_help to )
view a table structure. To view the stucture of The preceding statement creates a table named Project in
HumanResources.EmployeeLeave table, you can use the the HumanResources schema where all the columns can
following statement: store null values.
sp_help ‘HumanResources.EmployeeLeave’ Sometimes, a column in a table contains a maximum
When you execute the preceding statement, the output number of NULL values, instead of actual data. For
contains the details of all the columns, keys, constraints, example, there is a column named OrderReturnDate in the
and indexes applied on the Orders table. This column contains the date when the
HumanResources.EmployeeLeave table. order was returned. There could be less data in this
In the EmployeeLeave table, you want that the values for column as not many orders are returned. Therefore, most
all the columns should be inserted by a user. However, the of the rows in this column would contain NULL values.
values in the EmployeeID column should be generated To optimize the storage space for NULL values in the
automatically in sequential order. You can perform this database engine, you can declare such columns as
task by assigning an IDENTITY property to the SPARSE. If a column contains zero or NULL values, the
EmployeeID column. SPARSE column would require lesser space as compared
If you assign an IDENTITY property to a column, SQL to a regular column.
Server automatically generates sequential numbers for that Consider the following statement:
column whenever new rows are inserted in the table. You CREATE TABLE Ord_Det
( USE TekSoft
OrderID int, GO
OrderDate datetime, CREATE TABLE EmpDetails
OrderCost money, (
OrderRetDate datetime SPARSE NULL EmployeeID UNIQUEIDENTIFIER
) ROWGUIDCOL NOT NULL UNIQUE,
In the preceding staement, a table named Ord_Det is EmployeeName varchar(30),
created that contains a SPARSE column named EmployeeAddress varchar(100),
OrderRetDate. While defining a SPARSE column, you EmployeeDept varchar(20),
must consider the following guidelines: EmployeePhoto VARBINARY(MAX)
 A sparse column must be nullable. FILESTREAM
 A sparse column cannot have the )
ROWGUIDCOL property or the IDENTITY GO
property. In the preceding statements, the EmployeeID column is
 A sparse column can be defined on any, except declared as UNIQUEIDENTIFIER ROWGUIDCOL,
the following data types: which uniquely identifies the records in the table across
 Text the databases. The values of the column set with the
 Ntext FILESTREAM attribute are stored in the Windows file
 Image system. Therefore, you must declare a column as
 Timestamp UNIQUEIDENTIFIER ROWGUID while creating a table
 Geometry that stores the FILESTREAM data.
 geography In addtion to storing the FILESTREAM data, you can
 A sparse column cannot be of the user-defined store the spatial data in a table by using the geometry and
data type. geography data types. For example, you need to create the
 A sparse column cannot have the FILESTREAM County_Location table to store the country ID and
attribute. location of the country. The following table shows the
 A sparse column cannot have a default value. structure of the Country_Location table.
 A sparse column cannot be bound to a rule.
 A computed column cannot be marked as Columns Data Type Checks
SPARSE although a computed column can be a CountryID Int NOT NULL
part of the sparse column. CountryLocation geography NOT NULL
 A sparse column cannot be a part of a clustered
index or a unique primary key index. The Structure of the Country_Location Table
Consider another example, where you need to create the You can use the following statements to create the
EmpDetails table to maintain the employee details. The Country_Location table:
following table shows the structure of the EmpDetails USE AdventureWorks
table. GO
CREATE TABLE Country_Location
Columns Data Type Checks (
EmployeeID UNIQUEIDENTIF NOT NULL CountryID int,
IER CountryLocation geography
ROWGUIDCOL )
EmployeeName varchar(30) NOT NULL GO
EmployeeAddress varchar(100) NOT NULL In the preceding statements, the CountryLocation column
EmployeeDept varchar(20) NOT NULL is declared as the geography data type that can store the
EmployeePhoto varbinary(max) NOT NULL latitude and longitude cordinates, which indicate the
location of the country.
The Structure of the EmpDetails Table
In the preceding table, the photo of the employee needs to Task 4.2: Creating a Table by
be stored in the EmloyeePhoto column of the EmpDetails Using SQL Server Management
table. Photo is a graphic image and can be stored as a
FILESTREAM data in the table. You can use the Studio
following statements to create the EmpDetails table that
stores the FILESTREAM data:
bicycle has been ordered and an entry is to be
Task 4.3: Creating a Table by made in the OrderDetail table, then that bicycle
code should exist in the Product table. This
Using the Insert Snippet Menu ensures that an order is placed only for the
bicycle that is available.
 User-defined integrity: Refers to a set of rules
specified by a user, which do not belong to the
Implementing Data Integrity entity,domain, and referential integrity categories.
When creating tables, SQL Server allows you to maintain
If checks are not applied while defining and creating integrity by:
tables, the data stored in the tables can become redundant.  Applying constraints.
For example, if you do not store the data about all the  Enabling and disabling constraints.
employees with complete address details, then the data  Applying rules.
would not be useful.  Using user-defined data types.
Similarly, if a database used by the Human Resource Applying Constraints
department stores employee contact details in two separate
Consider an example where a user entered a duplicate
tables, the details of the employees might not match. This value in the EmployeeID column of the Employee table.
would result in inconsistency and confusion.
This would mean that the two employees have same
Therefore, it is important to ensure that the data stored in employee ID. This would further result in erroneous
tables is complete and consistent. The concept of results when anybody queries the table. As a database
maintaining consistency and completeness of data is called developer, you can prevent this by enforcing data integrity
data integrity. Data integrity is enforced to ensure that the on the table by using constraints.
data in a database is accurate, consistent, and reliable. It is
Constraints define rules that must be followed to maintain
broadly classified into the following categories:
consistency and correctness of data. A constraint can be
 Entity integrity: Ensures that each row can be either created while creating a table or added later. When a
uniquely identified by an attribute called the constraint is added after the table is created, it checks the
primary key. The primary key column contains a existing data. If there is any violation, then the constraint
unique value in all the rows. In addition, this is rejected.
column cannot be NULL. Consider a situation A constraint can be created by using either of the
where there might be two candidates for an following statements:
interview with the same name ‘Jack’. By
 CREATE TABLE statement
enforcing entity integrity, the two candidates can
 ALTER TABLE statement
be identified by using the unique code assigned to
A constraint can be defined on a column while creating a
them. For example, one candidate can have the
table. It can be created with the CREATE TABLE
code 001 and the other candidate can have the
statement. The syntax of adding a constraint at the time of
code 002.
table creation is:
 Domain integrity: Ensures that only a valid
CREATE TABLE table_name
range of values is stored in a column. It can be
(
enforced by restricting the type of data, the range
column_name CONSTRAINT
of values, and the format of data. For example, constraint_name constraint_type
you have a table called BranchOffice with a [,CONSTRAINT constraint_name
column called City that stores the names of the constraint_type]
cities where the branch offices are located. The
where,
offices are located in ‘Beijing’, ‘Nanjing’,
column_name is the name of the column on which the
‘Hangzhou’, ‘Dalian’, ‘Suzhou’, ‘Chengdu’, and
constraint is to be defined.
‘Guangzhou’. By enforcing domain integrity, you
can ensure that only valid values (as per the list constraint_name is the name of the constraint to be
specified) are entered in the City column of the created and must follow the rules for the identifier.
BranchOffice table. Therefore, the user will not constraint_type is the type of the constraint to be
be allowed to store any other city names like added.
‘New York’ or ‘London’ in the City column of the Constraints can be divided into the following types:
BranchOffice table.  Primary key constraint
 Referential integrity: Ensures that the values of  Unique constraint
the foreign key match the value of the  Foreign key constraint
corresponding primary key. For example, if a  Check constraint
 Default constraint statement to apply the composite primary key constraint:
Primary Key Constraint CREATE TABLE
A primary key constraint is defined on a column or a set of HumanResources.EmployeeLeave
columns whose values uniquely identify all the rows in a (
table. These columns are referred to as the primary key EmployeeID int,
columns. A primary key column cannot contain NULL LeaveStartDate datetime
values since it is used to uniquely identify rows in a table. CONSTRAINT cpkLeaveStartDate PRIMARY
The primary key constraint ensures entity integrity. KEY(EmployeeID, LeaveStartDate),
You can define a primary key constraint while creating the ...
table or you can add it later by altering the table. However, ...
if you define the primary key constraint after inserting ...
rows, SQL Server will give an error if the rows contain )
duplicate values in the column. While defining a primary The preceding statement creates the EmployeeLeave table
key constraint, you need to specify a name for the with a composite primary key constraint on EmployeeID
constraint. If a name is not specified, SQL Server and LeaveStartDate. The name of the constraint is
automatically assigns a name to the constraint. cpkLeaveStartDate.
If a primary key constraint is defined on a column that Unique Constraint
already contains data, then the existing data in the column The unique constraint is used to enforce uniqueness on
is screened. If any duplicate values are found, then the non-primary key columns. A primary key constraint
primary key constraint is rejected. The syntax of applying column automatically includes a restriction for
the primary key constraint while creating a table is: uniqueness. The unique constraint is similar to the primary
CREATE TABLE table_name key constraint except that it allows one NULL row.
( Multiple unique constraints can be created on a table. The
col_name [CONSTRAINT constraint_name syntax of applying the unique constraint when creating a
PRIMARY KEY [CLUSTERED|NONCLUSTERED] table is:
col_name [, col_name [, col_name CREATE TABLE table_name
[, ...]]] (
) col_name [CONSTRAINT constraint_name
where, UNIQUE [CLUSTERED | NONCLUSTERED]
constraint_name specifies the name of the constraint (col_name [, col_name [, col_name
to be created. [, ...]]])
CLUSTERED|NONCLUSTERED are the keywords that col_name [, col_name [, col_name
[, ...]]]
specify if a clustered or a nonclustered index is to be
)
created for the primary key constraint.
where,
col_name specifies the name of the column(s) on which
constraint_name specifies the name of the constraint
the primary key constraint is to be defined.
to be created.
CLUSTERED | NONCLUSTERED are the keywords that
You will learn more about indexes in Chapter 6. specify if a clustered or a nonclustered index is to be
In the Project table, you can add a primary key constraint created for the unique constraint.
while creating the table. You can use the following col_name specifies the name of the column(s) on which
statement to apply the primary key constraint: the unique constraint is to be defined.
CREATE TABLE HumanResources.Project You can use the following statement to enforce the unique
( constraint on the Description column of the Project table:
ProjectCode int CONSTRAINT CREATE TABLE HumanResources.Project
pkProjectCode PRIMARY KEY, (
... ProjectCode int CONSTRAINT
... pkProjectCode PRIMARY KEY,
) Description varchar(50) CONSTRAINT
The preceding statement will create the Project table with unDesc UNIQUE,
a primary key column, ProjectCode. ...
You can create a primary key using more than one column. ...
For example, you can set the EmployeeID and the )
LeaveStartDate columns of the EmployeeLeave table as a Foreign Key Constraint
composite primary key. You can use the following
You can use the foreign key constraint to remove the [, ...]]]
inconsistency in two tables when the data in one table )
depends on the data in the other table. A foreign key refers where,
the primary key column of another table, as shown in the constraint_name is the name of the constraint on
following figure. which the foreign key constraint is to be defined.
col_name is the name of the column on which the
foreign key constraint is to be enforced.
table_name is the name of the related table in which
the primary key constraint has been specified.
column_name is the name of the primary key column of
the related table on which the primary key constraint has
been defined.
For example, in the EmployeeLeave table of the
HumanResources schema, you need to add the foreign key
The Primary Key and Foreign Key Columns constraint to enforce referential integrity. The EmployeeID
The preceding figure contains two tables, Dept and Emp. column is set as a primary key in the Employee table of
The DeptNo column is the primary key in the Dept table the HumanResources schema. Therefore, you need to set
and foreign key in the Emp table. EmployeeID in the EmployeeLeave table as a foreign key.
Consider the example of Customers and Orders table. You can use the following statement to apply the foreign
The following figure represents the primary key and key constraint in the EmployeeLeave table:
foreign key relationship between the Customers and the CREATE TABLE
Orders table. HumanResources.EmployeeLeave
(
EmployeeID int CONSTRAINT
fkEmployeeID REFERENCES
HumanResources.Employee(EmployeeID),
LeaveStartDate datetime CONSTRAINT
cpkLeaveStartDate PRIMARY KEY
(EmployeeID, LeaveStartDate),
...
...
...
)
You can also apply the foreign key constraint in the
EmployeeLeave table by using the following statement:
The Primary Key and Foreign Key Relationship CREATE TABLE
In the preceding figure, ID is the primary key column of HumanResources.EmployeeLeave
the Customers table and Cust_ID is the foreign key (
column in the Orders table. EmployeeID int,
A foreign key constraint associates one or more columns LeaveStartDate datetime CONSTRAINT
(the foreign key) of a table with an identical set of cpkLeaveStartDate PRIMARY KEY
columns (a primary key column) in another table on which (EmployeeID, LeaveStartDate),
a primary key constraint has been defined. The syntax of ...
applying the foreign key constraint when creating a table ...
is:
CREATE TABLE table_name CONSTRAINT fkEmployeeID FOREIGN KEY
( (EmployeeID) REFERENCES
col_name [CONSTRAINT constraint_name HumanResources.Employee(EmployeeID)
FOREIGN KEY (col_name [, col_name )
[, ...]]) The preceding statement creates the EmployeeLeave table
REFERENCES table_name (column_name [, with a foreign key constraint on the EmployeeID column.
column_name [, ...]])] The name of the constraint is fkEmployeeID.
(col_name [, col_name [, col_name A foreign key can also refer to the primary key column in
[, ...]]]) the same table. Consider the following EmployeeDet
col_name [, col_name [, col_name table.
Emp_ID Mgr_ID Emp_Name assign a NULL value for Mgr_ID as done in the first
1 NULL Mark INSERT statement.
2 1 Jack In addition to the foreign key constraint, you can apply the
3 1 Joe cascading referential integrity constraint to the tables
having foreign key relationships. The cascading referential
4 2 Michel
integrity constraint defines the action that SQL Server
5 3 Steve performs when an attempt is made to update or delete a
row in a table with a key referenced by a foreign key in
The EmployeeDet Table another table. SQL Server supports the ON DELETE and
The table contains the details of the employees. Emp_ID is ON UPDATE clauses to apply the cascading referential
the primary key in the table. Each employee record has a integrity constraint.
field named Mgr_ID that contains the ID of the reporting The ON DELETE and ON UPDATE clauses can be used
Manager of that employee. Each Manager is also an with the following options:
employee. Therefore, the details of the Managers are also  ON DELETE | ON UPDATE NO ACTION:
stored in the same table. In this case, the Mgr_ID column Specifies that if an attempt is made to delete or
can be a foreign key that refers to the Emp_ID column of update a primary key record, an error will be
the same table. Therefore, the records of the EmployeeDet raised and the delete or update operation will be
table are related with other records of the same table by rolled backed.
creating a parent-child relationship in the same table. Such  ON DELETE | ON UPDATE CASCADE:
a table is called a self-referencing table. Specifies that if an attempt is made to update or
In this example, Joe is Steve’s Manager. Joe, in turn, delete a primary key record, the corresponding
reports to Mark. The Manager of Michel is Jack. Jack also foreign key record is also updated or deleted,
reports to Mark. Mark is the CEO of the organization and respectively.
is at the top most level in the organizational hierarchy. He  ON DELETE | ON UPDATE SET NULL:
does not have a Manager. Therefore, the value of the Specifies that if an attempt is made to delete or
Mgr_ID column is null for Mark. update a primary key record, the corresponding
You can create a self-referencing table by using the foreign key record is set to NULL.
following statement:  ON DELETE | ON UPDATE SET DEFAULT:
CREATE TABLE EmployeeDet Specifies that if an attempt is made to delete or
( update a primary key record, the corresponding
Emp_id int PRIMARY KEY, foreign key record is set to its default values.
Mgr_ID int, However, to apply this constraint, all foreign key
Emp_Name varchar(20), columns must have a default definition.
FOREIGN KEY (mgr_id) For example, the EmployeeLeave table is associated with
REFERENCES EmployeeDet(Emp_id) the Employee table through the foreign key relationship.
); You can use the following statement to apply the
The following statements can be used to insert the records cascading referential integrity constraint on the Employee
in the EmployeeDet table: table:
INSERT INTO EmployeeDet VALUES ALTER TABLE
(1,NULL,‘Mark’) HumanResources.EmployeeLeave
INSERT INTO EmployeeDet VALUES ADD CONSTRAINT rfkcEmployeeID FOREIGN
(2,1,‘Jack’) KEY(EmployeeID)
INSERT INTO EmployeeDet VALUES REFERENCES HumanResources.Employee
(3,1,‘Joe’) (EmployeeID)
INSERT INTO EmployeeDet VALUES ON DELETE NO ACTION ON UPDATE NO
(4,2,‘Michel’) ACTION
INSERT INTO EmployeeDet VALUES In the preceding statement, the ON DELETE NO
(5,3,‘Steve’) ACTION and ON UPDATE NO ACTION clauses ensure
The preceding statements execute successfully. Now, that any attempt to delete or update the EmployeeID in the
consider the following INSERT statement: Employee table will not be successful.
INSERT INTO EmployeeDet VALUES
(6,10,‘Harry’) Check Constraint
The preceding statement gives an error because the foreign A check constraint enforces domain integrity by restricting
key constraint prevents the insertion of a record with the values to be inserted in a column. It is possible to
Mgr_ID that does not exist in the table. However, you may define multiple check constraints on a single column.
These are evaluated in the order in which they are defined. type can be any one of the three values: CL, PL,
The syntax of applying the check constraint is: or SL. Here, CL stands for Casual Leave, SL
CREATE TABLE table_name stands for Sick Leave, and PL stands for
( Privileged Leave.
col_name [CONSTRAINT constraint_name]  LIKE: To ensure that the values entered in
CHECK (expression) specific columns are of a certain pattern. This can
(col_name [, col_name [, ...]]) be achieved by using wildcards. For example, the
. following statement creates a check constraint on
) DeptCode column of the Emp table:
where, CREATE TABLE Emp
constraint_name specifies the name of the constraint (
to be created. ...
expression specifies the conditions that define the ...
check to be made on the column. It can include elements, DeptNo char(4) CHECK (DeptNo
such as arithmetic operators and relational operators, or LIKE ‘[0-9][0-9][0-9][0-9]’)
keywords, such as IN, LIKE, and BETWEEN. )
A single check constraint can be applied to multiple In the preceding statement, the check constraint
columns when it is defined at the table level. For example, specifies that the DeptNo column can contain
while entering project details, you want to ensure that the only a value that consists of characters from 0-9.
start date of the project must be less than or equal to the  BETWEEN: To specify a range of constant
end date. expressions by using the BETWEEN keyword.
You can use the following statement to apply the check The upper and lower boundary values are
constraint on the Project table: included in the range. For example, the following
CREATE TABLE HumanResources.Project statement creates a check constraint on the sal
( column of the EmpTable table:
........ CREATE TABLE EmpTable
........ (
StartDate datetime, ...
EndDate datetime, ...
Constraint chkDate CHECK (StartDate sal money CHECK (sal BETWEEN
<= EndDate) 20000 AND 80000)
) )
A check constraint can be specified by using the following In the preceding statement, the check constraint
keywords: specifies that the sal column can have a value
 IN: To ensure that the values entered are from a only between 20000 and 80000.
list of constant expressions. The following The rules to be followed while creating the check
statement creates a check constraint, chkLeave on constraint are:
the LeaveType column of the  It can be created at the column level.
HumanResources.EmployeeLeave table, thereby  It can contain user-specified search conditions.
restricting the entries to valid leave types:  It cannot contain subqueries.
CREATE TABLE  It does not check the existing data in the table if
HumanResources.EmployeeLeave created with the WITH NOCHECK option.
(  It can reference other columns of the same table.
EmployeeID int, Default Constraint
LeaveStartDate datetime A default constraint can be used to assign a constant value
CONSTRAINT cpkLeaveStartDate to a column, and the user need not insert values for such a
PRIMARY column. Only one default constraint can be created for a
KEY(EmployeeID, LeaveStartDate), column, but the column cannot be an IDENTITY column.
LeaveEndDate datetime NOT NULL, The syntax of applying the default constraint while
LeaveReason varchar(100), creating a table is:
LeaveType char(2) CONSTRAINT CREATE TABLE table_name
chkLeave CHECK(LeaveType (
IN(‘CL’,‘SL’,‘PL’)) col_name [CONSTRAINT constraint_name]
) DEFAULT (constant_expression | NULL)
The preceding statement ensures that the leave (col_name [, col_name [, ...]])
)
where,
constraint_name specifies the name of the constraint
to be created.
constant_expression specifies an expression that
contains only constant values. This can contain a NULL
value.
For example, while creating the EmployeeLeave table,
you can insert a default constraint to add a default value
for the LeaveType column. You can set the default leave
type as PL.
You can use the following statement to create the default
constraint:
CREATE TABLE
HumanResources.EmployeeLeave
(
......
LeaveType char(2) CONSTRAINT chLeave
CHECK(LeaveType IN(‘CL’,‘SL’,‘PL’))
CONSTRAINT chkDefLeave DEFAULT ‘PL’
)
The preceding statement creates the EmployeeLeave table
with a default constraint on the LeaveType column, where
the default value is specified as PL. The name of the
constraint is chkDefLeave.

You can also use the DEFAULT database objects to


create a default constraint that can be applied to columns
across tables within the same database. For this, you can
create database objects by using the CREATE DEFAULT
statement.

Enabling and Disabling Constraints


Constraints are applied on a table to keep a check on the
values inserted in the table. Sometimes, after applying a
constraint, you need to insert a value that contradicts the
definition of a constraint applied on that table. To
accomplish this task, you first need to disable that
constraint, and then insert the value in the table. After
inserting the value, you can again enable the constraint.
For example, you have applied a check constraint on the
OrderDate column of the Orders table. This column
accepts date between 15/01/2001 and 15/01/2010. Later,
you realized that you need to insert details of an order with
the order date 12/01/2000 in the Orders table. Therefore,
you need to disable the check constraint applied on the
Orders table. After inserting the required data, you can
again enable the check constraint on the Orders table.
For example, the check constraint is applied on the
LeaveType column of the EmployeeLeave table. This
check constraint allows only the values CL, SL, and PL in
the LeaveType column. However, you need to insert a
record in the LeaveType column for maternity leave. For
this, you have to specify ML in the LeaveType column. To
accomplish this task, you need to disable the check conditional_expression specifies the condition(s)
constraint applied on the EmployeeLeave table by using that defines the rule. It can be any expression that is valid
the following statement: in a WHERE clause and can include elements such as
Use AdventureWorks arithmetic operators, relational operators, IN, LIKE, and
ALTER TABLE BETWEEN.
HumanResources.EmployeeLeave The variable specified in the conditional expression must
NOCHECK CONSTRAINT chkLeave be prefixed with the ‘@’ symbol. The expression refers to
In the preceding statement the NOCHECK the value that is being specified with the INSERT or
CONSTRAINT clause is used to disable the check UPDATE statement.
constraint applied on the EmployeeLeave table. Then, you In the preceding example of the EmployeeLeave table,
can successfully insert a record with the ML leave type you applied the check constraint on the LeaveType column
value by using the following statement: to accept only three values: CL, SL, and PL. You can
INSERT INTO perform the same task by creating a rule, as shown in the
HumanResources.EmployeeLeave following statement:
VALUES(101, 15/12/2009, 20/12/2009, USE AdventureWorks
‘Maternity Leave’, ‘ML’) GO
After inserting the required record, you can enable the CREATE RULE ruleType
check constraint applied on the EmployeeLeave table by AS @LeaveType IN (‘CL’, ‘SL’, ‘PL’)
using the following statement: GO
ALTER TABLE The following table lists the examples to create rule.
HumanResources.EmployeeLeave
CHECK CONSTRAINT chkLeave Example Description
The preceding statement enables the check constraint CREATE RULE Specifies that if the value of
again. If you try to execute the following statement after dept_name_rule deptname is accounts or
enabling the constraint, an error is displayed: AS @deptname NOT IN stores, then the value is to
INSERT INTO (‘accounts’,’stores’) be rejected from being
HumanResources.EmployeeLeave
inserted into the column to
VALUES(102, 20/12/2009, 20/02/2010,
which the rule is bound.
‘Maternity Leave’, ‘ML’)
CREATE RULE Allows a value of $5000 or
The execution of the preceding statement displays the
min_price_rule more to be inserted in the
following error:
AS @minprice >= $5000 column to which the rule is
Msg 547, Level 16, State 0, Line 1
bound.
The INSERT statement conflicted with
the CHECK constraint “chkLeave”. CREATE RULE Specifies that the value to be
The conflict occurred in database emp_code_rule inserted in the column, to
“AdventureWorks”, table AS @empcode LIKE ‘[F-M] which the rule will be
“HumanResources.EmployeeLeave”, column [A-Z][0-9][0-9][0-9]’ bound, must follow the
‘LeaveType’. pattern specified in the
The statement has been terminated. LIKE clause. The first
character of the string can
Applying Rules be any value between F to
A rule enforces domain integrity for columns or user- M; the second character can
defined data types. The rule is applied to a column or a be any value ranging from A
user-defined data type before an INSERT or UPDATE to Z; and from the third
statement is issued. In other words, a rule specifies a character onwards, the
restriction on the values of a column or a user-defined data acceptable range is a
type. Rules are used to implement business-related numeric value from 0 to 9.
restrictions or limitations. A rule can be created by using
the CREATE RULE statement.
The Examples of Creating Rules
The syntax of the CREATE RULE statement is: After you create a rule, you need to activate the rule by
CREATE RULE rule_name AS
using a stored procedure, sp_bindrule.
conditional_expression
The syntax of sp_bindrule is:
where,
sp_bindrule <‘rule’>, <‘object_name’>,
rule_name specifies the name of the new rule that must [<‘futureonly_flag’>]
conform to the rules for identifiers. where,
rule specifies the name of the rule that you want to bind. precision specifies a non-negative integer that
object_name specifies the object on which you want to indicates the maximum number of decimal digits that can
bind the rule. be stored both to the left and to the right of the decimal
futureonly_flag applies only when you want to bind point.
the rule to a user-defined data type. scale specifies a non-negative integer that indicates the
Consider the following example where the rulType rule maximum number of decimal digits that can be stored to
created for the LeaveType column of the EmployeeLeave the right of the decimal point. It can be specified only if
table is bound by using the sp_bindrule stored procedure. precision is specified and must be less than or equal to the
You can use the following statement to bind the rule: precision.
sp_bindrule NULL | NOT NULL specifies whether the data type can
‘ruleType’,‘HumanResources.EmployeeLea hold a null value. If not specified, NULL is the default.
ve.LeaveType’ The following statement creates a user-defined data type
Similarly, when you want to remove a rule, the for descriptive columns:
sp_unbindrule stored procedure is used. For example, to USE AdventureWorks
remove the rule from the EmployeeLeave table, you can GO
use the following statement to unbind the rule: CREATE TYPE DSCRP
sp_unbindrule FROM varchar(100) NOT NULL ;
‘HumanResources.EmployeeLeave.LeaveTyp GO
e’ In the preceding statement, a user-defined data type,
A rule can be deleted by using the DROP RULE DSCRP is created to store the varchar data type and the
statement. The syntax for the DROP RULE statement is: size limit is specified as 100. Further, it also specifies
DROP RULE rule_name NOT NULL. Therefore, you can use this data for the
where, columns that store description, address, and reason.
rule_name is the name of the rule to be dropped. For example, you can use the DSCRP data type to store
For example, you can use the following statement to delete the data of the LeaveReason column of the
the rule, ruleType: EmployeeLeave table, as shown in the following
DROP RULE ruleType statement:
CREATE TABLE
Using User-Defined Data Types HumanResources.EmployeeLeave
User-defined data types are custom data types defined by (
the users with a custom name. They are based on the .......
system data types. A user-defined data type is basically a LeaveReason DSCRP,
named object with the following additional features: LeaveType char(2) CONSTRAINT chkLeave
 Defined data type and length CHECK(LeaveType IN(‘CL’,‘SL’,‘PL’))
 Defined nullability CONSTRAINT chkDefLeave DEFAULT ‘PL’
 Predefined rule that may be bound to the user- )
defined data type
 Predefined default value that may be bound to the
user-defined data type
You can create user-defined data types by using the
CREATE TYPE statement. The syntax of the CREATE
TYPE statement is:
CREATE TYPE [ schema_name. ] type_name
{ FROM base_type [ ( precision [ ,
scale ] ) ] [ NULL | NOT NULL ] }
[ ; ]
where,
schema_name specifies the name of the schema to
which the alias data type or the user defined data type
belongs.
type_name specifies the name of the alias data type or
the user-defined data type.
base_type specifies SQL Server supplied data type on
which the alias data type is based.
[ schema_name_2 ].| database_name .
[ schema_name_2 ].| schema_name_2. ]
object_name
}
where,
schema_name_1 specifies the name of the schema in
which the synonym is created.
synonym_name is the name of the synonym to be
created.
server_name is the name of the server on which the
base object is located.
database_name is the name of the database in which
the base object is located.
schema_name_2 is the name of the schema of the base
object.
object_name is the name of the base object that the
synonym refers to.
To create a synonym for the Department table in the
Creating Synonyms AdventureWorks database, you can use the following
Consider a scenario. The Department table belongs to the query:
HumanResources schema of the AdventureWorks CREATE SYNONYM pro_dep
database. Whenever you need to refer to the Department for
table in any SQL query, the table name might precede with Adventureworks.HumanResources.Departme
the name of the schema and database. This is a tedious nt
task. To retrieve all the records from the Department table, You can retrieve the data from the table by using
you can use the following query: synonyms, as shown in the following query:
SELECT * FROM SELECT * from pro_dep
AdventureWorks.HumanResources.Departme The following figure displays the output of the preceding
nt query.
However, if the location of the Department table is
changed from AdventureWorks to NarrowFabrics, you
need to reflect this change, as shown in the following
query:
SELECT * FROM
NarrowFabrics.HumanResources.Departmen
t
If there are many such queries used in your application,
you need to make changes in each query manually. It is a
time-consuming task and may lead to errors if you forget The Output Derived After Using the Synonym Object
to make changes in few queries. To protect your If you make modifications in the definition of the object
application code from such changes, SQL Server provides referenced by a synonym, you need to drop that synonym
the concept of synonym. SQL Server provides the concept and create a new one. For example, the database of the
of synonym that is a single-part alias name for a database Department table is changed from AdventureWorks to
object with a name, which has multiple parts. NarrowFabrics. You first need to drop the synonym and
Synonyms provides a layer of abstraction that protects then recreate the same. You can drop a synonym by using
SQL statements from changes made to the database object. the DROP SYNONYM statement. The syntax of the
You can create a synonym for a database object by using DROP SYNONYM statement is:
the CREATE SYNONYM statement. DROP SYNONYM synonym_name
The syntax of the CREATE SYNONYM statement is: where, synonym_name is the name of the synonym.
CREATE SYNONYM [ schema_name_1. ] You can drop the pro_dep synonym by using the following
synonym_name FOR <object> statement:
<object> :: = DROP SYNONYM pro_dep
{ Now, to create a synonym for the Department table in the
[ server_name.[ database_name ] . Narrow Fabrics database, you can use the following query:
CREATE SYNONYM pro_dep be large. For example, if you are creating a database for a
for banking application and you know that the transaction
NarrowFabrics.HumanResources.Departmen details will be huge, you can create the transaction tables
t with partitions.
You can retrieve the data from the table by using the
following same query:
SELECT * from pro_dep Partitioning a table is only allowed in the Enterprise
Edition of SQL Server.
The following figure displays the output of the preceding
query. To create a partitioned table, you need to perform the
following tasks:
1. Create a partition function.
2. Create a partition scheme.
3. Create a table by using the partition scheme.
For example, an organization stores the sales data of all
the products for the last 11 years. However, this results in
generation of a large volume of data, which adversely
affects the query performance.
To improve the query performance, the database developer
needs to partition the table based on a condition, as shown
The Output Derived After Using the Synonym Object
in the following figure.

The synonyms are stored in the sys.synonyms catalog


view.

You can use a synonym with SELECT, INSERT,


UPDATE, DELETE, and EXECUTE statements. A synonym
cannot be altered.

Creating a Partitioned Table


When the volume of data in a table increases, it takes time
to query the data. You can partition such tables and store
different parts of the tables in multiple physical locations
based on a range of values for a specific column. This
helps in managing the data and improving the query Partitioning the Table
performance. In the preceding figure, the sales data has been physically
Consider the example of a manufacturing organization. stored in different filegroups, on an yearly basis.
The details of inventory movements are stored in the Consider a scenario of AdventureWorks database that
InventoryIssue table. The table contains a large volume of stores the data of all the employees for the last 11 years.
data. Therefore, the queries take a lot of time to execute, This data includes the personal details of the employees
thereby slowing the report generation process. and their payment rates. Whenever there is a change in the
To improve the query performance, you can partition the payment rate of an employee, it is recorded in a separate
table to divide the data based on a condition and store record. However, this results in generation of a large
different parts of the data in different locations. The volume of data and adversely affects the query
condition can be based on the date of transaction and you performance. To improve the query performance, the
can save the data pertaining to five years at a location. database developer needs to partition the table storing the
After partitioning the table, data can be retrieved directly changes in payment rate.
from a particular partition by mentioning the partition
number in the query.
Creating a Partition Function
A partition function specifies how a table should be
In the preceding example, the partitioned tables were
partitioned. It specifies the range of values in a particular
created after the database had been designed. You can also
column, based on which the table is partitioned. The
create partitioned tables while designing the database and
syntax of the CREATE PARTITION FUNCTION
creating tables. You can plan to create a partitioned table
statement is:
when you know that the data to be stored in the table will
CREATE PARTITION FUNCTION partition the data based on years.
partition_function_name The following statement creates a partition function for the
( input_parameter_type ) same:
AS RANGE [ LEFT | RIGHT ] CREATE PARTITION FUNCTION RateChngDate
FOR VALUES ( [ boundary_value ( datetime )
[ ,...n ] ] ) AS RANGE RIGHT FOR VALUES
where, (‘1996-01-01’, ‘2000-01-01’,
partition_function_name specifies the name of ‘2004-01-01’, ‘2008-01-01’)
the partition function. The preceding statement creates a partition function
input_parameter_type specifies the data type of named RateChngDate. It specifies that the data pertaining
the column used for partitioning. to the changes in the payment rate will be partitioned
boundary_value specifies the boundary values on based on the years.
which a table is to be partitioned. Creating a Partition Scheme
...n specifies the number of boundary_value, which After setting the partition function, you need to create the
should not exceed 999. The number of partitions created is partition scheme. A partition scheme associates a partition
equal to n + 1. function with various filegroups resulting in the physical
LEFT | RIGHT specifies the side of each boundary layout of the data. The syntax of the CREATE
value interval to which the boundary_value [ ,...n ] PARTITION SCHEME statement is:
belongs. Left is the default value. CREATE PARTITION SCHEME
For example, the following statement creates a partition partition_scheme_name
function to partition a table or index into four partitions: AS PARTITION partition_function_name
CREATE PARTITION FUNCTION pfrange(int) [ ALL ] TO ( { file_group_name |
AS RANGE LEFT FOR VALUES (1, 100, [ PRIMARY ] } [ ,...n ] )
1000); where,
In the preceding statement, partition 1 will store the partition_scheme_name specifies the name of the
column value less than or equal to 1. The column value partition scheme.
between 2 to 100 will be stored in partition 2. The column partition_function_name specifies the name of
value between 101 to 1000 will be stored in partition 3. the partition function. Partition functions are linked to the
Partition 4 will store the column value which is greater filegroups specified in the partition scheme.
than 1000. ALL specifies that all partitions map to a single filegroup
The following table lists the values in each partition for provided in the file_group_name.
RANGE LEFT. file_group_name | [ PRIMARY ] [ ,...n]
specifies the list of filegroup names. If [PRIMARY] is
specified, the partition is stored on the primary filegroup.
Partitions are assigned to filegroups starting with partition
1, in the order in which the filegroups are listed in [,...n]. n
is the number of partitions.
The Range LEFT Partition Values Therefore, before creating a partition scheme, you need to
In the preceding statement, if you specify RIGHT instead create filegroups.
of LEFT, partition 1 will store the column value which is
less than 1. The column value between 1 to 99 will be Task 4.4: Creating Filegroups
stored in partition 2. The column value between 100 to
999 will be stored in partition 3. Partition 4 will store the After creating the filegroups, execute the following
column value that is greater than or equal to1000. The statement in the Query Editor window to create the
following table lists the values in each partition for partition scheme:
RANGE RIGHT. CREATE PARTITION SCHEME RateChngDate
AS PARTITION RateChngDate
TO (Old, First, Second, Third,
Fourth) Creating a Table by Using the
Partition Scheme
Creating a Table by Using the Partition
The Range RIGHT Partition Values Scheme
Consider the scenario of AdventureWorks, where you can After you create a partition function and a partition
scheme, you need to create a table that will store the filegroups and files to be associated with the number of
partition records. You can use the following statement to partitions created in the partition function. For each
create the partitioned table: partition in the partition function, a filegroup must be
Create Table EmpPayHistPart defined in the partition scheme. Therefore, the partition
( scheme must be modified before modifying the partition
EmployeeID int, function.
RateChangeDate datetime, You can modify a partition scheme by using the ALTER
Rate money, PARTITION SCHEME statement. The syntax of the
PayFrequency tinyint, ALTER PARTITION SCHEME statement is:
ModifiedDate datetime ALTER PARTITION SCHEME
)ON RateChngDate(RateChangeDate) partition_scheme_name
In the preceding statement, the RateChngDate refers to the NEXT USED [ filegroup_name ] [ ; ]
partition scheme that is applied to the RateChangeDate where,
column. The records entered in the EmpPayHistPart table partition_scheme_name specifies the name of the
will be stored based on the condition specified in the partition scheme to be modified.
partition function. filegroup_name specifies the name of the new
If you want to display records from a partition instead of filegroup. This filegroup will accept a new partition that is
displaying the entire table, you can use the following created by modifying the partition function.
query: For example, the RateChngDate partition scheme created
SELECT * FROM EmpPayHistPart WHERE previously contains five filegroups. You want to create a
$PARTITION.RateChngDate new partition in the existing partitioned table. For this, you
(RateChangeDate) = 2 need to create a new filegroup and a file. After creating a
In the preceding query, $PARTITION.RateChngDate new filegroup named Fifth and a file named File5 inside
(RateChangeDate) function returns the partition number. that filegroup, you can use the following statement to
Therefore, the preceding statement will retrieve records modify the partition scheme:
from partition 2. ALTER PARTITION SCHEME RateChngDate
After partitioning a table, your requirements may change NEXT USED Fifth;
and the volume of data in the partitioned table may shrink In the preceding statement, the Fifth filegroup will receive
or expand. In such a case, you can manage the data in a any additional partition of a partitioned table as a result of
partitioned table by modifying the current structure of the modifying the partition function.
partitioned table. For example, you have partitioned a
Modifying a Partition Function
table into three partitions. As a result, the data in one of
While creating a partition function, you declare the
the partitions has increased many folds since the
boundary values that are used to partition a table in logical
partitioning of the table. To handle such a huge data, you
units. You can modify a partition function by splitting or
need to further divide that partition into two partitions.
merging its boundary values. The ALTER PARTITION
However, if data in any one of the partitions has reduced
FUNCTION statement can be used to split one partition of
significantly, then it is not relevant to assign a separate
a table into two partitions or merge two partitions of a
partition for such low volume of data. Therefore, you can
table into one.
merge this partition with another existing partition and
The syntax of the ALTER PARTITION FUNCTION
reduce the number of partitions in the table from three to
statement is:
two.
ALTER PARTITION FUNCTION
You can modify a partitioned table by:
partition_function_name()
 Modifying a partition scheme and a partition {
function. SPLIT RANGE(boundary_value)
 Assigning a table as a partition to an existing | MERGE RANGE(boundary_value)
partitioned table. }
Modifying a Partition Scheme and a where,
Partition Function partition_function_name specifies the name of
To modify a partitioned table, you need to perform the the partition function to be modified.
following tasks: SPLIT RANGE(boundary_value) adds a new
 Modify a partition scheme. partition to the partition function with the specified
 Modify a partition function. boundary value. The new boundary value must differ from
Modifying a Partition Scheme the existing boundary ranges of the partition function.
While creating a partition scheme, you need to create Based on the specified boundary value, one of the existing
ranges is split into two partitions. Of these two partitions, Existing Partitioned Table
the one where the new boundary value resides is In addition to modifying the partition scheme and partition
considered the new partition. function to make changes in the structure of a partitioned
MERGE RANGE(boundary_value) drops a partition table, you can add a table as a partition to the existing
from the partition function and merges the specified partitioned table.
boundary value with another existing boundary value of Table partitioning is performed to improve the
the partition function. The specified boundary value must performance of a data retrieval operation from large tables.
be an existing boundary value. The filegroup held by the However, insertion and updation of data in partitioned
dropped partition is removed from the partition scheme if tables may be a time-consuming operation. Therefore, to
it is not used by the remaining partitions. The merged insert or update data in a large partitioned table, you need
partition resides in the filegroup held by the boundary to use another table that has the same structure as the
value to which it is merged. partitioned table. The data is inserted or updated in that
For example, the following table lists the values in each another table, which is then added as a partition to the
partition for the RateChngDate partition function. partitioned table.
However, before adding the table as a partition to an
existing partitioned table, you must ensure that:
 The source and target tables must have the same
column structure and order.
 The nullability of the partitioning columns must
The Values in Each Partition for the RateChngDate Partition match.
Function  The computed columns in both the tables must
If you want to add one more partition to the RateChngDate have the same syntax.
partition function, you can use the following statement:  The ROWGUID properties of the tables must be
ALTER PARTITION FUNCTION RateChngDate the same.
()  The boundary values of the source partition must
SPLIT RANGE (‘2002-01-01’); be within the boundary of the target partition.
In the preceding statement, the boundary value,  The source and target tables must have the same
2002-01-01 falls in partition 3. Therefore, this partition FOREIGN KEY constraints.
will be split into two partitions. You can add a table as a partition to an existing partitioned
The following table lists the new values in each partition table by using the ALTER TABLE statement. The syntax
for the RateChngDate partition function. of the ALTER TABLE statement to add a table as a
partition is:
ALTER TABLE Partitioned_Table_Name
SWITCH PARTITION
Partition_Range TO New_Table_Name
where,
The New Values in Each Partition for the RateChngDate Partitioned_Table_Name specifies the name of the
Partition Function partitioned table to which the new table is to be added as a
Similarly, you can use the following statement to merge partition.
two partitions of the RateChngDate partition function: Partition_Range specifies the range of the partition
ALTER PARTITION FUNCTION RateChngDate to which the new table is to be added.
() New_Table_Name specifies the name of the new table
MERGE RANGE (‘2008-01-01’);
to be added as a partition.
The preceding statement merges the partition with
For example, you need to add a new partition, 2012-01-01
boundary value 2008-01-01 with the previous partition
to the RateChngDate partition function so that you can
having boundary value 2004-01-01. Now, the records are
assign a new table to this partition. For this, you need to
stored in the partitions as shown in the following table.
create a new filegroup and a file. In addition, you need to
alter the partition scheme and partition function, and then
create a table and add it as a partition.
To assign a table as a partition to an existing partitioned
table, you need to perform the following tasks:
1. Create a new filegroup and file.
The Records Stored After Merging the Partitions 2. Alter the partition scheme.
Assigning a Table as a Partition to an 3. Alter the partition function.
4. Create the table. You need to modify tables when there is a requirement to
5. Add the table as a partition. add a new column, alter the data type of a column, or add
Creating a New Filegroup and File or remove constraints on the existing columns. For
Create a filegroup, Sixth and a file named File6. example, AdventureWorks stores the leave details of all
Altering the Partition Scheme the employees in the EmployeeLeave table. According to
Execute the following statement in the Query Editor the requirements, you need to add another column named
window to alter the partition scheme: ApprovedBy in the table to store the name of the
ALTER PARTITION SCHEME RateChngDate supervisor who approved the leave of the employee. To
NEXT USED Sixth; implement this change, you can use the ALTER TABLE
statement.
Altering the Partition Function
The syntax of the ALTER TABLE statement is:
Execute the following statement in the Query Editor
ALTER TABLE [ database_name .
window to alter the partition function:
[ schema_name ] .] table_name
ALTER PARTITION FUNCTION RateChngDate
{
()
ALTER COLUMN column_name
SPLIT RANGE (‘2012-01-01’);
{
Creating the Table [ NULL | NOT NULL ]
Execute the following statement in the Query Editor }
window to create a new table to be added to the new | [ WITH { CHECK | NOCHECK } ] [ADD
partition: COLUMN <column_definition>][DROP
Create Table New_EmpPayHistPart COLUMN <column_name>]
( {
EmployeeID int, ADD CONSTRAINT constraint_name
RateChangeDate datetime, constraint_type
Rate money, where,
PayFrequency tinyint, database_name specifies the name of the database in
ModifiedDate datetime which the table is created.
)ON Sixth
schema_name specifies the name of the schema to
Adding the Table as a Partition which the table belongs.
You can use the following statement to add the New_ table_name is the name of the table that is to be
EmpPayHistPart table to the new partition of the altered. If the table is not in the current database, then the
EmpPayHistPart partitioned table: user needs to specify the database name and the schema
ALTER TABLE EmpPayHistPart SWITCH name explicitly.
PARTITION
ALTER COLUMN specifies the name of the altered
6 TO New_EmpPayHistPart
column.
ADD COLUMN specifies the name of the column to be
added.
column_definition specifies the new column
definition.
CHECK | NOCHECK specifies whether the existing data
is to be checked for a newly added constraint or a re-
enabled constraint.
constraint_name specifies the name of the constraint
to be created and must follow the rules for the identifier.
constraint_type specifies the type of the constraint.
The following statement adds a column named
ApprovedBy to the EmployeeLeave table:
USE AdventureWorks
GO
ALTER TABLE
HumanResources.EmployeeLeave
ADD ApprovedBy VARCHAR(30) NOT NULL
GO
Modifying a Table In the preceding statement, the ApprovedBy column is
added that can store string values. ALTER TABLE EmpTable
You can add a computed column to a table. A computed ADD CONSTRAINT chkRegion CHECK(Region
column contains values that are rather calculated than IN (‘South America’, ‘North America’,
inserted. When you define a computed column, you need ‘Middle East Asia’))
to include the expression that calculates the value for each GO
row of the column. The values for the computed columns In the preceding statement, a CHECK constraint is added
are not specified by using the INSERT statements. The on the Region column.
expression for a computed column may refer to the other While modifying a table, you can drop a constraint when it
non-computed columns from the same table. For example, is not required. You can perform this task by altering the
if the Orders table contains the UnitPrice column and the table by using the ALTER TABLE statement. The syntax
OrderQty column, the total cost of each order can be to drop a constraint is:
calculated as UnitPrice * OrderQty, as shown in the ALTER TABLE [ database_name .
following code snippet: [ schema_name ] . | schema_name . ]
ALTER TABLE Orders ADD TotalCost AS table_name DROP CONSTRAINT
UnitPrice * OrderQty constraint_name
The values generated by the expression of the computed where,
column are not stored within the database. Instead, the database_name specifies the name of the database in
values are calculated every time they are required by a which the table is created.
query. Therefore the computed column is virtual. schema_name specifies the name of the schema to
However, you can specify that a computed column be which the table belongs.
persisted. The values of the persisted column are stored in table_name specifies the name of the table that
the table. These values are recalculated whenever there is contains the constraint to be dropped.
a change in any reference column. You can use the constraint_name specifies the name of the constraint
PERSISTED keyword to define the computed column as to be dropped.
persisted, as shown in the following code snippet: The following statement drops the default constraint,
ALTER TABLE Orders ADD TotalCost AS chkDefLeave of the EmployeeLeave table:
UnitPrice * OrderQty PERSISTED USE AdventureWorks
A computed column must be persisted if you are adding a GO
check constraint to it or if the column is marked as NOT ALTER TABLE
NULL. HumanResources.EmployeeLeave DROP
If you need to make the column persisted, it must be CONSTRAINT chkDefLeave
deterministic. This means that the database engine should GO
be able to verify that the column will always produce the In the preceding statement, the chkDefLeave constraint is
same result. The GetDate() function is not deterministic as dropped from the EmployeeLeave table.
its value changes every time the expression is evaluated.
The following statement modifies the Description column
of the HumanResources.Project table: All constraints defined on a table are dropped
USE AdventureWorks automatically when the table is dropped.
GO
ALTER TABLE HumanResources.Project
If the column, you are trying to add a constraint to,
ALTER COLUMN Description varchar(100)
has some data in it, you can use CHECK | NOCHECK to
GO specify whether the existing data needs to be checked with the
In the preceding statement, the size of the description added constraint.
column is increased to varchar(100).
The following statement drops the column named
LeaveStatus from the EmployeeLeave table: Alter statement can be used to drop a column from a
USE AdventureWorks table definition. You cannot delete a row using the alter
GO statement.
ALTER TABLE
HumanResources.EmployeeLeave
DROP COLUMN LeaveStatus Renaming a Table
The following statement adds a constraint called
You can rename a table whenever required. The
chkRegion to the EmpTable table:
sp_rename stored procedure is used to rename the table.
USE AdventureWorks
You can use sp_rename to rename any database object,
GO
such as table, view, stored procedure, or function. The EmployeeVacation table under the same schema contains
syntax of the sp_rename stored procedure is: EmployeeID as its foreign key and is referenced with the
sp_rename old_name, new_name EmployeeID column of the Employee table. Therefore,
where, when you want to delete the Employee table, you first
old_name is the current name of the object. need to delete the EmployeeVacation table.
new_name is the new name of the object. The following statement deletes the EmployeeVacation
For example, the following statement renames the table:
EmployeeLeave table: USE AdventureWorks
USE AdventureWorks DROP TABLE
GO HumanResources.EmployeeVacation
sp_rename
[HumanResources.EmployeeLeave], You can also delete a table by right-clicking the Tables
[HumanResources.EmployeeVacation] folder under a specific database in the Object Explorer
GO window, and then selecting the Delete option from the pop-up
menu.
You can also rename a table by right-clicking the
Tables folder under a specific database in the Object
Explorer window, and then selecting the Rename option from
the pop-up menu.

After a table is created, you may need to view the


details of the table. Details of the table include the column
names and the constraints. For this purpose, you can use the
sp_help statement. The syntax of the sp_help statement is:
sp_help [table name]

Dropping a Table
At times, when a table is not required, you need to delete
it. A table can be deleted along with all the associated
database objects such as its index, triggers, constraints,
and permissions. You can delete a table by using the
DROP TABLE statement. The syntax of the DROP
TABLE statement is: Activity 4.1: Managing Tables
DROP TABLE [ database_name .
[ schema_name ] .] table_name
where, Activity 4.2: Creating a
database_name specifies the name of the database
where the table is created. Partitioned Table
schema_name specifies the name of the schema to
which the table belongs.
table_name specifies the name of the table that needs Summary
to be dropped.
In this chapter, you learned that:
When a table is deleted, any other database object
referenced by the table needs to be deleted explicitly. This  A database is a repository of information that
should be done before deleting the table. This is because if contains data in an organized way.
violations occur in the rule of referential integrity while  The master database records all the server-
deleting a table, then an error occurs that restricts you specific configuration information, including
from deleting the table. Therefore, if your table is authorized users, databases, system configuration
referenced, you must delete the referenced table or the settings, and remote servers.
referenced constraint, and then delete the table.  The tempdb database is a temporary database that
holds all the temporary tables and stored
For example, the Employee table in the HumanResource
procedures.
schema contains EmployeeID as its primary key. The
 The model database acts as a template or a  A check constraint enforces domain integrity by
prototype for new databases. restricting the values to be inserted in a column.
 The msdb database supports the SQL Server The IN, LIKE, and BETWEEN keywords are
Agent. SQL Server Agent includes features that used to define the check constraint.
schedule periodic activities of SQL Server.  A default constraint can be used to assign a
 The Resource database is a read-only database constant value to a column, and the user need not
that contains all the system objects that are insert values for such a column.
included with SQL Server.  A rule provides a mechanism for enforcing
 The user-defined databases are created by the domain integrity for columns or user defined data
users to store data for client/server applications. types.
 A database consists of the following types of  User-defined data types are custom data types
files: defined by the users with a custom name.
 Primary data file  A partitioned table is created to manage the data
 Secondary data file and improve the query performance.
 Transaction log file  You can modify a partition table by:
 A database must consist of a primary data file and  Modifying a partition scheme and partition
one transaction log file. function.
 The CREATE DATABASE statement is used to  Assigning a table as a partition to an
create a database, which also includes existing partitioned table.
determining the name of the database, the size of  You can modify a partition function by splitting
the database, and the files used to store data in the or merging its boundary values.
database.  Before adding the table as a partition to an
 The DROP DATABASE statement is used to existing partitioned table, you must ensure that:
delete a database.  The source and target tables must have the
 Tables are used to store data. same column structure and order.
 The CREATE TABLE statement is used to create  The nullability of the partitioning columns
a table. must match.
 Data integrity is enforced to keep the data in a  The computed columns in both the tables
database accurate, consistent, and reliable. It is must have the same syntax.
broadly classified into the following categories:  The ROWGUID properties of the tables
 Entity integrity: Ensures that each row can must be the same.
be uniquely identified by an attribute called  The boundary values of the source partition
the primary key. must be within the boundary of target
 Domain integrity: Ensures that only a partition.
valid range of values is allowed to be stored  The source and target tables must have the
in a column. same FOREIGN KEY constraints.
 Referential integrity: Ensures that the  The ALTER TABLE statement is used to modify
value of the foreign key match the value of a table.
the corresponding primary key.  The DROP TABLE statement is used to delete a
 User-defined integrity: Refers to a set of table.
rules specified by a user, which do not
belong to the entity, domain, and referential
integrity categories. Reference Reading
 Constraints define rules that must be followed to
maintain consistency and correctness of data. Managing Databases
 A primary key constraint is defined on a column
or a set of columns whose values uniquely Reference Reading: Books Reference Reading: URLs
identify rows in a table. Microsoft® SQL Server® http://msdn.microsoft.com/
 The unique constraint is used to enforce 2012 Pocket Consultant by en-us/library/cc645585.aspx
uniqueness on non-primary key columns. William R. Stanek http://msdn.microsoft.com/
 A foreign key constraint associates one or more Learning SQL by Alan en-us/library/
columns (the foreign key) of a table with an Beaulieu ms179422.aspx
identical set of columns (a primary key column in
another table) on which a primary key constraint
has been defined.
Managing Tables
Reference Reading: Books Reference Reading: URLs
SQL Server 2012 T-SQL http://msdn.microsoft.com/
Recipes: A Problem- en-us/library/
Solution Approach by Jason ms174979.aspx
Dewson http://msdn.microsoft.com/
en-us/library/
ms190273.aspx
A restriction placed on a value in a database used to
increase data integrity.
Cross Join
Glossary
A A join that combines each row from one table with each
Alias row of the other table.
A temporary intra-query substitute for a table name or D
column name. Data
APPLY Operator
The operator used to combine the result set of two queries Recorded facts pertaining to entities.
such that for each row of the first query, the second query Data Definition Language (DDL)
is evaluated to determine if any rows are returned.
Arithmetic Operators A language used to define the internal schema and
The operators used to perform mathematical operations, conceptual schema in a database.
such as addition, subtraction, division, and multiplication, Data Integrity
on numeric columns or on numeric constants. The concept of maintaining consistency and completeness
Attribute of data.
Data Manipulation Language (DML)
A column in a table. A language used to manipulate data using INSERT,
B UPDATE, and DELETE statements.
Batch Data Warehouse
An application with a computer database that collects,
A group of SQL statements submitted together to the SQL integrates, and stores an organization's data with the aim
Server for execution. of producing accurate and timely management of
Business Tier information and support for analysis techniques.
Database
The middle tier in a three-tier architecture where the A collection of logically associated or related data.
business logic is implemented. Default
C A value assigned to data when no value is supplied.
Cartesian Product Dialog
A bi-directional communication between two services.
A binary operation resulting in the combination of all rows Domain
of one table with all rows of another table. The set of all possible values that a column value can
Client Tier have.
E
The tier that handles the User Interface (UI) in a client- Equi Join
server architecture. A query that combines tables with the help of a foreign
Common Language Runtime (CLR) key and displays all the columns from both the tables. Is
similar to an inner join.
The core runtime environment in the Microsoft .NET Extensible Markup Language (XML)
Framework on which the applications run. A universal language used to generically identify data that
Common Table Expression (CTE) will be shared.
F
A temporary named result set defined within the execution Filegroup
scope of a single SQL statement. A collection of database files grouped together for
Concatenation Operator allocation and administration purposes.
I
An operator that is used to combine string expressions. Inner Query
Constant A subquery that is contained in another query. The result
of the inner query is used an input for the condition
An unvarying value used in a query. specified in the outer query.
Constraint J
Join
An operation used to combine related rows from two A design of the database typically using an entity
tables into one table based on a logical comparison of relationship diagram.
column values. Self Join
L A join condition where a table is joined with itself.
Literals Server Tier
The string values, enclosed in single quotes and added to The part that implements the application logic and
the SELECT statement, are printed in a separate column as manages the input data based on the business rules in a
they are written in the SELECT list. client server architecture.
Local Variables Structured Query Language (SQL)
Variables declared in a batch that can be used in any A language for defining the structure and processing of a
statement inside the batch. relational database.
M Subquery
Managed Code The inner query within the outer (main) query; usually one
The code written in any of the .Net supported languages SELECT query within another SELECT query.
that runs within the scope of the SQL CLR. U
Managed Database Object Unmanaged Code
A database object that can be created using CLR The code that is developed without considering the
integration in any of the .NET-supported languages and conventions and requirements of the common language
then can be embedded in the database. runtime.

N
.NET Framework
A software development platform focused on rapid
application development, platform independence, and
network transparency.
NULL
A value given to a data item when the result is unknown.
O
Outer Join
An join operation that displays the result set containing all
the rows from one table and the matching rows from
another table.
Q
Query
An SQL instruction used to retrieve data from one or more
tables or views. Queries begin with the SQL keyword
SELECT.
Queue
A container used to stores messages.
R
Referential Integrity
The property that guarantees the values from one column
depending on the values from another column are present
in the other column.
Result Set
Output of an SQL statement.
Row
A horizontal slice of a table. A row is also known as a
'tuple' and at times is called a 'record'. However, a record
usually refers to a physical representation of data and a
row refers to a logical representation.
S
Schema
stored procedures Stored Procedures
(simple
statements).
Certification Mapping Modify data by 5 Manipulating
The content in this course partially covers the objectives using INSERT, Data by Using
of Microsoft Exam 70-461 (Microsoft SQL Server 2012, UPDATE, and DML Statements
Database Development). DELETE
The following table lists the relevant exam objectives and statements.
the corresponding chapter numbers and section names. Combine datasets. 5 Manipulating
Data by Using
DML Statements
Exam Objective Chapter Section
Work with 7 Implementing
Create Database
functions. Functions
Objects (24%)
Troubleshoot and
Create and alter 4 Managing Tables
Optimize (25%)
tables using T-
SQL syntax(simple Optimize Queries. 9, 6, 7 Optimizing
statements). Performance,
Creating and
Create and alter 6 Creating and
Managing
views(simple Managing Views
Indexes,
statements).
Implementing
Create and modify 4 Managing Tables
Stored Procedures
constraints(simple
Manage 8 Implementing
statements).
transactions. Transactions
Create and alter 8 Implementing
Implement error 7 Implementing
DML triggers. Triggers
handling. Batches
Working with
Evaluate the use of 7 Implementing
Data (27%)
row-based Stored Procedures
Query data by 2 Retrieving Data
operations vs set-
using SELECT
based operations.
statements.
Implement 2, 3 Querying Data by Certification Mapping
subqueries Using Subqueries,
Managing Result
Sets, Summarizing To prepare for the exam, you need to refer to the
and Grouping Student Guide as well as the Activity Book.
Data
Implement data 2, 4, 5 Retrieving Data,
types. Managing Tables,
Manipulating
Data by Using
DML Statements
Implement 2, 4 Summarizing and
aggregate queries. Grouping Data,
Using Functions
to Customize the
Result Set,
Managing Tables
Query and 5 Manipulating
Manage XML XML Data
data.
Modify Data
(24%)
Create and alter 7 Implementing

You might also like