Ch05-Part 1
Ch05-Part 1
Ch05-Part 1
Relational Data
Retrieval: SQL
Fundamentals of Database Management Systems,
2nd ed by Mark L. Gillenson, Ph.D.
University of Memphis
John Wiley & Sons, Inc.
Chapter Objectives
Describe SQL as a relational data manipulation language.
Explain that you can create and update relational tables using SQL.
4-2
4-2
SQL
Structured Query Language.
Very heavily used in practice today.
4-6
4-6
Introduction: SQL SELECT
Used for data retrieval.
You specify what data you are looking for rather than
provide a logical sequence of steps that guide the
system in how to find the data.
Can be run in either a query or an embedded mode.
Command will work with Oracle, MS Access, SQL
Server, DB2, Informix, MySQL, etc.
SELECT <columns>
FROM <table>
WHERE <predicates identifying rows to be included>;
4-7
4-7
SQL Query Example
“Find all customers.”
SELECT * FROM Customers;
SELECT CustomerID, CustomerName FROM Customers;
SELECT * FROM Customers
WHERE Country='Mexico';
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
SELECT * FROM Customers
WHERE NOT Country='Germany';
SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';
4-11
SQL BETWEEN Operator
SELECT * FROM Products WHERE Price BETWEEN 10 AND 20;
SELECT * FROM Products WHERE Price NOT BETWEEN 10 AND 20;
SELECT * FROM Products WHERE Price BETWEEN 10 AND 20
AND CategoryID NOT IN (1,2,3);
SELECT * FROM Products WHERE ProductName BETWEEN 'Carnarvon
Tigers' AND 'Mozzarella di Giovanni';
SELECT * FROM Products WHERE ProductName NOT BETWEEN 'Carnarvon
Tigers' AND 'Mozzarella di Giovanni’;
SELECT * FROM Orders
WHERE OrderDate BETWEEN #07/01/1996# AND #07/31/1996#;
SELECT * FROM Orders
WHERE OrderDate BETWEEN '1996-07-01' AND '1996-07-31’;
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');
SELECT * FROM Customers
WHERE Country IN (SELECT Country FROM Suppliers);
SELECT * FROM Products
WHERE ProductName BETWEEN "Carnarvon Tigers" AND "Chef
Anton's Cajun Seasoning"
ORDER BY ProductName;
Single quotes are used to indicate the beginning and end of a string in SQL.
Double quotes generally aren't used in SQL. It depends on RDBMS.
4-13
4-13
SQL Query Example: LIKE
SELECT * FROM Customers
WHERE CustomerName LIKE '%a';
SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';
The single “_” character in the operator LIKE “_a%” specifies that there will be
one character followed by “a.”
SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'a%';
4-14
SQL Query Example: DISTINCT
SELECT Country FROM Customers;
SELECT DISTINCT Country FROM Customers;
SELECT COUNT(DISTINCT Country) FROM Customers;
SELECT COUNT(Country) FROM Customers;
4-15
SQL Query Example: ORDER BY
SELECT * FROM Customers ORDER BY Country;
SELECT * FROM Customers ORDER BY Country DESC;
By Several Columns
SELECT * FROM Customers ORDER BY Country, CustomerName;
4-16
SQL COUNT(), AVG() and SUM() Functions
SELECT COUNT(ProductID) FROM Products;
SELECT AVG(Price) FROM Products;
SELECT SUM(Quantity) FROM OrderDetails;
4-18
SQL Query Example: GROUP BY
The GROUP BY statement groups rows that have the same values into
summary rows, like "find the number of customers in each country".
SELECT Shippers.ShipperName, COUNT(Orders.OrderID) AS NumberOf
Orders FROM Orders
LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
GROUP BY ShipperName;
4-19
SQL Query Example: HAVING
The HAVING clause was added to SQL because the WHERE keyword
cannot be used with aggregate functions.
SELECT column_name(s) SELECT COUNT(CustomerID),
FROM table_name Country
WHERE condition FROM Customers
GROUP BY column_name(s) GROUP BY Country
HAVING condition HAVING COUNT(CustomerID) > 5;
ORDER BY column_name(s);
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
4-20
SQL CREATE TABLE
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName
varchar(255),
Address varchar(255),
City varchar(255)
);
4-21
SQL INSERT INTO Statement
INSERT INTO table_name (column1, column2, column3,
…)VALUES (value1, value2, value3, ...);
4-22
The Join
4-23
4-23
SQL Query Example: Join