IE 171 Lecture 8 - SQL (Part 2)
IE 171 Lecture 8 - SQL (Part 2)
Query
Language (SQL)
IE 171: INFORMATION SYSTEMS 1
P RE P ARE D B Y C A S O N DAY
04/18/2023 1
Outline
► Introduction to SQL
◦ DML
◦ DDL
► Why SQL?
► Writing SQL Commands
DML and DDL
DML (data manipulation language) – Adds rows, chooses columns to
show
◦ INSERT, UPDATE, SELECT
15 Tables
Install Sample Database
actor – stores actors data inventory – stores inventory data.
including first name and last
name. rental – stores rental data.
Example: {a | b} (, c…)
The SELECT Syntax
SELECT [DISTINCT | ALL]
{* | [columnExpression [AS newName]] [,...] }
FROM TableName [alias] [, ...]
[WHERE condition]
[GROUP BY columnList] [HAVING condition]
[ORDER BY columnList]
VS. VS.
34 PREPARED BY CASONDAY
MAX, MIN, and AVG
Find minimum, maximum, and average title lengths from table film
35 PREPARED BY CASONDAY
Grouping
Use GROUP BY clause to get sub-totals of your chosen
categories.
SELECT and GROUP BY closely integrated: each item in
SELECT list must be single-valued per group, and SELECT
clause may only contain:
◦ column names
◦ aggregate functions
◦ constants
◦ expression involving combinations of the above.
36 PREPARED BY CASONDAY
Grouping
All column names in SELECT list must appear in GROUP BY
clause unless name is used only in an aggregate function.
If WHERE is used with GROUP BY, WHERE is applied first,
then groups are formed from remaining rows satisfying
predicate.
ISO considers two nulls to be equal for purposes of GROUP
BY. (meaning, null value = null value)
37 PREPARED BY CASONDAY
Grouping
Find rating with min, max and average movie lengths for
each rating grade.
38 PREPARED BY CASONDAY
HAVING (Restricted GROUP BY)
HAVING clause is designed for use with GROUP BY to restrict
groups that appear in final result table.
Similar to WHERE, but WHERE filters individual rows whereas
HAVING filters groups.
Column names in HAVING clause must also appear in the GROUP
BY list or be contained within an aggregate function.
39 PREPARED BY CASONDAY
HAVING
Find the minimum, maximum, and average length for
Rated G films
40 PREPARED BY CASONDAY
Sub-queries
Some SQL statements can have a SELECT embedded within them.
A subselect can be used in WHERE and HAVING clauses of an outer
SELECT, where it is called a subquery or nested query.
Subselects may also appear in INSERT, UPDATE, and DELETE
statements.
41 PREPARED BY CASONDAY
WHERE based on a table
List all countries from table CITY where country is ‘Algeria’
in table COUNTRY
42 PREPARED BY CASONDAY
FROM
based on
a SELECT
How many copies of each
movie are there? Sort the
list in ascending counts of
inventory
PREPARED BY CASONDAY 43
Joining Tables
If result columns come from more than one table must
use a join.
To perform join, include more than one table in FROM
clause.
44 PREPARED BY CASONDAY
Sample
DB
DVD Rental
15 Tables
INNER JOIN
List names of all clients who have viewed a property along with any
comment supplied.
46 PREPARED BY CASONDAY
INNER JOIN
List all films with the category name ‘Horror’
47 PREPARED BY CASONDAY
INNER JOIN
If one row of a joined table is unmatched, row is omitted from result
table.
◦ If there is no branch in the property city, it will not be shown in the joined
table
◦ If there is no property in the branch city, it will not be shown in the joined
table
48 PREPARED BY CASONDAY
LEFT JOIN
Lists all unmatched rows of data on the right table
49 PREPARED BY CASONDAY
RIGHT JOIN
Lists all unmatched rows of data on the right table
Instead of
Branch1.bCity, aliases
allow as to type b.bCity
instead to refer to the
same column
50 PREPARED BY CASONDAY
FULL OUTER JOIN
Shows all unmatched rows
51 PREPARED BY CASONDAY
Creating Tables
CREATE TableName [ (columnList datatype) ]
Inserting Rows
INSERT INTO TableName [ (columnList) ]
VALUES (dataValueList)
columnList is optional; if omitted, SQL assumes a list of all columns in their
original CREATE TABLE order.
Any columns omitted must have been declared as NULL when table was
created, unless DEFAULT was specified when creating column
Inserting Rows
Insert new rows into basket_a and basket_b tables supplying data for
all columns.
Let’s go back to JOINs!
Let’s go back to JOINs!
Let’s go back to JOINs!
Let’s go back to JOINs!
Let’s go back to JOINs!
Let’s go back to JOINs!
Let’s go back to JOINs!
Exercise: Write the Queries for the Following
Count all the rentals that staff Jon Stephens worked on
PREPARED BY CASONDAY 63