Lesson 10 Creating Queries
Lesson 10 Creating Queries
10.1 INTRODUCTION
Access 2007 has a powerful feature of queries. Queries actually
mean the question you asked from the database to get the
desired result. A query can be designed to extract data from one •
or more database tables. Queries in Access can be created in
various ways and range from very simple field selection to
complex conditional statements or calculations.
, '.
10.2 OBJECTIVES
After going through this lesson you would be able to:
• create simple queries using wizard.
• create queries using design view.
• set various criteria in query.
• display query result.
• apply various logical operators to get only specific records
In query.
• create query based on sorted fields.
238 :: Data Entry Operations
10.3 CREATINGQUERIE~
Access 2007 allows you to create queries in two ways.
ExtemalOata Oatabas::..eT;.:.:OO:.:.:I'"'""':"'".:.s;Ad:.:;:.d·.:;,:In';
•••••••••.
~- ~~e~~~"":':""""':l~
••••••.•••••••••
.~ r= ~ nar.:l~ ~ 10PivotCI>.rt -= j f!i!i!l ta label,
T:W ~ ~ .~ ~ t1Sl DBlankForm = ~ Blank Rtport
Table Tabl. SilarePolnt Table Form Spl~ Multiple Form 1 Report Report
Te-mplate-s'" Usb" Design form Items ~ Mort Forms" Oesion Report Wtzatd Oeslon
Tabl~ Forms Reports
Fig 10.1
New Query
~erYW!iid
Crosstab Query Wizard
--=:1
Find Duplicates Query Wizard
I Find Unmatched Query Wizard
L OK .I [ Cancel
Fig 10.2
Creating Queries :: 239
_fioldsdovouwanlilv<u~1
You can moos.e from more tMn one table or qJeIy. __ .. You CZW)choose nom more than one bible or qJt:ty.
J
F!RSTIW£
lASTlW£
CITY
STATE
PINcooe
PHOI'£ NO
~_AoIds:
~NC
~STAiiiTE 8.----
~~~=
GJ
~
~AoIds:
6. In Fig 10.4, it has been shown how to select one field from
the available field's area and move it to the selected field's
area. In this case we have selected 3 fields, first name, last
name and phone number.
7. Click on the next
button. Next screen Simple Query Wizard
appears as shown in
Fig 10.5.
8. Type a title for your Thafs aI tn. informationthe wizardneeds to ceete you:
query in "t h e box ~y.
Doyou wonl to open tn. ~y or modifythe query's des9>?
provided as shown in
o Qpen the ~ to viownformation.
Fig 10.5. Here title for o Modifythe query cIe<i\In.
the query is Names
and numbers.
9. Click on the finish Cancel II <~ck r~yt) EWoh:
button.
Rig 10.5
:: Data Entry Operations
*
Fig 10.6
I student
rldtJtlPnt
IQueries I Both I
marl<s
'oers
-~I!
II
I II
I
II .
II
I - - -J
[::::)~I::]j gose 1
Fig 10.7
1. Click on the field name you want to display. (In this case it .•.
is student name field of student_pers table).
2. Drag it in the field line or you can double click on the field
to bring it to the field line (Fig. 10.8).
3. Click the Run button. Access shows the column you chose
(Fig. 10.9).
student narr •
--
"'----
,,-
':II --"-'Qw!y ""'~ ••,' "
III
mudit
• It.,.oQ
: ==~::..
~~-~::-':::==.::1,
_
I =~=,
'C~·._,
i SE~~'~, I<~"~
rfaisal
rahul
I
:II c- ••
-. ~ •..,.. :'"'••
,,-
'JI -_._-- ~_Ij
ishan
~:=
:==_... --- mainak
--.
____
_f-~'~~~~~~""'=="==~-
,ltnl(./ll:
"-"\'
~~~;~:;~"
~ rahan
abhinav
.I harshit
~~~= *
Fig 10.8 Fig 10.9
"' •....
;II _.wt.ot, "
(1-
IJI--
: ==~Iotr~r=-;=======p===p============~=====;=::::==~Gl¥======P===~P~
01 stuftftI.wtd
.j
Fig 10.10
45 sagar p.karya
11-"+-----'1.1------- -:r- -- - -1 ----
2: 30 mudit s.ksharma
II-,--~·-- -- - ~ -----~---__j
3 15 faisal it.ksingh .
11----1------.- - -- r - --- -
4 40 rahul r.ksinha delhi
------- - - - - - -- ---'
5; 20!ishan Ig.psingh .noida I
-- --=- 6~ __ ~8J~aina~ -j<i:'c ch~~iYedi-;i~~~~ J
7 431 rohan I n.k bahuguna.noida I
-----!L. -3,abhi':l.a~: lp,p~iir~ =-+~~id; ~-]
10
-----,--- - -17 harshit I r.k bhatia
-- - ~[delhi
--------"1I
* I
Fig 10.11
Creating Queries :: 243
T~ ~::''''5''U'1
• 1.tPtIllIltftOll
~H,:k I
';'~~SOfMlN( -t 1~~~~Jzn,fO!lBlOSOf'~'~:'t..
!!II
jl
fft1EHDSOfM!C".'1bIt
IUMfS.tHOHUl.illlS ~=
fRl!:~OfM!:JE
t SotHIC .••.
'?
~Tf !-
:~::';!o If
= ~
l.w..
Sort
~,
ShHO
flUENOSCfUll-ft:
0
flRSTtt.u.<f
fRlfI.OSOfl.f!Hf
0
l)oSTKloME
fRlf.'lOSOfJ.ei[
eJ.8
arr
fRlfJeKOf,..'"
51An
fRlEHDSOflMNf
"'<lnd""Ol
a
tiJ
FRlINDiOf LllUf
0
PI1OfffHO
flUEfC)SOf1Al1f
0 o o
d.l
Fig 10.12
a,\AMfs,~ NUMBERS fRIENDS OF MINE ~ QuelYl
~ fiRST NAME ~ jlAST NAME ~ I
---.;~~ CITY --"""""'=ST=ATE ~ PIN CODE ~ PHONE NO ~
I PUJA KAPOOR DELHI DELHI 110006 1122228980
r-=r---"""""
7 RAKESH KUMAR DELHI DELHI 110007 1123837367
6 SURBHI SARANG DELHI DELHI 110006 1122222222
2 SAGAR DOGRA DELHI DELHI 1100921 1122234345
1 RAMESH TALWAR DELHI DELHI 110007 1123456789
9 RAJESH BHATIA GHAZIABAD U.P 201010 l 1204678769
8 VANDITA SAXENA NaiDA U.P 201004 t 1204545454
5 VANDITA llAHUGUNA GHAZIABAD U.P 201014 1204556768
4 GEETA AWASTHI OIDA U.P 202004 1204555556
3 MANAV SHANKAR GHAZIABAD U.P 20101~-+ 12045!53360
-
*
Fig 10.13
244 :: DtdIJ Entry Operations
TABLE 10.1
Logical Operators
AI Tabits • «
_OIMIHE
!ill fltIIHDSOfI<M:T •••• fIl)ENCI.OfMNE _'
if SNNC .•.
@ NAMEl AND NUMBERS Fl~I'U.ME n
lASTNAME lJ
em IJI
:~OOE ~
PHOtJENO ..•.
, .
fJt1d: SUNO lFIRSTHAME lASTHAME .cn ·STATC. :~COOE!PHOHENO..L _~
TIbtt; fRlfHDSOfMlNE ,FRlEtlOSOfMIHE 'flIlENDSOHutl£ .rAUENDScifW;E~RIfN~OFMIHE ';FR1EtiOSCFMINf lFRIEHDSOf_~·'==- -4
SQrt: M<tfldinq, I , 1
-
(rUN:
a a El a _=_ .1lElW
0 . a i a - L_ lL J _D
or.
Fig 10.14
246 :: DtIIa Entry Operations
Rr<:ordi Sort&t1tr<
...
!-:---:_-::::::c=:::---="~'''.~''~'~l'~'S.
~ i Surd'l
""'" <••••••
Fig 10.15
You can apply multiple criteria to the same table. If you place two
criteria on the same line, Access will only retrieve records where
both criteria are met. For example, if you want all records where
the State is equal to "UP" and the CITY is equal to GHAZIABAD,
you would set the State field to = "UP" and the CITY field to =
"GHAZIABAD" and you would place both criteria on the same
line.
The steps for using and criteria are shown in Fig 10.16 and result
is shown in Fig 10.17.
If you place one set of criteria on the Criteria line and the second
set of criteria on the Or line, Access will retrieve records where
either of the two criteria is satisfied. You can add additional and
and or statements by using the lines below the Or line. For And
clauses, place the criteria on the same line; for Or clauses, place
the criteria on separate lines.
Creatin, Queries :: 247
:l.: T_ ~ProPtlt1Shtti
• TotAl! '\t~'!'aI'IIft~
="Sh0'l1ll1Idt
fiekt: SHHO fIRST NAME • LAST twtE CITY STto.n PlHCOOE PttOHEHO ,
TAbk: fRIENDS Of "11m
Show;
""
So't
... ~ .
flUfNOS Of MIN£
~
FlUfNOS Of MlNE
~
fRJf:NOS OF Mlt-lE
A'(fndin"g
·'GHAZlABAO'
~
fRIENDS Of "'1m
AICfn(lu~1l
"'U.I>"
0
fRlEHOS Of M1tlE
EO
-I FiUENOS
I
Of MINE
EO o - I o
.~
- .
Fig 10.16
r."~T-~~=~r
1~....usOf'"
WI
-
fRlfN050'MlNf:T... OOB~NOS fRl£NOS.Q! MN:
1m ~ ~:tMlc '=:::::
OAllOfBam1
~HAI'"
STATE
PPICOO£
PHONfHO •.
'"",
Show;
Cnt~
0 0 0 0 8 o o o o
'"
---~
Fig 10.18
PHONENO • J CITY
II--+--
SAGAR DOGRA -1 _ lI9/19'?~ 1122234345 DELHI
- - -
MANAV SHANKAR 8/13/1998 1204553360 GHAZIABAD
GEETA AWASTHI 8/11/1997 1204555556 NaiDA
VANDITA ,BAHUGUNA 6/13/1997 1204556768 GHAZIABAD
SURBHI SARANG 9/9/1999 1122222222 DELHI
RAKESH KUMAR 6/17/1996 1123837367 DELHI
VANDITA SAXENA 8/13/1998 1204545454 NOIDA
-- ----- - -
RAJESH BHATIA 3/1/1994 1204678769 GHAZIABAD
,- - -- - - -
PUJA KAPOOR 4/24/1995 1122228980 DELHI
*
Fig 10.19
After you create a query, you can save it. You can re-run a saved
query at any time. If you change the data on which the saved
Creating Queries :: 249
query is based, you will see the changes when you re-run the
query. The steps are as follows:
1.
-- Click the Save button on the Quick Access toolbar. The
Save As dialog box appears.
2. Type the name you want to give to your query.
3. Click OK. Access saves the query. (Fig 10.20)
4. Alternately you can also
save by right-clicking a
query's tab and then Save As
selecting save from the Query !lame:
!llenu that appears. IFRIENDS DOBi
5. Once you have saved a [.,OK I[ cancel .. I.
query, you can run it by
opening the Navigation Fig 10.20
pane and then clicking
the name of the query.