Module 7
Querying Tables
Vidya Vrat Agarwal. | MCT, MCSD
Overview
How Queries are Processed
Select Statement
Manipulating Column Names
Literals
Range Operator
List Operator
String Operator – Wildcard
Unknown Value
ISNULL
How Queries are Processed
Process Description
Parse Checks Syntax for Accuracy.
Resolve Validates that the names of the Objects are Present
Optimize Determines the indexes to use
Compile Translates the Query into an Executable Plan
Execute Submits Compiled requests for Processing and then
Run.
Select Statement
Select statement is to access and retrieve data from
database.
The Keywords Select, From, Where makes up the basic
Select Statement.
The basic from of the select statement sometimes called a
“Mapping” or a “ Select-from-where” block.
Selecting Data
Select name from Students
Select All name from Students
Select Distinct name from students
Manipulating Column Names
When query results are displayed, a column name of the
result set is always displayed as it was specified at the
time of Table creation.
Create Table Students
( Sid int identity,
Name varchar(20)
)
Select * from Students
A User defined Column heading can replace the default
column 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 be
specified in place of the default Column heading.
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 Books
9999 Lucerne Publishing
(8 row(s) affected)
Literals
The result set of the data query statement can be made
more readable by including a String called Literal in
Select 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
Literals
Use pubs
select 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 & Hardley
1389 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)
Range Operator
The Range Operator is used to retrieve data that can be
extracted in ranges.
The Range Operators are :
Between and
Not Between
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)
List Operator
SQL Server allows the IN operator that allows the
Selection 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)
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)
String Operator - Wildcard
SQL Server provides a Pattern-Matching method for string
expressions using LIKE Keyword with the Wildcard mechanism.
Wildcard Description
% Represents any string of zero or more character (s)
_ (UnderScore) Represents a Single Character.
[] single character within specified range. [a-f] or [abcdef]
[^] single character not within specified range. [^a - f]
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 any
single letter from M through Z (Ringer).
LIKE 'M[^c]%' searches for all names beginning with the letter M that do not have the letter
c as the second letter (MacFeather).
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)
%
SELECT phone
FROM [Link]
WHERE phone NOT LIKE '415%'
Or
SELECT phone
FROM [Link]
WHERE NOT phone LIKE '415%'
_ ( 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)
[]
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)
[]
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)
UnKnown value
Unknown values refer to the data entered in the form of
NULL keyword.
The NULL values can be retrieved from the table using
IS NULL or IS NOT NULL keyword in the where clause.
select title,ytd_sales
from titles
where ytd_sales IS NULL
title ytd_sales
-------------------------------------------------------------------------------- -----------
The Psychology of Computer Cooking NULL
Net Etiquette NULL
(2 row(s) affected)
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)
Check Your Understanding.
Q.1. How Query is Processed.?
Q.2. What is the Purpose of SELECT Statement.?
Q.3. What is the difference between ALL and DISTINCT.?
Q.4. What is Column Alias.? What is the Advantage.?
Q.5. What is Literal and what is its purpose.?
Q.6. What are Range Operators and what is the Purpose.?
Q.7. What is List Operator and what is the Purpose.?
Q.8. What are String Operators- wildcard, and what are the
types and Purpose.?
Q.9. What is UNKNOWN value.? How this Value can be
Retrieved.?
Q.10. What is the Purpose of ISNULL.?
Review
How Queries are Processed
Select Statement
Manipulating Column Names
Literals
Range Operator
List Operator
String Operator – Wildcard
Unknown Value
ISNULL
Practice is the Mother
Of Skill
Thank You.