Download presentation
Presentation is loading. Please wait.
Published byGeoffrey Baker Modified over 6 years ago
1
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
2
Plan for the SQL DML Topic
Data Manipulation Language Single table queries Multiple table queries Nested queries Reading: Chapter 4 Chapter 5 PostgreSQL Manual
3
Queries in SQL SELECT is the basic SQL statement for retrieving data from a database SELECT [ DISTINCT ] 〈attribute_list〉 FROM 〈table_list〉 [ WHERE 〈condition〉 ] SELECT statement has nothing to do with relational algebra select operation. 〈attribute_list〉 defines relational algebra projection 〈condition〉 contains conditional expression of relational algebra select operation
4
Queries in SQL 〈attribute_list〉= attributes whose values will be retrieved by query ( “∗” denotes all table attributes) e.g. FName, LName, CName, Grade 〈table_list〉= relations needed to process the query (tables containing attributes from 〈attribute_list〉 must be included), e.g. Student, Course, Grades 〈condition〉 is a Boolean expression defining the properties of the tuples to be retrieved, and join conditions (optional clause) e.g. StudId = SQL considers a relation to be a multiset (or bag) of tuples, not a set ⇒ allows duplicates! SELECT statement has nothing to do with relational algebra select operation. 〈attribute_list〉 defines relational algebra projection 〈condition〉 contains conditional expression of relational algebra select operation
5
Conditional Expression (Reference)
Assume { =, <, <=, >, >=, < > }, A and B attributes or function of attributes, ai dom (A ), i = 1,… Conditional expression of the WHERE clause can be any plausible combination of the following: [(A a] [A B ] [A IS [ NOT ] NULL] [A [ NOT ] BETWEEN a1 AND a2 ] [A [ NOT ] LIKE 〈pattern〉 ] (string matching ) [A [NOT] SIMILAR TO 〈regular expression〉 ] [A [ NOT ] IN 〈value_list〉] [A ANY 〈value_list〉] [A SOME 〈value_list〉] [A ALL 〈value_list〉] [(EXISTS | NOT EXISTS) 〈sub query〉]
6
The University Database
Student LName FName StudId Major Smith Susan 131313 Comp Bond James 007007 Math 555555 Cecil John 010101 Grades StudId CourId Grade 007007 C302 A+ 555555 C301 A M214 131313 C201 B- C 010101 Course CName CourId Points Dept DB Sys C302 15 Comp SofEng C301 DisMat M214 22 Math Pr&Sys C201 Here, we suppose that every student can have at most one grade for any course. That means that only pass grades are recorded in the database, and non pass grades are forgotten. Otherwise, {StudId, CourId } wouldn't be a key. A special symbol is used to denote an unknown value, so called null value. In tutorial, discuss the options: a student can have more than one grade for a course, but all hers/his grades for the same course are different, a student can have more than one grade for a course, and more than one grade can be same for the same course.
7
Single Table Queries Retrieve the first and last names of Comp students SELECT FName, LName FROM Student WHERE Major = 'Comp'; Find all different grades SELECT DISTINCT Grade FROM Grades ; FName LName Susan Smith Grade A+ A B- C
8
Substring Comparisons
Can match to SQL patterns: 〈string〉 LIKE 〈pattern〉 '%' to replace an arbitrary number of characters, and ‘ _ ‘ to replace exactly one character. Can extract part of a string with the function substring(<string>, <start pos>, <length>) e.g. Retrieve course names of all 300 level courses (have '3' as the second character in CourId) SELECT CName FROM Course WHERE CourId LIKE '_3%'; or SELECT CName FROM Course WHERE substring(CourId, 2,1) = ’3’; PName DB Sys SofEng
9
Arithmetic Operations, Sorting
SQL provides capability to perform four basic arithmetic operations (+, -, *, / ) that can be applied to numeric attributes and constants only SELECT 2 + 2; Sorting of the query result tuples is done using ORDER BY {〈attribute_name〉 [(ASC | DESC)], …} clause after the WHERE clause (ASC is default) SELECT * FROM Grades ORDER BY StudId ASC, CourId DESC;
10
Qualification and Aliasing
Attributes in different relation schemas can have the same names. How do we prevent ambiguity? In the SELECT clause, we prefix attributes by table name: SELECT Student.StudId … To change name of an attribute in the result, alias the attribute name using AS : SELECT CourId AS CourseId In the FROM clause, specify a tuple variable from the table: FROM Course c, Grades g, Student s In the WHERE clause, prefix an attribute by the tuple variable: WHERE c.CourId = g.CourId
11
Multiple Table Queries and Joins
To retrieve data from more than one table, we need a new operation: JOIN There are different joins: INNER (theta join, equi join, natural join) OUTER (left, right, full) Most often, we use the equi-join Each join operation is based on concatenating those tuples from two relations, which satisfy the join condition An equi-join concatenates tuples with equal join attribute values An equi-join is most frequently based on a (FK, PK) pair
12
SELECT * FROM t1, t2 WHERE t1.B = t2.B ;
A JOIN Example SELECT * FROM t1, t2 WHERE t1.B = t2.B ; Join Condition t t t1 EQUI-JOIN t2 A B C 1 b1 c1 2 b2 3 b3 4 b4 c2 5
13
Multiple Table Queries
Retrieve course names with grades, and the surname for student James SELECT CName, Grade, LName AS Surname, FROM Student s, Grades g, Course c WHERE FName = 'James' AND s.StudId = g.StudId AND c.CourId = g.CourId ; Conditional expression is blue, Join condition is red CName Grade Surname DB Sys A+ Bond SofEng A DisMat Pr&Sys
14
Set Theoretic Operations
SQL has directly implemented set operations UNION, EXCEPT (difference), and INTERSECT on union compatible relations (same attributes, in the same order) e.g. Retrieve student ids of the students that didn't enroll M214 (SELECT StudId FROM Student) EXCEPT (SELECT StudId FROM Grades WHERE CourId = 'M214'); StudId 131313 555555 010101 Student that didn't enroll are not recorded in the Grades table
15
Nested Queries Some queries require comparing a tuple to a collection of tuples (eg, students doing courses that have more than 100 students) This task can be accomplished by embedding a SQL query into WHERE clause of another query The embedded query is called nested query, The query containing the nested query is called outer query The comparison is made by using IN, ANY, SOME, and ALL operators, where { =, <, <=, >=, >, < > } Note: IN =ANY and IN =SOME SQL allows multiple nesting
16
Example Nested Query Retrieve first names of students that sat M214
SELECT FName FROM Student s WHERE s.StudId IN (SELECT StudId FROM Grades WHERE CourId = 'M214' AND Grade IS NOT NULL); A nested query defined by using IN (or =ANY) operator can be (often but not always) expressed as a single block query SELECT FName FROM Student s, Grades g WHERE s.StudId = g.StudId AND g.CourId = 'M214' AND g.Grade IS NOT NULL; FName James Nested Query To evaluate the query: Retrieve first names of students that passed M214 SQL firstly computes the set of id's of those students that passed M214, and then compares every student id from Student table. If the comparison evaluates true, the student's name is included in the answer.
17
Correlated Nested Queries
Let the variable s contain the current tuple of the outer query If the nested query doesn’t refer to s (in its WHERE clause): The nested query computes the same result for each tuple in s The outer query and the nested query are said to be uncorrelated If a condition in the WHERE clause of the nested query refers to some attribute of a relation declared in the outer query, the two queries are said to be correlated Have to compute the inner query for each tuple considered by the outer query Correlated nested queries consume more computer time than uncorrelated ones
18
Correlated Nested Query
Retrieve id's and surnames of those students that sat at least one course SELECT s.StudId, FName FROM Student s WHERE s.StudId IN (SELECT StudId FROM Grades WHERE s.StudId = StudId AND Grade IS NOT NULL); When s.StudId = , ⇒ result of the nested query is {131313, }, ⇒ (131313, Susan) is in the final result When s.StudId = , ⇒ result of the nested query is { }, ⇒ (010101, John) is NOT in the final result Evaluation of the correlated nested query is done in the following way: for every s.StudId value, SQL computes the multiset of StudentId values, and if that multiset is not empty, student data is included into query result. Obviously, the result will not contain freshman data.
19
Correlated Nested Query
Again, the nested query can be expressed as a single block query: SELECT DISTINCT s.StudId, s.LName FROM Student s, Grades g WHERE s.StudId = g.StudId AND Grade IS NOT NULL; Have to be careful of duplicates! Have to be careful, not all nested queries can be transformed into single block queries Equi-join condition This query retrieves students that passed at least one exam.
20
EXISTS and NOT EXISTS Retrieve Id‘s and surnames of students who sat at least one course exam: SELECT s.StudId, LName FROM Student s WHERE EXISTS (SELECT * FROM Grades WHERE s.StudId = StudId AND Grade IS NOT NULL); Retrieve Id's and surnames of students who didn't sat exam of any course: SELECT s.StudId, LName FROM Student s WHERE NOT EXISTS (SELECT * FROM Grades WHERE s.StudId = StudId AND Grade IS NOT NULL ); EXIST and NOT EXIST are used in conjunction with correlated nested queries. So, for every tuple of the outer query, there is a specific set of tuples computed in the nested query. Specific means that the nested query is computed by placing the values of the current outer query tuple into the WHERE clause of the nested query. EXIST (Q), where Q is the nested query, evaluates true if there is at least one tuple returned by Q. NOT EXIST (Q) evaluates true if there is no tuples returned by Q. In other words, EXIST checks whether nested query returns a non empty set, and NOT EXIST checks whether nested query returns an empty set.
21
Summary SQL query language Next lecture, SQL advanced options:
Queries against a single table Queries against multiple tables Substring comparisons Arithmetic operations Sorting Nested queries (outer and inner - nested query) Correlated nested queries Next lecture, SQL advanced options: Joined tables, Aggregate functions Grouping Having
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.