You are on page 1of 15

Simple Queries

TCS Internal 1
Objective

 To be able to retrieve data from the tables

TCS Internal 2
Scope

 Retrieving selected rows


 Eliminating duplicates
 Sorting results
 Generating summary data
 Temporary Tables
 Computed Column

TCS Internal 3
Retrieving Rows (Based On Comparisons)

 Comparison operators allow to SELECT rows by comparing a row


to given value or expression.

 Different comparison operators are:


=, >, <, >=, <=, <>, !>, !<

TCS Internal 4
Retrieving Rows (Based On Ranges)

 BETWEEN operator allows to select rows of data such that data in


a given column contain values within a range.

 The keyword BETWEEN is followed by


 the beginning value
 the keyword AND
 the ending value

TCS Internal 5
Retrieving Rows (Based On Lists)

 IN operator is used when rows are to be selected that


match one of the several listed values.
 If the row value matches any value in the list of value, the
row is selected.
 IN defines a set by explicitly naming the members by the
set in parentheses, separated by comma.
 NOT IN selects the rows that do not match items in the
value list.

TCS Internal 6
Eliminating the duplicates

 The default result is to show all the rows which satisfy the WHERE clause.

 If only distinct values are required for a particular combination of columns, the
keyword DISTINCT is used

TCS Internal 7
Sorting the Results

 The ORDER BY Clause defines the particular order of the rows in the result of a
query.

Syntax:

ORDER BY col_name ASC | DESC

TCS Internal 8
Aggregate Functions

 Aggregate are functions that are used to get summary values. All aggregate
functions can be divided into three groups:

 Convenient aggregate functions


 Statistical aggregate functions
 Super aggregates

TCS Internal 9
Convenient Aggregates

 The Transact –SQL language supports six aggregates functions.

 MIN
 MAX
 SUM
 AVG
 COUNT
 COUNT_BIG

TCS Internal 10
Statistical Aggregates

 The following aggregates functions belong to the group of statistical aggregates:

 VAR
 VARP
 STDEV
 STDEVP

TCS Internal 11
Super Aggregates

 The following aggregates functions belong to the group of super aggregates:

 CUBE
 ROLLUP

TCS Internal 12
Temporary Tables

 A temporary table is a special kind of a table that differs in two ways from base
tables.

 Each temporary table is implicitly dropped by the system


 Each temporary table is stored in the tempdb system database

 Temporary table can be local or global

 Local temporary tables are removed at the end of the current session. They are
specified with the prefix #.
 Global temporary tables are dropped at the end of the current session. They
are specified with the prefix ##.

TCS Internal 13
Computed Columns

 Computed Columns are (by default) virtual columns not physically stored in the table.
 Their values are recalculated each time they are referenced in a query.

Example:

CREATE TABLE ORDERS


(
orderid INT NOT NULL,
price MONEY NOT NULL,
quantity INT NOT NULL,
total AS price * quantity PERSISTED
)

TCS Internal 14
Summary

 Retrieving selected rows


 Eliminating duplicates
 Sorting results
 Generating summary data
 Temporary Tables
 Computed Column

TCS Internal 15

You might also like