SQL_4
SQL_4
Session # 4:
Querying database
Focus Points :
Identify the input requirements for a query.
Plan and create a format for the query output.
Identify and use sources of data for a query.
Identify and use different types of queries.
Use different types of operators.
Use aggregate functions in querries.
Sort the query output.
Group the query output.
Use the top keyword.
Calculate and display totals and subtotals.
Use string functions.
Use date functions.
Use mathematical functions.
Use wild cards.
Use the distinct keyword in a query.
Use the querries with different types of joins.
Use different types of subquerries.
Contents
Order by,
Aggregate functions,
Contents
Getting Started
Start the Query Analyzer.
Connect to the Server.
Connect to the Database.
Type the Query.
Execute the query.
Select, and using Alias
Filtering
Select [column1,..] [*] from tablename where [clause]
Some usefull operators
Relational operators >, <, >=,<=,!>,!<,<>,=,!=
In , between (Range Operator)
And , or (logical operator) , not
is null/not null
Order By
Select [column1,..] [*] from tablename order by [column1,..]
where [clause]
Aggregate Functions
max()
min()
count()
avg()
sum()
Group by with having
Function Results
ABS( numeric Absolute value
_expr )
ACOS, Angle in radians whose cosine, sine, or tangent is a floating-
ASIN ATAN point value
( float_expr )
ATN2( float_e Returns the angle in radians whose tangent is
xpr1 , float_ex between float1 and float2
pr2 )
COS, Cosine, sine, or tangent of the angle (in radians)
SIN COT,
TAN( float_ex
pr )
CEILING( nu Smallest integer greater than or equal to specified value
meric_expr )
Math Functions
Function Results
DEGREES( nu Conversion from radians to degrees
meric_expr )
EXP( float_ex Exponential value of specified value
pr )
FLOOR( nume Largest integer less than or equal to specified value
ric_expr )
LOG( float_ex Natural log
pr )
LOG10( float_ Base-10 log
expr )
PI() Constant 3.141592653589793
POWER( num Value of numeric_expr to the power of y
eric_expr , y )
Math Functions
Function Results
RADIANS( nu Conversion from degrees to radians
meric_expr )
RAND([seed]) Random float number between 0 and 1
ROUND( num Numeric_exp rounded to the specified length, length in an
eric_expr,len ) integer value
SIGN( numeri Positive, negative, or zero
c_expr )
SQUARE( floa Squares the specified value
t_expr )
SQRT(float_ex Square root of specified value
pr)
Wildcard
Self Join
a self join correlates rows of a table with other rows in the
same table. Comparison queries for the same information
are used the most for self joins.
Cross or Unrestricted Joins
Cross or unrestricted joins return a combination of all
rows of all tables in the join as the result set. A cross or
unrestricted join is created not by using the WHERE clause
in the SQL Server join of two or more tables, but by using
the CROSS JOIN keyword.
Combining all rows from all tables involved in the join
yields what is known as a Cartesian product.
Joins
Outer Joins
You can restrict rows from one table while allowing all rows
from another table as your result set by using outer joins. One
of the most common uses for this type of join is to search for
orphan records. The outer join operators and keywords are as
follows:
Includes all rows from the first table and only the matching rows
in the second table – Left outer Join
Includes all rows from the second table and only the matching
rows in the first table – Right outer Join
Includes all nonmatching rows from both tables – Full outer
Join
Set Operators
Subquerries
A SELECT statement nested inside another SELECT statement
is commonly referred to as a subquery. Subquerries can
produce the same results as a join operation.
Single Row Subquerry
A subquery can return a single column or single value
anywhere a single value expression can be used and can be
compared against using the following operators: =, <, >, <=,
>=, <>, !>, and !<.
It can return single column/many values that can be used with
the IN list comparison operator in the WHERE clause.
Subquerry
Multi-Row Subquerry
A subquery can also return many rows that will be used for an
existence check by using the EXISTS keyword in
the WHERE clause.
Select into
Client/Server Architecture
Where
Order by
Group by
wildcard
Set Operators
Join
Subquerry
Select into