0% found this document useful (0 votes)
5 views

9 SQL and More

The document provides an overview of SQL queries, including their structure with SELECT, FROM, and WHERE clauses. It illustrates various examples of SQL queries for retrieving data from different relation schemas, as well as the concept of Cartesian products and joins. Additionally, it discusses correlated subqueries and their application in SQL for more complex data retrieval scenarios.

Uploaded by

huss awd
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

9 SQL and More

The document provides an overview of SQL queries, including their structure with SELECT, FROM, and WHERE clauses. It illustrates various examples of SQL queries for retrieving data from different relation schemas, as well as the concept of Cartesian products and joins. Additionally, it discusses correlated subqueries and their application in SQL for more complex data retrieval scenarios.

Uploaded by

huss awd
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

SQL and More Databases Final

Simple SQL Queries


 A SQL query has a form:
SELECT . . .
FROM . . .
WHERE . . .;
 The SELECT clause indicates which attributes should
appear in the output.
 The FROM gives the relation(s) the query refers to
 The WHERE clause is a Boolean expression indicating
which tuples are of interest.
 The query result is a relation
 Note that the result relation is unnamed.
Example SQL Query
 Relation schema:
Course (courseNumber, name, noOfCredits)
 Query:
Find all the courses stored in the database
 Query in SQL:
SELECT 
FROM Course;
 Note:
“  “ means all the attributes in the relations involved.
Example SQL Query
 Relation schema:
Movie (title, year, length, filmType)
 Query:
Find the titles of all movies stored in the database
 Query in SQL:
SELECT title
FROM Movie;
Example SQL Query
 Relation schema:
Student (ID, firstName, lastName, address, GPA)
 Query:
Find the ID of every student who has GPA > 3
 Query in SQL:
SELECT ID
FROM Student
WHERE GPA > 3;
Example SQL Query
 Relation schema:
Student (ID, firstName, lastName, address, GPA)
 Query:
Find the ID and last name of every student with first name ’John’,
who has GPA > 3
 Query in SQL:
SELECT ID, lastName
FROM Student
WHERE firstName = ’John’ AND GPA > 3;
WHERE clause
 The expressions that may follow WHERE are conditions
 Standard comparison operators Θ includes { =, <>, <, >, <=, >= }
 The values that may be compared include constants and
attributes of the relation(s) mentioned in FROM clause
• Simple expression
• A op Value
• A op B
• Where A, B are attributes and op is a comparison operator
 We may also apply the usual arithmetic operators, +,-,*,/, etc. to
numeric values before comparing them
• (year - 1930) * (year - 1930)  100
 The result of a comparison is a Boolean value TRUE or FALSE
 Boolean expressions can be combined by the logical operators
AND, OR, and NOT
Example SQL Query
 Relation schema:
Movie (title, year, length, filmType)
 Query:
Find the titles of all color movies produced in 1990
 Query in SQL:
SELECT title
FROM Movie
WHERE filmType = ’color’ AND year = 1990;
Example SQL Query
 Relation schema:
Movie (title, year, length, filmType)
 Query:
Find the titles of all color movies that are either made after 1970 or
are less than 90 minutes long
 Query in SQL:
SELECT title
FROM Movie
WHERE (year > 1970 OR length < 90) AND filmType = ’color’;
 Note on precedence rules:
AND takes precedence over OR, and
NOT takes precedence over both
Products and Joins
 SQL has a simple way to couple relations in one query
 list each relevant relation in the FROM clause
 All the relations in the FROM clause are coupled through
Cartesian product (, in algebra)
Cartesian Product
 From Set Theory:
 The Cartesian Product of two sets R and S is the
set of all pairs (a, b) such that: a ∈ R and b ∈ S.
 Denoted as R  S
 Note:
• In general, R  S  S  R
Example
Instance R: Instance S:
A B B C D
1 2 2 5 6
3 4 4 7 8
9 10 11

R x S: A R.B S.B C D
1 2 2 5 6
1 2 4 7 8
1 2 9 10 11
3 4 2 5 6
3 4 4 7 8
3 4 9 10 11
Example
Instance of Student: Instance of Course:
ID firstName lastNam GPA Address courseNumbe name noOfCredits
e r
111 Joe Smith 4.0 45 Pine av. Comp352 Data structures 3
22 Sue Brown 3.1 71 Main st. Comp353 Databases 4
2
33 Ann
3 SELECT Johns
FROM3.7Student,
39 Bay st.
Course;
ID firstName lastNam GPA Address courseNumbe name noOfCredits
e r
111 Joe Smith 4.0 45 Pine av. Comp352 Data structures 3
111 Joe Smith 4.0 45 Pine av. Comp353 Databases 4
22 Sue Brown 3.1 71 Main st. Comp352 Data structures 3
2
22 Sue Brown 3.1 71 Main st. Comp353 Databases 4
2
33 Ann Johns 3.7 39 Bay st. Comp352 Data structures 3
3
Example
Instance of Student: Instance of Course:
ID firstName lastNam GPA Address courseNumbe name noOfCredits
e r
111 Joe Smith 4.0 45 Pine av. Comp352 Data structures 3
22 Sue Brown 3.1 71 Main st. Comp353 Databases 4
2
33 Ann Johns 3.7 39 Bay st.
3
ID courseNumbe
SELECT ID, courseNumber r
FROM Student, Course; 111 Comp352
111 Comp353
22 Comp352
2
22 Comp353
2
33 Comp352
3
Example
 Relation schemas:
Student (ID, firstName, lastName, address, GPA)
Ugrad (ID, major)
 Query:
Find all information available about every undergraduate student
 We can try to compute the Cartesian product ()
SELECT  FROM Student, Ugrad;
Example
Instance of Student: Instance of Ugrad:
ID firstName lastNam GPA Address ID major
e
111 CS
111 Joe Smith 4.0 45 Pine av. 333 EE
22 Sue Brown 3.1 71 Main st.
2
33 Ann
3 SELECT Johns
FROM3.7Student,
39 Bay st.
Ugrad;
ID firstName lastNam GPA Address ID major
e
111 Joe Smith 4.0 45 Pine av. 111 CS
111 Joe Smith 4.0 45 Pine av. 33 EE Which tuples should
3
22 Sue 111 CS
be in the query result
Brown 3.1 71 Main st.
2 and which shouldn’t?
22 Sue Brown 3.1 71 Main st. 33 EE
2 3
33 Ann Johns 3.7 39 Bay st. 111 CS
Example
Instance of Student: Instance of Ugrad:
ID firstName lastNam GPA Address ID major
e
111 CS
111 Joe Smith 4.0 45 Pine av. 333 EE
22 Sue Brown 3.1 71 Main st.
2
33 Ann Johns 3.7 39 Bay st.
3
SELECT 
FROM Student, Ugrad
WHERE Student.ID = Ugrad.ID;
ID firstName lastNam GPA Address ID major
e
111 Joe Smith 4.0 45 Pine av. 111 CS
33 Ann Johns 3.7 39 Bay st. 33 EE
3 3
Joins in SQL
 The above query is an example of Join operation
 There are various kinds of joins and we will study them
later in detail
 To join relations R1,…,Rn in SQL:
 List all these relations in the FROM clause
 Express the conditions in the WHERE clause in order to get the
desired join
Joining Relations
 Relation schemas:
Movie (title, year, length, filmType)
Owns (title, year, studioName)
 Query:
Find title, length, and studio name of every movie
 Query in SQL:
SELECT Movie.title, Movie.length, Owns.studioName
FROM Movie, Owns
WHERE Movie.title = Owns.title AND Movie.year = Owns.year;
Is Owns in Owns.studioName necessary?
Joining Relations
 Relation schemas:
Movie (title, year, length, filmType)
Owns (title, year, studioName)
 Query:
Find the title and length of every movie produced by Disney
 Query in SQL:
SELECT Movie.title, length
FROM Movie, Owns
WHERE Movie.title = Owns.title AND Movie.year = Owns.year
AND studioName = ’Disney’;
Joining Relations
 Relation schemas:
Movie (title, year, length, filmType)
Owns (title, year, studioName)
StarsIn (title, year, starName)
 Query:
Find the title and length of each movie with Julia Roberts,
produced by Disney
 Query in SQL:
SELECT Movie.title, Movie.length
FROM Movie, Owns, StarsIn
WHERE Movie.title = Owns.title AND Movie.year = Owns.year
AND Movie.title = StarsIn.title AND Movie.year = StarsIn.year
AND studioName = ’Disney’ AND starName = ’Julia Roberts’;
Example
Movie Owns
title year length filmType title year studioName
T1 1990 124 color T1 1990 Disney
T2 1991 144 color T2 1991 MGM

StarsIn
title year starName title length
T1 1990 JR T1 124
T2 1991 JR

SELECT Movie.title, Movie.length


FROM Movie, Owns, StarsIn
WHERE Movie.title = Owns.title AND Movie.year = Owns.year AND
Movie.title = StarsIn.title AND Movie.year = StarsIn.year AND
studioName = ’Disney’ AND starName = ’Julia Roberts’;
Example
 Relation schemas:
Movie (title, year, length, filmType, studioName, producerC#)
Exec (name, address, cert#, netWorth)
 Query:
Find the name of the producer of “Star Wars”
 Query in SQL:
SELECT Exec.name
FROM Movie, Exec
WHERE Movie.title = ’Star Wars’ AND
Movie.producerC# = Exec.cert#;
Example
 Relation schemas:
Movie (title, year, length, filmType, studioName, producerC#)
Exec (name, address, cert#, netWorth)
 Query:
Find the name of the producer of “Star Wars”
 Query with Subquery:
SELECT name
FROM Exec
WHERE cert# = ( SELECT producerC#
FROM Movie
WHERE title = ’Star Wars’ );
Example
 Relation schemas:
Movie(title, year, length, filmType, studioName, producerC#)
Exec(name, address, cert#, netWorth)
StarsIn(title, year, starName)
 Query: Find the names of the producers of Harrison Ford’s movies
 Query in SQL:
SELECT name
FROM Exec
WHERE cert# IN (SELECT producerC#
FROM Movie
WHERE (title, year) IN (SELECT title, year
FROM StarsIn
WHERE starName = ’Harrison Ford’));
Movie
#Producer Studio Name Film Type Length Year Title
2 Disney Color 90 199 Star Wars
5
1 MTM Color 60 199 Gladiator
6
3 Boly Color 90 200 Star wars
0 SELECT name
FROM Exec
4 Disney Color 45 200 Brave heart
WHERE cert# IN (SELECT producerC#
FROM Movie
3
Exec StarIn50
WHERE (title, year) IN (SELECT title, year
FROM StarsIn
3 MTM Color 200 Star wars WHERE starName = ’Harrison Ford’));
Cert Address Name Star Name Year Title
2
#
4 Disney Color Russel110
Crowe 200 Brave heart
1996 Gladiator
1 UK Douglas
Wick Connie Nielsen 5 1996 Gladiator

US Mark Hamill 1995 Star wars


2 Gary Kurtz
US Howard Harrison Fords 2000 Star wars
3
Kazanjian Peter Gushing 2002 Star wars
4 US Mil Gibson Sophie Marceau 2003 Brave Heart
Patrick McGoohan 2005 Brave Heart
Example
 Relation schemas:
Movie(title, year, length, filmType, studioName, producerC#)
Exec(name, address, cert#, netWorth)
StarsIn(title, year, starName)
 Query:Find names of the producers of Harrison Ford’s movies

 Query in SQL:
SELECT Exec.name
FROM Exec, Movie, StarsIn
WHERE Exec.cert# = Movie.producerC# AND
Movie.title = StarsIn.title AND
Movie.year = StarsIn.year AND
starName = ’Harrison Ford’;
Correlated Subqueries
 Relation schema:
Movie(title, year, length, filmType, studioName, producerC#)
 Query:
Find movie titles that appear more than once
 Query in SQL:
SELECT title
FROM Movie Old
WHERE year < ANY (SELECT year
FROM Movie
WHERE title = Old.title);
Note the scopes of the variables in this query.
Correlated Subqueries
 Query in SQL
SELECT title
FROM Movie Old
WHERE year  ANY (SELECT year
FROM Movie
WHERE title = Old.title);
 The condition in the outer WHERE is true only if there is a movie with same
title as Old.title that has a later year
 The query will produce a title one fewer times than there are movies with that title
 What would be the result if we used “”, instead of “” ?
 For a movie title appearing 3 times, we would get 3 copies of the title in the output
Correlated Subqueries
Old
#Producer Studio Name Film Type Length Year Title
2 Disney Color 90 199 Star Wars
5
1 MTM Color 60 199 Gladiator
6
3 Boly Color 90 200 Star wars
0
4 Disney Color 45 200 Brave heart
3
Movie
3
#Producer MTM
Studio Name Color
Film Type 50
Length 200
Year Star
Title wars
Disney Color 2 Star Wars
2 90 199
4 Disney Color 110 200
5 Brave heart
MTM Color 5 Gladiator
1 60 199
6
3 Boly Color 90 200 Star wars
0
4 Disney Color 45 200 Brave heart
Aggregation in SQL
 SQL provides five operators that apply to a column of
a relation and produce “some kind of summary”
 These operators are called aggregations
 These operators are used by applying them to a
scalar-valued expression, typically a column name, in
a SELECT clause
Aggregation Operators
 SUM
 the sum of values in the column
 AVG
 the average of values in the column
 MIN
 the least value in the column
 MAX
 the greatest value in the column
 COUNT
 the number of values in the column, including the duplicates, unless
the keyword DISTINCT is used explicitly
Example
 Relation schema:
Exec(name, address, cert#, netWorth)
 Query:
Find the average net worth of all movie executives
 Query in SQL:
SELECT AVG(netWorth)
FROM Exec;
 The sum of “all” values in the column netWorth divided by
the number of these values
 In general, if a tuple appears n times in a relation, it will be
counted n times when computing the average
Example
 Relation schema:
Exec (name, address, cert#, netWorth)
 Query:
How many tuples are there in the Exec relation?
 Query in SQL:
SELECT COUNT(*)
FROM Exec;
 The use of * as a parameter is unique to COUNT;

using * does not make sense for other aggregation operations


Example
 Relation schema:
Exec (name, address, cert#, netWorth)
 Query:
How many different names are there in the Exec relation?
 Query in SQL:
SELECT COUNT (DISTINCT name)
FROM Exec;
 In query processing time, the system first eliminates the duplicates

from column name, and then counts the number of values there
Aggregation -- Grouping
 Often we need to consider the tuples in an SQL query in
groups, with regard to the value of some other column(s)
 Example: suppose we want to compute:
Total length in minutes of movies produced by each studio:
Movie(title, year, length, filmType, studioName, producerC#)
 We must group the tuples in the Movie relation according to
their studio, and get the sum of the length values within each
group; the result would be something like:
studio SUM(length)
Disney 12345
MGM 54321
… …
Aggregation - Grouping
 Relation schema:
Movie(title, year, length, filmType, studioName, producerC#)
 Query: What is the total length in minutes produced by each studio?
 Query in SQL:
SELECT studioName, SUM(length)
FROM Movie
GROUP BY studioName;
 Whatever aggregation used in the SELECT clause will be applied
only within groups
 Only those attributes mentioned in the GROUP BY clause may
appear unaggregated in the SELECT clause
 Can we use GROUP BY without using aggregation? (Yes/No)
Aggregation -- Grouping
 Relation schema:
Movie(title, year, length, filmType, studioName, producerC#)
Exec(name, address, cert#, netWorth)
 Query:
For each producer (name), list the total length of the films produced
 Query in SQL:
SELECT Exec.name, SUM(Movie.length)
FROM Exec, Movie
WHERE Movie.producerC# = Exec.cert#
GROUP BY Exec.name;
Aggregation – HAVING clause
 We might be interested in not all but some groups of tuples
that satisfy certain conditions
 We can follow a GROUP BY clause with a HAVING clause
 HAVING is followed by some conditions about the group
 We can not use a HAVING clause without GROUP BY
Aggregation – HAVING clause
 Relation schema:
Movie (title, year, length, filmType, studioName, producerC#)
Exec(name, address, cert#, netWorth)
 Query:
For those producers who made at least one film prior to 1930, list the
total length of the films produced
 Query in SQL:
SELECT Exec.name, SUM(Movie.length)
FROM Exec, Movie
WHERE producerC# = cert#
GROUP BY Exec.name
HAVING MIN(Movie.year)  1930;
Aggregation – HAVING clause
 This query chooses the group based on the property of the group

SELECT Exec.name, SUM(Movie.length)


FROM Exec, Movie
WHERE producerC# = cert#
GROUP BY Exec.name
HAVING MIN(Movie.year) < 1930;

 This query chooses the movies based on the property of each movie tuple

SELECT Exec.name, SUM(Movie.length)


FROM Exec, Movie
WHERE producerC# = cert# AND Movie.year < 1930
GROUP BY Exec.name;

Note the difference!


Order By
 The SQL statements/queries we looked at so far return an unordered
relation/bag (except when using ORDER BY)
Movie (title, year, length, filmType, studioName, producerC#)

SELECT Exec.name, SUM(Movie.length)


FROM Exec, Movie
WHERE producerC# = cert#
GROUP BY Exec.name
HAVING MIN(Movie.year) < 1930
ORDER BY Exec.name ASC;

In general:
ORDER BY A1 ASC, B DESC, C ASC;
Database Modifications
 SQL & Database Modifications?
 Next we will look at SQL statements that do not return something,
but rather change the state of the database
 There are three types of such SQL statements/transactions:
 Insert tuples into a relation
 Delete certain tuples from a relation
 Update values of certain components of certain existing tuples
We refer to these types of operations collectively as database
modifications, and refer to such requests as transactions
Insertion
 The insertion statement consists of:
 The keyword INSERT INTO
 The name of a relation R
 A parenthesized list of attributes of the relation R
 The keyword VALUES
 A tuple expression, that is, a parenthesized list of concrete values,
one for each attribute in the attribute list
 The form of an insert statement:
INSERT INTO R(A1, …An) VALUES (v1,… vn);
• A tuple is created and added, where vi is the value of attribute Ai,
for i = 1,2,…,n
Insertion
 Relation schema:
StarsIn (title, year, starName)
 Update the database:
Add “Sydney Greenstreet” to the list of stars of The Maltese Falcon
 In SQL:
INSERT INTO StarsIn (title,year, starName)
VALUES(’The Maltese Falcon’, 1942, ’Sydney Greenstreet’);
Another formulation of this query:
INSERT INTO StarsIn
VALUES(’The Maltese Falcon’, 1942, ’Sydney Greenstreet’);
Insertion
 The previous insertion statement was very simple
 It added only one tuple into a relation
 Instead of using explicit values for one tuple, we can compute
a set of tuples to be inserted using a subquery
 This subquery replaces the keyword VALUES and the tuple
expression in the INSERT statement
Insertion
 Database schema:
Studio(name, address, presC#)
Movie(title, year, length, filmType, studioName, producerC#)
 Update the database:
Add to Studio, all studio names mentioned in the Movie relation

 If the list of attributes does not include all attributes of relation


R, then the tuple created has default values for the missing
attributes
 Since there is no way to determine an address or a
president for such a studio value, NULL will be used for the
attributes address and presC#
Insertion
 Database schema:
Studio(name, address, presC#)
Movie(title, year, length, filmType, studioName, producerC#)
 Update the database:
Add to Studio, all studio names mentioned in the Movie relation
 In SQL:
INSERT INTO Studio(name)
SELECT DISTINCT studioName
FROM Movie
WHERE studioName NOT IN (SELECT name
FROM Studio);
Deletion
 A deletion statement consists of :
 The keyword DELETE FROM
 The name of a relation R
 The keyword WHERE
 A condition
 The form of a delete statement:
DELETE FROM R WHERE condition;
 The effect of executing this statement is that every tuple in
relation R satisfying the condition will be deleted from R
 Note: unlike the INSERT, we need a WHERE clause here
Deletion
 Relation schema:
StarsIn(title, year, starName)
 Update:
Delete: Sydney Greenstreet was a star in The Maltese Falcon
 In SQL:
DELETE FROM StarIn
WHERE title = ’The Maltese Falcon’ AND
starName = ’Sydney Greenstreet’;
Deletion
 Relation schema:
Exec(name, address, cert#, netWorth)
 Update:
Delete every movie executive whose net worth is < $10,000,000
 In SQL:
DELETE FROM Exec
WHERE netWorth < 10,000,000;

Anything wrong here?!


Deletion
 Relation schema:
Studio(name, address, presC#)
Movie(title, year, length, filmType, studioName, producerC#)
 Update:
Delete from Studio, all movies produced by studios not mentioned in
Movie (i.e., we don’t want to have non-producing studios)
 In SQL:
DELETE FROM Studio
WHERE name NOT IN (SELECT StudioName
FROM Movie);
Defining Database Schema
 To create a table in SQL:
 CREATE TABLE name (list of elements);
• Principal elements are attributes and their types, but key
declarations and constraints may also appear
 Example:
CREATE TABLE Star (
name CHAR(30),
address VARCHAR(255),
gender CHAR(1),
birthdate DATE
);
Defining Database Schema
 To delete a table:
 DROP TABLE name;
 Example:
DROP TABLE Star;
Data types
 INT or INTEGER
 REAL or FLOAT
 DECIMAL(n, d) -- NUMERIC(n, d)
 DECIMAL(6, 2), e.g., 0123.45
 CHAR(n)/BIT(B) fixed length character/bit string
 Unused part is padded with the "pad character”, denoted as 
 VARCHAR(n) / BIT VARYING(n) variable-length strings up to
n characters
Data types (cont’d)
 Time:
 SQL2 format is TIME 'hh:mm:ss[.ss...]'
 Date:
 SQL2 format is DATE ’yyyy-mm-dd’ (m =0 or 1)
 The default format of date in Oracle is ’dd-mon-yy’
 Example:
CREATE TABLE Days(d DATE);
INSERT INTO Days VALUES(’08-aug-02’);
 Oracle function to_date converts a specified format into
default format, e.g.,
 INSERT INTO Days VALUES (to_date('2002-08-08', 'yyyy-mm-dd'));
Altering Relation Schemas
 Adding Columns
 Add an attribute to a relation R with
 ALTER TABLE R ADD column declaration;
 Example: Add attribute phone to table Star
 ALTER TABLE Star ADD phone CHAR(16);
 Removing Columns
 Remove an attribute from a relation R using DROP:
 ALTER TABLE R DROP COLUMN column_name;
 Example: Remove column phone from Star
 ALTER TABLE Star DROP COLUMN phone;
Note: Can’t drop if it is the only column
Attribute Properties
 We can assert that the value of an attribute to be:
 NOT NULL
• every tuple must have a “real” (non-null) value for this attribute
 DEFAULT value
• Null is the default value for every attribute A
• The owner of the relation can define some other value as the
default, instead of NULL
Attribute Properties
CREATE TABLE Star (
name CHAR(30),
address VARCHAR(255),
gender CHAR(1) DEFAULT ’?’,
birthdate DATE NOT NULL);
 Example: Add an attribute with a default value:
 ALTER TABLE Star ADD phone CHAR(16) DEFAULT ’unlisted’;
 INSERT INTO Star(name, birthdate) VALUES (’Sally’ ,’0000-00-00’)
name address gender birthdate phone

Sally NULL ? 0000-00-00 unlisted


 INSERT INTO Star(name, phone) VALUES (’Sally’,’333-2255’)
 this insertion could not be performed since the value for birthdate is
not given and it is disallowed to use NULL as the default

You might also like