Databases for Many Majors
Queries in Access and SQL
Screen shots from Access
SQL: Queries are given as specified in SQL Server
Tables Note that Name was not allowed as a field name in Access so the attribute in Students was changed to Sname.
Query: Which students have taken 'College Algebra'?
Section: Introduction to Database Animation: Queries
Access:
SQL:
select S.Id, S.SName, S.Classification, S.Major from Courses C, StudentsTakingCourses T, Students S where C.Course = T.Course and T.Id = S.Id and C.CourseTitle = 'College Algebra'
Result:
Query: Find the semester that 'Jeff Carter ' took 'CSE 303'
Section: Query and SQL
Access:
SQL:
select T.Semester from Students S, StudentsTakingCourses T where S.Id = T.Id and S.SName = 'Jeff Carter' and T.Course = 'CSE 303'
Result:
Query: CSE returns unique Ids of students taking CSE courses
Section: Sets, supporting the discussion of combining tables as sets of tuples
Access:
Criteria for Course: LIKE 'CSE* ' Property Sheet: Unique Values: Yes
SQL: The keyword distinct provides unique values in the result. In SQL, a named query is represented as a view. Note that SQL Server uses the % sign as the wildcard to match the rest of the string. create view CSE as select distinct Id from StudentsTakingCourses where Course LIKE 'CSE%';
Result: Similarly, for students taking MAT courses
Query: Ids of students who took CSE or MAT courses
Section: Sets, illustrating union
Access: Union Query
Access QBE does not support set operations: union, negation, intersection However, Access does support the specification of the union query in SQL.
SQL: Note that the * symbol represents a shortcut for selecting ALL attributes from a table.
select * from CSE union select * from MAT;
Result:
Query: Ids of students who have taken CSE courses and not MAT courses
Section: Sets, illustrating negation
Access:
Negation set-based queries are not inherently supported in Access QBE or Access SQL. However, Access does provide a “Find Unmatched Query Wizard” that will generate a complex SQL statement that will find the answer. A discussion of the generated SQL is beyond the scope of this work.
SQL:
select * from CSE except select * from MAT;
Result:
Query: Ids of students who have taken CSE courses and MAT courses
Section: Sets, illustrating intersection
Access:
Intersection queries are not inherently supported in Access QBE or Access SQL. However, there are typically multiple ways to find an answer to a query. In this case, the same result can be obtained by joining CSE and MAT so that the Ids are equal.
SQL:
select * from CSE intersect select * from MAT;
Result:
Query: Find the students who are 'Math' majors
Section: Filtering, illustrating horizontal filtering
Access:
SQL: Recall that the * symbol represents a shortcut for selecting ALL attributes from a table.
select * from Students where Major = 'Math';
Result:
Query: Retrieve the Name and Classification of all students
Section: Filtering, illustrating vertical filtering
Access:
SQL:
select SName, Classification from Students;
Result:
Query: Find the Names of students who are 'Math' majors
Section: Filtering, combining horizontal and vertical filtering
Access:
SQL:
select SName from Students where Major = 'Math';
Result:
Query: Illustrating a Cartesian product of students who have taken a MAT course with a table representing the vertical filtering of Id and Course on the StudentsTakingCourses table.
Section: Joining, illustrating Cartesian product
Access:
SQL:
create view NameIdStudentsMAT as select distinct S.SName, S.Id as MId from StudentsTakingCourses T, Students S where Course LIKE 'MAT%' and T.Id = S.Id;
create view TakesMATCourse as select Id as SId, Course from StudentsTakingCourses where Course LIKE 'MAT%';
select * from NameIdStudentsMAT, TakesMATCourse;
Result:
Query: Illustrating a join of students who have taken a MAT course with a table representing the vertical filtering of Id and Course on the StudentsTakingCourses table.
Section: Joining, illustrating a join is a Cartesian product followed by horizontal filtering
Access:
SQL:
select * from NameIdStudentsMAT, TakesMATCourse where MId = SId;
Result:
Querying Summary terminology symbol QBE in MS-Access SQL project; vertical filter
π √ in “show” box select
selection; horizontal filter
σ “criteria” where
which tables are needed
Show Table in Design View
from
Cartesian product
X commas in “from” clause
(natural) join
automatic (links shown in relationships diagram)
condition in “where” clause (may need to prefix attribute names!)
union
“or” in “criteria” union
intersection
“and” in “criteria” intersect
negation
‐ “and not” in “criteria” except
Top Related