SQL 1
Database Languages A DBMS provides two types of languages: DDL Data Definition Language Language for defining a database schema DML Data Manipulation Language Language for accessing & manipulating the data 2
SQL (Structured Query Language) DDL DML 3
Simple Queries in SQL SELECT L A list of expressions indicates which attributes should appear in the output. FROM R A relation gives the relation(s) the query refers to WHERE C A condition is a Boolean expression indicating which tuples are of interest 4
Order of Clauses in SQL Query SELECT FROM WHERE GROUP BY Having ORDER BY 5
Exercise #1 Find all the courses information in database Course coursenumber name credit 346 Operating Systems 4 353 Databases 4 6
Exercise #1 Find all the courses information in database SELECT * FROM Course Star as List of All Attributes Query Result: Course coursenumber name credit 346 Operating Systems 4 353 Databases 4 No WHERE clause OK. The query result is unnamed 7
Exercise #2 Find all the students ids enrolled in at least one course EnrolledIn ID coursenumber grade 100 346 A+ 200 353 B 100 353 A- 200 346 B 8
Exercise #2 Find all the students ids enrolled in at least one course SELECT ID FROM EnrolledIn EnrolledIn ID coursenumber grade 100 346 A+ 200 353 B 100 353 A- 200 346 B a) ID 100 200 100 200 b) ID 200 100 The query result is a bag. 9
Exercise #3 Find all the students ids enrolled in 353 EnrolledIn ID coursenumber grade 100 346 A+ 200 353 B 100 353 A- 200 346 B 10
Exercise #3 Find all the students ids enrolled in 353 EnrolledIn ID coursenumber grade 100 346 A+ 200 353 B 100 353 A- SELECT ID 200 346 B FROM EnrolledIn WHERE coursenumber =353 ID 200 100 11
Exercise #3 Find all the students ids enrolled in 353 SELECT ID AS Student_ID FROM EnrolledIn Renaming Attributes WHERE coursenumber =353 Student_ID 200 100 12
Exercise #4 Find all the students first and last names enrolled in at least one course Students(ID, firstname, lastname, GPA, address) EnrolledIn(ID, coursenumber, grade) 13
Exercise #4 Describe the tuples that would appear in the result: EnrolledIn ID coursenumber grade SELECT * FROM EnrolledIn, Students 100 346 A+ 200 353 B 100 353 A- 200 346 B Students ID firstname lastname GPA address 100 Joe Smith 3.4 45 Main St. 200 Sue Brown 4 32 Bay St. 300 Ann John 3.7 26 Pine St. 14
Exercise #4 Result=The Cartesian Product of two relations 15
Exercise #4 Find all the students first and last names enrolled in at least one course Students(ID, firstname, lastname, GPA, address) EnrolledIn(ID, coursenumber, grade) SELECT firstname, lastname FROM EnrolledIn, Students WHERE? 16
Exercise #4 17
Exercise #4 Find all the students first and last names enrolled in at least one course Students(ID, firstname, lastname, GPA, address) EnrolledIn(ID, coursenumber, grade) SELECT firstname, lastname FROM EnrolledIn E, Students S WHERE S.ID=E.ID Renaming Relations 18
Exercise #4 Find all the students first and last names enrolled in at least one course Students(ID, firstname, lastname, GPA, address) EnrolledIn(ID, coursenumber, grade) SELECT DISTINCT firstname, lastname FROM EnrolledIn E, Students S WHERE S.ID=E.ID Eliminating Duplicates 19
Exercise #5 Find all the students first and last names enrolled in COMP 353 Students(ID, firstname, lastname, GPA, address) EnrolledIn(ID, coursenumber, grade) 20
Exercise #6 Find all the students ID s enrolled in at least two courses. EnrolledIn ID coursenumber grade 100 346 A+ 200 353 B 200 346 B 300 353 A- 300 346 A+ 300 355 B 21
Grouping SELECT ID FROM EnrolledIn GROUP BY ID SELECT DISTINCT ID FROM EnrolledIn ID 100 200 300 22
Grouping SELECT ID FROM EnrolledIn GROUP BY ID SELECT DISTINCT ID FROM EnrolledIn ID 100 200 300 SELECT ID, count(coursenumber) FROM EnrolledIn GROUP BY ID ID count 100 1 200 2 300 3 23
Grouping SELECT ID FROM EnrolledIn GROUP BY ID SELECT DISTINCT ID FROM EnrolledIn ID 100 200 300 SELECT ID, count(coursenumber) FROM EnrolledIn GROUP BY ID ID count 100 1 200 2 300 3 24
Grouping SELECT ID FROM EnrolledIn GROUP BY ID SELECT ID, count(coursenumber) FROM EnrolledIn GROUP BY ID SELECT ID, coursenumber FROM EnrolledIn GROUP BY ID SELECT DISTINCT ID FROM EnrolledIn ID 100 200 300 ID count 100 1 200 2 300 3? Only those attributes mentioned in the GROUP BY clause may appear unaggregated in SELECT clause. 25
Exercise #7 Find all the students ID s enrolled in at least two courses. (you cannot use GROUP BY ) 26
Exercise #8 Find all the students ID s with max GPA. Students ID firstname lastname GPA 100 Joe Smith 3.4 200 Sue Brown 4 300 Ann John 3.7 27
Exercise #8 Find all the students ID s with max GPA. SELECT ID FROM Students WHERE GPA = MAX(GPA) the aggregate max cannot be used in the WHERE clause. using a subquery Students ID firstname lastname GPA 100 Joe Smith 3.4 200 Sue Brown 4 300 Ann John 3.7 28
Where vs. Having WHERE selects input rows before groups and aggregates are computed. It controls which rows go into the aggregate computation. Thus, the WHERE clause must not contain aggregate functions. it makes no sense to try to use an aggregate to determine which rows will be inputs to the aggregates. HAVING selects group rows after groups and aggregates are computed. HAVING clause always contains aggregate functions. Strictly speaking, you are allowed to write a HAVING clause that doesn't use aggregates, but it's seldom useful. The same condition could be used more efficiently at the WHERE stage. 29
Exercise #9 Find all the students ID s with max GPA. (You cannot use MAX) Students ID firstname lastname GPA 100 Joe Smith 3.4 200 Sue Brown 4 300 Ann John 3.7 30
Pattern Matching in SQL % any sequence of zero or more characters address LIKE %Montreal% A sequence of characters of any length containing Montreal _ any single character address LIKE H There must be exactly three characters in the string, the first of which must be an H. To search for a % or a _ character (for example Montreal% ) LIKE Montrealx% ESCAPE x Example: 'abcd' LIKE 'abcd' 'abcd' LIKE 'a%' 'abcd' LIKE '_b ' 'abcd' LIKE 'c 'abcd' NOT LIKE 'c true true true false true SELECT firstname, lastname FROM Students WHERE Montreal LIKE %Montreal% 31
Exercise #10 Find all the students who live in Montreal. (i.e. have string Montreal as part of their address) Students ID firstname lastname GPA address 100 Joe Smith 3.4 45 Main St. Montreal 200 Sue Brown 4 32 Bay St. Montreal H3H 2M8 300 Ann John 3.7 26 Pine St. Toronto 32
Truth-values True Unknown False Truth-values 1 1/2 0 a AND b Min(a,b) a OR b Max(a,b) NOT a 1-a WHERE condition Tuples with either unknown or false as value are excluded from the answer. 33
NULL Values NULL is not a constant : NULL+7 not a legal SQL expression How to ask if x has the value NULL? x IS NULL x IS NOT NULL What is the answer? x x? x 0? Any arithmetic operations (+,-,*,/) involving null values must return NULL Except for the COUNT(*) function, all aggregate functions perform a Nullelimination step, so that Null values are not included in the final result of the calculation Any comparison (<,>,=,<=,>=) involving a NULL value evaluate to UNKNOWN 34
Now Go Back to Exercise #9 Find all the students ID s with max GPA. (You cannot use MAX) Students ID firstname lastname GPA 100 Joe Smith 3.4 200 Sue Brown 4 300 Ann John 3.7 400 Joe Brown NULL What if we don t know one of the students GPA? (NULL value) 35
Exercise #11 What are AVG(M) and AVG(N)? Are they equal? R M N 10 10 50 50 60 60 NULL 0 36
Exercise #11 What are AVG(M) and AVG(N)? Are they equal? AVG( M ) (10 50 60) / 3 40 AVG( N) (10 50 60 0) / 4 30 R M N 10 10 50 50 60 60 NULL 0 The elimination of Null values is not equivalent to replacing those values with zero. In SQL, AVG(X) SUM(X)/COUNT(*) 37
Exercise #12 What is the output? 1) SELECT * FROM R WHERE M=NULL 2) SELECT * FROM R WHERE M <> 50 3) SELECT * FROM R R N M 1 50 2 10 3 20 4 NULL 4) SELECT * FROM R WHERE ( M = 50 ) OR NOT ( M = 50 ); 38
Exercise #13 Find all students whose address or phone number is known, but not both. Students ID firstname lastname GPA address phone 100 Joe Smith 3.4 45 Main St. NULL 200 Sue Brown 4 NULL NULL 300 Ann John 3.7 NULL 6579065 400 Joe Brown 3 32 Bay St. 7863590 39
http://users.encs.concordia.ca/~m_oran/ 40