Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queries and SQL in Access

Similar presentations


Presentation on theme: "Queries and SQL in Access"— Presentation transcript:

1 Queries and SQL in Access
This slide show will introduce SQL in Access.

2 Textbook.mdb This is the database that we are looking at. The database has one table called book.

3 Query - all fields, all rows
This shows the results of a query showing all columns/fields for all rows/records. The SQL equivalent of this query is shown on the next slide.

4 The table name also appears in the FROM clause
Query with SQL All columns/fields for all rows/records means that in this case all of the columns/fields are listed in the SELECT clause with their table name in front. The table name also appears in the FROM clause Note that SQL code end with a semi-colon. This shows a basic SELECT where columns/fields are listed in the SELECT and the table name is given in the FROM.

5 SQL versions In this version, the book.ISBN has been changed to ISBN - this has been applied to all columns/fields. The table name is only needed if two tables are being used and the column/field appears on both. When Access translates the query into SQL it creates a query that is more complex than required if you know SQL. On this slide, I have include two simplified SQL codings. In this version, the SELECT * means select all columns/fields. You only need to list the columns/fields by name if you want only specific ones to appear in the results.

6 The results are shown below.
Query - AND This is an AND query asking for all books with a year published greater than 1998 AND a price > 40. The results are shown below. This shows the simple AND query and the results.

7 The FROM clause specifies the table name.
SQL for AND query The SELECT statement is specifying certain fields/columns that should be shown. In this case the fields are ISBN, title, yrpub and price. As you can see the table name is shown in front of the column name in the format book.ISBN etc. When a query is developed, the SQL is automatically generated. The FROM clause specifies the table name. The WHERE clause gives the condition. In this case the condition is the yrpub > 1998 or price > 40.

8 1998 is in quotes because it was defined as a text field.
SQL - AND The SELECT only lists the columns/fields by name because there is no chance of duplication with only one table being used. The WHERE is coded by simply using the column name, the comparison operator and the field being compared against. 1998 is in quotes because it was defined as a text field. 40 is not in quotes because it was defined as a number (currency) field. The simplified version eliminates all of the unneeded parenthesis around the fields. They are their for clarity in some sense of the word!

9 OR Query Note that the OR requires separate lines in the query. Yrpub must be greater than 1999 OR price must be greater than 40. Either one is sufficient.

10 SQL - OR Again I have eliminated the table name in the select because there is no chance for duplication and the multiple parenthesis in the WHERE. This is very similar to the AND query on the previous slides. The simple version is shown as well!

11 The table is shown in ascending order by ISBN.
AND - OR The table is shown in ascending order by ISBN. The query wants books with price greater than 40 AND with a pubid of either 001 OR 002. This is a case where the price must be greater than 40 AND either the pubid is 001 or the pubid is 002. Note that when the condition is expressed in logical terms the OR has to be surrounded by parenthesis. price > 40 AND (pubid = 001 OR pubid = 002) The rule is ANDs are resolved before ORs. If there were no parenthesis then the two conditions around the AND would be treated as a unit and the condtion after the OR would stand alone.

12 Notice the ORDER BY ISBN clause which accomplishes the ascending sort.
AND - OR Notice the ORDER BY ISBN clause which accomplishes the ascending sort. Logic: cond A AND either (cond B OR cond C) It should be notes that when I went back and looked at the query after these changes they were expressed differently. Price must be greater than 40 AND either pubid = 001 OR pubid = 002. Note the way the OR is expressed.


Download ppt "Queries and SQL in Access"

Similar presentations


Ads by Google