SQL “Structured Query Language; standard language for relational data manipulation” DB2, SQL/DS,...

Post on 13-Jan-2016

237 views 0 download

Transcript of SQL “Structured Query Language; standard language for relational data manipulation” DB2, SQL/DS,...

SQL

“Structured Query Language; standard language for relational data manipulation”

DB2, SQL/DS, Oracle, INGRES, SYBASE, SQL Server, dBase/Win, Paradox, Access, and others all rely upon SQL

Introduction to SQL

• IBM in the mid-1970s as SEQUEL• SQ92 = 1992 ANSI standard [a newer

standard exists for SQL to extend it to object-oriented languages]

• data access language that is embedded in application programs

• result of an SQL statement is a relation

many vendors go beyond the ANSI standards for SQL because they want to better position their product in relation to their competitors

consequently there are minor variations among vendors

• stand-alone– SQL can be used by itself to retrieve

and report information

• embedded– SQL is frequently embedded in

application programs

• SQL is not a programming language

Sample Data

STUDENT Relation

ENROLLMENT Relation

CLASS Relation

Simple Select

SELECT SID, Name, MajorFROM STUDENT

STUDENT Relation

SELECT MajorFROM STUDENT

SELECT DISINCT MajorFROM STUDENT

STUDENT Relation

as opposed to

SELECT DISINCT Major FROM STUDENT

Selection

SELECT SID, Name, Major, GradeLevel, Age

FROM STUDENTWHERE Major = ‘MATH’

STUDENT Relation

SELECT SID, Name, Major, GradeLevel, Age

FROM STUDENTWHERE Major = ‘MATH’

Selection

SELECT SID, Name, Major, GradeLevel, Age

FROM STUDENTWHERE Major = ‘MATH’ AND Age>21

STUDENT Relation

SELECT SID, Name, Major, GradeLevel, AgeFROM STUDENTWHERE Major = ‘MATH’ AND Age>21

Selection

SELECT SID, Name, Major, GradeLevel, Age

FROM STUDENTWHERE GradeLevel IN [‘FR’, ‘SO’]

STUDENT Relation

What about NOT IN ?

SELECT SID, Name, Major, GradeLevel, AgeFROM STUDENTWHERE GradeLevel IN [‘FR’, ‘SO’]

Selection

SELECT NameFROM STUDENTWHERE Name LIKE ‘R%’

STUDENT Relation

% is a wildcard match, like * is a wildcard match

_ (the underscore symbol) is for a character-by-character match

SortingSELECT Name, Major, AgeFROM STUDENTWHERE Major = ‘ACCOUNTING’ORDER BY Name

STUDENT Relation

SELECT Name, Major, AgeFROM STUDENTWHERE Major = ‘ACCOUNTING’ORDER BY Name

SortingSELECT Name, Major, AgeFROM STUDENTWHERE Major = ‘ACCOUNTING’ORDER BY Name DESC

STUDENT Relation

ASC is for ascending

SQL Built-In Functions

• there are five– COUNT– SUM [only applies to numeric fields]– AVG [only applies to numeric fields]– MIN– MAX

SQL Built-In Functions

SELECT COUNT(Major)FROM STUDENT

SELECT COUNT(DISTINCT Major)FROM STUDENT

yields 8 as the answer

yields 3 as the answer

SELECT COUNT(DISTINCT Major)FROM STUDENT

Grouping

SELECT Major, COUNT(*)FROM STUDENTGROUP BY MajorHAVING COUNT(*) > 1

FROM and WHERE go together and GROUP BY and HAVING go together

Querying Multiple Tables

• Retrieval Using Subquery• Joining with SQL

what are the names of students in BD445?

ENROLLMENT Relation

CLASS Relation

STUDENT Relation

Subquery (the second SELECT)

SELECT NameFROM STUDENTWHERE SID IN

(SELECT StudentNumber FROM ENROLLMENT WHERE ClassName = ‘BD445’)

this SELECT yields 100 and 200

so this SELECT yields Jones and Baker

Joining with SQL

SELECT STUDENT.SID, STUDENT.Name, ENROLLMENT.ClassName

FROM STUDENT, ENROLLMENTWHERE STUDENT.SID =

ENROLLMENT.StudentNumber

column names are unique within a table but it helps to ‘fully qualify’ a column name when more than one table is targeted by the FROM parameter

students not in a class don’t get reported, why?

in a JOIN always look to match the common column values

SELECT STUDENT.SID, STUDENT.Name, ENROLLMENT.ClassNameFROM STUDENT, ENROLLMENTWHERE STUDENT.SID = ENROLLMENT.StudentNumber

What Is The Answer To This Query?

SELECT SID, Name FROM STUDENT WHERE SID NOT IN (SELECT DISTINCT StudentNumber

FROM ENROLLMENT)

what is this query really asking?

why is DISTINCT used?

Inserting Data

INSERT INTO ENROLLMENT VALUES (400, ‘BD445’, 44)

you must know both the field order and field type; text fields require surrounding apostrophes

to insert a STUDENT record where you don’t know the Major or GradeLevel

INSERT INTO STUDENT

VALUES (500, ‘Hamilton’, , , 45)

notice the empty positions will place null values in the table

key values must always be entered

Modifying Data

UPDATE ENROLLMENTSET PositionNumber = 44WHERE SID = 400

be careful of wildcard matches

Deleting Data

DELETE STUDENTWHERE STUDENT.SID = 100

probably the most dangerous SQL statement