Upload
sathish-kumar-r
View
223
Download
0
Embed Size (px)
Citation preview
8/9/2019 Module 07_Querying Table
1/36
Module 7Querying Tables
Vidya Vrat Agarwal. | MCT, MCSD
8/9/2019 Module 07_Querying Table
2/36
Overview
How Queries are Processed
Select Statement
Manipulating Column Names
Literals
Range Operator
List Operator
String Operator Wildcard
Unknown Value
ISNULL
8/9/2019 Module 07_Querying Table
3/36
How Queries are Processed
Submits Compiled requests for Processing and thenRun.
Execute
Translates the Query into an Executable PlanCompile
Determines the indexes to useOptimizeValidates that the names of the Objects are PresentResolve
Checks Syntax for Accuracy.Parse
DescriptionProcess
8/9/2019 Module 07_Querying Table
4/36
Select Statement
Select statement is to access and retrieve data fromdatabase.
The Keywords Select, From, Where makes up the basicSelect Statement.
The basic from of the select statement sometimes called a
Mapping or a Select-from-where block.
8/9/2019 Module 07_Querying Table
5/36
Selecting Data
Select name from Students
Select All name from Students
Select Distinct name from students
8/9/2019 Module 07_Querying Table
6/36
Manipulating Column Names
When query results are displayed, a column name of theresult set is always displayed as it was specified at thetime of Table creation.
Create Table Students
( Sid int identity,
Name varchar(20)
)
Select * from Students
8/9/2019 Module 07_Querying Table
7/36
A User defined Column heading can replace the defaultcolumn heading.
Select Column_alias = column_name, [Column_alias=column_name]
From table_name
Or
Select Column_name Column_alias, [Column_name Column_alias]
From table_name
Where Column_alias is the User defined column heading that is to bespecified in place of the default Column heading.
8/9/2019 Module 07_Querying Table
8/36
Use Pubs
select Publishers Identity =pub_id, Publishers Name pub_name
from publishers
Publishers Identity Publishers Name
------------------- ----------------------------------------
0736 New Moon Books
0877 Binnet & Hardley
1389 Algodata Infosystems
1622 Five Lakes Publishing
1756 Ramona Publishers
9901 GGG&G
9952 Scootney Books9999 Lucerne Publishing
(8 row(s) affected)
8/9/2019 Module 07_Querying Table
9/36
Literals
The result set of the data query statement can be mademore readable by including a String called Literal inSelect list enclosed in single quotes .
A Literal should typically be used before the column name
for which a string is to be displayed.
Select Column_name ,String_literal, [Column_name ,String_literal]
From table_name
8/9/2019 Module 07_Querying Table
10/36
Literals
Use pubsselect pub_id,'Publishers Name is :',pub_name from publishers
pub_id pub_name
------ -------------------- ----------------------------------------
0736 Publishers Name is : New Moon Books
0877 Publishers Name is : Binnet & Hardley1389 Publishers Name is : Algodata Infosystems
1622 Publishers Name is : Five Lakes Publishing
1756 Publishers Name is : Ramona Publishers
9901 Publishers Name is : GGG&G
9952 Publishers Name is : Scootney Books
9999 Publishers Name is : Lucerne Publishing
(8 row(s) affected)
8/9/2019 Module 07_Querying Table
11/36
Range Operator
The Range Operator is used to retrieve data that can beextracted in ranges.
The Range Operators are :
Between and
Not Between
8/9/2019 Module 07_Querying Table
12/36
Range Operators
Use pubs
select advance,title from titles
where advance between 2000 and 4000
advance title--------------------- ------------------------------------------
2275.0000 Is Anger the Enemy?
2000.0000 Prolonged Data Deprivation: Four Case Studies
4000.0000 Emotional Security: A New Algorithm
4000.0000 Fifty Years in Buckingham Palace Kitchens
(4 row(s) affected)
8/9/2019 Module 07_Querying Table
13/36
List Operator
SQL Server allows the IN operator that allows theSelection of values, that match any value in List.
The List operators are :
IN and
NOT IN
Select Column_list
From table_name
Where Column_name IN ( Value_list)
8/9/2019 Module 07_Querying Table
14/36
List Operators
Use pubs
select pub_name, city,state
from publishers
where state IN ('MA','DC')
pub_name city state
---------------------------------------- -------------------- -----
New Moon Books Boston MA
Binnet & Hardley Washington DC
(2 row(s) affected)
8/9/2019 Module 07_Querying Table
15/36
String Operator - Wildcard
SQL Server provides a Pattern-Matching method for stringexpressions using LIKE Keyword with the Wildcard mechanism.
single character not within specified range. [^a - f][^]
single character within specified range. [a-f] or [abcdef][ ]
Represents a Single Character._(UnderScore)
Represents any string of zero or more character (s)%
DescriptionWildcard
8/9/2019 Module 07_Querying Table
16/36
String Operator - Wildcard
Enclose the wildcard(s) and the character string in single quotation marks, for example:
LIKE 'Mc%' searches for all strings that begin with the letters Mc (McBadden).
LIKE '%inger' searches for all strings that end with the letters inger (Ringer, Stringer).
LIKE '%en%' searches for all strings that contain the letters en anywhere in the string(Bennet, Green, McBadden).
LIKE '_heryl' searches for all six-letter names ending with the letters heryl (Cheryl, Sheryl).
LIKE '[CK]ars[eo]n' searches for Carsen, Karsen, Carson, and Karson (Carson).
LIKE '[M-Z]inger' searches for all names ending with the letters inger that begin with anysingle letter from M through Z (Ringer).
LIKE 'M[^c]%' searches for all names beginning with the letter M that do not have the letterc as the second letter (MacFeather).
8/9/2019 Module 07_Querying Table
17/36
String Operator - Wildcard
SELECT phone
FROM authors
WHERE phone LIKE '415%
phone
------------
415 986-7020
415 548-7723
415 834-2919
415 658-9932
415 836-7128
415 585-4620
415 935-4228
415 843-2991
415 354-7128
415 534-9219
415 836-7128
(11 row(s) affected)
8/9/2019 Module 07_Querying Table
18/36
%
SELECT phone
FROM pubs.dbo.authors
WHERE phone NOT LIKE '415%'
Or
SELECT phone
FROM pubs.dbo.authorsWHERE NOT phone LIKE '415%'
8/9/2019 Module 07_Querying Table
19/36
_ ( Underscore)
SELECT au_lname, au_fname, phone
FROM authors
WHERE au_fname LIKE _heryl'
au_lname au_fname phone
---------------------------------------- -------------------- ------------
Carson Cheryl 415 548-7723
Hunter Sheryl 415 836-7128
(2 row(s) affected)
8/9/2019 Module 07_Querying Table
20/36
[ ]
SELECT au_lname, au_fname, phone
FROM authors
WHERE au_fname LIKE '[CS]heryl'
au_lname au_fname phone
---------------------------------------- -------------------- ------------
Carson Cheryl 415 548-7723
Hunter Sheryl 415 836-7128
(2 row(s) affected)
8/9/2019 Module 07_Querying Table
21/36
[ ]
SELECT au_lname, au_fname, phone
FROM authors
WHERE au_lname LIKE '[CK]ars[eo]n'
au_lname au_fname phone
---------------------------------------- -------------------- ------------
Carson Cheryl 415 548-7723
Karsen Livia 415 534-9219
(2 row(s) affected)
8/9/2019 Module 07_Querying Table
22/36
UnKnown value
Unknown values refer to the data entered in the form ofNULL keyword.
The NULL values can be retrieved from the table usingIS NULL or IS NOT NULL keyword in the where clause.
select title,ytd_salesfrom titles
where ytd_sales IS NULL
title ytd_sales
-------------------------------------------------------------------------------- -----------
The Psychology of Computer Cooking NULLNet Etiquette NULL
(2 row(s) affected)
8/9/2019 Module 07_Querying Table
23/36
ISNULL
Replaces NULL with the specified replacement value.
Select title,'ytd_sales = ISNULL(ytd_sales,'0')
from titles
where ytd_sales IS NULL
title ytd_sales
---------------------------------------------------------- -----------
The Psychology of Computer Cooking 0
Net Etiquette 0
(2 row(s) affected)
8/9/2019 Module 07_Querying Table
24/36
Check Your Understanding.
8/9/2019 Module 07_Querying Table
25/36
Q.1. How Query is Processed.?
8/9/2019 Module 07_Querying Table
26/36
Q.2. What is the Purpose of SELECT Statement.?
8/9/2019 Module 07_Querying Table
27/36
Q.3. What is the difference between ALL and DISTINCT.?
8/9/2019 Module 07_Querying Table
28/36
Q.4. What is Column Alias.? What is the Advantage.?
8/9/2019 Module 07_Querying Table
29/36
Q.5. What is Literal and what is its purpose.?
8/9/2019 Module 07_Querying Table
30/36
Q.6. What are Range Operators and what is the Purpose.?
8/9/2019 Module 07_Querying Table
31/36
Q.7. What is List Operator and what is the Purpose.?
8/9/2019 Module 07_Querying Table
32/36
Q.8. What are String Operators- wildcard, and what are the
types and Purpose.?
8/9/2019 Module 07_Querying Table
33/36
Q.9. What is UNKNOWN value.? How this Value can be
Retrieved.?
8/9/2019 Module 07_Querying Table
34/36
Q.10. What is the Purpose of ISNULL.?
8/9/2019 Module 07_Querying Table
35/36
Review
How Queries are Processed
Select Statement
Manipulating Column Names
Literals
Range Operator
List Operator
String Operator Wildcard
Unknown Value
ISNULL
8/9/2019 Module 07_Querying Table
36/36
Practice is the MotherOf SkillThank You.