Presentation is loading. Please wait.

Presentation is loading. Please wait.

Notes on SQL This slide show will introduce SQL using Access. It assumes only an introductory level of knowledge about Access.

Similar presentations


Presentation on theme: "Notes on SQL This slide show will introduce SQL using Access. It assumes only an introductory level of knowledge about Access."— Presentation transcript:

1 Notes on SQL This slide show will introduce SQL using Access. It assumes only an introductory level of knowledge about Access.

2 This is the database that I am using to do my queries.

3 First I choose doing a query in design view the way I do when I am creating other queries.
Now I am going to select the table which is tablestu to do the query. I do this by clicking on Add and then clicking on Close since I am only using 1 table.

4 I have now selected the table, I click on the down arrow beside the Datasheet View icon to review SQL View. This is what I am going to select.

5 The table I selected is tablestu so it knows that I am going to SELECT information FROM tablestu. Note that the semi-colon ends SQL statements. Since I did not select any fields when I was in the query interface, just a table, this is what appears in the SQL Query window. Now I have to make changes to tell it what to SELECT.

6 SELECT * means to select all fields so we see all fields from all records in the table tablestu.
In SQL, you can select all of the fields from the database records by using an * or you can choose individual fields by naming them. The named elements will be separated by a comma as shown on the next slide.

7 SELECT idno, name, major, gpa, credits FROM tablestu;
The code: SELECT idno, name, major, gpa, credits FROM tablestu; means that only the named fields will be part of the query. Naming fields in the SELECT statement is the same as clicking on them or otherwise bringing them down to the query. You do not have to bring down all of the fields to make the query work. Note that the fields appear on the output in the order that they are named in the SELECT.

8 SELECT idno, name, gpa, credits, major FROM tablestu;
Notice that I said I wanted the order to be idno, name, gpa, credits and major in my SELECT and this is the order that I see the data in my output.

9 SELECT idno, name, major FROM tablestu WHERE major = “CI”;
WHERE lets me select specific records. In this example, I want to select the records where the major field contains CI. Note that the output only has the records where the major = “CI” which means that only 7 of the original 14 records are shown. The where clause allows me to put conditions on the select. In this example I only want records that meet the condition or criteria of major = "CI". CI must be enclosed in quotes because it is a literal using characters or text. If it was not enclosed in quotes it would be unclear whether this was a variable name or a literal. Note that the commands can be in either upper or lower case. In this example, the where was coded in lower case. I prefer the uppercase, because they stand out, but programming conventions seem to be leaning toward lowercase.

10 In this example, I am comparing gpa which is a numeric field against the number 3. The 3 is not in quotes because it is a literal and a number. Non-numeric literals must be in quotes, but numeric literals should not be in quotes. This query is showing the idno, name, major and gpa for all students with a gpa > 3. Note that I could also have used a decimal, for example gpa > 3.5.

11 This shows a compound AND where I want major = "CI" and gpa > 3. 25
This shows a compound AND where I want major = "CI" and gpa > Note that this is a typical AND where both components of the condition have to be true.

12 This first line got displayed because the major = “CI”
This first line got displayed because the major = “CI”. Since the gpa is not > 3.25 it would not have been displayed if both criteria had to be true. The third line got displayed because gpa > Since the major is BU, it would not have been displayed if both criteria had to be true. This is a classic OR where one or the other of the criteria has to be true to be displayed. Either one can be true. For that matter both can be true, but that is not what we are testing for. We are testing to see if either one is true.

13 Y Y Processed when Ques 1 AND Ques 2 must both be true Y N N Y N N
In the AND SELECT and the OR SELECT on the previous slide, we had two questions. When you have 2 questions, there are four possible outcomes and they are handled differently depending on whether there is an AND condition or an OR condition. AND: Ques Ques 2 Y Y Processed when Ques 1 AND Ques 2 must both be true Y N N Y N N Not processed when Ques 1 AND Ques 2 must both be true OR: Ques Ques 2 Y Y Y N N Y N N This shows the relationship of 2 questions when looked at when the conditions are in an AND relationship and when looked at when the conditions are in an OR relationship. Processed when Ques1 OR Ques2 must be true Not processed when Ques 1 OR Ques 2 must be true

14 Record 2 does not meet the gpa requirement but it does meet the credits requirement. Since one or the other had to be true, it is showing. Record 3 does not meet the credits requirement but it does meet the gpa requirement. Since one or the other had to be true, it is showing. In this condition, the major has to be CI and then either of the other two criteria must be true. Remember that in logic AND gets resolved before OR. This would mean that major = "CI" and gpa > 3.25 would be grouped together and credits > 40 would stand alone. This is not what we want. We can use parenthesis to change the order. By grouping the two conditions in the or relationship with the parenthesis, we end up with major = "CI" and EITHER gpa > 3.25 OR credits > 40 which is exactly what we want. All records have CI as their major since this is a requirement in our criteria.

15 Record 1 has credits of 15 which means it is less than 20.
Record 2 has credits of 45 which means it is greater than 40 Record 5 is outside the range of 20 to 40 because 3 is less than 20. In this example I want major = “CI” and credits outside a certain range. I do not want CI majors with credits in the range of 20 to 40. I want CI majors below the minimum of 20 and above the maximum of 40.


Download ppt "Notes on SQL This slide show will introduce SQL using Access. It assumes only an introductory level of knowledge about Access."

Similar presentations


Ads by Google