Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Query Language b Querying a Single Table b Querying Multiple Tables b EXISTS and NOT EXISTS b Changing Data.

Similar presentations


Presentation on theme: "Structured Query Language b Querying a Single Table b Querying Multiple Tables b EXISTS and NOT EXISTS b Changing Data."— Presentation transcript:

1 Structured Query Language b Querying a Single Table b Querying Multiple Tables b EXISTS and NOT EXISTS b Changing Data

2 Querying a Single Table b Projections Using SQL SELECT MajorSELECT Major FROM STUDENT FROM STUDENTand SELECT DISTINCT Major SELECT DISTINCT Major FROM STUDENT FROM STUDENT

3 Querying a Single Table (Cont.) b Selections Using SQL SELECT Name, AgeSELECT Name, Age FROM STUDENT FROM STUDENT WHERE Major=MATH AND Age>21 WHERE Major=MATH AND Age>21and SELECT Name SELECT Name FROM STUDENT FROM STUDENT WHERE Major IN [MATH,HISTORY] WHERE Major IN [MATH,HISTORY]

4 Querying a Single Table (Cont.) b Sorting SELECT Name, Major, AgeSELECT Name, Major, Age FROM STUDENT FROM STUDENT WHERE GradeLevel IN [FR,SO,SN] WHERE GradeLevel IN [FR,SO,SN] ORDER BY Major ASC, Age DESC ORDER BY Major ASC, Age DESC

5 Querying a Single Table (Cont.) b SQL Built-In Functions COUNT, SUM, AVG, MAX, MINCOUNT, SUM, AVG, MAX, MIN e.g. SELECT COUNT(DISTINCT Major)e.g. SELECT COUNT(DISTINCT Major) FROM STUDENT FROM STUDENTand SELECT Major, COUNT(*) SELECT Major, COUNT(*) FROM STUDENT WHERE Major IN [MATH,HISTORY] GROUP BY Major HAVING COUNT(*)>2

6 Querying Multiple Tables b Using Subqueries What are the names of the students enrolled in the class BD445?What are the names of the students enrolled in the class BD445? SELECT Name FROM STUDENT WHERE SID IN (SELECT StudentNumber FROM ENROLMENT FROM ENROLMENT WHERE ClassName=BD445) WHERE ClassName=BD445)

7 Querying Multiple Tables (Cont.) What are the names of the students enrolled in classes on Monday, Wednesday, and Friday at 3 oclock (MWF3)?What are the names of the students enrolled in classes on Monday, Wednesday, and Friday at 3 oclock (MWF3)? SELECT STUDENT.Name FROM STUDENT WHERE STUDENT.SID IN (SELECT ENROLLMENT.StudentNumber FROM ENROLLMENT FROM ENROLLMENT WHERE ENROLLMENT.ClassName IN WHERE ENROLLMENT.ClassName IN

8 (SELECT CLASS.Name FROM CLASS WHERE CLASS.Time=MWF3)) Querying Multiple Tables (Cont.)

9 b Joining with SQL List the student number and class name of all students named RYE who were first to enroll in a class.List the student number and class name of all students named RYE who were first to enroll in a class. SELECT STUDENT.SID, ENROLMENT.ClassName FROM STUDENT, ENROLLMENT WHERE STUDENT.SID=ENROLLMENT.StudentNumber AND ENROLLMENT.PositionNumber=1 AND STUDENT.Name= RYE

10 Querying Multiple Tables (Cont.) b EXISTS and NOT EXISTS List the student numbers of students enrolled in more than one class.List the student numbers of students enrolled in more than one class. SELECT DISTINCT StudentNumber FROM ENROLLMENTA WHERE EXISTS (SELECT * FROM ENROLLMENTB FROM ENROLLMENTB WHERE A.StudentNumber=B.StudentNumber WHERE A.StudentNumber=B.StudentNumber AND A.ClassName NOT = B.ClassName) AND A.ClassName NOT = B.ClassName)

11 Querying Multiple Tables (Cont.) b CHANGING DATA Inserting DataInserting Data INSERT INTO ENROLLMENT VALUES(400,BD445,44) Deleting DataDeleting Data DELETE STUDENT WHERE STUDENT.Major=Accounting Modifying DataModifying Data UPDATE ENROLLMENT SET PositionNumber=44 WHERE SID=400


Download ppt "Structured Query Language b Querying a Single Table b Querying Multiple Tables b EXISTS and NOT EXISTS b Changing Data."

Similar presentations


Ads by Google