0% found this document useful (0 votes)
2 views94 pages

SQL 3

The document provides an overview of SQL basics, including its components such as DDL, DML, DQL, DCL, and TCL, as well as the differences between relational and NoSQL databases. It outlines the structure and functionality of databases and introduces key concepts related to database management systems. Additionally, it emphasizes the importance of SQL as a standard language for accessing and manipulating relational databases.

Uploaded by

and311003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views94 pages

SQL 3

The document provides an overview of SQL basics, including its components such as DDL, DML, DQL, DCL, and TCL, as well as the differences between relational and NoSQL databases. It outlines the structure and functionality of databases and introduces key concepts related to database management systems. Additionally, it emphasizes the importance of SQL as a standard language for accessing and manipulating relational databases.

Uploaded by

and311003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 94

SQL Basics

Introduction to Querying.
DDL/DQL/DML/DCL/TCL

Zlata Chernysh
Junior Software Engineer
Tetiana Lampiha
Software Engineer
CONFIDENTIAL | © 2021 EPAM Systems, Inc.
Recommendations

1 M U T E YO U R M I C

2 R A I S E H A N D A S K Q U E ST IO NS

3 D U R AT I O N : 3 H O U R S

4 C O FFEE BRE AK: 15 MINUT ES

5 Q & A A FT E R E A C H MO D ULE

6 T E A M S Q U E S TIO N S C H A NN EL

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 2


Z L ATA C H E R N Y S H

Junior S of tw are Engineer

• EPAM BI Lab 2020 Participant


• EPAM Career Path: Production Project -> DWBI Software
Engineer

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 3


TETIANA LAMPIHA

S of tw are Engineer

• EPAM BI Lab 2019 Participant


• EPAM Career Path: Production Project -> DWBI Software
Engineer

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 4


AGENDA

1 D AT A B A S E . D B M S . R E L AT I O N A L M O D E L

2 SQL. DDL, DML, DQL, DCL, TCL

3 D AT A D E F I N I T I O N L A N G U A G E ( D D L )

4 D AT A M A N I P U L AT I O N L A N G U A G E ( D M L )

5 S E L E C T S T AT E M E N T ( D Q L )

6 ALIASES

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


DATABASE. DBMS. Relational Model

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


Database and DBMS

A Database is an organized collection of structured information, or data, typically stored Users


electronically in a computer system. It is organized so that it can be easily accessed,
managed and updated.
Software used to manage and control the database is called Database Management
System (DBMS). It was created to handle/change/retrieve/etc data in a database. DBMS Database
generally manipulates the data itself, the data format, field names, record structure etc. It Application
also defines rules to validate and manipulate this data.
DBMS interacts with end-users, other applications, and the database itself to capture and
analyze data.
Database Management System
A general-purpose DBMS allows the definition, creation, querying, update, and (DBMS)
administration of databases.

Database

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


Conceptual Data Models: Hierarchical and Network

Hierarchical Data Model: Network Data Model:


• It organizes data in the tree-like structure. • It is the advance version of the hierarchical data model.
• It contains nodes which are connected by branches. • To organize data it uses directed graphs instead of the tree-
structure. In this child can have more than one parent.
• The topmost node is called the root node.
• It uses the concept of the two data structures i.e. Records and
• If there are multiple nodes appear at the top level, then these can
Sets.
be called as root segments.
• Each node has exactly one parent.
• One parent may have many child.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 8


Relational Database

A relational database (RDB) is a collective set of multiple data sets organized by tables, records and columns. RDBs establish a well-defined
relationship between database tables. Tables are uniquely identified by their names. They communicate and share information, which facilitates
data searchability, organization and reporting.
RDBs use Structured Query Language (SQL)
Briefly, the features of relational databases:
• Data is stored in tables consisting of columns ("attributes") and rows ("records", "tuples").
• There is exactly one value at the intersection of each column and row.
• Each column has a name, and all values ​in one column are of the same type.
• Database queries return results in the form of tables, which can also act as a query object.
• Rows in a relational database are unordered: the ordering is performed at the moment of generating a response to a query.
• The generally accepted language standard for working with relational databases is the SQL language.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 9


Relational Model

A D V A N TA G E S D I S A D V A N TA G E S

• simplicity and accessibility for user understanding. Only "table“ is • subject area not always can be represented in the form of "tables";
used as an information structure;
• as a result of logical design there is a set of "tables". This leads to
• strict design rules are based on the mathematical theory; difficulties in understanding the data structure;
• complete independence of data. When the relational database is • The database takes up a relatively large amount of external
changed, appropriate changes in the applications are minimal. memory;
• relatively low speed of data access.

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


Relational Model Concepts

The Relational Model (RM) for DBMS is an approach to work with data based on the relation algebra (tuples, sets, relations, a ttributes).
• Tables. In the Relational model the relations are saved in the table format. A table consists of rows and columns. Rows represent records and
columns represent attributes.
• Tuple – a single row of a table, which contains a single record.
• Attribute - each column in a Table. Attributes are the properties which define a relation.
• Attribute domain. Every attribute has some pre-defined value and scope which is known as attribute domain
• Relation Schema - A relation schema represents the name of the relation with its attributes.
• Degree - total number of attributes in the relation.
• Cardinality - total number of rows present in the table.
• Relation instance – Relation instance is a finite set of tuples in the relation. Relation instances never have duplicate tuples.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 11


Relational Model Concepts

Supplier Address Product


Peter & partners Green Street Spoons

Peter & partners Green Street Forks

Roy brothers 34 avenue Plates

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


CONFIDENTIAL 12
RDBMS

• Databases using relational model are called Relational Databases. They are managed by Relational DBMSs => RDBMSs.
• RDBMS stands for Relational Database Management System.
• RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle, MySQL etc.
• The data in RDBMS is stored in database objects called tables. A table is a collection of related data entries and it consist s of columns and rows.
• Every table is broken up into smaller entities called fields. A field is a column in a table that is designed to maintain specific information about
every record in the table.
• A record, also called a row, is each individual entry that exists in a table. A record is a horizontal entity in a table.
• A column is a vertical entity in a table that contains all information associated with a specific field in a table.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 13


NoSQL
NoSQL is a class of Database Management Systems (DBMS) that does not use SQL and does not follow Relation Model.
NoSQL – no strict following of ACID.
No SQL -> Not only SQL (OR) Non-relational.
It is an alternative solution, was created to SUPPORT SQL.
A NoSQL database provides a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in
relational databases.
NoSQL databases are increasingly used in big data and real-time web applications.
The data structures used by NoSQL databases (e.g. key-value, wide-column, graph, or document) are different from those used by default in
Relational Databases, making some operations faster in NoSQL.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 14


NoSQL Database Examples

• Document model: These NoSQL databases replace the familiar rows and columns structure with a document storage model. Each document is
structured, frequently using the JavaScript Object Notation (JSON) model. The document data model is associated with object -oriented
programming where each document is an object (use cases: catalog).
• Graph model: This class of databases uses structures like data modes, edges and properties, making it easier to model relationships between
entities in an application (use cases: recommendation engines).
• Key-value model: In this NoSQL database model, a key is required to retrieve and update data. The key-value data model is very simple and
therefore scales well. However, this simplicity and scalability come at the cost of query complexity (use cases: shopping cart).
• Wide-column model: Wide-column stores use the typical tables, columns, and rows, but unlike relational databases (RDBs), columnal formatting
and names can vary from row to row inside the same table. And each column is stored separately on disk.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 15


SQL

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


What is SQL?

Structured Query Language (SQL) is a standard computer language for accessing and manipulating databases in
Relational Database Management System.
This language is used to query, update and manage relational databases, create a database schema and modify it,
control access to database etc. SQL statements are used to retrieve and update data in a database.

N O T E : S Q L I S N E I T H E R A D A TA B A S E M A N A G E M E N T S Y S T E M N O R A S TA N D A L O N E S O F T W A R E P R O D U C T.

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


SQL is a Standard - BUT....

SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO)
in 1987.
Although SQL is an ANSI/ISO standard, there are different versions of the SQL language.
However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE)
in a similar manner.

NOTE: M O S T O F T H E S Q L D A TA B A S E P R O G R A M S A L S O H A V E T H E I R O W N P R O P R I E TA R Y
E X T E N S I O N S I N A D D I T I O N T O T H E S Q L S TA N D A R D !

Examples of SQL Procedural Extensions:


• T-SQL (Transact-SQL) – used in Microsoft SQL Server;
• PL/SQL (Procedural Language/Structured Query Language) - used in Oracle Database;
• PL/pgSQL (Procedural Language/PostgreSQL) - used in PostgreSQL.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 18


What Can SQL Do?

• SQL can execute queries against a database


• SQL can retrieve data from a database
• SQL can insert records in a database Data
Manipulation
• SQL can update records in a database Language
Data (DML)
• SQL can delete records from a database Data Control
Definition
Language
• SQL can create new databases Language
(DCL)
(DDL)
• SQL can create new tables in a database
• SQL can create stored procedures in a database
Transaction
Data query
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views
language
(DQL)
SQL Control
Language
(TCL)

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 19


SQL Components

Data
Manipulation
Language
Data (DML) Data Control
Definition
Language
Language
(DCL)
(DDL)

Transaction
Data query
language
(DQL)
SQL Control
Language
(TCL)

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 20


SQL Components

• Data Query Language (DQL) - SQL commands used to perform


queries on the data within schema objects. The purpose of DQL
commands is to get the schema relation based on the query
passed to it.
Example: SELECT

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 21


SQL Components

• Data Query Language (DQL) - SQL commands used to perform


queries on the data within schema objects. The purpose of DQL
commands is to get the schema relation based on the query
passed to it.
Example: SELECT

• Data Definition Language (DDL) – SQL commands used to create


and modify database objects such as tables, indexes, and users.
Example: CREATE, ALTER, DROP, TRUNCATE

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 22


SQL Components

• Data Query Language (DQL) - SQL commands used to perform


queries on the data within schema objects. The purpose of DQL
commands is to get the schema relation based on the query
passed to it.
Example: SELECT

• Data Definition Language (DDL) – SQL commands used to create


and modify database objects such as tables, indexes, and users.
Example: CREATE, ALTER, DROP, TRUNCATE

• Data Manipulation Language (DML) - SQL commands used to


manipulate/modify data presented in the database.
Example: INSERT, UPDATE, DELETE

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 23


SQL Components

• Data Query Language (DQL) - SQL commands used to perform • Data Control Language (DCL) – SQL commands used to control
queries on the data within schema objects. The purpose of DQL access to data stored in a database (rights, permissions and other
commands is to get the schema relation based on the query controls).
passed to it.
Example: GRANT, REVOKE
Example: SELECT

• Data Definition Language (DDL) – SQL commands used to create


and modify database objects such as tables, indexes, and users.
Example: CREATE, ALTER, DROP, TRUNCATE

• Data Manipulation Language (DML) - SQL commands used to


manipulate/modify data presented in the database.
Example: INSERT, UPDATE, DELETE

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 24


SQL Components

• Data Query Language (DQL) - SQL commands used to perform • Data Control Language (DCL) – SQL commands used to control
queries on the data within schema objects. The purpose of DQL access to data stored in a database (rights, permissions and other
commands is to get the schema relation based on the query controls).
passed to it.
Example: GRANT, REVOKE
Example: SELECT

• Transaction Manipulation Language (TCL) - SQL commands used


• Data Definition Language (DDL) – SQL commands used to create to deal with transactions within a database
and modify database objects such as tables, indexes, and users.
Example: BEGIN TRANSACTION, COMMIT TRANSACTION
Example: CREATE, ALTER, DROP, TRUNCATE

• Data Manipulation Language (DML) - SQL commands used to


manipulate/modify data presented in the database.
Example: INSERT, UPDATE, DELETE

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 25


SQL Syntax

NOTE: The syntax of the SQL programming language is defined and maintained by standard. Despite the existence of the standard, SQL
code is not completely portable among different database systems without adjustments.
Most of the actions you need to perform on a database are done with SQL statements.
Some database systems require a semicolon at the end of each SQL statement. Semicolon is the standard way to separate each SQL
statement in database systems that allow more than one SQL statement to be executed in the same call to the server.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 26


SQL Syntax. Language elements

The SQL language is subdivided into several language elements, including:


Keywords are words that are defined in SQL language. They are either reserved (e.g. SELECT, COUNT and YEAR), or non-reserved
(e.g. DOMAIN and KEY).
NOTE:SQL keywords are NOT case sensitive: select is the same as SELECT.
Identifiers are names of database objects, like tables, columns and schemas. An identifier may not be equal to a reserved keyword,
unless it is a delimited identifier. Delimited identifiers means identifiers enclosed in double quotation marks. They can contain characters
normally not supported in SQL identifiers, and they can be identical to a reserved word, e.g. YEAR.
Clauses, which are constituent components of statements and queries. (In some cases they are optional).

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 27


DATA DEFINITION LANGUAGE
(DDL)

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


CREATE DATABASE Statement

The CREATE DATABASE statement is used to create a new SQL Tip: Make sure you have admin privilege before creating any
database. database. Once a database is created, you can check it in the list
of databases with the following SQL command:
Syntax:
CREATE DATABASE databasename; SELECT * FROM SYS.databases;

The following SQL statement creates a database called "testDB":


CREATE DATABASE testDB;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 29


DROP Database Statement

The DROP DATABASE statement is used to drop an existing SQL The following SQL statement drops the existing database "testDB":
database.
DROP DATABASE testDB;
Syntax:
Tip: Make sure you have admin privilege before dropping any
DROP DATABASE databasename; database. Once a database is dropped, you can check it in the list of
databases with the following SQL command: SELECT * FROM
Note: Be careful before dropping a database. Deleting a database will
SYS.databases;
result in loss of complete information stored in the database!

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 30


BACKUP DATABASE Statement

The BACKUP DATABASE statement is used in SQL Server to create a full The following SQL statement creates a full back up of the existing
back up of an existing SQL database. database "testDB" to the D disk:

Syntax: BACKUP DATABASE testDB


TO DISK = 'D:\backups\testDB.bak';
BACKUP DATABASE databasename
TO DISK = 'filepath';

A differential back up only backs up the parts of the database that The following SQL statement creates a differential back up of the
have changed since the last full database backup. database "testDB":

Syntax: BACKUP DATABASE testDB


TO DISK = 'D:\backups\testDB.bak'
BACKUP DATABASE databasename
WITH DIFFERENTIAL;
TO DISK = 'filepath'
WITH DIFFERENTIAL;

TIPS!
ALWAYS BACK UP THE DATABASE TO A DIFFERENT DRIVE THAN THE ACTUAL DATABASE. THEN, IF YOU GET A DISK CRASH, YOU
WILL NOT LOSE YOUR BACKUP FILE ALONG WITH THE DATABASE.
A DIFFERENTIAL BACK UP REDUCES THE BACK UP TIME (SINCE ONLY THE CHANGES ARE BACKED UP).
CONFIDENTIAL | © 2021 EPAM Systems, Inc. 31
Schema in SQL

A Schema in SQL is a collection of database objects associated with Advantages of using Schema
a database.
You can apply security permissions for separating and protecting
The username of a database is called a Schema owner (owner of database objects based on user access rights.
logically grouped structures of data).
A logical group of database objects can be managed within a database.
Schema always belong to a single database whereas a database can Schemas play an important role in allowing the database objects to be
have single or multiple schemas. organized into these logical groups.

The schema also helps in situations where the database object name is
the same. But these objects fall under different logical groups.

A single schema can be used in multiple databases.

The schema also helps in adding security.

It helps in manipulating and accessing the objects which otherwise is a


complex method.

You can also transfer the ownership of several schemas.

The objects created in the database can be moved among schemas.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 32


CREATE, ALTER, DROP SCHEMA Statements

The CREATE SCHEMA Statement is used to create a new SQL schema The following SQL statement creates a schema called "Business" that
under an existing database. is owned by user "dbo":

CREATE SCHEMA [schema_name] CREATE SCHEMA Business


[AUTHORIZATION owner_name]; AUTHORIZATION dbo;

The schema in a database can be altered by using the ALTER SCHEMA The following example modifies the Schema "HumanResources" by
statement. This statement is specifically used to rename a schema. The transferring the table Address from schema" Person" into the
new owner must be a pre-existing user. "HumanResources" schema.
ALTER SCHEMA schema_name ALTER SCHEMA HumanResources
TRANSFER [ <entity_type> :: ] securable_name ; TRANSFER Person.Address;

In order to drop schema we use the following syntax:


DROP SCHEMA <schema name>

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 33


CREATE TABLE Statement

The CREATE TABLE statement is used to create a new table in a A copy of an existing table can also be created using CREATE TABLE.
database.
The new table gets the same column definitions. All columns or
Syntax: specific columns can be selected.
CREATE TABLE table_name ( If you create a new table using an existing table, the new table will be
column1 datatype, filled with the existing values from the old table.
column2 datatype,
Syntax:
column3 datatype,
.... CREATE TABLE new_table_name AS
); SELECT column1, column2,...
FROM existing_table_name
The column parameters specify the names of the columns of the
WHERE ....;
table.
The datatype parameter specifies the type of data the column can
hold (e.g. varchar, integer, date, etc.).

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 34


CREATE TABLE Example
The following example creates a table called "Persons" that contains The empty "Persons" table will now look like this:
five columns: PersonID, LastName, FirstName, Address, and City:
CREATE TABLE Persons ( PersonID LastName FirstName Address City
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Tip: The empty "Persons" table can now be filled with data with the
Address varchar(255),
SQL INSERT INTO statement.
City varchar(255)
);
The PersonID column is of type int and will hold an integer.
The LastName, FirstName, Address, and City columns are of type
varchar and will hold characters, and the maximum length for these
fields is 255 characters.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 35


DROP TABLE and TRUNCATE TABLE Statements

The DROP TABLE statement is used to drop an existing table in a The TRUNCATE TABLE statement is used to delete the data
database. inside a table, but not the table itself.
Syntax: Syntax:
DROP TABLE table_name; TRUNCATE TABLE table_name;
Note: Be careful before dropping a table. Deleting a table will
result in loss of complete information stored in the table!
The following SQL statement drops the existing table
“TestTable":
DROP TABLE TestTable;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 36


DROP TABLE and TRUNCATE TABLE Statements Difference

DROP TABLE TRUNCATE TABLE


The DROP command is used to remove table definition and its Whereas the TRUNCATE command is used to delete all the rows from
contents. the table.
In the DROP command, table space is freed from memory. While the TRUNCATE command does not free the table space from
memory.
DROP is a DDL(Data Definition Language) command.
Whereas the TRUNCATE is also a DDL(Data Definition Language)
In the DROP command, view of table does not exist.
command.
In the DROP command, integrity constraints will be removed.
While in this command, view of table exist.
In the DROP command, undo space is not used.
While in this command, integrity constraints will not be removed.
The DROP command is quick to perform but gives rise to
While in this command, undo space is used but less than DELETE.
complications.
While this command is faster than DROP.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 37


ALTER TABLE Statement

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing table.
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype;
To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name;
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
ALTER COLUMN column_name datatype;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 38


ALTER TABLE Example
Now we want to add a column named "DateOfBirth" in the "Persons" Notice that the "DateOfBirth" column is now of type datetime.
table. We use the following SQL statement:
Next, we want to delete the column named "DateOfBirth" in the
ALTER TABLE Persons "Persons" table. We use the following SQL statement:
ADD DateOfBirth date; ALTER TABLE Persons
Notice that the new column, "DateOfBirth", is of type date and is DROP COLUMN DateOfBirth;
going to hold a date. The data type specifies what type of data the
The "Persons" table will now look like this:
column can hold.
The "Persons" table will now look like this:

PersonID LastName FirstName Address City DateOfBirth PersonID LastName FirstName Address City

Now we want to change the data type of the column named


"DateOfBirth" in the "Persons" table. We use the following SQL
statement:
ALTER TABLE Persons
ALTER COLUMN DateOfBirth datetime;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 39


DATA MANIPULATION LANGUAGE
(DML)

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 40


Data Manipulation Language

Data Manipulation Language (DML) - SQL commands used to manipulate/modify data presented in the database.
Example: INSERT, UPDATE, DELETE

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 41


INSERT INTO Statement

The INSERT INTO statement is used to insert new records in a table.


Syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Or
If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make
sure the order of the values is in the same order as the columns in the table. The INSERT INTO syntax would be as follows:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);

If a table contains an auto-increment field it will be generated automatically when a new record is inserted into the table.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 42


INSERT INTO Statement Example

The following SQL statement inserts a new record in the “Customers” table:

INSERT INTO Customers (CustomerID, CompanyName, ContactName, Address, City, PostalCode, Country)
VALUES (12345, 'Cardinal', 'Tom Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 43


INSERT INTO Statement Example

The following SQL statement inserts a new record in the “Customers” table:

INSERT INTO Customers (CustomerID, CompanyName, ContactName, Address, City, PostalCode, Country)
VALUES (12345, 'Cardinal', 'Tom Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');

Insert Data Only in Specified Columns


It is also possible to only insert data in specific columns. The following SQL statement will insert a new record, but only insert data in the
"Company", "City", and "Country" columns:

INSERT INTO Customers (CustomerID, CompanyName, City, Country)


VALUES (123, 'Cardinal', 'Stavanger', 'Norway');

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 44


UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.


Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Note: Be careful when updating records in a table! Notice the WHERE clause in the UPDATE statement. The WHERE clause specifies
which record(s) that should be updated. If you omit the WHERE clause, all records in the table will be updated!

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 45


UPDATE Statement Example

The following SQL statement updates the first customer (id = ‘123’) with a new contact person and a new city.

UPDATE Customers
SET ContactName = 'Alfred Schmidt’,
City = 'Frankfurt’
WHERE CustomerID = '123';

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 46


UPDATE Statement Example. UPDATE M ultiple Records

It is the WHERE clause that determines how many records will be updated.
The following SQL statement will update the ContactName to "Juan" for all records where country is "Mexico":

UPDATE Customers
SET ContactName = 'Juan’
WHERE Country = 'Mexico';

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 47


UPDATE Statement Example

WARNING! Be careful when updating records. If you omit the WHERE clause, ALL records will be updated!

UPDATE Customers
SET ContactName = 'Juan'

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 48


DELETE Statement

The DELETE statement is used to delete existing records in a table.


Syntax:
DELETE FROM table_name
WHERE condition;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 49


DELETE Statement

The DELETE statement is used to delete existing records in a table.


Syntax:
DELETE FROM table_name
WHERE condition;

Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE statement. The WHERE clause specifies which
record(s) should be deleted. If you omit the WHERE clause, all records in the table will be deleted!

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 50


DELETE Statement Example

The following SQL statement deletes customers which have ContactName " Tom Erichsen " from the "Customers" table:

DELETE FROM Customers


WHERE ContactName = 'Tom Erichsen';

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 51


DELETE Statement Example

It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and
indexes will be intact:

Syntax:
DELETE FROM table_name;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 52


DELETE Statement Example

It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be
intact:

Syntax:
DELETE FROM table_name;

The following SQL statement deletes all rows in the "Customers" table, without deleting the table:

DELETE FROM Customers;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 53


Data Manipulation Language

DML (Data Manipulation Language) : The SQL commands that deals with the manipulation of data present in the
database belong to DML or Data Manipulation Language and this includes most of the SQL statements.

Examples of DML:
INSERT – is used to insert data into a table
UPDATE – is used to update existing data within a table
DELETE – is used to delete records from a database table

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 54


S ELECT S tatement

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


SELECT Statement

The SELECT statement is used to select data (read and return it) from a database.
The data returned is stored in a result table, called the result-set. You can perform the same actions on a result set as
on a regular table.​
SELECT Syntax:
SELECT column1, column2, ...
FROM table_name;
Here, column1, column2, ... are the field names of the table you want to select data from.
If you want to select all the fields available in the table, use the following syntax:
SELECT * FROM table_name;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 56


SELECT Statement Example

The following SQL statement selects the "CustomerName" and "City" The following SQL statement selects all the columns from the
“Customers" table:
columns from the "Customers" table:
SELECT * FROM Customers;
SELECT CompanyName, ContactName, City
FROM Customers;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 57


SELECT DISTINCT Statement

The SELECT DISTINCT statement is used to return only distinct The following SQL statement selects only the DISTINCT values from the
(different) values. "City" column in the "Customers" table:

Inside a table, a column often contains many duplicate values; and


sometimes you only want to list the different (distinct) values.
SELECT DISTINCT City

FROM Customers;
SELECT DISTINCT Syntax:

SELECT DISTINCT column1, column2, ...


FROM table_name;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 58


SELECT Statement Logical Processing Order

1) FROM The following steps show the logical processing order, or binding
order, for a SELECT statement. This order determines when the
2) ON objects defined in one step are made available to the clauses in
3) JOIN subsequent steps.
4) WHERE Because the SELECT clause is step 7, any column aliases or derived
columns defined in that clause cannot be referenced by preceding
5) GROUP BY
clauses. However, they can be referenced by subsequent clauses
6) HAVING such as the ORDER BY clause. The actual physical execution of the
statement is determined by the query processor and the order may
7) SELECT
vary from this list.
8) DISTINCT
The listed sequence is usually true. However, there are uncommon
9) ORDER BY cases where the sequence may differ.
10)TOP

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 59


SELECT INTO Statement

The SELECT INTO statement copies data from one table into a new table.
Syntax:
Tip: SELECT INTO can also be used to create a new, empty table
Copy all columns into a new table:
using the schema of another. Just add a WHERE clause that
SELECT * causes the query to return no data:
INTO newtable
FROM oldtable SELECT * INTO newtable
WHERE condition; FROM oldtable
WHERE 1 = 0;
Copy only some columns into a new table:
SELECT column1, column2, column3, ...
INTO newtable
FROM oldtable
WHERE condition;
The new table will be created with the column-names and types as defined in the old table. You can create new column names using
the AS clause.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 60


SELECT INTO Statement Examples

The following SQL statement creates a backup copy of Customers:


SELECT * INTO CustomersBackup
FROM Customers;

The following SQL statement copies only a few columns into a new table:
SELECT CompanyName, ContactName INTO CustomersBackup
FROM Customers;

NOTE: You can add WHERE, JOIN and other Clauses to your SELECT INTO Statement, just like to usual SELECT Statement.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 61


INSERT INTO SELECT Statement

The INSERT INTO SELECT statement copies data from one table and inserts it into another table.
The INSERT INTO SELECT statement requires that the data types in source and target tables matches.

Note: The existing records in the target table are unaffected.

Syntax:
Copy all columns from one table to another table:

INSERT INTO table2


SELECT * FROM table1
WHERE condition;

Copy only some columns from one table into another table:

INSERT INTO table2 (column1, column2, column3, ...)


SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 62


INSERT INTO SELECT Statement Examples

The following SQL statement copies "Suppliers" into "Customers" (the columns that are not filled with data, will contain NULL):

INSERT INTO CustomersBackup (CustomerID,CompanyName, City, Country)


SELECT SupplierID, CompanyName, City, Country FROM Suppliers;

The following SQL statement copies "Suppliers" into "Customers" (fill all columns):

INSERT INTO Customers (CustomerID, CompanyName, ContactName, Address, City, PostalCode, Country)
SELECT SupplierID, CompanyName, ContactName, Address, City, PostalCode, Country FROM Suppliers;

NOTE: You can add WHERE, JOIN and other Clauses to your SELECT INTO Statement, just like to usual SELECT Statement.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 63


WHERE Clause

The WHERE clause is used to filter records, which means to extract THE FOLLOWING OPERATORS CAN BE USED IN WHERE CLAUSE:
only those records that fulfill a specified condition.
WHERE Syntax: Operator Description

SELECT column1, column2, ... = Equal


FROM table_name > Greater than
WHERE condition;
< Less than
SQL requires single quotes around text values (most database systems will >= Greater than or equal
also allow double quotes).
<= Less than or equal

However, numeric fields should not be enclosed in quotes. <> Not equal. Note: In some versions of SQL this operator
may be written as !=

N O T E : T H E W H E R E C L A U S E I S N O T O N LY U S E D BETWEEN Between a certain range

I N S E L E C T S TA T E M E N T, I T I S A L S O U S E D I N LIKE Search for a pattern


IN To specify multiple possible values for a column
U P D A T E , D E L E T E S TA T E M E N T, E T C . !

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 64


WHERE Clause Example

The following SQL statement selects all the customers from the city The following SQL statement selects all employees with employee id 1,
"Mexico D.F.", in the "Customers" table: in the “Employees" table:
SELECT CompanyName, Country SELECT * FROM Employees
FROM Customers WHERE EmployeeID=1;
WHERE Country=‘Mexico D.F.’;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 65


BETWEEN Operator

The BETWEEN operator selects values within a given range. The values The following SQL statement selects all products with a price between
can be numbers, text, or dates. 10 and 20:

The BETWEEN operator is inclusive: begin and end values are included.
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
BETWEEN Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 66


IN Operator

The IN operator allows you to specify multiple values in The following SQL statement selects all customers that are located in
a WHERE clause. "Germany", "France" or "UK":

The IN operator is a shorthand for multiple OR conditions.


SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
IN Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

or:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 67


LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specified The following SQL statement selects all customers with a
pattern in a column. CustomerName starting with "a":

There are two wildcards often used in conjunction with


the LIKE operator: SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
• The percent sign (%) represents zero, one, or multiple characters

• The underscore sign (_) represents one, single character

The percent sign and the underscore can also be used in combinations!

LIKE Syntax:

SELECT column1, column2, ...


FROM table_name
WHERE column N LIKE pattern;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 68


AND, OR and NOT Operators

The WHERE clause can be combined with AND, OR, and NOT AND Syntax:
operators.
SELECT column1, column2, ...
The AND and OR operators are used to filter records based on more FROM table_name
than one condition: WHERE condition1 AND condition2 AND condition3 ...;
The AND operator displays a record if all the conditions separated by OR Syntax:
AND are TRUE.
SELECT column1, column2, ...
The OR operator displays a record if any of the conditions separated FROM table_name
by OR is TRUE. WHERE condition1 OR condition2 OR condition3 ...;
The NOT operator displays a record if the condition(s) is NOT TRUE. NOT Syntax:
SELECT column1, column2, ...
N OTE: YOU CA N COMB I N E THE A N D, OR
FROM table_name
A N D N O T O P E R ATO R S . WHERE NOT condition;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 69


AND, OR and NOT Operators Examples

The following SQL statement selects all fields from "Customers" The following SQL statement selects all fields from "Customers"
where country is "USA" AND city must be "Portland" OR "Seattle" OR where country is NOT "Germany" and NOT "USA":
"San Francisco"(use parenthesis to form complex expressions):
SELECT * FROM Customers
SELECT * WHERE NOT Country='Germany’
FROM Customers AND NOT Country='USA';
WHERE Country='USA' AND (City = 'Seattle'
OR City = 'Portland'
OR City = 'San Francisco');

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 70


Logical Operators Precedence

Parentheses

Operator precedence describes the order in which


Multiplication, division operations are performed when an expression is
evaluated.
Operations with a higher precedence are performed
Substraction, addition
before those with a lower precedence.
Parentheses has the highest precedence and OR has
NOT the lowest.

AND

OR

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 71


ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set in ascending or ORDER BY Syntax:
descending order.
SELECT column1, column2, ...
The ORDER BY keyword sorts the records in ascending order by FROM table_name
default. To sort the records in descending order, use the DESC ORDER BY column1, column2, ... ASC|DESC;
keyword. OR
NOTE: when you sort by multiple columns, don`t forget to clearly
SELECT column1, column2, ...
identify sort direction for each of them.
FROM table_name
For example:
ORDER BY 1, 2, ... ASC|DESC;
ORDER BY column1, column2 DESC
IS EQUIVALENT TO:
ORDER BY column1 ASC, column2 DESC
In ORDER BY statement you can use column aliases or numbers of
columns in SELECT statement

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 72


ORDER BY Example

The following SQL statement selects all customers from the


"Customers" table, sorted descending by the "CustomerName" and
ascending by the "Country" column. This means that it orders
descending by company name, but if some customers have more than
one country, it orders them ascending by country:

SELECT Country, CompanyName


FROM Customers
ORDER BY CompanyName DESC, Country;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 73


SELECT TOP Clause

N O T E : N O T A L L D ATA B A S E S Y S T E M S S U P P O R T T H E S E L E C T T O P C L A U S E .
M y S Q L S UP P O R T S L I M I T C L A US E TO S E L E C T A L I M I T E D N UM B E R O F R E C O R D S ,
W H I L E O R A C L E U S E S F E T C H F I R S T n R O W S O N LY A N D R O W N U M .

The SELECT TOP clause is used to specify the number of records to return.
It is useful on large tables with thousands of records. Returning a large number of records can impact performance.
SELECT TOP Syntax:
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 74


SELECT TOP Example

The following SQL statement selects the first three records from the The following SQL statement selects the first 50% of the records from
"Customers" table, where the country is "Germany": the "Customers" table:
SELECT TOP 3 * SELECT TOP 50 PERCENT *
FROM Customers FROM Customers;
WHERE Country='Germany';

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 75


OFFSET and FETCH Clauses

The OFFSET and FETCH clauses are the options of the ORDER BY The OFFSET clause specifies the number of rows to skip before starting
clause. They allow you to limit the number of rows to be returned by to return rows from the query. The offset_row_count can be a
a query. constant, variable, or parameter that is greater or equal to zero.
The FETCH clause specifies the number of rows to return after the
OFFSET clause has been processed. The offset_row_count can a
OFFSET and FETCH Syntax:
constant, variable or scalar that is greater or equal to one.
The OFFSET clause is mandatory while the FETCH clause is optional.
ORDER BY column_list [ASC |DESC] Also, the FIRST and NEXT are synonyms respectively so you can use
OFFSET offset_row_count {ROW | ROWS} them interchangeably. Similarly, you can use the FIRST and NEXT
interchangeably.
FETCH {FIRST | NEXT} fetch_row_count {ROW | ROWS} ONLY.

NOTE THAT YOU MUST USE THE OFFSET AND FETCH CLAUSES WITH
THE ORDER BY CLAUSE. OTHERWISE, YOU WILL GET AN ERROR.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 76


OFFSET and FETCH Example

The following SQL statement selects all customers from the


"Customers" table, sorted descending by the "CompanyName" and
ascending by the "Country" column, skips the first 10 Customers and
selects the next 10 Customers.
OFFSET 10
SELECT Country, CompanyName ROWS
FROM Customers
ORDER BY CompanyName DESC, Country
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY; FETCH 10 ROWS
ONLY

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 77


NULL Value

A field with a NULL value is a field with no value. IS NULL Syntax:

If a field in a table is optional, it is possible to insert a new record or SELECT column_names


update a record without adding a value to this field. Then, the field will FROM table_name
be saved with a NULL value. WHERE column_name IS NULL;
It is not possible to test for NULL values with comparison operators, IS NOT NULL Syntax:
such as =, <, or <>.
SELECT column_names
We will have to use the IS NULL and IS NOT NULL operators instead. FROM table_name
WHERE column_name IS NOT NULL;
The IS NULL operator is used to test for empty values (NULL values),
IS NOT NULL for non-empty values.

N O T E : A N U L L V A L U E I S D I F F E R E N T F R O M A Z E R O V A L U E O R A F I E L D T H A T C O N TA I N S
S PA C E S .
A F I E L D W I T H A N U L L VA L U E I S O N E T H AT H A S B E E N L E F T B L A N K D U R I N G R E C O R D C R E AT I O N !

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 78


NULL Value Example

The following SQL lists all customers with a NULL value in the “Region"
field:

SELECT CompanyName, ContactName, Region


FROM Customers
WHERE Region IS NULL
ORDER BY 1;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 79


SQ L ALIASES

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


SQL Aliases

SQL aliases are used to give a table, or a column in a table, a temporary name.
Aliases are often used to make column names more readable.
An alias only exists for the duration of the query.

Alias Column Syntax:


SELECT column_name AS alias_name
FROM table_name;
Alias Table Syntax:
SELECT column_name(s)
FROM table_name AS alias_name;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 81


Alias for Columns Examples

The following SQL statement creates two aliases, one for the CustomerID column and one for the CompanyName column:
Example:
SELECT CustomerID AS ID, CompanyName AS Customer
FROM Customers;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 82


Alias for Columns Examples

The following SQL statement creates two aliases, one for the CustomerID column and one for the CompanyName column:
Example:
SELECT CustomerID AS ID, CompanyName AS Customer
FROM Customers;

The following SQL statement creates two aliases, one for the CompanyName column and one for the ContactName column.
(Note: It requires double quotation marks or square brackets if the alias name contains spaces)

Example:
SELECT CompanyName AS Customer, ContactName AS [Contact Person]
FROM Customers;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 83


Alias for Columns Examples

The following SQL statement creates an alias named "Address" that combine four columns (Address, PostalCode, City and Country):
Example:
SELECT CompanyName, Address + ', ' + PostalCode + ' ' + City + ', ' + Country AS Address
FROM Customers;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 84


Alias for Tables Example

The following SQL statement selects all the orders from the customer with CustomerID=4 (Around the Horn). We use the "Customers" and "Orders"
tables, and give them the table aliases of "c" and "o" respectively (Here we use aliases to make the SQL shorter):

SELECT o.OrderID, o.OrderDate, c.CompanyName


FROM Customers AS c, Orders AS o
WHERE c.CompanyName='Around the Horn' AND c.CustomerID=o.CustomerID;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 85


Alias for Tables Example

The following SQL statement selects all the orders from the customer with CustomerID=4 (Around the Horn). We use the "Customers" and "Orders"
tables, and give them the table aliases of "c" and "o" respectively (Here we use aliases to make the SQL shorter):

SELECT o.OrderID, o.OrderDate, c.CompanyName


FROM Customers AS c, Orders AS o
WHERE c.CompanyName='Around the Horn' AND c.CustomerID=o.CustomerID;

The following SQL statement is the same as above, but without aliases:
SELECT Orders.OrderID, Orders.OrderDate, Customers.CompanyName
FROM Customers, Orders
WHERE Customers.CompanyName='Around the Horn' AND Customers.CustomerID=Orders.CustomerID;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 86


Advantages of Aliases

Aliases can be useful when:

• There are more than one table involved in a query;


• Functions are used in the query;
• Column names are big or not very readable;
• Two or more columns are combined together.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 87


SQL Comments. Single Line Comments

Comments are used to explain sections of SQL statements, or to prevent execution of SQL statements.
Single line comments start with --.
Any text between -- and the end of the line will be ignored (will not be executed).
The following example uses a single-line comment as an explanation:
--Select all:
SELECT * FROM Customers;
The following example uses a single-line comment to ignore the end of a line:
SELECT * FROM Customers -- WHERE City='Berlin’;
The following example uses a single-line comment to ignore a statement:
--SELECT * FROM Customers;
SELECT * FROM Products;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 88


SQL Comments. Multi-Line Comments

Multi-line comments start with /* and end with */. To ignore just a part of a statement, also use the /* */ comment.
Any text between /* and */ will be ignored.
The following example uses a comment to ignore part of a line:
The following example uses a multi-line comment as an
explanation: SELECT CompanyName, /*City,*/ Country
/*Select all the columns FROM Customers;
of all the records The following example uses a comment to ignore part of a
in the Customers table:*/ statement:
SELECT * FROM Customers;
SELECT * FROM Customers
The following example uses a multi-line comment to ignore WHERE (CompanyName LIKE 'L%’
many statements: OR CompanyName
/*SELECT * FROM Customers; LIKE 'R%' /*OR CompanyName LIKE 'S%’
SELECT * FROM Products; OR CustomerName LIKE 'T%’*/
SELECT * FROM Orders; OR CompanyName LIKE 'W%’)
SELECT * FROM Categories;*/ AND Country='USA’
SELECT * FROM Suppliers; ORDER BY CompanyName;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 89


SQL Naming Conventions

Naming convention - some common rules developers tend to follow.


Basic Conventions:
• Use uppercase letters for the keywords (SELECT, FROM, WHERE etc.)
• Align the keywords on separate lines
• Write appropriate comments.
In most Production cases, Customer provides some naming policies to follow. Sometimes they are pushing developers to make a step back from common
rules. Please follow the Customer's Naming Policy, if any.
Just some of typical suggestions:
• Avoid quotes in DB object names: Person instead of “Person”
• Follow the single pattern for naming: If common style says PersonName, don’t use Person_Name
• Data types are not good column/table names: column name Date is bad one
• Full words, not abbreviations: tend to use PersonName instead of PrsnNMa
• Use commonly used abbreviations: if Naming Convention says “NM stands for NAME”, don’t use “NME”
• Avoid reserved word: do not use SQL or DBMS reserved words like Delete, Lock, User for any naming
• Use singular form in entity names: Person instead of Persons

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 90


AdventureWorks Sample Database

1. Download backup files


Use these links to download the sample database: AdventureWorks2019.bak
2. Restore to SQL Server
To restore your database in SQL Server Management Studio, follow these steps:

1) Download the bak file from link provided in the download section.
2) Move the bak file to your SQL Server backup location (this varies depending on your installation location, instance name and version of
SQL Server. For example, the default location for a default instance of SQL Server 2019 is: C:\\Program Files
Microsoft\SQLServer\MSSQL15.MSSQLSERVER\MSSQL\Backup
3) Open SQL Server Management Studio (SSMS) and connect to your SQL Server in.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 91


AdventureWorks Sample Database

4) Right click Databases in Object Explorer > Restore Database... to launch the Restore
Database wizard.

5) Select Device and then select the ellipses to choose a device.


6)Select Add and then choose the .bak file you recently moved to this location. If you
moved your file to this location but you're not able to see it in the wizard, this typically
indicates a permissions issue SQL Server or the user signed into SQL Server does not
have permission to this file in this folder.

7) Select OK to confirm your database backup selection and close the Select backup devices
window.

8) Check the Files tab to confirm the Restore as location and file names match your
intended location and file names in the Restore Database wizard.
9) Select OK to restore your database.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 92


Useful Links

1 DOWNLOAD SQL SERVER

2 DOWNLOAD SQL SERVER MANAGEMENT STUDIO

3 D O W NLO AD A D V EN TUR EWO RKS

4 N O R T H W I N D S A M P L E D A TA B A S E

5 S Q L T U TO R I A L

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


THANKS FOR AT TENDING!
QUESTIONS?

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 94

You might also like