530 likes | 623 Views
STRUCTURED QUERY LANGUAGE SECTION 6. The Core of SQL (1992). Introduction. Most important relational data manipulation language. ANSI Used by many commercial databases. SEQUEL. 1992 ANSI standard SQL3. An interactive query language. Or embedded in other languages
E N D
STRUCTURED QUERY LANGUAGESECTION 6 The Core of SQL (1992)
Introduction • Most important relational data manipulation language • ANSI • Used by many commercial databases
SEQUEL • 1992 ANSI standard • SQL3
An interactive query language • Or embedded in other languages • SQL is not a programming language
Querying a Single Table • SQL is a transform-oriented language • Accepts as input • The result?
Relations used for SQL examples • JUNIOR (Snum, Name, Major) • HONOUR-STUDENT (Number, Name, Interest) • STUDENT (SID, Name, Major, GradeLevel, Age) • CLASS (Name, Time, Room) • ENROLLMENT ( StudentNumber, ClassName, PositionNumber) • FACULTY (FID, Name, Department) Is there a problem with the ENROLLMENT table and the primary key?
Projections Using SQL • The projection • STUDENT [SID, NAME, MAJOR] • Specified in SQL SELECT SID, Name, Major FROM STUDENT
The result • The word SELECT
Another Example SELECT Major FROM STUDENT SELECT DISTINCT Major FROM STUDENT
Selections Using SQL • The Relational Algebra selection operator is also performed • Specified in SQL SELECT SID, Name, Major, GradeLevel, Age FROM STUDENT WHERE Major = ‘Math’
SELECT - FROM - WHERE SELECT * FROM STUDENT WHERE Major = ‘Math’
Combination of Selection and Projection SELECT Name, Age FROM STUDENT WHERE Major = ‘Math’
Conditions in a WHERE clause SELECT Name, Age FROM STUDENT WHERE Major = ‘Math’ AND Age > 21
Conditions in a WHERE clause can refer to a set of values SELECT Name FROM STUDENT WHERE Major IN (‘Math’, ‘ACCOUNTING’)
The following expression SELECT Name FROM STUDENT WHERE Major NOT IN (‘Math’, ‘ACCOUNTING’)
WHERE clause and ranges of values SELECT Name, Major FROM STUDENT WHERE Age BETWEEN 19 AND 30 • Equivalent to: SELECT Name, Major FROM STUDENT WHERE Age > 19 AND Age < 30
WHERE clause and the LIKE keyword SELECT Name, GradeLevel FROM STUDENT WHERE GradeLevel LIKE ‘_R’
Finding last names ending with S SELECT Name FROM STUDENT WHERE Name LIKE ‘%S’
IS NULL keyword SELECT Name FROM STUDENT WHERE GradeLevel IS NULL • What will this return?
Sorting • Rows of a result can be sorted SELECT Name, Major, Age FROM STUDENT WHERE Major = ‘Accounting’ ORDER BY Name
Sorting more than one column SELECT Name, Major, Age FROM STUDENT WHERE GradeLevel IN (‘FR’, ‘SO’, ‘SN’) ORDER BY Major ASC, Age DESC
SQL Built-In Functions • Five functions • COUNT, SUM, AVG, MAX, and MIN SELECT COUNT (*) FROM STUDENT
Consider the following SELECT COUNT (Major) FROM STUDENT vs. SELECT COUNT (DISTINCT Major) FROM STUDENT
Built-In Functions and Grouping • To increase built-in function utility • Formed by collecting rows that have the same value of a specified column • GROUP BY
An example SELECT Major, COUNT (*) FROM STUDENT GROUP BY Major
Grouping subsets SELECT Major, COUNT (*) FROM STUDENT GROUP BY Major HAVING COUNT (*) > 2
Greater generality using WHERE SELECT Major, AVG (Age) FROM STUDENT WHERE GradeLevel = ‘SN’ GROUP BY Major HAVING COUNT (*) > 1
Querying Multiple Tables • Study now extended to two or more tables • Will use the data from the following tables • STUDENT (SID, Name, Major, GradeLevel, Age) • CLASS (Name, Time, Room) • ENROLLMENT ( StudentNumber, ClassName, PositionNumber)
Retrieval Using Subquery SELECT Name FROM STUDENT WHERE SID IN (100,200) • Want to know the names of the students enrolled in class BD445 SELECT StudentNumber FROM ENROLLMENT WHERE ClassName = ‘BD445’
Use it as a subquery SELECT Name FROM STUDENT WHERE SID IN (SELECT StudentNumber FROM ENROLLMENT WHERE ClassName = ‘BD445’)
Subqueries can consist of three or more tables SELECT ENROLLMENT.StudentNumber FROM ENROLLMENT WHERE ENROLLMENT.ClassName IN (SELECT CLASS.Name FROM CLASS WHERE ClassTime = ‘MWF3’)
To get the names of the students SELECT STUDENT.Name FROM STUDENT WHERE STUDENT.SID IN (SELECT ENROLLMENT.StudentNumber FROM ENROLLMENT WHERE ENROLLMENT.ClassName IN (SELECT CLASS.Name FROM CLASS WHERE ClassTime = ‘MWF3’))
Joining With SQL • Want to produce SID, StudentName, and ClassName for every student • What two tables must be joined? SELECT STUDENT.SID, STUDENT.Name, ENROLLMENT.ClassName FROM STUDENT, ENROLLMENT WHERE STUDENT.SID = ENROLLMENT.StudentNumber
What operations make up a join? • FROM equates to • WHERE expresses
The WHERE clause and qualifiers SELECT STUDENT.SID, ENROLLMENT.ClassName FROM STUDENT, ENROLLMENT WHERE STUDENT.SID = ENROLLMENT.StudentNumber AND STUDENT.Name = ‘RYE’ AND ENROLLMENT.PositionNumber = 1
Data from more than two tables SELECT STUDENT.SID, CLASS.Name, CLASS.Time ENROLLMENT.ClassName FROM STUDENT, ENROLLMENT, CLASS WHERE STUDENT.SID = ENROLLMENT.StudentNumber AND ENROLLMENT.ClassName = CLASS.Name AND STUDENT.Name = ‘BAKER’
Comparison of SQL Subquery and Join • A join is an alternative to expressing many subqueries • For example, “Classes taken by undergrads?” SELECT DISTINCT ENROLLMENT.ClassName FROM ENROLLMENT, STUDENT WHERE ENROLLMENT.StudentNumber = STUDENT.SID AND STUDENT.Grade.Level NOT = ‘GR’
As a subquery SELECT DISTINCT Class.Name FROM ENROLLMENT WHERE StudentNumber IN (SELECT SID FROM STUDENT WHERE GradeLevel NOT = ‘GR’)
If we want to know: “Class names and grade levels of undergrads?” SELECT DISTINCT ENROLLMENT.ClassName, SUDENT.GradeLevel FROM ENROLLMENT, STUDENT WHERE ENROLLMENT.StudentNumber = STUDENT.SID AND STUDENT.GradeLevel NOT = ‘GR’ • Why cannot a subquery be used?
Thus in the previous subquery example: SELECT DISTINCT Class.Name FROM ENROLLMENT WHERE StudentNumber IN (SELECT SID FROM STUDENT WHERE GradeLevel NOT = ‘GR’) • This would not work: SELECT DISTINCT ENROLLMENT.ClassName, STUDENT.GradeLevel FROM ENROLLMENT WHERE StudentNumber IN (SELECT SID FROM STUDENT WHERE GradeLevel NOT = ‘GR’)
EXISTS and NOT EXISTS • Logical operators SELECT DISTINCT StudentNumber FROM ENROLLMENT A WHERE EXISTS (SELECT * FROM ENROLLMENT B WHERE A.StudentNumber = B.StudentNumber AND A.ClassName NOT = B.ClassName)
Both query and subquery refer to the same table • Two uses assigned a different name • What is the meaning of the subquery?
General procedure: • First row in A is compared with first row in B • First Row in A compared with second row in B
SELECT STUDENT.Name FROM STUENT WHERE NOT EXISTS (SELECT * FROM ENROLLMENT WHERE NOT EXISTS (SELECT * FROM CLASS WHERE CLASS.Name = ENROLLMENT.ClassName AND ENROLLMENT.StudentNumber = STUDENT.SID)) • NOT EXISTS operator • Query has three parts
Changing Data Inserting Data • Inserting a single row INSERT INTO ENROLLMENT VALUES (400, ‘BD445’, 44)
INSERT INTO ENROLLMENT (StudentNumber, ClassName) VALUES (400, ‘BD445’) • Some data in a row not known • Mass copy rows INSERT INTO JUNIOR VALUES (SELECT SID, Name, Major FROM STUDENT WHERE GradeLevel = ‘JR’)
Can be deleted one at a time DELETE STUDENT WHERE STUDENT.SID = 100 Deleting Data
DELETE ENROLLMENT WHERE ENROLLMENT.StudentNumber IN (SELECT STUDENT.SID FROM STUDENT WHERE STUDENT.Major = ‘Accounting’) DELETE STUDENT WHERE STUDENT.Major = ‘Accounting’ • Groups of rows can be deleted