Copyright 2003 Curt Hill Queries in SQL Syntax and semantics
Copyright 2003 Curt Hill
Queries in SQL
Syntax and semantics
Copyright 2003 Curt Hill
Select is the basic query command
• The most common form is:SELECT field1, field2…fieldnFROM table1,table2…tablemWHERE condition
• Select f.name, c.dept, c.number, c.crhrFrom faculty as f, course as c, faculty_teach as ftWhere f.naid=ft.naid AND ft.dept = c.dept AND ft.number = c.number
Copyright 2003 Curt Hill
Syntax
• The statement is free form– May be on one line or many– Words, constants and strings must be
complete on one line• Punctuation is minimal
– Commas in lists• My prefererance
– Select, From, Where on separate lines– One condition per line
Copyright 2003 Curt Hill
Three pieces
• Select– Describes the resulting table
• From – Lists the tables that will be used to generate
the final table
• Where– Gives the conditions or comparisons
• Join fields• Selection comparisons
– Where is optional
Copyright 2003 Curt Hill
Select• List fields desired• This may include constants and
calculations• Field names may be qualified by
table names if needed• Corresponds to the project of
algebra• Use * to get all fields• Many clauses to be discussed later
Copyright 2003 Curt Hill
From• Specify the tables• Allows renaming the table for
convenience in the rest of the query• Form is:
FROM Tab1, Tab2 AS t, Tab3 u– The AS reserved word renames for the
rest of the statement– May be left out
• Only tables mentioned here may be accessed
Copyright 2003 Curt Hill
Example Without Where
• Select name, degreeFrom Faculty
• Select *From Faculty, Faculty_Teach– Cartesian Product
Copyright 2003 Curt Hill
Where
• Supply conditions• Compare the field name with a value
– Does the selection part
• Compare two fields from different tables– Does the join
• Is actually optional but leaving it out gives the whole table or a cartesian product – Not a join
Copyright 2003 Curt Hill
Where Comparisons
• Comparison operators– =– >– <– >=– <=– <>
Copyright 2003 Curt Hill
Where boolean operators
• And• Or• Not• ()
Copyright 2003 Curt Hill
Student Score Example• Select name, dept, course, score
From Students, GradesWhere Students.naid=Grades.naid
• Naid need qualification, since it exists in both, rest are unique
• From allows rename or synonyms• Select name, dept, course, score
From Students s, Grades as gWhere S.naid=G.naid
Copyright 2003 Curt Hill
Course a faculty member teaches
• Select name, ft.dept, coursefrom faculty, faculty_teach ftwhere faculty.naid = ft.naid
• Put the credit hours in:• Select name, ft.dept, course, crhr
from faculty, faculty_teach ft, course cwhere faculty.naid = ft.naid AND ft.dept=c.dept AND ft.number = c.number
Copyright 2003 Curt Hill
All students who got a B or better in any CS class
• Select nameFrom grades, studentsWhere score>=80 and students.naid = grades.naid And dept = ‘CS’
Copyright 2003 Curt Hill
Syntax Again• Reserved words and names are not
sensitive to case• Blanks, tabs, line feeds and other white
space are ignored• Layout only affects readability• The order of things following Select
determines the table order• The order of tables following From does
not matter• The order of conditions does not matter
Copyright 2003 Curt Hill
Designing Queries• Start with the From clause
– What tables are needed– Will duplicates of these be needed
• Make the Where next– Consider joins first– Consider selection next
• Finally design the Select– This is the final projection that shows
the fields that you will see
Copyright 2003 Curt Hill
Considering Joins• How are tables connected?• Two tables can be joined on any
sets of fields provided:– The number and type of each set
matches• Names do not need to match• The types are defined in the create table
statement
• Especially look at foreign keys– Items in one that are primary in the
other– Most joins are equijoins on a foreign
key and primary key
Copyright 2003 Curt Hill
Find all students that each faculty member teaches
• Select f.name, s.namefrom faculty as f, students s, faculty_teach ft, grades gwhere s.naid = g.naid AND f.naid = ft.naid And ft.dept = g.dept AND ft.number = g.number
Copyright 2003 Curt Hill
Faculty members and their department and divisional
chair• Multiple copies are needed for
multiple names• Select f.name, dp.name, dv.name
from faculty as f, faculty dp, faculty dv, departments dept, division divwhere f.dept = dept.dept AND dept.chair = dp.naid And dept.division = div.division AND div.chair = dv.naid
Copyright 2003 Curt Hill
More options• A select usually joins several
tables creating large unique tuples• The select only shows some of
these fields– A projection on the larger tuple– This projection does not eliminate
duplicates
• We now get a choice to retain or eliminate duplicates– Reserved words ALL and DISTINCT
Copyright 2003 Curt Hill
All students who got a B or better in any CS class
• Select distinct nameFrom grades, studentsWhere score>=80 and students.naid = grades.naid