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

Queries Notes

Database Notes

Uploaded by

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

Queries Notes

Database Notes

Uploaded by

csecclassnrmss
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
Peron ean @ ‘Computers have important advantages over manual systems since they can work extremely quickly, and automatically, to make calculations and to retrieve and sort data into some useful form. All these functions are known as data processing. Searching Searching a database is reasonably straightforward once ‘you realise that you must tell the program precisely what to search for. You do this by giving clear criteria (conditions) for the search. You might know what you want to find, but the software does not ~ until you tell it. A query (also known asa filter or search) is used to answer a question using the data in a database. The database is searched to find all the records that match a particular condition. A query is the question you ask, such as ‘List the first names of the customers who ordered round labels.’ it is not the results (for example, ‘Ann and Lou). You may make several queries to ask different specific questions. When a query is‘run’it produces as output Simple Query Wizard ables queries [Fable Product ‘valable Feds: << cancel ij 820 Using the Query Wizard 0 slat felcs ina query a list of all the records that match the condition that defined the query. Once you have created a query, you can run it as often as needed. Even the results of the query will be updated if you add more data to the table. When you begin to design your queries, ask yourself © What specific question do I want to ask? + What data (fields) do I need displayed? * What sort order will help me the most? Queries can be as simple or as complex as you like. “To search a database, you must first enter the search, criteria into one or more fields for their tables. Then you instruct the program to find all the records that match the conditions that you have set. For example, to find the products with orders that are less than 50, you would type <50 in the QTY field of the Order table. First Description ary Lou ‘Small Clips 10 Bev Pocket Diary 5 Ann Small Clips 10 ‘which fields do you wantin your query? ‘You can choose from more than one table or query Bl Selected Fes: lot ‘nish 8 Databases FB customers who ordered products with quantities of less than 50) Product B prad Description cost Customer Foo Last Fist Description Product Description | QTY ‘small Clips Pocket Diary Wall ‘Small Clips * Record M(t of Fig 82! Creating aque to ist the procs with orders of ess than 50 After executing the query, you may see one or more records that were produced from your search, ‘The comparisons that you can make in queries will depend on which database program you are using Table 85 lists some common operators used. Searching for specific records ‘The word ‘criteria’ in a database means you want to find only certain records and ignore others (the ‘ones that do not meet the criteria conditions). If you want to limit the results of your query, you can set up specific criteria. If a record meets those criteria, it a oor No fiter_| Search is included in the results. Note also in Table 8.5 that the criteria does not include any formatting such as the dollar sign ($) or percentage symbol (9%). Any search using text as the criteria is enclosed in double quotation marks. For example, suppose you wish to find the product with ID P3430. This produces one result: Small Clips costing $2.75. Figure 8.22 illustrates how you place P3430 in the criteria row below the field name where you wish to find the specific records. The other fields can be selected (ticked) to indicate that you wish to see their results as well. IF you only wish to see the product description and the costs, make sure only those two fields are selected. 8.5 Searching and sorting Table 8.5 Operators used in searching databases Operator Meaning Example = Equal to *P3122" searches the product ID number field to find all products with that identical product ID > Greater than > 5 searches the cost field to find all products that cpst more than $5.00 Less than ‘<5 searches the cost field to find all products that cost less than $5.00 os Greater than or equal to >= 5 searches the cost field to finds products whose cost is greater than or equal to $5.00 = Less than or equal to <= 5 searches the cost field to find products that cost less than or equal 1055.00 * Asterisk, known as a wild-card character, can “R* or like*R* will find all products whose first character or letter beused to represent one or more characters begins with R, for example Receipt books and Round labels Oesripton of Proaucid a0 x Products costing more than $5 = Product ° Bodud. “ io nea 8 prio re Description cos ‘ . aI , fat (rio Desction ce Description cos * Te oa rele roost] — =. saove a a a ot: 3 on . > a ig 82 Searching fora descotion fo product P3430 ‘ > Also notice that there is only one table selected. If iption Cost the fields you need for the query are found in one Packet Diary 3895 tuble then the other tables should be deleted from the [Receipt Book 3755 query. Otherwise you may find duplicate records in ig 823 Query (find products that cost mare than $5.00 ands sue your result. To delete a table in query design, select the table, right-click and select remove table or select the table and press the Delete key on your keyboard. Figures 8.23 and 8.24 show other examples of how to find products that cost more than $5.00 (Fig 8.23) or those that begin with ‘R’ (Fig 8.24). 8 Databases FF cunt x Product oo Description cost Field Table: Sort: Show: crteria: * No fites |Search] Fig 24 Query to fndthe names of products that begin with Rand as result Queries using more than one field ‘You can create more complicated queries by linking together more than one search condition. For example, find the customers who ordered products costing less than $5 in quantities of more than 100. Figure 8.25 shows how in Microsoft Access <5 and >100 are placed on the same criteria row but under their field names. This joins the two queries with the AND operator. Another query finds the customers who ordered products costing less than $5 OR products with quantities of more than 100. Figure 8.26 shows how in Microsoft Access <5 and >100 are placed on two lines labelled as ‘criteria’ and ‘or’ It does not matter which one is entered in the criteria or the OR row once they are not in the same row. This joins the two queries with the OR operator. The result below can be saved as Less than $5 or More than 100. First Description | Cost ‘ary Lou ‘Small Clips 3273 10 ‘Ann ‘Small Clips $275 10 Tou Round Labels $395 500 Ann Round Labels $395 100 Lou Black Pen $1.25 50 Product C Fon vo = |t__a| oem bd 2 PRD ES wma nemo fl om RE Fig 8250 Query find the customers who.rdered products casting less than Sin quantities ofimore than 100 a 8.5 Searching and sorting | -P customers who ordre products with quantities of less than 50)\ x Description «| Cost. | any - Round Labels $3.95. +500 * [Recor #11» won| = NoFiter | Search ig 825 rests fom query to find the customers who odered proces costing less than $5 in quonties of more than 100 | tata eto ton 8 ~caoner | [om | Prout so [4 _.| ye 1) 9 mo tat eel] carts me | con |__| cra G le ex ‘Desenption ‘cost ant 1 Cone room cert oar 1 — a = ce - ig 826 Query to ind the customers who oidered products costing less than $508 quanaties ofmore than 100, Notice that in these queries all three tables were used since fields were needed from each table. However, if fields from the Customer and Product tables were needed, the Order table should not be deleted since it is linking these two tables in the database. For example, suppose the Bookshop needed to contact all customers who ordered Black Pens.’The First and Last field names would be selected from the Customer table, while the Product ID (PR-ID) or Description for the Black Pens could be selected from the Product table and not the Order table. ‘The words AND and OR are the only ones that can be used to link two simple conditions together to make a more complicated query:’The difference between using AND or OR to join two conditions is: » AND: Find only records which match doth of the conditions, * OR: Find records that match either or both of the conditions, Reversing queries Sometimes you may want to search for records which do not match a particular criteria. For example, you may want to find all customer orders except those for customer Lou. The keyword NOT is used to do this (Fig 8.27a). et Ds 8 Databases (Wh Garo ets ecg Product ‘Customer oar va jt 2] Fm tat Fimo Fest -_ Fat: Te Fig8.27a Qvery:ofind at rer NOT paced by Customer Lou (5 conor oor capi x Deterpton = scp ga a ev Pocket Diary $8.95 on Small Cpe $275 Fe an Rovnd Labels $3.95 100 ier wn pt [Se Fig 27o Result of ery 0 find al orders NOT placed by Customer Lou Sorting Most databases will let you sort data so that it is displayed in a specified order. To sort a database into an order you must specify: + which field in the database you wish to use to order the records ‘+ whether you want the records in ascending (A to Z) 21 or descending (Z to A) order Al , Sorting records temporarily reorders the database file, Sorting allows you to browse, update, export or print records in a particular order. You can also sort numbers, dates and times in ascending and descending order. Being able to sort records quickly is one of the advantages of using a database, Depending on your version of Microsoft Access, you may have one or more methods of sorting your data. However, the result remains the same. “The following three examples show how your results can be sorted by the last name field in ascending order. OSS Figure 8.28 shows how the sort option in Design view is used to order the data. customer ig 8.28 Design view showing how the lst pamesof the customers can be sened m ascending order Figure 8.29 shows how the Sort and Filter menu Datasheet view is used to sort the data, Fig829Using the Sot and iter meny option in Datosbee! view to sort the dota =) Deiciton =| Cant =| OY $595 3 S75 0 13s 0 5395 soo + srs 10 S195 100 755 100 Fig 830 Using thesort option tothe right ofthe feld name tacxder the dara Questions 1. Select the most suitable options that describe the sorting of data as A to Z: 4 ascending i descending top to bottom iv bottom totop, Use the query template shown in Figure 8.31 to answer questions 2 and 3: 2. You wish to lst the names of the products that cost under $3.00. ‘a Write one or more tables that would be used in the query. 8.5 Searching and sorting Figure 8.30 uses the Sort option in the field names to order the data. All these methods will give the following results. Last First [Description [Cost [QTY King Bev | Pocket Diary $895 5 Tedd Tou __| Small Clips 5275 10 Tedd Tou [Round Labels 33:95 | 500 Tedd Lou | Black Pen 12550 Wall ‘Aan | Small Clips s275[ 10 Wall ‘Ann __| Round Labels $3.95| 100 Yod Ed__| Receipt Book 3755| 100 b List the field names that would be used in the query, € Write the field name and the criteria for the query. d_ How would the criteria change ifthe query was changed to list the products that cost $3.00 or fess? 3. Youwish to list the names of the products that are on discount. a Write one or more tables that would be used in the query. List the field names that would be used in the query € Write the field name and the criteria for the query. Coomer ‘onter vo0 12] 20 us Imo ome Desergin x Fig8.31 Query template

You might also like