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

6_SQL_Post_1

The document outlines the course CPSC 304, focusing on Structured Query Language (SQL) and its applications in database systems. It includes important dates, learning goals, SQL commands, and examples of SQL statements for creating and manipulating database tables. Additionally, it provides a detailed schema for a sample retail company database, including instructions for assignments and tutorials.

Uploaded by

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

6_SQL_Post_1

The document outlines the course CPSC 304, focusing on Structured Query Language (SQL) and its applications in database systems. It includes important dates, learning goals, SQL commands, and examples of SQL statements for creating and manipulating database tables. Additionally, it provides a detailed schema for a sample retail company database, including instructions for assignments and tutorials.

Uploaded by

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

CPSC 304

Introduction to Database Systems

Structured Query Language (SQL)

References: Database Management Systems, by


Ramakrishnan and Gehrke, Third Edition (all of
Chapter 5 in this textbook)

Hazra Imran
Slides are adapted from Jukic material with necessary modification

(Acknowledging work on these slides by CPSC 304 instructors over the years)
SQL 1
Reminders and Important Dates
Mar 3, 2020
Upcoming deadlines

• March 2 to 6: Tutorial 5 is due


• March 9 to 13: Tutorial 6 is due
• March 16 to 20: Tutorial 7 is due
• March 17 @ 7PM: Midterm 2

• RA and Datalog practice exercises are available on canvas –


Please check them out!

Please check piazza post @7 for Important Dates and Deadlines


Milestone 4
1. Your cover page, as usual. (Important.)
2. All the code used in the application.
If your codebase is too large to submit on Canvas, submit a link to your repository. For private
repositories, ensure that sharing permissions are given your project TA and instructor. You can find their
emails in the “Contact Information” page on Canvas.
3. A single script that can be used to create all the tables and data in the database.
4. A PDF file containing:
o A short description of the final project, and what it accomplished
o A description of how your final schema differed from the schema you turned in.
▪ If the final schema differed, why?
▪ Note that turning in a final schema that's different from what you planned is fine, we just want
to know what changed and why.
5. A list of all SQL queries used. For SQL query requirements, check the rubric listed in milestone 5.
6. Screenshots of the sample output of the queries
7. Lastly, include a README.txt file if there’s anything you want to add that’s not included in your PDF file.

Due date : April 5 @ 11:59PM ; No late submissions will be accepted.


3
Outline

1. Learning goals
2. Basic SQL
• Select
• From
• Where
• Distinct
• Group By
3. Practice Exercise
4. Revisit learning objectives

4
Databases: the continuing saga

When last we left databases…


• We had decided they were great things
• We knew how to conceptually model them in ER diagrams
• We knew how to logically model them in the relational
model
• We knew how to normalize our database relations
• We could formally specify queries

Now: how do most people write queries? SQL!

SQL 5
Learning Goals
Given the schemas of a relation you will be able to:

1. Create SQL queries using: SELECT, FROM, WHERE, DISTINCT,


GROUP BY, HAVING, ANY, ALL, JOINS, EXISTS AND NOT EXISTS

2. Show that there are alternative ways of coding SQL queries to yield the
same result. Determine whether or not two SQL queries are equivalent.

3. Comment on the relative expressive power of SQL.

4. Explain the purpose of NULL values and justify their use. Also describe
the difficulties added by having nulls.

5. Create and modify table schemas and views in SQL.

6. Translate a query between SQL and RA/Datalog


SQL 6
Coming up in SQL…

• Data Definition Language (Recap)


• Basic Structure
• Set Operations
• Aggregate Functions
• Null Values
• Nested Subqueries
• Modification of the Database
• Views
• Integrity Constraints

SQL 7
You can write your own queries
• Your Oracle (DBMS) accounts on the department servers are ready
• Your login is "ora_[CWL ID]" Your password is "a[your student id]“. To
login:
• Log onto remote.students.cs.ubc.ca.
• Start sqlplus (the interface to Oracle by typing "sqlplus [Oracle
login]@stu" (”stu" is the specific database to access)
• Note: if you run into problems getting things to work, make sure that:
You're logging onto remote.students.cs.ubc.ca
• Login to the undergraduate machine using your regular user
name and password
• You have the "@stu" at the end of logging in (that says what
database to access)
• you don't forget to prepend your student ID with an "a" for your
sql login

SQL 8
You can write your own queries
• https://apex.oracle.com/en/ (online and freely available)

• MySQL (free) https://dev.mysql.com/downloads/mysql/

• XAMPP (free)

SQL 9
Introduction

• SQL - Structured Query Language


• SQL is used for:
- Creating databases
- Adding, modifying and deleting database structures
- Inserting, deleting, and modifying records in databases
- Querying databases (data retrieval)

• SQL functions as a standard relational database language

SQL 10
SQL Command

➢ CREATE
Data Definition Language ➢ ALTER
(DDL) ➢ DROP

➢ INSERT INTO
Data Manipulation ➢ UPDATE
SQL command ➢
Language (DML) DELETE
➢ SELECT

Data Control Language


(DCL) ➢ Date Access Control

SQL 11
Domain Types in SQL Reference Sheet

• char(n). Fixed length character string with length n.


• varchar(n). Variable length character strings, with maximum length n.
• int. Integer (machine-dependent).
• smallint. Small integer (machine-dependent).
• numeric(p,d). Fixed point number, with user-specified precision of p
digits, with d digits to the right of decimal point.
• real, double precision. Floating point and double-precision
floating point numbers, with machine-dependent precision.
• float(n). Floating point number, with user-specified precision of at
least n digits.
• Null values are allowed in all the domain types.
- To prohibit null values declare attribute to be not null

12
SQL 12
Date/Time Types in SQL Reference Sheet

- date. Dates, containing a (4 digit) year, month and


date E.g. date ‘2001-7-27’

- time. Time of day, in hours, minutes and seconds.


E.g. time ’09:00:30’ time ’09:00:30.75’

-timestamp: date plus time of day


E.g. timestamp ‘2001-7-27 09:00:30.75’

Relational DBMS offer a variety of functions to


extract values of individual fields from date/time/timestamp
convert strings to dates and vice versa

For instance in Oracle (date is a timestamp):


TO_CHAR( date, format)
TO_DATE( string, format)

13
SQL 13
Specifying Attribute Constraints

• Default value of an attribute


• DEFAULT <value>
• City varchar(255) DEFAULT ’Vancouver’

• NULL is not permitted for a particular attribute (NOT NULL)


• City varchar(255)NOT NULL

• CHECK clause
• Dnumber INT NOT NULL CHECK (Dnumber > 0 AND Dnumber <
21);

SQL 14
Basic Constraints in SQL

Relational Model has 3 basic constraint types that are supported in


SQL:

1. Key constraint: A primary key value cannot be duplicated.

2. Entity Integrity Constraint: A primary key value cannot be null.

3. Referential integrity constraints : The “foreign key “ must have a


value that is already present as a primary key, or may be null.

SQL 15
Specifying Key and Referential Integrity
Constraints
• PRIMARY KEY clause
- Specifies one or more attributes that make up the primary key of a relation
- Dnumber INT PRIMARY KEY;

• UNIQUE clause
- Specifies alternate (secondary) keys (called CANDIDATE keys in the relational
model).
- Dname VARCHAR(15) UNIQUE;

• FOREIGN KEY clause


- Default operation: reject update on violation
- Attach referential triggered action clause (include SET NULL, CASCADE,
and SET DEFAULT)

SQL 16
Basic SQL

SQL 17
CREATE TABLE

• CREATE TABLE
- Used for creating and connecting relational tables

• Base tables (base relations)


- Relation and its tuples are actually created and stored
as a file by the DBMS

• Virtual relations (views)


- Created through the CREATE VIEW statement. Do not
correspond to any physical file.

SQL 18
TABLE vs VIEW

Table 1
View

Create
VIEW
Table 2 View created based on Table 1 and Table 2
- Query stored in the database
- It is computed dynamically
Main purpose:
• Simplify the complex SQL queries.
• Provide restriction to users from accessing
sensitive data.

SQL 19
CREATE TABLE

• A SQL relation is defined using the create table command:


CREATE TABLE r (A1 D1, A2 D2, ..., An Dn,
(integrity-constraint1),
...,
(integrity-constraintk))

SQL 20
Relational schema: ZAGI Retail Company Sales Department Database
CREATE TABLE statements for ZAGI Retail Company Sales
Department Database

CREATE TABLE vendor


( vendorid CHAR(2) NOT NULL,
vendorname VARCHAR(25) NOT NULL,
PRIMARY KEY (vendorid) );

CREATE TABLE category


( categoryid CHAR(2) NOT NULL,
categoryname VARCHAR(25) NOT NULL,
PRIMARY KEY (categoryid) );

CREATE TABLE product


( productid CHAR(3) NOT NULL,
productname VARCHAR(25) NOT NULL,
productprice NUMERIC(7,2) NOT NULL,
vendorid CHAR(2) NOT NULL,
categoryid CHAR(2) NOT NULL,
PRIMARY KEY (productid),
FOREIGN KEY (vendorid) REFERENCES vendor(vendorid),
FOREIGN KEY (categoryid) REFERENCES category(categoryid) );

CREATE TABLE region


( regionid CHAR(1) NOT NULL,
regionname VARCHAR(25) NOT NULL,
PRIMARY KEY (regionid) );
CREATE TABLE store
( storeid VARCHAR(3) NOT NULL,
storezip CHAR(5) NOT NULL,
regionid CHAR(1) NOT NULL,
PRIMARY KEY (storeid),
FOREIGN KEY (regionid) REFERENCES region(regionid) );

CREATE TABLE customer


( customerid CHAR(7) NOT NULL,
customername VARCHAR(15) NOT NULL,
customerzip CHAR(5) NOT NULL,
PRIMARY KEY (customerid) );

CREATE TABLE salestransaction


( tid VARCHAR(8) NOT NULL,
customerid CHAR(7) NOT NULL,
storeid VARCHAR(3) NOT NULL,
tdate DATE NOT NULL,
PRIMARY KEY (tid),
FOREIGN KEY (customerid) REFERENCES customer(customerid),
FOREIGN KEY (storeid) REFERENCES store(storeid) );

CREATE TABLE soldvia


( productid CHAR(3) NOT NULL,
tid VARCHAR(8) NOT NULL,
noofitems INT NOT NULL,
PRIMARY KEY (productid, tid),
FOREIGN KEY (productid) REFERENCES product(productid),
FOREIGN KEY (tid) REFERENCES salestransaction(tid) );
DROP TABLE

- Used to remove a table from the database


- DROP TABLE soldvia;

SQL 24
INSERT INTO
- Used to populate the created relations with data

SQL 25
Data records: ZAGI Retail Company Sales Department
Database
INSERT INTO statements for ZAGI Retail Company Sales Department Database

INSERT INTO vendor VALUES ('PG','Pacifica Gear');


INSERT INTO vendor VALUES ('MK','Mountain King');

INSERT INTO category VALUES ('CP','Camping');


INSERT INTO category VALUES ('FW','Footwear');

INSERT INTO product VALUES ('1X1','Zzz Bag',100,'PG','CP');


INSERT INTO product VALUES ('2X2','Easy Boot',70,'MK','FW');
INSERT INTO product VALUES ('3X3','Cosy Sock',15,'MK','FW');
INSERT INTO product VALUES ('4X4','Dura Boot',90,'PG','FW');
INSERT INTO product VALUES ('5X5','Tiny Tent',150,'MK','CP');
INSERT INTO product VALUES ('6X6','Biggy Tent',250,'MK','CP');

INSERT INTO region VALUES ('C','Chicagoland');


INSERT INTO region VALUES ('T','Tristate');

INSERT INTO store VALUES ('S1','60600','C');


INSERT INTO store VALUES ('S2','60605','C');
INSERT INTO store VALUES ('S3','35400','T');

INSERT INTO customer VALUES ('1-2-333','Tina','60137');


INSERT INTO customer VALUES ('2-3-444','Tony','60611');
INSERT INTO customer VALUES ('3-4-555','Pam','35401');
INSERT INTO statements for ZAGI Retail Company Sales
Department Database

INSERT INTO salestransaction VALUES ('T111','1-2-333','S1','01/Jan/2013');


INSERT INTO salestransaction VALUES ('T222','2-3-444','S2','01/Jan/2013');
INSERT INTO salestransaction VALUES ('T333','1-2-333','S3','02/Jan/2013');
INSERT INTO salestransaction VALUES ('T444','3-4-555','S3','02/Jan/2013');
INSERT INTO salestransaction VALUES ('T555','2-3-444','S3','02/Jan/2013');

INSERT INTO soldvia VALUES ('1X1','T111',1);


INSERT INTO soldvia VALUES ('2X2','T222',1);
INSERT INTO soldvia VALUES ('3X3','T333',5);
INSERT INTO soldvia VALUES ('1X1','T333',1);
INSERT INTO soldvia VALUES ('4X4','T444',1);
INSERT INTO soldvia VALUES ('2X2','T444',2);
INSERT INTO soldvia VALUES ('4X4','T555',4);
INSERT INTO soldvia VALUES ('5X5','T555',2);
INSERT INTO soldvia VALUES ('6X6','T555',1);
Select

- Used for the retrieval of data from the database relations

- Most commonly issued SQL statement

- Basic form:
SELECT <columns>
FROM <table>

In SQL, π is in the SELECT clause


Note duplication can happen!
You can get the same value multiple times
SQL 29
SELECT
Query 1 text: Retrieve the entire contents of the relation PRODUCT

Query 1: SELECT productid, productname, productprice,


vendorid, categoryid
FROM product;

Query 1 result:

SQL 30
SELECT

Query 1 text: Retrieve the entire contents of the relation PRODUCT

Query 1a: SELECT *


FROM product;

Query 1a result:

SQL 31
SELECT

Query 2 text: Retrieve the entire contents of the relation PRODUCT and
show the columns in the following order: ProductName,
ProductID, VendorID, CategoryID, ProductPrice

Query 2: SELECT productname, productid, vendorid,


categoryid, productprice
FROM product;

Query 2 result:

SQL 32
SELECT
Query 3 text: For the relation PRODUCT, show the columns ProductID and
ProductPrice

Query 3: SELECT productid, productprice


FROM product;

Query 3 result:

SQL 33
SELECT
Query 3 text: For the relation PRODUCT, show the columns ProductID and
ProductPrice

Query 3: SELECT productid, productprice


FROM product;

Query 3 result:

SQL 34
SELECT
- In addition to displaying columns, the SELECT clause can be
used to display derived attributes (calculated columns)
represented as expressions

- SELECT statement can be structured as follows:


SELECT <columns, expressions>
FROM <table>

SQL 35
SELECT
Query 4 text: For the relation PRODUCT, show the columns ProductID and
ProductPrice and a column showing ProductPrice increased by
10%

Query 4: SELECT productid, productprice, productprice * 1.1


FROM product;

Query 4 result:

SQL 36
Clicker Question
• Given the table scores:
what is result of
SELECT Score1, Score2
Team1 Team2 Score1 Score2
FROM Scores;
Dragons Tigers 5 3
Carp Swallows 4 6
• Which of the
Bay Stars Giants 2 1
following rows is
Marines Hawks 5 3
in the answer?
Ham Fighters Buffaloes 1 6
A. (1,2)
Lions Golden 8 12
B. (5,3) Eagles
C. (8,6)
D. All are in the answer
E. None are in the answer

SQL 37
Clicker Question: SQL projection
• Given the table scores:
what is result of
SELECT Score1,Score2
FROM Scores; Team1 Team2 Score1 Score2
Dragons Tigers 5 3
• Which of the Carp Swallows 4 6
Bay Stars Giants 2 1
following rows is Marines Hawks 5 3
in the answer? Ham Fighters Buffaloes 1 6
Lions Golden 8 12
A. (1,2) Eagles
B. (5,3) Correct
C. (8,6)
D. All are in the answer
E. None are in the answer

SQL 38
SELECT
- The SELECT FROM statement can contain other optional
keywords, such as WHERE, GROUP BY, HAVING, and
ORDER BY, appearing in this order:

SELECT <columns, expressions>


FROM <tables>
WHERE <row selection condition>
GROUP BY <grouping columns>
HAVING <group selection condition>
ORDER BY <sorting columns,
expressions>

SQL 39
WHERE

- WHERE condition determines which rows should be


retrieved and consequently which rows should not be
retrieved
- The logical condition determining which records to retrieve
can use one of the following logical comparison operators:
= Equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
!= Not equal to
<> Not equal to (alternative notation)

In SQL, σ is in WHERE clause


SQL 40
WHERE
Query 5 text: Retrieve the product ID, product name, vendor ID, and
product price for each product whose price is above $100

Query 5: SELECT productid, productname, vendorid,


productprice
FROM product
WHERE productprice > 100;

Query 5 result:

SQL 41
Clicker Question: Selection
• Consider Scores1(Team, Opponent, RunsFor, RunsAgainst) and
query
SELECT * Team Opponent RunsFo RunsAgainst
FROM Scores1 r
Dragons Tigers 5 3
WHERE RunsFor > 5 ;
Carp Swallows 4 6
Bay Stars Giants 2 1
• Which tuple is Marines Hawks 5 3
in the result? Ham Fighters Buffaloes 1 6

A. (Swallows, Carp,6, 4) Lions Golden 8 12


Eagles
B. (Swallows, Carp,1,2) Tigers Dragons 3 5
C. (12) Swallows Carp 6 4

D. (*) Giants Bay Stars 1 2


Hawks Marines 3 5
Buffaloes Ham Fighters 6 1
Golden Lions 12 8 42
Eagles

SQL 42
Clicker Question: Selection

SELECT *
FROM Scores1
WHERE RunsFor > 5 ;

TEAM OPPONENT RUNSFOR RUNSAGAINST

Lions Golden Eagles 8 12


Swallows Carp 6 4
Buffaloes Ham Fighters 6 1
Golden Eagles Lions 12 8

SQL 43
Clicker Question: Selection
• Consider Scores1(Team, Opponent, RunsFor, RunsAgainst) and
query
SELECT * Team Opponent RunsFor RunsAgainst
FROM Scores1
Dragons Tigers 5 3
WHERE RunsFor > 5 ;
Carp Swallows 4 6
Bay Stars Giants 2 1
• Which tuple is Marines Hawks 5 3
in the result? Ham Fighters Buffaloes 1 6
A. (Swallows, Carp,6, 4) answer A Lions Golden Eagles 8 12

B. (Swallows, Carp,1,2) Tigers Dragons 3 5


Swallows Carp 6 4
C. (12)
Giants Bay Stars 1 2
D. (*) Hawks Marines 3 5
Buffaloes Ham Fighters 6 1
Golden Eagles Lions 12 8
44

SQL 44
So how does a typical SQL query relate to relational
algebra then?
SQL:
SELECT A1, A2, ..., An
FROM r1, r2, ..., rm
WHERE P
Is approximately equal to
Relational algebra
πA1, A2, ..., An(σP (r1 x r2 x ... x rm))

Difference? Duplicates.
Remove them? Distinct
45
SQL 45
DISTINCT

- Can be used in conjunction with the SELECT statement


- Eliminates duplicate values from a query result

SQL 46
DISTINCT
Query 6 text: Retrieve the VendorID value for each record in the relation
PRODUCT

Query 6: SELECT vendorid


FROM product;

Query 6 result:

SQL 47
DISTINCT

Query 7 text: Show one instance of all the different


VendorID values in the relation PRODUCT

Query 7: SELECT DISTINCT vendorid


FROM product;

Query 7 result:

SQL 48
Clicker question:
distinct

Team Opponent Runs Runs


• Consider the relation: For Against
Scores4(Team, Opponent, RunsFor, Dragons Tigers 5 3
RunsAgainst) and the query: Carp Swallows 4 6
SELECT DISTINCT Team, RunsFor Bay Stars Giants 2 1
FROM Scores4 Marines Hawks 5 3
Ham Buffaloes 1 6
Fighters
• Which is true: Lions Golden 8 12
A. 1 appears once Eagles
B. 5 appears twice Tigers Dragons 3 5
C. 6 appears 4 times Swallows Carp 6 4

D. All are true Giants Bay Stars 1 2


Hawks Marines 3 5
E. None are true
Buffaloes Ham Fighters 6 1
Golden Lions 12 8
Eagles

SQL 49
Clicker question: distinct
SELECT DISTINCT Team, RunsFor
FROM Scores4
TEAM RUNSFOR
Bay Stars 2
Marines 5
Swallows 6
Hawks 3
Carp 4
Giants 1
Golden Eagles 12
Tigers 3
Buffaloes 6
Ham Fighters 1
Lions 8
Dragons 5
SQL 50
Clicker question: distinct
• Consider the relation: Team Opponent Runs Runs
Scores4(Team, Opponent, For Against
Dragons Tigers 5 3
RunsFor, RunsAgainst) and
Carp Swallows 4 6
the query: Bay Stars Giants 2 1
SELECT DISTINCT Team, Marines Hawks 5 3
RunsFor Ham Buffaloes 1 6
Fighters
FROM Scores4 Lions Golden 8 12
Eagles
Tigers Dragons 3 5
• Which is true: Swallows Carp 6 4
A. 1 appears once Giants Bay Stars 1 2
B. 5 appears twice Correct Hawks Marines 3 5
Buffaloes Ham Fighters 6 1
C. 6 appears 4 times Golden Lions 12 8
D. All are true Eagles

E. None are true


SQL 51
ORDER BY

- Used to sort the results of the query by one or more columns (or
expressions)
- By default it is ascending

SQL 52
ORDER BY
Query 8 text: Retrieve the product ID, product name, category ID, and
product price for each product in the FW product category,
sorted by product price

Query 8: SELECT productid, productname, categoryid,


productprice
FROM product
WHERE categoryid = 'FW'
ORDER BY productprice;

Query 8 result:

SQL 53
ORDER BY
Query 9 text: Retrieve the product ID, product name, category ID, and
product price for each product in the FW product category,
sorted by product price in descending order

Query 9: SELECT productid, productname, categoryid,


productprice
FROM product
WHERE categoryid = 'FW'
ORDER BY productprice DESC;

Query 9 result:

SQL 54
Clicker question: Order By

Relation R has schema R(a,b,c). In the result of the


query
SELECT a, b, c
FROM R
ORDER BY c DESC, b ASC;
What condition must a tuple t satisfy so that t
necessarily precedes the tuple (5,5,5)? Identify one
such tuple from the list below.
A. (3,6,3)
B. (1,5,2)
C. (5,5,6)
D. All of the above
E. None of the above 55
SQL 55
Clicker question: Order By

Relation R has schema R(a,b,c). In the result of the


query
SELECT a, b, c
FROM R
ORDER BY c DESC, b ASC;
What condition must a tuple t satisfy so that t
necessarily precedes the tuple (5,5,5)? Identify one
such tuple from the list below.
A. (3,6,3) 3<5
B. (1,5,2) C desc and then b asc

C. (5,5,6) Right

D. All of the above


E. None of the above 55
SQL 56
AGGREGATE FUNCTIONS

- For calculating and summarizing values in queries.


- SQL provides the following aggregate functions:
➢ COUNT
➢ SUM
➢ AVG
➢ MIN
➢ MAX Aggregate
functions

SQL 57
AGGREGATE FUNCTIONS
Query 10 text: Retrieve the average price of all products

Query 10 : SELECT AVG(productprice)


FROM product;

Query 10 result:

SQL 58
AGGREGATE FUNCTIONS

Query 11 text: Show how many products we offer for sale

Query 11 : SELECT COUNT(*) the * symbol in the


COUNT(*) function refers
FROM product; to the records

Query 11 result:

SQL 59
Aggregate Operators
These functions operate on the multiset of values of a column of
a relation, and return a value
AVG: average value
MIN: minimum value
MAX: maximum value
SUM: sum of values
COUNT: number of values

The following versions eliminate duplicates before applying


the operation to attribute A:
COUNT ( DISTINCT A)
SUM ( DISTINCT A)
AVG ( DISTINCT A)
SQL 60
GROUP BY
• Divide tuples into groups and apply aggregate operations to each group.
• Example: For each vendor, retrieve the average price of the products
supplied by the vendor

Vendor ID Average Price


Average
product price in MK 121.2
PRODUCT
table for each PG 95
Vendor id

SQL 61
GROUP BY

For each vendor, retrieve the average price of the products


supplied by the vendor

SELECT AVG(productprice)
For i = ‘PG’, ‘MK’… FROM product
WHERE vendorid= ‘PG’;

◼ Problem:
We don’t know how many Vendor ID exist, not to mention this is not
good practice

SQL 62
GROUP BY
Query 12 text: For each vendor, retrieve the vendor ID, number of products
supplied by the vendor, and average price of the products
supplied by the vendor

Query 12 : SELECT vendorid, COUNT(*), AVG(productprice)


FROM product
GROUP BY vendorid;

Query 12 result:

SQL 63
GROUP BY
• All columns in the SELECT list that are not in aggregate
functions must be in the GROUP BY clause.

Query 13.
SELECT vendorid, COUNT(*), AVG(productprice)
FROM product
GROUP BY vendorid;

Vendor ID Count (*) Average


( ProductPrice)
MK 4 121.2
PG 2 95

SQL 64
GROUP BY

• The GROUP BY column does not have to be in the


SELECT list.

SELECT COUNT(*), AVG(productprice)


FROM product
GROUP BY vendorid;

Count (*) Average (ProductPrice)

4 121.2
2 95

SQL 65
GROUP BY - on Multiple Columns

SELECT vendorid, categoryid,COUNT(*),AVG(productprice)


FROM product
GROUP BY vendorid, categoryid;

VendorID CategoryID Count(*) Avg


(productprice)

MK CP 2 200
PG CP 1 100
MK FW 2 42.5
PG FW 1 90

SQL 66
Illegal Query
• Any column or expression in the SELECT list that is not
an aggregate function must be in the GROUP BY clause:

SELECT vendorid, AVG(productprice)


FROM product

Column 'product.vendorid' is invalid in the select list because it is


not contained in either an aggregate function or the GROUP BY clause.

SQL 67
Illegal Query
• We cannot use the WHERE clause to restrict groups.
• We have to use the HAVING clause to restrict groups.
• You cannot use aggregate functions in the WHERE clause.

SELECT vendorid, AVG(productprice)


FROM product
WHERE AVG(productprice) > 100
GROUP BY vendorid;

An aggregate may not appear in the WHERE clause unless it is in a subquery


contained in a HAVING clause or a select list, and the column being aggregated is an
outer reference.

SQL 68
Practice Exercise – Try!

Query 1. Retrieve the product ID, product name, vendor ID, categoryid, and product
price for each product in the FW category whose price is equal to or below $200

Query 2. Retrieve the number of products, average product price, lowest product
price, and highest product price in the CP product category.

Query 3. Display the ProductID, ProductName, and ProductPrice for products in the
category whose CategoryID value is ‘CP’. Sort the results by ProductID.

Ques 4. Display the TID and the total number of items (of all products) sold within all
sales transactions.
Practice Exercise – Try!
Query 1. Retrieve the product ID, product name, vendor ID, categoryid, and product
price for each product in the FW category whose price is equal to or below $200
SELECT ProductID, ProductName, VendorID, CategoryID, ProductPrice
FROM PRODUCT
WHERE CategoryID = 'FW' AND ProductPrice <= 200;

Query 2. Retrieve the number of products, average product price, lowest product
price, and highest product price in the CP product category.
SELECT COUNT(*), AVG(ProductPrice), MIN(ProductPrice), MAX(ProductPric​e)
FROM PRODUCT
WHERE CategoryID = 'CP’;

Ques 3 . Display the ProductID, ProductName, and ProductPrice for products in the
category whose CategoryID value is ‘CP’. Sort the results by ProductID.
SELECT ProductID, ProductName, ProductPrice
FROM PRODUCT
WHERE CategoryID = 'CP'
ORDER BY ProductID;
Practice Exercise – Try!
Ques 4. Display the TID and the total number of items (of all products) sold within all
sales transactions.

SELECT TID, SUM(NoOfItems)


FROM SOLDVIA
GROUP BY TID
Learning Goals Revisited
Given the schemas of a relation you will be able to:

1. Create SQL queries using: SELECT, FROM, WHERE, DISTINCT,


GROUP BY, HAVING, ANY, ALL, JOINS, EXISTS AND NOT EXISTS

2. Show that there are alternative ways of coding SQL queries to yield the
same result. Determine whether or not two SQL queries are equivalent.

3. Comment on the relative expressive power of SQL.

4. Explain the purpose of NULL values and justify their use. Also describe
the difficulties added by having nulls.

5. Create and modify table schemas and views in SQL.

6. Translate a query between SQL and RA/Datalog


SQL 72

You might also like