Querying and Managing Data SQL Server
Querying and Managing Data SQL Server
Pertemuan 2
In this session, you will learn to:
Retrieve data
Retrieving Specific Attributes
The SELECT statement is used for accessing and retrieving data from a
database.
Syntax:
SELECT [ALL | DISTINCT] select_column_list
[INTO [new_table_name]]
[FROM {table_name | view_name}
[WHERE search_condition]
Retrieving Specific Attributes (Contd.)
To display all the details of employees, you can use the following
query:
SELECT * FROM HumanResources.Employee
Output
To view specific details, you can specify the column names in the
Retrieving SELECT statement, as shown in the following query:
Attributes Output
(Contd.)
Retrieving Customizing the display:
Specific Consider the following example that displays the Department ID and
Department Names from the Department table of the AdventureWorks
Attributes database.
The report should contain column headings different from those given
(Contd.) in the table, as shown in the following figure.
Attributes OR
OR
Specific
Attributes
(Contd.)
Literals are string values that are enclosed in single quotes and
Retrieving added to the SELECT statement.
The following SQL query retrieves the employee ID and their
Specific titles from the Employee table along with a literal ‘Designation’:
snowball
The following SQL query concatenates the data of the Name and
Retrieving GroupName columns of the Department table into a single
column:
(Contd.)
Output
Calculating column values:
Retrieving
Arithmetic operators are used to perform mathematical operations,
Specific such as addition, subtraction, division, and multiplication, on
numeric columns or on numeric constants.
Attributes SQL Server supports the following arithmetic operations:
+ (for addition)
(Contd.) - (for subtraction)
/ (for division)
* (for multiplication)
% (for modulo)
The following SQL query retrieves the per day rate of the
Retrieving employees from the EmployeePayHistory table:
Rows Output
(Contd.)
Logical operators:
Are used in the SELECT statement to retrieve records based on
Selected OR
AND
Rows Syntax:
NOT
For example:
SELECT * FROM HumanResources.Department
WHERE GroupName = 'Manufacturing'
OR GroupName = ' Quality Assurance'
Selected BETWEEN
NOT BETWEEN
Syntax:
Rows SELECT column_list
FROM table_name
(Contd.) WHERE expression1 range_operator expression2 AND
expression3
For example:
SELECT EmployeeID, VacationHours
FROM HumanResources.Employee
WHERE VacationHours BETWEEN 20 AND 50
Rows []
[^]
(Contd.) Matches the given character string with the specified pattern.
For example:
SELECT * FROM HumanResources.Department
WHERE Name LIKE 'Pro%'
Retrieves records from the Department table where the
values of Name column begin with ‘Pro’.
NULL values:
Selected Syntax:
SELECT column_list
Output
TOP keyword:
Retrieving Can be used with the SELECT statement to retrieve only the
first set of rows from the top of a table.
Selected Syntax:
SELECT [TOP n [PERCENT]] column_name
Rows [,column_name…]
FROM table_name
(Contd.) WHERE search_conditions
[ORDER BY [column_name[,column_name…]
For example:
SELECT TOP 10 * FROM HumanResources.Employee