© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language
© 2002 by Prentice Hall 2 Structure Query Language Structure Query Language is known as either –Its acronym, SQL, or –SEQUEL, the name of the original version of SQL SEQUEL was developed by IBM in the mid-1970s.
© 2002 by Prentice Hall 3 SQL, not a Procedural Programming Language SQL is not a programming language itself, it is a data access language SQL may be embedded in traditional procedural programming languages (like COBOL)
© 2002 by Prentice Hall 4 SQL Syntax SQL is not case sensitive. SELECT field(s) ‘what columns will be retrieved FROM table(s); ‘which table contains the column data e.g., SELECT Name, Phone FROM Student;
© 2002 by Prentice Hall 5 The DISTINCT Qualifier Eliminating duplicate rows on the output… SELECT DISTINCT StateAddress FROM Employee;
© 2002 by Prentice Hall 6 The WHERE Clause Reducing the output based on specified criteria… SELECT StudentName FROM Students WHERE GradePointAverage >= 3.0;
© 2002 by Prentice Hall 7 Comparison Operators Equals = Not equals <> Greater than > Less than < Greater than or equal to >= Less than or equal to <= Within a list of values IN A logical NOT Within a range BETWEEN
© 2002 by Prentice Hall 8 IN a List of Values SELECT StudentName FROM Student WHERE State IN [‘PA’, ‘MA’, ‘CA’];
© 2002 by Prentice Hall 9 The Logical NOT SELECT StudentName FROM Students WHERE State NOT IN [‘NJ’, ‘NM’, ‘NY’]; SELECT StudentName FROM Students WHERE NOT GradePointAverage >= 3.0;
© 2002 by Prentice Hall 10 Within a Range of Values SELECT StudentName FROM Student WHERE StudentID BETWEEN 250 and 300;
© 2002 by Prentice Hall 11 Using Wildcard Character Substitutions The LIKE keyword is used in place of the = sign when you use wildcard characters. The underscore character (_) is a single character substitution The percent character (%) is a multi- character substitution
© 2002 by Prentice Hall 12 Using LIKE SELECT StudentID FROM Student WHERE StudentName LIKE ‘K%’; SELECT PartName FROM Part WHERE PartNumber LIKE ‘_ABC%’;
© 2002 by Prentice Hall 13 NULL Means Nothing A NULL character means that nothing has been entered. This is different from a space or a zero. SELECT Name FROM Student WHERE Major IS NULL;
© 2002 by Prentice Hall 14 ORDER BY… Sorting Outputs Sorting in descending order… SELECT StudentID, Name FROM Student ORDER BY Name DESC; Sorting in ascending order… SELECT StudentID, Name FROM Student ORDER BY Name ASC;
© 2002 by Prentice Hall 15 Built-in Functions Counting number of rows COUNT Adding the values in a column SUM Averaging the values in a column AVG Finding the maximum value in a column MAX Finding the minimum value in a column MIN
© 2002 by Prentice Hall 16 Built-in Functions SELECT Count (*) FROM Student WHERE State = ‘WI’; SELECT Sum (Amount) FROM SalesReceipt; SELECT Max (Score) FROM Assignments;
© 2002 by Prentice Hall 17 Grouping the Output SELECT Name, State FROM Student GROUP BY State;
© 2002 by Prentice Hall 18 Reducing the Groups Displayed SELECT Name, State FROM Student GROUP BY State HAVING Count (*) > 4;
© 2002 by Prentice Hall 19 Sub-Queries SELECT Name FROM Student WHERE SID IN (SELECT StudentNumber FROM Enrollment WHERE ClassName = ‘MIS445’);
© 2002 by Prentice Hall 20 Joining Tables SELECT Student.SID, Student.Name, Enrollment.ClassName FROM Student, Enrollment WHERE Student.SID = Enrollment.StudentNumber AND Student.State = ‘OH’;
© 2002 by Prentice Hall 21 EXISTS SELECT DISTINCT StudentNumber FROM Enrollment A WHERE EXISTS (SELECT * FROM Enrollment B WHERE A.StudentNumber = B.StudentNumber AND A.ClassName NOT = B.ClassName);
© 2002 by Prentice Hall 22 Inputting Data INSERT INTO Enrollment VALUES (400, ‘MIS445’, 44);
© 2002 by Prentice Hall 23 Deleting Data DELETE Student WHERE Student.SID = 100;
© 2002 by Prentice Hall 24 Modifying Data UPDATE Enrollment SET SeatNumber = 44 WHERE SID = 400;
© 2002 by Prentice Hall 25 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language