0% found this document useful (0 votes)
67 views30 pages

Structured Query Language (SQL) : Database Systems: Design, Implementation, and Management 7th Edition

Structured Query Language (SQL) is a standardized language used to manipulate and retrieve data from relational databases. SQL statements specify what to do with the data rather than how to do it. The main SQL statements are SELECT for retrieval, INSERT for addition, UPDATE for modification, and DELETE for removal. SQL also supports functions, operators, and clauses for more advanced querying and manipulation of data.

Uploaded by

Arvind Lal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views30 pages

Structured Query Language (SQL) : Database Systems: Design, Implementation, and Management 7th Edition

Structured Query Language (SQL) is a standardized language used to manipulate and retrieve data from relational databases. SQL statements specify what to do with the data rather than how to do it. The main SQL statements are SELECT for retrieval, INSERT for addition, UPDATE for modification, and DELETE for removal. SQL also supports functions, operators, and clauses for more advanced querying and manipulation of data.

Uploaded by

Arvind Lal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter 7

Structured Query Language (SQL)


7
Database Systems: Design, Implementation, and Management
7th Edition

Peter Rob & Carlos Coronel


SQL
 Standardized
 Data Manipulation and Data Definition
 Can be embedded into general programming languages

7  Statements specify what is to be done, NOT how to do it


DML - Retrieval
 SELECT statement - can do MANY different things
 List whole table
SELECT * FROM STUDENT
 Relational Algebra PROJECT

7 SELECT StdID,LNAME,FNAME
FROM STUDENT
 Unlike RA, duplicates aren’t (automatically) removed. E.g.
SELECT MAJOR
FROM STUDENT
 If we want duplicates removed, specify DISTINCT after SELECT
SELECT DISTINCT MAJOR
FROM STUDENT
DML - RESTRICT
 Relational Algebra RESTRICT/SELECT
 e.g. list all students who are freshmen
SELECT *

7 FROM STUDENT
WHERE YEAR = ‘Fr’
 PROJECT and RESTRICT together
SELECT StdID,LNAME,FNAME
FROM STUDENT
WHERE YEAR = ‘Fr’
More Retrieval
 WHERE condition can be as complicated as you need
it to be. E.g. freshmen with poor grades
SELECT StdID,LNAME,FNAME

7 FROM STUDENT
WHERE YEAR = ‘Fr’ AND GPA < 2.5
Ordering
 With a little extra complexity, we can get our output in
order by some particular attribute(s) E.g. order
students by major

7 SELECT StdID,LNAME,FNAME, MAJOR


FROM STUDENT
ORDER BY MAJOR DESC
The Microsoft Access QBE and its
SQL

7
Joining Tables
 As you know, an important task with relational
databases is relating info from more than one table
(for instance, natural join in RA) E.g. show all

7 students with the class indices of enrollments


SELECT StudID,LNAME,FNAME, INDEX
FROM STUDENT, ENROLLMENTS
WHERE StudID = Enrollments.Student
Restrict with Join
 Sometimes you don’t want the entire join - you might
want to restrict the results (sometimes called select-
project-join) E.g. show all students enrolled in a

7 particular section
SELECT StdID,LNAME,FNAME
FROM STUDENT, ENROLLMENTS
WHERE STUDENT. StdID = ENROLLMENTS.Student
AND ENROLLMENTS.INDEX = 70238
Multi-way Join
 We can join more than two tables together (which is a
good thing; joining students with enrollments was a little
unsatisfying because we didn’t see any info about the

7 section. Let’s show all students with the section info for
sections they enrolled in
SELECT STUDENT.*, SECTION.*
FROM STUDENT, ENROLLMENTS, SECTION
WHERE STUDENT.StdID = ENROLLMENTS.Student
AND ENROLLMENTS.INDEX = SECTION.INDEX
Alias
 Alternate name (for a table)
 SELECT STUDENT.*, SECTION.*
FROM STUDENT A, ENROLLMENTS B, SECTION C

7 WHERE A.StdID = B.student AND B.index = C.index;


 Here A,B, and C are aliases
 Used as a convenience, not necessary
Queries
 Special Operators
 BETWEEN - used to define range limits.

7
 IS NULL - used to check whether an attribute value is
null
 LIKE - used to check for similar character strings.
 IN - used to check whether an attribute value matches a
value contained within a (sub)set of listed values.
 EXISTS - used to check whether an attribute has a
value. In effect, EXISTS is the opposite of IS NULL.
 Can also be used to check if a subquery returns any rows
Queries
 Special Operators
BETWEEN is used to define range limits.

7 SELECT *
FROM STUDENT
WHERE GPA BETWEEN 2.0 AND 2.1;
Queries
 Special Operators
IS NULL is used to check whether an attribute value

7 is null.
SELECT INDEX, DEPT, CLASS, TIME
FROM SECTION
WHERE ROOM IS NULL;
Queries
 Special Operators
LIKE is used to check for similar character strings.

7 SELECT * FROM CATALOG_CLASS


WHERE TITLE LIKE ‘%Lang%’;

 % stands for 0 or more char wildcard


 _ stands for a one char wildcard
 e.g. WHERE TITLE LIKE ‘%Network%’ finds classes whose
title includes the substring ‘Network’

 NOTE: While SQL commands are not case-sensitive, SQL


strings are
Queries
 Special Operators
IN is used to check whether an attribute value
matches a value contained within a (sub)set of

7 listed values.
SELECT * FROM ENROLLMENT
WHERE INDEX IN (66415, 66421);

EXISTS is used to check whether an attribute has


value.
SELECT * FROM SECTIONs
WHERE PROFESSOR EXISTS;
Queries
 - Can do computation
 e.g. SELECT SECTION.*,stop - enroll
FROM SECTION

7  gives for all sections, the number of remaining seats


 E.g. SELECT room.*, capacity / NumbStudentWorkstations
AS StdPerComp
FROM room
 Gives number of students per computer in the rooms

 Usual mathematical operators  + - * / ^


Some SQL Numeric Aggregate
Some Basic SQL Numeric Functions
Functions

7
Aggregate Functions - AVG
 e.g. find ave GPA for all students
SELECT AVG(GPA)
FROM STUDENT

7  What is the average GPA of all freshmen


SELECT AVG(GPA)
FROM STUDENT
WHERE YEAR = ‘Fr’
COUNT
 How many sections are offered
SELECT COUNT(*)
FROM SECTION

7  How many computer science majors are there?


SELECT COUNT(*)
FROM STUDENT
WHERE MAJOR = ‘CS’

 How many distinct classes are being offered


SELECT COUNT(DISTINCT DEPT,CLASS)
FROM SECTION
Group By - Subtotals
 Total Enrollments by Dept
SELECT DEPT, SUM(enroll)
FROM SECTION

7 GROUP BY DEPT
Improper Use of the GROUP BY Clause

7
Having
 Total Enrollments by Dept for depts offering more than
10 sections
SELECT DEPT, SUM(credits)
FROM CATALOGCOURSE

7 GROUP BY DEPT
HAVING COUNT(*) > 10
 Average Highest Enrollment (capacity) for upper division
courses by Depts – for depts with many upper division
sections
SELECT DEPT, AVG(MaxEnroll)
FROM SECTIONS
WHERE Course >= 200
GROUP BY DEPT
HAVING COUNT(*) >= 10
Nested Queries
 Sometimes the result of one query can be used in
another query - thus we can have nested queries
 e.g. find students whose GPA is above average

7 SELECT *
FROM STUDENT
WHERE GPA > ( SELECT AVG(GPA)
FROM STUDENT )
Nested Queries
 If is fairly common for a nested query to use the set
operation IN - which tests whether a value is a member
of a set of values. So for instance, suppose we want to

7 know all sections in which there are any freshman


SELECT DISTINCT CLASSINDEX
FROM ENROLLMENTS
WHERE STDSSN IN ( SELECT SSN
FROM STUDENT
WHERE YEAR = ‘Fr’ )
Left Outer Join
SELECT P_CODE, VENDOR.V_CODE, V_NAME
FROM VENDOR LEFT JOIN PRODUCT ON
VENDOR.V_CODE = PRODUCT.V_CODE

7
The Left Outer Join Results

7
Right Outer Join
SELECT PRODUCT.P_CODE, VENDOR.V_CODE,
V_NAME
FROM VENDOR RIGHT JOIN PRODUCT ON

7 VENDOR.V_CODE = PRODUCT.V_CODE
The Right Outer Join Results

7
End Queries – Next - Updates

You might also like