0% found this document useful (0 votes)
39 views

Lesson 10 Creating Queries

The document discusses creating queries in Access 2007. It describes using a query wizard to create a simple query, guiding the user through dialog boxes to select fields from a table. It also explains creating a query in design view. The summary provides the steps to create a query using the wizard, including selecting a table, fields, and specifying a title for the query results.

Uploaded by

ektameena0018
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Lesson 10 Creating Queries

The document discusses creating queries in Access 2007. It describes using a query wizard to create a simple query, guiding the user through dialog boxes to select fields from a table. It also explains creating a query in design view. The summary provides the steps to create a query using the wizard, including selecting a table, fields, and specifying a title for the query results.

Uploaded by

ektameena0018
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Creatmg 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.

(1) Using the query wizard


(2) Using design view.

10.3.1 Creating a simple query using the query wizard.


When you use this method for creating query, the query wizard
guides you through ~ series of dialogue boxes and you can
proceed by selecting various options. The steps are given as
follows:
1. Click create tab. ...
2. In the other group, click on the query wizard (Fig 10.1).
3. When new query dialogue box appears, Select simple
query wizard and click OK (Fig 10.2).
FRIENDS OF MINE: Database (Access 20(7) - Microsoft Access

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

This wizard creates a select query


from the fields you pick.

L OK .I [ Cancel

Fig 10.2
Creating Queries :: 239

4. A simple query wizard dialogue box appears. From the


Tables/Queries list box, select the table on which you want
to create the simple query. Here we have selected the table
named Friends of Mine .(Fig 10.3).
5. All the fields of selected table are shown in the available
field's area. Select the field you want to add in your query
and then click on the> button. If you want to select all the
fields than you can click on » button. (See fig 10.3)

Simple Query WIZard Simple Query WIZard

_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:

jjext > :I [ Eri'b CNocei I r,;>. [ tiexl > I[ fri!h

Fig 10.3 Fig 10.4

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

10. Access 2007 displays the result of query in the form of a


table as shown in Fig 10.6. Since we have asked only for 3
fields, First name, last name and the phone number to be
shown from the table Friends of Mine, only these 3 fields
are displayed as you can see in Fig 10.6. This is a simple
query.
FRIENDS OF MINE :§j NAMES AND NUMBERS

FIRST A ~E· LAST NAME • i PHONE NO •


TAlWAR 1123456789

.. SAGAR DOGRA 1122234345


JMA AV SHA KAR 1204553360
IGEETA AWASTHI 1204555556
VA DITA BAHUGU A 1204555768
SURBHI SARANG 1122222222
L RAKESH KUMAR 1123837357

'- VA DITA SAXE A 1204~4


1RAJESH BHA TIA 1204578759

r, PUJA KAPOOR 1122228980

*
Fig 10.6

10.3.2 Creating Query Using Design View


A query can also be created using Design View in which you can
define the structure of query. To create a query, you open
the tables on which you are going to base your query in Query
Design view, and then use the options to create your query. Click
the Run button to display the results. Queries can be saved for
later use also. The steps are as follows:

1. Click the Create tab.


2. Click the Query Design button in the other group. The
Show Table dialog box appears (Fig. 10.7).
3. Click on the tables tab. A list of tables appears.
4. Double Click to choose the table on which you want to base
your query. Here student_pers table has been selected
(Fig10.7). (You can select more than one table also).
5. Click Add. The table appears in the window.
6. Click Close to close 'the show table box. Access changes
to Query Design view. If you select more than one table, you
have to show relationship between them through Primary
key.
Creating Quem. :: 241

Show Table I~~


-"-

Tables

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

To Show a single column:

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

Showing Multiple Columns


You can use an Access query to show multiple columns of data.
On the Field line in Query Design view, choose the field name
of each field you want to show in the same order as you want to
display them in the query. The steps are as follows:
242 :: Data Entry Operations

1. As the students table is already selected you can select the


field from the given list.
2. Click on the field names you want to show one by one in
the order you want to display them. For example you can
place roll no field before the student name field in the
field's grid area (Fig 10.10).

0'..- ~U¥lon. ~TII'" AtrOtHt Ot'IOn

- ~ i!!i!' +! "" [!!I x! 00...... '9.::l ~--,- r •.•,,",",,,, 1: IJ'•••••• "".


- • • • I'-.I.lhroUl/f> ~~'6' ~¢W1.,. Odft. Cotu-nl l.tM
H1=""'P "\kIdM.ClO••t.bDrift.t.'f£DotoOd1rlil:ioII ~~:;:;::..'I.riI"U
•.•• ~~um:M • TotaI''\th ••• ct."
~~( o..c,),W1>9 ~ SlI_/l:1O<:I1

"' •....
;II _.wt.ot, "

(1-
IJI--

: ==~Iotr~r=-;=======p===p============~=====;=::::==~Gl¥======P===~P~
01 stuftftI.wtd

: :::._-""'' ' ' i=-;' ;=..":' !~-; LW n-- -!- !'r-- ~ II

.j

Fig 10.10

3. Click the Run button. Access displays the columns you


chose (Fig 10.11).

studentnarr > parent'snan-+_._


• .••.•.••
!.o

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

10.4 SORT A QUERY


When creating a query, you can sort the columns in ascending
or descending order by choosing the option you want on the Sort
row in Query Design view. The steps are as follows:
1. Open the student_pers table in the design view
2. Select and drag the fields in the field grid area in the order
you want them to be displayed.
3. Click in the sort row in the column which you want to sort
(in this case it is the State column) and Click the down
pointing arrow that appears and then choose ascendi~g (or
descending)from the drop down list (Fig 10.12).
4. Click Run button.
5. The sorted column is shown along with others in the Fig 10.13.

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

10.5 EXTRACTING RECORDS USING CRITERIA


STATEMENTS
Adding a criteria statement to the query design grid will help you
to extract only those records which match the given criteria and
which you want to display from the complete table.
You can use logical operators such as = (equal), <> (not equal), >
(greater than), or < (less than) to restrict the records you retrieve.
For example, if you only want to display friends who live in DELHI,
enter = "DELHI" in the State column on the Criteria line. Access
will only retrieve records where the value in the State column is equal
to DELHI. Selection criteria are not case-sensitive, so Access will
retrieve records where the entry is DELHI, delhi, Delhi, or dELHI.
The list of logical operators which you can use in making criteria
statements in making your query is given in the table 10.1.

TABLE 10.1
Logical Operators

Operator Meaning Field Type .Entry Format

= Equal to Character = "DELHI"


Number =5
Date = #12/16/98#
<> Not equal to Character <> "DELHI"
Number <> 5
Date <> #2/16/97#
> Greater than Character > "DELHI"
Number >5
Date > #7/16/98#
>= Greater, than or equal to Character > = "DELHI"
Number >=5
Date > = #6/21/96#
< Less than Character < "DELHI"
Number <5
Date < #4/18/98#
<= Less than or equal to Character <= "DELHI"
Number <= 5
Date <= #11/19/98#
In Equal to any item in a list Character In ("DELHI", "UP")
Number In (5, 9, 17)
Date In (#2/11/99#, #2/3/96#,
#12/15/98#)
Not In Not equal to any item in a Character Not In ("DELHI", "UP")
list Number Not In (5, 9, 17)
Date Not In (#2/17/97#,

Between Between two values,


. Character
#2/3/96#, #12/15/98#)
Between "C" And "F"
greater than or equal to Number Between 5 And 10
one and less than or equal Date Between #1/1/98# And
to the other #12/31/99#
Cretlting Queries :: 245

To retrieve specific records, the steps are as follows:


1. Open a table or query in Query Design view.
2. Choose the fieldnames you want to display in the order you
want to show them.
3. Choose the fieldnames you want to sort by in the order you
want to sort.
4. Under the fields you want to sort by, choose Ascending or
Descending.
5. Deselect the Show button for columns you do not want to
display.
6. Enter your selection criteria on the Criteria line (Here the
criteria entered in the state column is = "DELHI")(Fig
10.14).
7. Click the RUD button. The query results can be viewed in ....
the Fig 10.15.

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

. ;J, ,;;,_I'rw :riot.h U


~ ..!!.I .:§ Sft! ~ 5ptling 11 tJAdHtKtd'"
=::: oIb./ ~~h x~.!lt.&Q(t. 1; ftttl '"opf>llttt

Rr<:ordi Sort&t1tr<

• STATE • PIN CODE • PHONE NO •


DELHI UOOO6 U2222$80
DELHI 110007 1123837367
DEtHI 1l.OOO6 lU2222222
DEllil 110092 1122234345
DElHI 110007 1123455189

...

!-:---:_-::::::c=:::---="~'''.~''~'~l'~'S.
~ i Surd'l
""'" <••••••

Fig 10.15

Adding Multiple Criteria

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

QueryTool$ FRIENDSOF MINE: Oatahase (ACcess 2001\ - MIcrosoft ACCHS

: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

~.:.J!!!!!:m..:::j~FIRST NAME • CITY


RAJESH BHATIA GHAZIABAD
'-'1-------1"-
5 VANDITA ;BAHUGUNA GHAZIABAD
3 MANAV I SHANKAR GHAZIABAD
--------~ ~ -
-----'--- -
Fig 10.17

10.6 CREATE A QUERY USING TWO OR MORE TABLES


A query can also be created using two or more tables. The only
thing you have to do here is to see that the tables from which you
extract your data should have a relationship. The steps are as
follows.
1. Double click the required tables in the show table dialogue
box (In this case table selected are FRIENDS OF MINE and
DOB FRIENDS). Close the show table dialogue box. Two
tables are shown in the figure 10.18 with relationship line
.joining the fields SLNO of both the tables.
2. Choose-the field names you want to retrieve in the order
you want to display them, from both tables.
248 :: DtIIIJ Entry Operations

r."~T-~~=~r
1~....usOf'"
WI
-
fRlfN050'MlNf:T... OOB~NOS fRl£NOS.Q! MN:

:jl tw.uSANOtA1I.18ERS 'f 10 ''''NO


001''''''''-- •. SLNO """NAME ~

1m ~ ~:tMlc '=:::::
OAllOfBam1
~HAI'"

STATE
PPICOO£
PHONfHO •.

fidd: fYtST twotf lAST NAME o<\Tf Of BIRTH PHONE If() em


TabM: D08 fRlf.NOS DOS AUtHOS Doe flUfHOS fRlfHOS Of MIUf Ht(fH)S Of tANf

'"",
Show;
Cnt~
0 0 0 0 8 o o o o
'"

---~
Fig 10.18

3. Click the RUD button. Access retrieves the columns you


chose and displays the rows in the order you specified from
the two tables. (Fig 10.19)

PHONENO • J CITY

_--4 TALWAR 4/14/1995 1123456789 DELHI

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

10.7 SAVE A QUERY

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.

10.8 INTEXT QUESTIONS


1. Fill in the blanks.
a) Simple query wizard option is available in the--- I
- dialogue box.
b) Access 2007 displays the result of query in the form
of a --------
c) Queries can be created using the Query Wizard or the
-------vlew.
d) In order to see the result of query in design view, you
have to click on the button.
e) -----operators are used to extract specific
records which meet the given criteria.
2. Write TRUE or FALSE.
a) While sorting the records two options available are
Up and Down.
b) A query is saved with a name ..
250 :: Data Entry Operations

c) Queries actually mean the questions you ask from


the database to get the required result.
d) You cannot run a saved query at any given time.
e) To show the result of query, click on the "Final"
button.

10.9 WHAT YOU HAVE LEARNT


After going through this lesson you learnt about queries, different
ways of creating queries such as simple query and design view
method and query based on a single table or multiple tables. You
also learnt how to save and run the query.

10.10 TERMINAL QUESTIONS


1. How can you create a query based on'"2 tables? What is the
, most necessary condition for creating such type of query?
2. What is a criteria? What are the various logical operators
used to create criteria statements in queries?
3. How can you change the positions of the columns in the
query design view? Explain with an example.
4. How can you add multiple criteria to make a query? Give
example.

10.12 FEEDBACK TO INTEXT QUESTIONS


1. (a) New Query (b) Table (c) Query Design/Design
(d) Run (e) Logical

2. (a) False (b) True (c) True


(d) False (e) False

You might also like