0% found this document useful (0 votes)
90 views

Querying and Managing Data SQL Server

The document discusses various techniques for retrieving data from a SQL Server database using the SELECT statement. It covers selecting specific attributes, customizing output, concatenating text, calculating columns, filtering rows with WHERE and logical operators, and ordering results. Techniques like TOP, NULL, LIKE and IN are also summarized. The document is intended to teach readers how to query and manage data using SQL Server.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Querying and Managing Data SQL Server

The document discusses various techniques for retrieving data from a SQL Server database using the SELECT statement. It covers selecting specific attributes, customizing output, concatenating text, calculating columns, filtering rows with WHERE and logical operators, and ordering results. Techniques like TOP, NULL, LIKE and IN are also summarized. The document is intended to teach readers how to query and manage data using SQL Server.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

TEKNOLOGI INFORMASI

Querying and Managing Data


Using SQL Server
Implementing a Database Design
Apriliya Kurnianti S.T., M.Eng

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:

Specific SELECT EmployeeID, ContactID, LoginID, Title


FROM HumanResources.Employee

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.

Department Number Department Name


You can write the query in the following ways:
Retrieving
SELECT 'Department Number'= DepartmentID,
Specific 'Department Name'= Name
FROM HumanResources.Department

Attributes OR

(Contd.) SELECT DepartmentID 'Department Number', Name


'Department Name' FROM HumanResources.Department

OR

SELECT DepartmentID AS 'Department Number', Name


AS 'Department Name'
FROM HumanResources.Department
The SQL Server will display the same output for all the
Retrieving preceding queries, as shown in the following figure.

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’:

Attributes SELECT EmployeeID, 'Designation: ', Title


FROM HumanResources.Employee
(Contd.)
Output
Retrieving Concatenating the text values in the output:
The concatenation operator is used to concatenate string

Specific expressions and is represented by the + sign.


To concatenate two strings, you can use the following query:
SELECT 'snow' + 'ball'
Attributes
(Contd.) Output

snowball
The following SQL query concatenates the data of the Name and
Retrieving GroupName columns of the Department table into a single
column:

Specific SELECT Name + ' department comes under ‘ +


GroupName + ' group' AS Department FROM
Attributes HumanResources.Department

(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:

Specific SELECT EmployeeID, Rate,


Per_Day_Rate = 8 * Rate
FROM HumanResources.EmployeePayHistory
Attributes
(Contd.) Rate is multiplied by 8, assuming that an
employee works for eight hours in a day.
Selected rows can be retrieved using the WHERE clause in
Retrieving the SELECT statement.

Selected The following SQL query retrieves the department details


from the Department table, where the group name is
Rows Research and Development:
SELECT * FROM HumanResources.Department

(Contd.) WHERE GroupName = 'Research and Development'


Output
Comparison operators:
Retrieving Test for similarity between two expressions.
Allow row retrieval from a table based on the condition specified
Selected in the WHERE clause.
Cannot be used on text, ntext, or image data type expressions.
Rows Syntax:
SELECT column_list
(Contd.) FROM table_name
WHERE expression1 comparison_operator expression2
The following SQL query retrieves records from the
Retrieving Employee table where the vacation hour is less than 5:
SELECT EmployeeID, NationalIDNumber, Title,
Selected VacationHours FROM HumanResources.Employee
WHERE VacationHours < 5

Rows Output

(Contd.)
Logical operators:
Are used in the SELECT statement to retrieve records based on

Retrieving one or more conditions.


Are of the following types:

Selected OR
AND

Rows Syntax:
NOT

(Contd.) SELECT column_list


FROM table_name
WHERE conditional_expression1 {AND/OR} [NOT]
conditional_expression2

For example:
SELECT * FROM HumanResources.Department
WHERE GroupName = 'Manufacturing'
OR GroupName = ' Quality Assurance'

Retrieves records from the Department table when the


GroupName is either Manufacturing or Quality Assurance.
Range operator:
Retrieves data based on a range.
Retrieving Are of the following types:

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

Retrieves records of employees whose vacation hours are


not between 40 and 50.
IN keyword:
Retrieving Selects values that match any one of the values in a list.
NOT IN keyword:
Selected Restricts the selection of values that match any one of the
values in a list.
Rows Syntax:
SELECT column_list
(Contd.) FROM table_name
WHERE expression list_operator (‘value_list’)
For example:
SELECT EmployeeID, Title, LoginID FROM
HumanResources.Employee
WHERE Title IN ('Recruiter', 'Stocker')

Retrieves records of employees who are Recruiter or


Stocker from the Employee table.
The LIKE keyword:
Retrieving Is used to search a string by using the following wildcard
characters:
Selected %
_

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:

Retrieving Can be retrieved by using the IS NULL keyword with the


SELECT statement.

Selected Syntax:
SELECT column_list

Rows FROM table_name


WHERE column_name unknown_value_operator

(Contd.) For example:


SELECT EmployeeID, EndDate
FROM HumanResources.EmployeeDepartmentHistory
WHERE EndDate IS NULL

Retrieves only those rows from the EmployeeDepartmentHistory


table for which value in the EndDate column is NULL.
ORDER BY clause:
Can be used with the SELECT statement to display records in
a specific order.
Retrieving Displays record in ascending or descending order.
Syntax:
Selected SELECT select_list
FROM table_name
Rows [ORDER BY order_by_expression [ASC|DESC]
[, order_by_expression [ASC|DESC]…]

(Contd.) For example:


SELECT GroupName, DepartmentID, Name
FROM HumanResources.Department
ORDER BY GroupName, DepartmentID

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

Retrieves the top 10 rows of the Employee table.


DISTINCT keyword:
Retrieving Eliminates the duplicate rows from the result set.
Syntax:
Selected SELECT [ALL|DISTINCT] column_names
FROM table_name

Rows WHERE search_condition


For example:
(Contd.) SELECT DISTINCT Title FROM
HumanResources.Employee
WHERE Title LIKE 'PR%'

Retrieves all the titles beginning with PR from the


Employee table.
EXERCISE

You might also like