Beruflich Dokumente
Kultur Dokumente
CHAPTER-2
QUERYING DATA
Structured Data:-
Structured Data has a definite structure.
It follows a defined format and has a predefined length.
Ex- String, Numeric Data etc.
It can be easily stored in a table.
Example:-
Roll number of a student is a numeric value and
can be easily stored in the ROLLNO column of
STUDENT table.
Name of student can be easily stored in NAME
column of STUDENT table.
Geographical data like longitude and latitude.
Geometrical data with coordinates.
Semi-Structured Data:-
Semi-structured data has an indefinite structure.
It cannot be stored easily in a database table.
The attributes of semi-structured data can change.
Semi-structured data is generally stored in the XML
format.
Example:-
The details of customers contain various attributes such
as Customer ID and Customer Name that can change
for every customer.
Page 2 of 76
Unstructured Data:-
Unstructured data does not have a defined structure.
It does not necessarily follow any format or sequence.
It does not follow any rule and it is not predictable.
Example:-
Text file, Image file, Videos, Sound clips etc.
(b) Datatypes:-
Datatypes represents the type of data that a database object can
store. This data can be in the form of characters, numbers or
symbols.
The datatypes are associated with the columns, local variables
or expressions defined in the database.
We must specify the datatype of a column according to the
data to be stored in it.
Types Of Datatypes:-
SQL server supports various datatypes which are categorized
as:-
Numeric Datatypes
Character and String Datatypes
Date and Time Datatypes
Binary Datatypes
Other Datatypes
Example:-
To store name of the employee in EMPLOYEE NAME column,
we must specify a Character And String Datatype for the
column like char, varchar,varchar2 etc.
2. Types Of Datatypes:-
(a) Numeric Datatypes:-
Numeric Datatypes allows user to store numeric values in the
database.
Page 3 of 76
3. To save new query , click on Save button, then type a name for
the query and Click on Save .
4. To Execute a Query, select the statements and then click on
Execute option or press F5 on keyboard.
(b) Creating a Database:-
To create a database, we use syntax:-
CREATE DATABASE DATABASE_NAME
Page 7 of 76
Example:-
(Attribute1 Datatype1,
Attribute2 Datatype2,
Attribute3 Datatype3,
AttributeN DatatypeN) ;
Page 8 of 76
Naming Of Attributes:-
The Attribute Name should be a sequence of letters, digits
and underscore. Underscore should be treated as letter.
Attribute Name should start with either a letter.
Symbols can also be included in Attribute Name but it
should not be written at first place of attribute name. Not
all symbols can be included.
Attribute Name should not be a Keyword/Reserved Word.
(d) Inserting Data Into Table:-
We can insert data into table by using the syntax:-
INSERT INTO TABLE_NAME VALUES(VALUE1,VALUE2,................VALUEn) ;
Provided:-
Data should be entered in same order as that of Attributes at
the time of table creation.
By default Date should be written in YYYY-MM-DD format.
If a value is of Character and String Datatype Or Date-Time
Datatype, then it should be written within single quotes.
Ex- John, 1992-12-23 etc.
Example:-
(Horizontal View)
Page 11 of 76
INTO New_Table_Name
FROM [Table_Name|View_Name]
WHERE Search_Condition
Provided:-
FROM Table_Name
Example:-
Page 13 of 76
Example:-
SELECT DISTINCT *FROM EMPLOYEE
After executing this statement, only unique rows will be displayed
and all repeated entries will be hidden.
(d) To Display Selected Columns with Distinct Rows:-
Syntax:-
SELECT DISTINCT Column_List FROM Table_Name
Example:-
SELECT DISTINCT EMPID,EMP_NAME,[EMP DESIG]
FROM EMPLOYEE
After executing this statement, only unique rows of these selected
columns will be displayed.
5. Customizing The Display:-
In SQL Server, display of the tables can be customized by using user
defined headings and literals. This does not changes the name of the
column in the table, instead it is just a customized display of the table.
(a) Creating User Defined Headings:-
User-Defined Headings should be enclosed within single quotes if
they contain blank spaces, otherwise written without single quotes.
The User-Defined Headings can be created in three ways:-
Using Equal to Sign:-
Syntax:-
SELECT Heading1=ColumnName1, Heading2=ColumnName2
FROM Table_Name
FROM Table_Name
Page 14 of 76
FROM Table_Name
FROM Table_Name
Example:-
Example:-
FROM Table_Name
FROM Table_Name
Conditional Expression
FROM Table_Name
FROM Table_Name
FROM Table_Name
FROM Table_Name
FROM Table_Name
Where:-
Check_Column is the name of column to be checked for NULL
Replacement_Value is the value that replaces the null values in result
set.
Syntax for using ISNULL( ) with SELECT :-
SELECT Column_List , ISNULL(Check_Column,Replacement_Value)
Points To Remember:-
While using ISNULL( ), Column_List should not include
Check_Column.
Replacement_Value must be of Numeric Type i.e it should
be a number.
ISNULL( ) only displays the Replacement_Value in place of
NULL, but it does not change the values in the Table.
Example:-
Page 27 of 76
COALESCE( Check_Column_List)
FROM Table_Name
Example:-
To show the use of WITH TIES, we have added 2 more records in the
EMPLOYEE table. The new EMPLOYEE table is as follows:-
Page 33 of 76
FROM Table_Name
ORDER BY Column_Name
ONLY
Where:-
Out of FIRST and NEXT, only one is used at a time because both have same
meaning.
Page 34 of 76
Example:-
Syntax :-
SELECT Column_List,
Example:-
Example:-
Example:-
All these functions return the values from the Partially Similar
Rows (having at least one instance as common) in the same result
set.
lead (scalar_expression,[offset],[default] )
AS New_Column_Name
FROM Table_Name
Where:-
scalar_expression is the value to be returned
based on specified offset. It can be any expression
or function.
lag (scalar_expression,[offset],[default] )
AS New_Column_Name
FROM Table_Name
Where:-
scalar_expression is the value to be returned
based on specified offset. It can be any expression
or function.
Here, if scalar_expression contains an Aggregate
Function then we must use GROUP BY clause
after the FROM Table_Name statement.
offset is the interval by which the number of rows
to be sorted backward. It is always a positive
integer.
default denotes the default value(i.e NULL) which
is to be returned if scalar_expression is not
present.
OVER( ) specifies the order of data before the
function is applied.
lag() works similar to lead() , the only difference is
that:-
lead() retrieves data from upcoming rows and
display it corresponding to current row.
BUT
lag() retrieves data from previous rows and
display it corresponding to current row.
Page 43 of 76
Example:-
Page 44 of 76
first_value (scalar_expression)
AS New_Column_New
FROM Table_Name
Where:-
scalar_expression is the value to be returned
based on specified offset. It can be any expression
or function.
row_range_clause:-
It limits the number of rows within the partition
by specifying start & end points. It is used inside
OVER( ). (will be discussed in brief later)
PartitionBy Clause:-
It is used to partition the rows according to the
specified distinct instance. [Used inside OVER()]
Syntax:-
PARTITION BY Column_Name
Example:-
Page 45 of 76
last_value (scalar_expression)
AS New_Column_New
FROM Table_Name
Example:-
Page 46 of 76
FROM Table_Name
Where:-
ALL specifies that the aggregate function is
applied to all the values in the specified column.
DISTINCT specifies that the aggregate function is
applied to only unique values in the specified
column.
expression may be a column or an expression
with operators.
Different Aggregate Functions in SQL:-
We can summarize data using the following aggregate
functions:-
SUM( ) :- Returns the sum total of values in a
numeric expression, either all or distinct.
Functions :- There are different kinds of functions in SQL server which are used to perform specific tasks.
Ex- String Functions, Conversion Functions, Date Functions, Mathematical Functions, Logical Functions,
System Functions, Aggregate Functions, Analytical Functions, etc. Syntax for using a function is:-
SELECT function_name(parameters)
Where, parameters can be column_name or any other required parameters according to function type.
Page 48 of 76
Style Values used in conversion functions are given below along with their
formats:-
Page 53 of 76
Some abbreviations used to extract different parts of date are given below:-
Page 54 of 76
FROM Table_Name
WHERE Search_Condition
HAVING Search_Condition
Where:-
Search_Condition is the conditional expression on which
the result set is to be produced.
We have created SALESHISTORY table for representing the use of GROUP BY &
HAVING clauses:-
Page 64 of 76
FROM Table_Name
WHERE Search_Condition
HAVING Search_Condition
Example:-
FROM Table_Name
WHERE Search_Condition
GROUP BY
(Column_Name1),
(Column_Name2) )
Page 66 of 76
(b) CUBE :-
It extends the functionality of ROLL UP. It generates the result
that contains:-
1. Subtotal for each group.
2. Grand total of all groups.
3. All other possibilities involved within groups.
Page 68 of 76
Example of CUBE:-
FROM
PIVOT ( AggregateFunction(Value_Column)
AS PivotedTable_Alias
Where:-
Example Of PIVOT:-
The DAILY TRANSACTION Table:-
We have created the DAILY TRANSACTION table to demonstrate the use
of PIVOT & UNPIVOT:-
Page 71 of 76
Process Of Pivoting:-
Example Of PIVOT:-
Syntax of UNPIVOT:-
SELECT Column_Name,Pivot_Column,Value_Column
AS PivotedTable_NewAlias
UNPIVOT
( Value_Column
) AS UnpivotedTable_Alias
Where:-
Process of Unpivoting:-
Example of UNPIVOT:-
27. Schema :-
A Schema is a collection of database objects (like tables), that
comes under the same database and are logically related to each
other. Basically, it is a group of tables that are referenced by same
group name in SQL server. This group name is called Schema.
TRANSFER OldSchemaName.TableName
Example:-
In the following table, we are adding all the tables we have
created in this chapter under the schema CH2 :-
Page 76 of 76
(Attribute1 Datatype1,
Attribute2 Datatype2,
Attribute3 Datatype3,
AttributeN DatatypeN) ;
Example:-